add msec timeout to osal_queue_receive(), tud_task() and tuh_task() both pending for event queue with timeout of 1 ms

This commit is contained in:
hathach
2022-04-20 13:25:38 +07:00
parent 9c8c5c1c53
commit a90839688c
11 changed files with 109 additions and 101 deletions

View File

@@ -475,8 +475,7 @@ void tud_task (void)
while (1) while (1)
{ {
dcd_event_t event; dcd_event_t event;
if ( !osal_queue_receive(_usbd_q, &event, 1) ) return;
if ( !osal_queue_receive(_usbd_q, &event) ) return;
#if CFG_TUSB_DEBUG >= 2 #if CFG_TUSB_DEBUG >= 2
if (event.event_id == DCD_EVENT_SETUP_RECEIVED) TU_LOG2("\r\n"); // extra line for setup if (event.event_id == DCD_EVENT_SETUP_RECEIVED) TU_LOG2("\r\n"); // extra line for setup

View File

@@ -401,7 +401,7 @@ void tuh_task(void)
while (1) while (1)
{ {
hcd_event_t event; hcd_event_t event;
if ( !osal_queue_receive(_usbh_q, &event) ) return; if ( !osal_queue_receive(_usbh_q, &event, 1) ) return;
switch (event.event_id) switch (event.event_id)
{ {

View File

@@ -66,19 +66,19 @@ typedef void (*osal_task_func_t)( void * );
// OSAL Porting API // OSAL Porting API
// Should be implemented as static inline function in osal_port.h header // Should be implemented as static inline function in osal_port.h header
/* /*
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef); osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef);
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr); bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr);
static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec); bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec);
static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl); // TODO removed void osal_semaphore_reset(osal_semaphore_t sem_hdl); // TODO removed
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef); osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef);
static inline bool osal_mutex_lock (osal_mutex_t sem_hdl, uint32_t msec); bool osal_mutex_lock (osal_mutex_t sem_hdl, uint32_t msec);
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl); bool osal_mutex_unlock(osal_mutex_t mutex_hdl);
static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef); osal_queue_t osal_queue_create(osal_queue_def_t* qdef);
static inline bool osal_queue_receive(osal_queue_t qhdl, void* data); bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec);
static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr); bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr);
static inline bool osal_queue_empty(osal_queue_t qhdl); bool osal_queue_empty(osal_queue_t qhdl);
*/ */
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@@ -40,7 +40,7 @@ extern "C" {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// TASK API // TASK API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static inline void osal_task_delay(uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
{ {
vTaskDelay( pdMS_TO_TICKS(msec) ); vTaskDelay( pdMS_TO_TICKS(msec) );
} }
@@ -51,12 +51,12 @@ static inline void osal_task_delay(uint32_t msec)
typedef StaticSemaphore_t osal_semaphore_def_t; typedef StaticSemaphore_t osal_semaphore_def_t;
typedef SemaphoreHandle_t osal_semaphore_t; typedef SemaphoreHandle_t osal_semaphore_t;
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
{ {
return xSemaphoreCreateBinaryStatic(semdef); return xSemaphoreCreateBinaryStatic(semdef);
} }
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
{ {
if ( !in_isr ) if ( !in_isr )
{ {
@@ -78,13 +78,13 @@ static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
} }
} }
static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
{ {
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec); uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
return xSemaphoreTake(sem_hdl, ticks); return xSemaphoreTake(sem_hdl, ticks);
} }
static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
{ {
xQueueReset(sem_hdl); xQueueReset(sem_hdl);
} }
@@ -95,17 +95,17 @@ static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl)
typedef StaticSemaphore_t osal_mutex_def_t; typedef StaticSemaphore_t osal_mutex_def_t;
typedef SemaphoreHandle_t osal_mutex_t; typedef SemaphoreHandle_t osal_mutex_t;
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
{ {
return xSemaphoreCreateMutexStatic(mdef); return xSemaphoreCreateMutexStatic(mdef);
} }
static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
{ {
return osal_semaphore_wait(mutex_hdl, msec); return osal_semaphore_wait(mutex_hdl, msec);
} }
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
{ {
return xSemaphoreGive(mutex_hdl); return xSemaphoreGive(mutex_hdl);
} }
@@ -114,7 +114,7 @@ static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
// QUEUE API // QUEUE API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// role device/host is used by OS NONE for mutex (disable usb isr) only // _int_set is not used with an RTOS
#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ #define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
static _type _name##_##buf[_depth];\ static _type _name##_##buf[_depth];\
osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf }; osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf };
@@ -130,17 +130,18 @@ typedef struct
typedef QueueHandle_t osal_queue_t; typedef QueueHandle_t osal_queue_t;
static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
{ {
return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq); return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
} }
static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
{ {
return xQueueReceive(qhdl, data, portMAX_DELAY); uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec);
return xQueueReceive(qhdl, data, ticks);
} }
static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
{ {
if ( !in_isr ) if ( !in_isr )
{ {
@@ -162,7 +163,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in
} }
} }
static inline bool osal_queue_empty(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
{ {
return uxQueueMessagesWaiting(qhdl) == 0; return uxQueueMessagesWaiting(qhdl) == 0;
} }

View File

@@ -36,7 +36,7 @@
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// TASK API // TASK API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static inline void osal_task_delay(uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
{ {
os_time_delay( os_time_ms_to_ticks32(msec) ); os_time_delay( os_time_ms_to_ticks32(msec) );
} }
@@ -47,18 +47,18 @@ static inline void osal_task_delay(uint32_t msec)
typedef struct os_sem osal_semaphore_def_t; typedef struct os_sem osal_semaphore_def_t;
typedef struct os_sem* osal_semaphore_t; typedef struct os_sem* osal_semaphore_t;
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
{ {
return (os_sem_init(semdef, 0) == OS_OK) ? (osal_semaphore_t) semdef : NULL; return (os_sem_init(semdef, 0) == OS_OK) ? (osal_semaphore_t) semdef : NULL;
} }
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
{ {
(void) in_isr; (void) in_isr;
return os_sem_release(sem_hdl) == OS_OK; return os_sem_release(sem_hdl) == OS_OK;
} }
static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
{ {
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec); uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec);
return os_sem_pend(sem_hdl, ticks) == OS_OK; return os_sem_pend(sem_hdl, ticks) == OS_OK;
@@ -75,18 +75,18 @@ static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
typedef struct os_mutex osal_mutex_def_t; typedef struct os_mutex osal_mutex_def_t;
typedef struct os_mutex* osal_mutex_t; typedef struct os_mutex* osal_mutex_t;
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
{ {
return (os_mutex_init(mdef) == OS_OK) ? (osal_mutex_t) mdef : NULL; return (os_mutex_init(mdef) == OS_OK) ? (osal_mutex_t) mdef : NULL;
} }
static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
{ {
uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec); uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec);
return os_mutex_pend(mutex_hdl, ticks) == OS_OK; return os_mutex_pend(mutex_hdl, ticks) == OS_OK;
} }
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
{ {
return os_mutex_release(mutex_hdl) == OS_OK; return os_mutex_release(mutex_hdl) == OS_OK;
} }
@@ -116,7 +116,7 @@ typedef struct
typedef osal_queue_def_t* osal_queue_t; typedef osal_queue_def_t* osal_queue_t;
static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
{ {
if ( OS_OK != os_mempool_init(&qdef->mpool, qdef->depth, qdef->item_sz, qdef->buf, "usbd queue") ) return NULL; if ( OS_OK != os_mempool_init(&qdef->mpool, qdef->depth, qdef->item_sz, qdef->buf, "usbd queue") ) return NULL;
if ( OS_OK != os_mempool_init(&qdef->epool, qdef->depth, sizeof(struct os_event), qdef->evbuf, "usbd evqueue") ) return NULL; if ( OS_OK != os_mempool_init(&qdef->epool, qdef->depth, sizeof(struct os_event), qdef->evbuf, "usbd evqueue") ) return NULL;
@@ -125,8 +125,10 @@ static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
return (osal_queue_t) qdef; return (osal_queue_t) qdef;
} }
static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
{ {
(void) msec; // os_eventq_get() does not take timeout, always behave as msec = WAIT_FOREVER
struct os_event* ev; struct os_event* ev;
ev = os_eventq_get(&qhdl->evq); ev = os_eventq_get(&qhdl->evq);
@@ -161,7 +163,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in
return true; return true;
} }
static inline bool osal_queue_empty(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
{ {
return STAILQ_EMPTY(&qhdl->evq.evq_list); return STAILQ_EMPTY(&qhdl->evq.evq_list);
} }

View File

@@ -46,13 +46,13 @@ typedef struct
typedef osal_semaphore_def_t* osal_semaphore_t; typedef osal_semaphore_def_t* osal_semaphore_t;
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
{ {
semdef->count = 0; semdef->count = 0;
return semdef; return semdef;
} }
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
{ {
(void) in_isr; (void) in_isr;
sem_hdl->count++; sem_hdl->count++;
@@ -60,7 +60,7 @@ static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
} }
// TODO blocking for now // TODO blocking for now
static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
{ {
(void) msec; (void) msec;
@@ -70,7 +70,7 @@ static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
return true; return true;
} }
static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
{ {
sem_hdl->count = 0; sem_hdl->count = 0;
} }
@@ -82,18 +82,18 @@ static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
typedef osal_semaphore_def_t osal_mutex_def_t; typedef osal_semaphore_def_t osal_mutex_def_t;
typedef osal_semaphore_t osal_mutex_t; typedef osal_semaphore_t osal_mutex_t;
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
{ {
mdef->count = 1; mdef->count = 1;
return mdef; return mdef;
} }
static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
{ {
return osal_semaphore_wait(mutex_hdl, msec); return osal_semaphore_wait(mutex_hdl, msec);
} }
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
{ {
return osal_semaphore_post(mutex_hdl, false); return osal_semaphore_post(mutex_hdl, false);
} }
@@ -120,27 +120,29 @@ typedef osal_queue_def_t* osal_queue_t;
} }
// lock queue by disable USB interrupt // lock queue by disable USB interrupt
static inline void _osal_q_lock(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl)
{ {
// disable dcd/hcd interrupt // disable dcd/hcd interrupt
qhdl->interrupt_set(false); qhdl->interrupt_set(false);
} }
// unlock queue // unlock queue
static inline void _osal_q_unlock(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl)
{ {
// enable dcd/hcd interrupt // enable dcd/hcd interrupt
qhdl->interrupt_set(true); qhdl->interrupt_set(true);
} }
static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
{ {
tu_fifo_clear(&qdef->ff); tu_fifo_clear(&qdef->ff);
return (osal_queue_t) qdef; return (osal_queue_t) qdef;
} }
static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
{ {
(void) msec; // not used, always behave as msec = 0
_osal_q_lock(qhdl); _osal_q_lock(qhdl);
bool success = tu_fifo_read(&qhdl->ff, data); bool success = tu_fifo_read(&qhdl->ff, data);
_osal_q_unlock(qhdl); _osal_q_unlock(qhdl);
@@ -148,7 +150,7 @@ static inline bool osal_queue_receive(osal_queue_t qhdl, void* data)
return success; return success;
} }
static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
{ {
if (!in_isr) { if (!in_isr) {
_osal_q_lock(qhdl); _osal_q_lock(qhdl);
@@ -165,7 +167,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in
return success; return success;
} }
static inline bool osal_queue_empty(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
{ {
// Skip queue lock/unlock since this function is primarily called // Skip queue lock/unlock since this function is primarily called
// with interrupt disabled before going into low power mode // with interrupt disabled before going into low power mode

View File

@@ -39,7 +39,7 @@
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// TASK API // TASK API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static inline void osal_task_delay(uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
{ {
sleep_ms(msec); sleep_ms(msec);
} }
@@ -49,25 +49,25 @@ static inline void osal_task_delay(uint32_t msec)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
typedef struct semaphore osal_semaphore_def_t, *osal_semaphore_t; typedef struct semaphore osal_semaphore_def_t, *osal_semaphore_t;
static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
{ {
sem_init(semdef, 0, 255); sem_init(semdef, 0, 255);
return semdef; return semdef;
} }
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
{ {
(void) in_isr; (void) in_isr;
sem_release(sem_hdl); sem_release(sem_hdl);
return true; return true;
} }
static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
{ {
return sem_acquire_timeout_ms(sem_hdl, msec); return sem_acquire_timeout_ms(sem_hdl, msec);
} }
static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
{ {
sem_reset(sem_hdl, 0); sem_reset(sem_hdl, 0);
} }
@@ -78,18 +78,18 @@ static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
typedef struct mutex osal_mutex_def_t, *osal_mutex_t; typedef struct mutex osal_mutex_def_t, *osal_mutex_t;
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
{ {
mutex_init(mdef); mutex_init(mdef);
return mdef; return mdef;
} }
static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
{ {
return mutex_enter_timeout_ms(mutex_hdl, msec); return mutex_enter_timeout_ms(mutex_hdl, msec);
} }
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
{ {
mutex_exit(mutex_hdl); mutex_exit(mutex_hdl);
return true; return true;
@@ -121,26 +121,28 @@ typedef osal_queue_def_t* osal_queue_t;
} }
// lock queue by disable USB interrupt // lock queue by disable USB interrupt
static inline void _osal_q_lock(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl)
{ {
critical_section_enter_blocking(&qhdl->critsec); critical_section_enter_blocking(&qhdl->critsec);
} }
// unlock queue // unlock queue
static inline void _osal_q_unlock(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl)
{ {
critical_section_exit(&qhdl->critsec); critical_section_exit(&qhdl->critsec);
} }
static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
{ {
critical_section_init(&qdef->critsec); critical_section_init(&qdef->critsec);
tu_fifo_clear(&qdef->ff); tu_fifo_clear(&qdef->ff);
return (osal_queue_t) qdef; return (osal_queue_t) qdef;
} }
static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
{ {
(void) msec; // not used, always behave as msec = 0
// TODO: revisit... docs say that mutexes are never used from IRQ context, // TODO: revisit... docs say that mutexes are never used from IRQ context,
// however osal_queue_recieve may be. therefore my assumption is that // however osal_queue_recieve may be. therefore my assumption is that
// the fifo mutex is not populated for queues used from an IRQ context // the fifo mutex is not populated for queues used from an IRQ context
@@ -153,7 +155,7 @@ static inline bool osal_queue_receive(osal_queue_t qhdl, void* data)
return success; return success;
} }
static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
{ {
// TODO: revisit... docs say that mutexes are never used from IRQ context, // TODO: revisit... docs say that mutexes are never used from IRQ context,
// however osal_queue_recieve may be. therefore my assumption is that // however osal_queue_recieve may be. therefore my assumption is that
@@ -170,7 +172,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in
return success; return success;
} }
static inline bool osal_queue_empty(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
{ {
// TODO: revisit; whether this is true or not currently, tu_fifo_empty is a single // TODO: revisit; whether this is true or not currently, tu_fifo_empty is a single
// volatile read. // volatile read.

View File

@@ -37,7 +37,7 @@ extern "C" {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// TASK API // TASK API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static inline void osal_task_delay(uint32_t msec) { TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) {
rt_thread_mdelay(msec); rt_thread_mdelay(msec);
} }
@@ -47,22 +47,22 @@ static inline void osal_task_delay(uint32_t msec) {
typedef struct rt_semaphore osal_semaphore_def_t; typedef struct rt_semaphore osal_semaphore_def_t;
typedef rt_sem_t osal_semaphore_t; typedef rt_sem_t osal_semaphore_t;
static inline osal_semaphore_t TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t
osal_semaphore_create(osal_semaphore_def_t *semdef) { osal_semaphore_create(osal_semaphore_def_t *semdef) {
rt_sem_init(semdef, "tusb", 0, RT_IPC_FLAG_FIFO); rt_sem_init(semdef, "tusb", 0, RT_IPC_FLAG_FIFO);
return semdef; return semdef;
} }
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
(void) in_isr; (void) in_isr;
return rt_sem_release(sem_hdl) == RT_EOK; return rt_sem_release(sem_hdl) == RT_EOK;
} }
static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) {
return rt_sem_take(sem_hdl, rt_tick_from_millisecond(msec)) == RT_EOK; return rt_sem_take(sem_hdl, rt_tick_from_millisecond(msec)) == RT_EOK;
} }
static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
// TODO: implement // TODO: implement
} }
@@ -72,16 +72,16 @@ static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
typedef struct rt_mutex osal_mutex_def_t; typedef struct rt_mutex osal_mutex_def_t;
typedef rt_mutex_t osal_mutex_t; typedef rt_mutex_t osal_mutex_t;
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) { TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) {
rt_mutex_init(mdef, "tusb", RT_IPC_FLAG_FIFO); rt_mutex_init(mdef, "tusb", RT_IPC_FLAG_FIFO);
return mdef; return mdef;
} }
static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) {
return rt_mutex_take(mutex_hdl, rt_tick_from_millisecond(msec)) == RT_EOK; return rt_mutex_take(mutex_hdl, rt_tick_from_millisecond(msec)) == RT_EOK;
} }
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) {
return rt_mutex_release(mutex_hdl) == RT_EOK; return rt_mutex_release(mutex_hdl) == RT_EOK;
} }
@@ -104,22 +104,24 @@ typedef struct {
typedef rt_mq_t osal_queue_t; typedef rt_mq_t osal_queue_t;
static inline osal_queue_t osal_queue_create(osal_queue_def_t *qdef) { TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t *qdef) {
rt_mq_init(&(qdef->sq), "tusb", qdef->buf, qdef->item_sz, rt_mq_init(&(qdef->sq), "tusb", qdef->buf, qdef->item_sz,
qdef->item_sz * qdef->depth, RT_IPC_FLAG_FIFO); qdef->item_sz * qdef->depth, RT_IPC_FLAG_FIFO);
return &(qdef->sq); return &(qdef->sq);
} }
static inline bool osal_queue_receive(osal_queue_t qhdl, void *data) { TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec) {
return rt_mq_recv(qhdl, data, qhdl->msg_size, RT_WAITING_FOREVER) == RT_EOK;
rt_tick_t tick = rt_tick_from_millisecond((rt_int32_t) msec));
return rt_mq_recv(qhdl, data, qhdl->msg_size, tick) == RT_EOK;
} }
static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) { TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) {
(void) in_isr; (void) in_isr;
return rt_mq_send(qhdl, (void *)data, qhdl->msg_size) == RT_EOK; return rt_mq_send(qhdl, (void *)data, qhdl->msg_size) == RT_EOK;
} }
static inline bool osal_queue_empty(osal_queue_t qhdl) { TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) {
return (qhdl->entry) == 0; return (qhdl->entry) == 0;
} }

