From 4e8d414bc666d4636efd29e1c9feffd622d055c5 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 13:38:41 +0700 Subject: 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(-) (limited to 'src/osal') 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 -- cgit v1.3.1 From d8a15aca777abb0094935440f7cd551b36d62f43 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 14:38:34 +0700 Subject: 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(-) (limited to 'src/osal') 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); -- cgit v1.3.1 From a0fe3a80e74fceee6c97fcf734822280d1cfd62e Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 15:09:46 +0700 Subject: remove queue lock/unlock per review --- src/osal/osal_none.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src/osal') 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 -- cgit v1.3.1 From 8b66098335cb93504c7680fadad502b59ff73b48 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 May 2020 15:21:11 +0700 Subject: fix freeRTOS logic --- src/osal/osal_freertos.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/osal') 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 -- cgit v1.3.1 From 3822a6a385a99c88e1fb9f6293be9d2a84556a25 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Sun, 19 Jul 2020 23:55:35 +0300 Subject: FreeRTOS: Yield from ISR to notify the USB task If we do not yeld in ISR when we write to queue/give semaphore, the scheduler will not know of the change and will not check the queue untill the next OS tick. This change causes the task to be called immediately and makes communication many times faster. --- src/osal/osal_freertos.h | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src/osal') diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index 42ea1fd19..416065eeb 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -32,6 +32,7 @@ #include "semphr.h" #include "queue.h" #include "task.h" +#include "tusb_option.h" #ifdef __cplusplus extern "C" { @@ -58,7 +59,19 @@ static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semde static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { - return in_isr ? xSemaphoreGiveFromISR(sem_hdl, NULL) : xSemaphoreGive(sem_hdl); + if(!in_isr){ + return xSemaphoreGive(sem_hdl) != 0; + } + BaseType_t xHigherPriorityTaskWoken; + BaseType_t res = xSemaphoreGiveFromISR(sem_hdl, &xHigherPriorityTaskWoken); +#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 + if (xHigherPriorityTaskWoken) { + portYIELD_FROM_ISR(); + } +#else + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); +#endif + return res != 0; } static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) @@ -125,7 +138,19 @@ 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) { - return in_isr ? xQueueSendToBackFromISR(qhdl, data, NULL) : xQueueSendToBack(qhdl, data, OSAL_TIMEOUT_WAIT_FOREVER); + if(!in_isr){ + return xQueueSendToBack(qhdl, data, OSAL_TIMEOUT_WAIT_FOREVER) != 0; + } + BaseType_t xHigherPriorityTaskWoken; + BaseType_t res = xQueueSendToBackFromISR(qhdl, data, &xHigherPriorityTaskWoken); +#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 + if (xHigherPriorityTaskWoken) { + portYIELD_FROM_ISR(); + } +#else + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); +#endif + return res != 0; } static inline bool osal_queue_empty(osal_queue_t qhdl) -- cgit v1.3.1 From dfca92d29b8f984ebe5f4752154fd8187ec24120 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 21 Jul 2020 21:06:10 +0700 Subject: follow up to pr468 --- src/osal/osal_freertos.h | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'src/osal') diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index 416065eeb..004bd1b6b 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -32,7 +32,6 @@ #include "semphr.h" #include "queue.h" #include "task.h" -#include "tusb_option.h" #ifdef __cplusplus extern "C" { @@ -59,19 +58,23 @@ static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semde static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { - if(!in_isr){ + if ( !in_isr ) + { return xSemaphoreGive(sem_hdl) != 0; } - BaseType_t xHigherPriorityTaskWoken; - BaseType_t res = xSemaphoreGiveFromISR(sem_hdl, &xHigherPriorityTaskWoken); + else + { + BaseType_t xHigherPriorityTaskWoken; + BaseType_t res = xSemaphoreGiveFromISR(sem_hdl, &xHigherPriorityTaskWoken); + #if CFG_TUSB_MCU == OPT_MCU_ESP32S2 - if (xHigherPriorityTaskWoken) { - portYIELD_FROM_ISR(); - } + if ( xHigherPriorityTaskWoken ) portYIELD_FROM_ISR(); #else - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); #endif - return res != 0; + + return res != 0; + } } static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) @@ -138,19 +141,23 @@ 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) { - if(!in_isr){ + if ( !in_isr ) + { return xQueueSendToBack(qhdl, data, OSAL_TIMEOUT_WAIT_FOREVER) != 0; } - BaseType_t xHigherPriorityTaskWoken; - BaseType_t res = xQueueSendToBackFromISR(qhdl, data, &xHigherPriorityTaskWoken); + else + { + BaseType_t xHigherPriorityTaskWoken; + BaseType_t res = xQueueSendToBackFromISR(qhdl, data, &xHigherPriorityTaskWoken); + #if CFG_TUSB_MCU == OPT_MCU_ESP32S2 - if (xHigherPriorityTaskWoken) { - portYIELD_FROM_ISR(); - } + if ( xHigherPriorityTaskWoken ) portYIELD_FROM_ISR(); #else - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); #endif - return res != 0; + + return res != 0; + } } static inline bool osal_queue_empty(osal_queue_t qhdl) -- cgit v1.3.1