27#ifndef _TUSB_OSAL_FREERTOS_H_
28#define _TUSB_OSAL_FREERTOS_H_
31#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,FreeRTOS.h)
32#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,semphr.h)
33#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,queue.h)
34#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,task.h)
44#if configSUPPORT_STATIC_ALLOCATION
46 typedef StaticSemaphore_t osal_mutex_def_t;
50 typedef uint8_t osal_mutex_def_t;
53typedef SemaphoreHandle_t osal_semaphore_t;
54typedef SemaphoreHandle_t osal_mutex_t;
55typedef QueueHandle_t osal_queue_t;
63#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0)
67#if configSUPPORT_STATIC_ALLOCATION
72#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0)
73 #define _OSAL_Q_NAME(_name) .name = #_name
75 #define _OSAL_Q_NAME(_name)
79#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
80 static _type _name##_##buf[_depth];\
81 osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf, _OSAL_Q_NAME(_name) };
87TU_ATTR_ALWAYS_INLINE
static inline uint32_t _osal_ms2tick(uint32_t msec) {
88 if ( msec == OSAL_TIMEOUT_WAIT_FOREVER )
return portMAX_DELAY;
89 if ( msec == 0 )
return 0;
91 uint32_t ticks = pdMS_TO_TICKS(msec);
95 if ( ticks == 0 ) ticks = 1;
100TU_ATTR_ALWAYS_INLINE
static inline void osal_task_delay(uint32_t msec) {
101 vTaskDelay(pdMS_TO_TICKS(msec));
108TU_ATTR_ALWAYS_INLINE
static inline osal_semaphore_t osal_semaphore_create(
osal_semaphore_def_t *semdef) {
109#if configSUPPORT_STATIC_ALLOCATION
110 return xSemaphoreCreateBinaryStatic(semdef);
113 return xSemaphoreCreateBinary();
117TU_ATTR_ALWAYS_INLINE
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl,
bool in_isr) {
119 return xSemaphoreGive(sem_hdl) != 0;
121 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
122 BaseType_t res = xSemaphoreGiveFromISR(sem_hdl, &xHigherPriorityTaskWoken);
124#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
126 if ( xHigherPriorityTaskWoken ) portYIELD_FROM_ISR();
128 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
135TU_ATTR_ALWAYS_INLINE
static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
136 return xSemaphoreTake(sem_hdl, _osal_ms2tick(msec));
139TU_ATTR_ALWAYS_INLINE
static inline void osal_semaphore_reset(osal_semaphore_t
const sem_hdl) {
140 xQueueReset(sem_hdl);
147TU_ATTR_ALWAYS_INLINE
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) {
148#if configSUPPORT_STATIC_ALLOCATION
149 return xSemaphoreCreateMutexStatic(mdef);
152 return xSemaphoreCreateMutex();
156TU_ATTR_ALWAYS_INLINE
static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) {
157 return osal_semaphore_wait(mutex_hdl, msec);
160TU_ATTR_ALWAYS_INLINE
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
161 return xSemaphoreGive(mutex_hdl);
168TU_ATTR_ALWAYS_INLINE
static inline osal_queue_t osal_queue_create(
osal_queue_def_t* qdef) {
171#if configSUPPORT_STATIC_ALLOCATION
172 q = xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
174 q = xQueueCreate(qdef->depth, qdef->item_sz);
177#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0)
178 vQueueAddToRegistry(q, qdef->name);
184TU_ATTR_ALWAYS_INLINE
static inline bool osal_queue_receive(osal_queue_t qhdl,
void* data, uint32_t msec) {
185 return xQueueReceive(qhdl, data, _osal_ms2tick(msec));
188TU_ATTR_ALWAYS_INLINE
static inline bool osal_queue_send(osal_queue_t qhdl,
void const *data,
bool in_isr) {
190 return xQueueSendToBack(qhdl, data, OSAL_TIMEOUT_WAIT_FOREVER) != 0;
192 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
193 BaseType_t res = xQueueSendToBackFromISR(qhdl, data, &xHigherPriorityTaskWoken);
195#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
197 if ( xHigherPriorityTaskWoken ) portYIELD_FROM_ISR();
199 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
206TU_ATTR_ALWAYS_INLINE
static inline bool osal_queue_empty(osal_queue_t qhdl) {
207 return uxQueueMessagesWaiting(qhdl) == 0;
Definition osal_freertos.h:58
Definition osal_none.h:47