From 8a1e0dab0370c5b454fcb9e95de0f1349000b380 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Wed, 27 May 2020 19:03:46 +0200 Subject: [PATCH] continued with task notifications --- osal/FreeRTOS/BinarySemaphore.cpp | 48 ++++++++++++++++++++++++++--- osal/FreeRTOS/BinarySemaphore.h | 8 +++-- osal/FreeRTOS/CountingSemaphore.cpp | 4 +++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/osal/FreeRTOS/BinarySemaphore.cpp b/osal/FreeRTOS/BinarySemaphore.cpp index f7e879830..150f8c993 100644 --- a/osal/FreeRTOS/BinarySemaphore.cpp +++ b/osal/FreeRTOS/BinarySemaphore.cpp @@ -2,7 +2,7 @@ #include #include -#if ( configUSE_OLD_SEMAPHORES == 1 ) +#if ( configUSE_TASK_NOTIFICATIONS == 0 ) BinarySemaphore::BinarySemaphore() { handle = xSemaphoreCreateBinary(); @@ -174,9 +174,6 @@ ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout( } ReturnValue_t BinarySemaphore::giveBinarySemaphore() { - if (handle == nullptr) { - return SEMAPHORE_NULLPOINTER; - } BaseType_t returncode = xTaskNotifyGive(handle); if (returncode == pdPASS) { return HasReturnvaluesIF::RETURN_OK; @@ -185,4 +182,47 @@ ReturnValue_t BinarySemaphore::giveBinarySemaphore() { } } +TaskHandle_t BinarySemaphore::getTaskHandle() { + return handle; +} + +uint8_t BinarySemaphore::getSemaphoreCounter() { + uint32_t notificationValue; + xTaskNotifyAndQuery(handle, 0, eNoAction, ¬ificationValue); + return notificationValue; +} + +uint8_t BinarySemaphore::getSemaphoreCounterFromISR(TaskHandle_t taskHandle) { + uint32_t notificationValue; + BaseType_t higherPriorityTaskWoken; + xTaskNotifyAndQueryFromISR(taskHandle, 0, eNoAction, ¬ificationValue, + &higherPriorityTaskWoken); + if(higherPriorityTaskWoken) { + TaskManagement::requestContextSwitch(CallContext::isr); + } + return notificationValue; +} + + +ReturnValue_t BinarySemaphore::giveBinarySemaphore(TaskHandle_t taskHandle) { + BaseType_t returncode = xTaskNotifyGive(taskHandle); + if (returncode == pdPASS) { + return HasReturnvaluesIF::RETURN_OK; + } else { + return SEMAPHORE_NOT_OWNED; + } +} + +// Be careful with the stack size here. This is called from an ISR! +ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR( + TaskHandle_t taskHandle, BaseType_t * higherPriorityTaskWoken) { + vTaskNotifyGiveFromISR(taskHandle, higherPriorityTaskWoken); + if(*higherPriorityTaskWoken == pdPASS) { + // Request context switch because unblocking the semaphore + // caused a high priority task unblock. + TaskManagement::requestContextSwitch(CallContext::isr); + } + return HasReturnvaluesIF::RETURN_OK; +} + #endif diff --git a/osal/FreeRTOS/BinarySemaphore.h b/osal/FreeRTOS/BinarySemaphore.h index 4d95fc27c..2ee102267 100644 --- a/osal/FreeRTOS/BinarySemaphore.h +++ b/osal/FreeRTOS/BinarySemaphore.h @@ -6,7 +6,7 @@ extern "C" { #include -#if ( configUSE_OLD_SEMAPHORES == 1 ) +#if ( configUSE_TASK_NOTIFICATIONS == 0 ) #include #else #include @@ -28,7 +28,7 @@ extern "C" { * @author R. Mueller * @ingroup osal */ -#if ( configUSE_OLD_SEMAPHORES == 1 ) +#if ( configUSE_TASK_NOTIFICATIONS == 0 ) class BinarySemaphore: public SemaphoreIF, public HasReturnvaluesIF { @@ -155,7 +155,7 @@ public: ReturnValue_t giveBinarySemaphore(); /** - * Get Handle to the semaphore. + * Get handle to the task related to the semaphore. * @return */ TaskHandle_t getTaskHandle(); @@ -179,6 +179,8 @@ public: static ReturnValue_t giveBinarySemaphoreFromISR(TaskHandle_t taskToNotify, BaseType_t * higherPriorityTaskWoken); + static uint8_t getSemaphoreCounterFromISR(TaskHandle_t taskHandle); + protected: TaskHandle_t handle; }; diff --git a/osal/FreeRTOS/CountingSemaphore.cpp b/osal/FreeRTOS/CountingSemaphore.cpp index 2b81848a2..e04d3defd 100644 --- a/osal/FreeRTOS/CountingSemaphore.cpp +++ b/osal/FreeRTOS/CountingSemaphore.cpp @@ -1,6 +1,10 @@ #include #include +extern "C" { +#include +} + // Make sure #define configUSE_COUNTING_SEMAPHORES 1 is set in // free FreeRTOSConfig.h file. CountingSemaphore::CountingSemaphore(uint8_t count, uint8_t initCount):