View File

@@ -37,7 +37,7 @@ extern "C" {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// TASK API // TASK API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static inline void osal_task_delay(uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
{ {
uint16_t hi = msec >> 16; uint16_t hi = msec >> 16;
uint16_t lo = msec; uint16_t lo = msec;
@@ -47,7 +47,7 @@ static inline void osal_task_delay(uint32_t msec)
os_dly_wait(lo); os_dly_wait(lo);
} }
static inline uint16_t msec2wait(uint32_t msec) { TU_ATTR_ALWAYS_INLINE static inline uint16_t msec2wait(uint32_t msec) {
if (msec == OSAL_TIMEOUT_WAIT_FOREVER) if (msec == OSAL_TIMEOUT_WAIT_FOREVER)
return 0xFFFF; return 0xFFFF;
else if (msec >= 0xFFFE) else if (msec >= 0xFFFE)
@@ -62,12 +62,12 @@ static inline uint16_t msec2wait(uint32_t msec) {
typedef OS_SEM osal_semaphore_def_t; typedef OS_SEM osal_semaphore_def_t;
typedef OS_ID osal_semaphore_t; typedef OS_ID osal_semaphore_t;
static inline OS_ID osal_semaphore_create(osal_semaphore_def_t* semdef) { TU_ATTR_ALWAYS_INLINE static inline OS_ID osal_semaphore_create(osal_semaphore_def_t* semdef) {
os_sem_init(semdef, 0); os_sem_init(semdef, 0);
return semdef; return semdef;
} }
static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
if ( !in_isr ) { if ( !in_isr ) {
os_sem_send(sem_hdl); os_sem_send(sem_hdl);
} else { } else {
@@ -76,11 +76,11 @@ static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) {
return true; return true;
} }
static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) { TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) {
return os_sem_wait(sem_hdl, msec2wait(msec)) != OS_R_TMO; return os_sem_wait(sem_hdl, msec2wait(msec)) != OS_R_TMO;
} }
static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
// TODO: implement // TODO: implement
} }
@@ -90,18 +90,18 @@ static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
typedef OS_MUT osal_mutex_def_t; typedef OS_MUT osal_mutex_def_t;
typedef OS_ID osal_mutex_t; typedef OS_ID osal_mutex_t;
static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
{ {
os_mut_init(mdef); os_mut_init(mdef);
return mdef; return mdef;
} }
static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
{ {
return os_mut_wait(mutex_hdl, msec2wait(msec)) != OS_R_TMO; return os_mut_wait(mutex_hdl, msec2wait(msec)) != OS_R_TMO;
} }
static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl)
{ {
return os_mut_release(mutex_hdl) == OS_R_OK; return os_mut_release(mutex_hdl) == OS_R_OK;
} }
@@ -127,23 +127,23 @@ typedef struct
typedef osal_queue_def_t* osal_queue_t; typedef osal_queue_def_t* osal_queue_t;
static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
{ {
os_mbx_init(qdef->mbox, (qdef->depth + 4) * 4); os_mbx_init(qdef->mbox, (qdef->depth + 4) * 4);
_init_box(qdef->pool, ((qdef->item_sz+3)/4)*(qdef->depth) + 3, qdef->item_sz); _init_box(qdef->pool, ((qdef->item_sz+3)/4)*(qdef->depth) + 3, qdef->item_sz);
return qdef; return qdef;
} }
static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
{ {
void* buf; void* buf;
os_mbx_wait(qhdl->mbox, &buf, 0xFFFF); os_mbx_wait(qhdl->mbox, &buf, msec2wait(msec));
memcpy(data, buf, qhdl->item_sz); memcpy(data, buf, qhdl->item_sz);
_free_box(qhdl->pool, buf); _free_box(qhdl->pool, buf);
return true; return true;
} }
static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr)
{ {
void* buf = _alloc_box(qhdl->pool); void* buf = _alloc_box(qhdl->pool);
memcpy(buf, data, qhdl->item_sz); memcpy(buf, data, qhdl->item_sz);
@@ -158,7 +158,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in
return true; return true;
} }
static inline bool osal_queue_empty(osal_queue_t qhdl) TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl)
{ {
return os_mbx_check(qhdl->mbox) == qhdl->depth; return os_mbx_check(qhdl->mbox) == qhdl->depth;
} }

View File

@@ -363,7 +363,7 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t to
if (usbdcd_driver.setup_processed) if (usbdcd_driver.setup_processed)
{ {
if (osal_queue_receive(usbdcd_driver.setup_queue, &ctrl)) if (osal_queue_receive(usbdcd_driver.setup_queue, &ctrl, 100))
{ {
usbdcd_driver.setup_processed = false; usbdcd_driver.setup_processed = false;
dcd_event_setup_received(0, (uint8_t *)&ctrl, false); dcd_event_setup_received(0, (uint8_t *)&ctrl, false);