From 4e8d414bc666d4636efd29e1c9feffd622d055c5 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 13:38:41 +0700 Subject: [PATCH 1/5] added osal_queue_empty() API ported for osal none/freertos/mynewt --- src/osal/osal.h | 1 + src/osal/osal_freertos.h | 13 +++++++++---- src/osal/osal_mynewt.h | 6 ++++++ src/osal/osal_none.h | 12 ++++++++++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/osal/osal.h b/src/osal/osal.h index 0421b3294..3729ad94d 100644 --- a/src/osal/osal.h +++ b/src/osal/osal.h @@ -80,6 +80,7 @@ static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl); static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef); static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data); static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, bool in_isr); +static inline bool osal_queue_empty(osal_queue_t const qhdl); #if 0 // TODO remove subtask related macros later // Sub Task diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index 240206e33..839c1924f 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -118,14 +118,19 @@ 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); } -static inline bool osal_queue_receive(osal_queue_t const queue_hdl, void* data) +static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data) { - return xQueueReceive(queue_hdl, data, portMAX_DELAY); + return xQueueReceive(qhdl, data, portMAX_DELAY); } -static inline bool osal_queue_send(osal_queue_t const queue_hdl, void const * data, bool in_isr) +static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, bool in_isr) { - return in_isr ? xQueueSendToBackFromISR(queue_hdl, data, NULL) : xQueueSendToBack(queue_hdl, data, OSAL_TIMEOUT_WAIT_FOREVER); + return in_isr ? xQueueSendToBackFromISR(qhdl, data, NULL) : xQueueSendToBack(qhdl, data, OSAL_TIMEOUT_WAIT_FOREVER); +} + +static inline bool osal_queue_empty(osal_queue_t const qhdl) +{ + return uxQueueMessagesWaiting(qhdl) > 0; } #ifdef __cplusplus diff --git a/src/osal/osal_mynewt.h b/src/osal/osal_mynewt.h index e47e140c1..f7c871c4b 100644 --- a/src/osal/osal_mynewt.h +++ b/src/osal/osal_mynewt.h @@ -161,6 +161,12 @@ static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, b return true; } +static inline bool osal_queue_empty(osal_queue_t const qhdl) +{ + return STAILQ_EMPTY(&qhdl->evq.evq_list); +} + + #ifdef __cplusplus } #endif diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h index b27e628a9..c9b762915 100644 --- a/src/osal/osal_none.h +++ b/src/osal/osal_none.h @@ -142,7 +142,7 @@ typedef osal_queue_def_t* osal_queue_t; }\ } -// lock queue by disable usb isr +// lock queue by disable USB interrupt static inline void _osal_q_lock(osal_queue_t qhdl) { (void) qhdl; @@ -176,7 +176,6 @@ static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) return (osal_queue_t) qdef; } -// non blocking static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data) { _osal_q_lock(qhdl); @@ -203,6 +202,15 @@ static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, b return success; } +static inline bool osal_queue_empty(osal_queue_t const qhdl) +{ + _osal_q_lock(qhdl); + bool is_empty = tu_fifo_empty(&qhdl->ff); + _osal_q_unlock(qhdl); + + return is_empty; +} + #ifdef __cplusplus } #endif From 88a455a9b99099bf27ce239006ecfd2da3894bcf Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 14:31:45 +0700 Subject: [PATCH 2/5] added tud_task_event_ready() to check if there is pending events in the tud task without executing it. Useful to check before entering low power mode with WFI/WFE --- src/device/usbd.c | 8 ++++++++ src/device/usbd.h | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/device/usbd.c b/src/device/usbd.c index 06e724cb3..f7918c07e 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -339,6 +339,14 @@ static void usbd_reset(uint8_t rhport) } } +bool tud_task_event_ready(void) +{ + // Skip if stack is not initialized + if ( !tusb_inited() ) return false; + + return !osal_queue_empty(_usbd_q); +} + /* USB Device Driver task * This top level thread manages all device controller event and delegates events to class-specific drivers. * This should be called periodically within the mainloop or rtos thread. diff --git a/src/device/usbd.h b/src/device/usbd.h index 713bee2b2..ecef63e18 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -47,6 +47,9 @@ bool tud_init (void); // Task function should be called in main/rtos loop void tud_task (void); +// Check if there is pending events need proccessing by tud_task() +bool tud_task_event_ready(void); + // Interrupt handler, name alias to DCD #define tud_int_handler dcd_int_handler From d8a15aca777abb0094935440f7cd551b36d62f43 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 14:38:34 +0700 Subject: [PATCH 3/5] remove osal_queue_t const qhdl from osal API since it doesn't make any differences. --- src/osal/osal.h | 6 +++--- src/osal/osal_freertos.h | 6 +++--- src/osal/osal_mynewt.h | 6 +++--- src/osal/osal_none.h | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/osal/osal.h b/src/osal/osal.h index 3729ad94d..2e7a3539f 100644 --- a/src/osal/osal.h +++ b/src/osal/osal.h @@ -78,9 +78,9 @@ static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl); //------------- Queue -------------// static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef); -static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data); -static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, bool in_isr); -static inline bool osal_queue_empty(osal_queue_t const qhdl); +static inline bool osal_queue_receive(osal_queue_t qhdl, void* data); +static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr); +static inline bool osal_queue_empty(osal_queue_t qhdl); #if 0 // TODO remove subtask related macros later // Sub Task diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index 839c1924f..d2d5f51c9 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -118,17 +118,17 @@ 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); } -static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data) +static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) { return xQueueReceive(qhdl, data, portMAX_DELAY); } -static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, bool in_isr) +static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) { return in_isr ? xQueueSendToBackFromISR(qhdl, data, NULL) : xQueueSendToBack(qhdl, data, OSAL_TIMEOUT_WAIT_FOREVER); } -static inline bool osal_queue_empty(osal_queue_t const qhdl) +static inline bool osal_queue_empty(osal_queue_t qhdl) { return uxQueueMessagesWaiting(qhdl) > 0; } diff --git a/src/osal/osal_mynewt.h b/src/osal/osal_mynewt.h index f7c871c4b..6882329c1 100644 --- a/src/osal/osal_mynewt.h +++ b/src/osal/osal_mynewt.h @@ -125,7 +125,7 @@ static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) return (osal_queue_t) qdef; } -static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data) +static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) { struct os_event* ev; ev = os_eventq_get(&qhdl->evq); @@ -137,7 +137,7 @@ static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data) return true; } -static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, bool in_isr) +static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) { (void) in_isr; @@ -161,7 +161,7 @@ static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, b return true; } -static inline bool osal_queue_empty(osal_queue_t const qhdl) +static inline bool osal_queue_empty(osal_queue_t qhdl) { return STAILQ_EMPTY(&qhdl->evq.evq_list); } diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h index c9b762915..3bbf15119 100644 --- a/src/osal/osal_none.h +++ b/src/osal/osal_none.h @@ -176,7 +176,7 @@ static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) return (osal_queue_t) qdef; } -static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data) +static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) { _osal_q_lock(qhdl); bool success = tu_fifo_read(&qhdl->ff, data); @@ -185,7 +185,7 @@ static inline bool osal_queue_receive(osal_queue_t const qhdl, void* data) return success; } -static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, bool in_isr) +static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) { if (!in_isr) { _osal_q_lock(qhdl); @@ -202,7 +202,7 @@ static inline bool osal_queue_send(osal_queue_t const qhdl, void const * data, b return success; } -static inline bool osal_queue_empty(osal_queue_t const qhdl) +static inline bool osal_queue_empty(osal_queue_t qhdl) { _osal_q_lock(qhdl); bool is_empty = tu_fifo_empty(&qhdl->ff); From a0fe3a80e74fceee6c97fcf734822280d1cfd62e Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 15:09:46 +0700 Subject: [PATCH 4/5] remove queue lock/unlock per review --- src/osal/osal_none.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h index 3bbf15119..0ac5e8aac 100644 --- a/src/osal/osal_none.h +++ b/src/osal/osal_none.h @@ -204,11 +204,9 @@ 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) { - _osal_q_lock(qhdl); - bool is_empty = tu_fifo_empty(&qhdl->ff); - _osal_q_unlock(qhdl); - - return is_empty; + // Skip queue lock/unlock since this function is primarily called + // with interrupt disabled before going into low power mode + return tu_fifo_empty(&qhdl->ff); } #ifdef __cplusplus From 8b66098335cb93504c7680fadad502b59ff73b48 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 15:21:11 +0700 Subject: [PATCH 5/5] fix freeRTOS logic --- src/osal/osal_freertos.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index d2d5f51c9..42ea1fd19 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -130,7 +130,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) { - return uxQueueMessagesWaiting(qhdl) > 0; + return uxQueueMessagesWaiting(qhdl) == 0; } #ifdef __cplusplus