From 88e3dc15b2604de0c337f2bf4911ee5e4780588f Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Wed, 27 May 2020 21:27:31 +0200 Subject: [PATCH] replaced old semaph api --- osal/FreeRTOS/BinSemaphUsingTask.cpp | 16 +++++++++++++--- osal/FreeRTOS/BinSemaphUsingTask.h | 3 +++ osal/FreeRTOS/BinarySemaphore.cpp | 22 ++++++++-------------- osal/FreeRTOS/BinarySemaphore.h | 11 ++++------- osal/FreeRTOS/CountingSemaphUsingTask.cpp | 15 ++++++++++++--- osal/FreeRTOS/CountingSemaphUsingTask.h | 6 +++--- osal/FreeRTOS/SemaphoreFactory.cpp | 8 ++++---- tasks/SemaphoreFactory.h | 2 +- 8 files changed, 48 insertions(+), 35 deletions(-) diff --git a/osal/FreeRTOS/BinSemaphUsingTask.cpp b/osal/FreeRTOS/BinSemaphUsingTask.cpp index d6bc02bc..8cdcbc33 100644 --- a/osal/FreeRTOS/BinSemaphUsingTask.cpp +++ b/osal/FreeRTOS/BinSemaphUsingTask.cpp @@ -4,6 +4,8 @@ BinarySemaphoreUsingTask::BinarySemaphoreUsingTask() { handle = TaskManagement::getCurrentTaskHandle(); + xTaskNotifyGive(handle); + locked = false; } ReturnValue_t BinarySemaphoreUsingTask::acquire(uint32_t timeoutMs) { @@ -25,6 +27,7 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphore(uint32_t timeoutMs) BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeout); if (returncode == pdPASS) { + locked = true; return HasReturnvaluesIF::RETURN_OK; } else { @@ -36,6 +39,7 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphoreTickTimeout( TickType_t timeoutTicks) { BaseType_t returncode = ulTaskNotifyTake(pdTRUE, timeoutTicks); if (returncode == pdPASS) { + locked = true; return HasReturnvaluesIF::RETURN_OK; } else { return SEMAPHORE_TIMEOUT; @@ -43,11 +47,15 @@ ReturnValue_t BinarySemaphoreUsingTask::takeBinarySemaphoreTickTimeout( } ReturnValue_t BinarySemaphoreUsingTask::giveBinarySemaphore() { + if(not locked) { + return SemaphoreIF::SEMAPHORE_NOT_OWNED; + } BaseType_t returncode = xTaskNotifyGive(handle); if (returncode == pdPASS) { return HasReturnvaluesIF::RETURN_OK; } else { - return SEMAPHORE_NOT_OWNED; + // This should never happen + return HasReturnvaluesIF::RETURN_FAILED; } } @@ -77,8 +85,10 @@ ReturnValue_t BinarySemaphoreUsingTask::giveBinarySemaphore(TaskHandle_t taskHan BaseType_t returncode = xTaskNotifyGive(taskHandle); if (returncode == pdPASS) { return HasReturnvaluesIF::RETURN_OK; - } else { - return SEMAPHORE_NOT_OWNED; + } + else { + // This should never happen. + return HasReturnvaluesIF::RETURN_FAILED; } } diff --git a/osal/FreeRTOS/BinSemaphUsingTask.h b/osal/FreeRTOS/BinSemaphUsingTask.h index 8b8a7773..e44b838c 100644 --- a/osal/FreeRTOS/BinSemaphUsingTask.h +++ b/osal/FreeRTOS/BinSemaphUsingTask.h @@ -85,6 +85,9 @@ public: protected: TaskHandle_t handle; + // This boolean is required to track whether the semaphore is locked + // or unlocked. + bool locked; }; diff --git a/osal/FreeRTOS/BinarySemaphore.cpp b/osal/FreeRTOS/BinarySemaphore.cpp index d56be763..fa54b71f 100644 --- a/osal/FreeRTOS/BinarySemaphore.cpp +++ b/osal/FreeRTOS/BinarySemaphore.cpp @@ -7,6 +7,7 @@ BinarySemaphore::BinarySemaphore() { if(handle == nullptr) { sif::error << "Semaphore: Binary semaph creation failure" << std::endl; } + // Initiated semaphore must be given before it can be taken. xSemaphoreGive(handle); } @@ -34,7 +35,7 @@ BinarySemaphore& BinarySemaphore::operator =( return *this; } -ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) { +ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) { if(handle == nullptr) { return SEMAPHORE_NULLPOINTER; } @@ -55,7 +56,7 @@ ReturnValue_t BinarySemaphore::takeBinarySemaphore(uint32_t timeoutMs) { } } -ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout( +ReturnValue_t BinarySemaphore::acquireWithTickTimeout( TickType_t timeoutTicks) { if(handle == nullptr) { return SEMAPHORE_NULLPOINTER; @@ -69,7 +70,7 @@ ReturnValue_t BinarySemaphore::takeBinarySemaphoreTickTimeout( } } -ReturnValue_t BinarySemaphore::giveBinarySemaphore() { +ReturnValue_t BinarySemaphore::release() { if (handle == nullptr) { return SEMAPHORE_NULLPOINTER; } @@ -96,26 +97,19 @@ ReturnValue_t BinarySemaphore::giveBinarySemaphore(SemaphoreHandle_t semaphore) return HasReturnvaluesIF::RETURN_FAILED; } } - -ReturnValue_t BinarySemaphore::acquire(uint32_t timeoutMs) { - return takeBinarySemaphore(timeoutMs); -} - -ReturnValue_t BinarySemaphore::release() { - return giveBinarySemaphore(); -} uint8_t BinarySemaphore::getSemaphoreCounter() { return uxSemaphoreGetCount(handle); } // Be careful with the stack size here. This is called from an ISR! -ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR(SemaphoreHandle_t semaphore, - BaseType_t * higherPriorityTaskWoken) { +ReturnValue_t BinarySemaphore::giveBinarySemaphoreFromISR( + SemaphoreHandle_t semaphore, BaseType_t * higherPriorityTaskWoken) { if (semaphore == nullptr) { return SEMAPHORE_NULLPOINTER; } - BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, higherPriorityTaskWoken); + BaseType_t returncode = xSemaphoreGiveFromISR(semaphore, + higherPriorityTaskWoken); if (returncode == pdPASS) { if(*higherPriorityTaskWoken == pdPASS) { // Request context switch because unblocking the semaphore diff --git a/osal/FreeRTOS/BinarySemaphore.h b/osal/FreeRTOS/BinarySemaphore.h index e5c0ce4b..0c2fe3bf 100644 --- a/osal/FreeRTOS/BinarySemaphore.h +++ b/osal/FreeRTOS/BinarySemaphore.h @@ -43,9 +43,6 @@ public: //! @brief Destructor virtual ~BinarySemaphore(); - ReturnValue_t acquire(uint32_t timeoutMs = - SemaphoreIF::NO_TIMEOUT) override; - ReturnValue_t release() override; uint8_t getSemaphoreCounter() override; /** @@ -57,8 +54,8 @@ public: * @return -@c RETURN_OK on success * -@c RETURN_FAILED on failure */ - ReturnValue_t takeBinarySemaphore(uint32_t timeoutMs = - SemaphoreIF::NO_TIMEOUT); + ReturnValue_t acquire(uint32_t timeoutMs = + SemaphoreIF::NO_TIMEOUT) override; /** * Same as lockBinarySemaphore() with timeout in FreeRTOS ticks. @@ -66,7 +63,7 @@ public: * @return - @c RETURN_OK on success * - @c RETURN_FAILED on failure */ - ReturnValue_t takeBinarySemaphoreTickTimeout(TickType_t timeoutTicks = + ReturnValue_t acquireWithTickTimeout(TickType_t timeoutTicks = BinarySemaphore::NO_TIMEOUT); /** @@ -74,7 +71,7 @@ public: * @return - @c RETURN_OK on success * - @c RETURN_FAILED on failure */ - ReturnValue_t giveBinarySemaphore(); + ReturnValue_t release() override; /** * Get Handle to the semaphore. diff --git a/osal/FreeRTOS/CountingSemaphUsingTask.cpp b/osal/FreeRTOS/CountingSemaphUsingTask.cpp index 10365d7e..eecd986b 100644 --- a/osal/FreeRTOS/CountingSemaphUsingTask.cpp +++ b/osal/FreeRTOS/CountingSemaphUsingTask.cpp @@ -1,10 +1,19 @@ #include #include +#include -CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(uint8_t count, - uint8_t initCount): - count(count), initCount(initCount) { +CountingSemaphoreUsingTask::CountingSemaphoreUsingTask(uint8_t maxCount, + uint8_t initCount): maxCount(maxCount) { + if(initCount > maxCount) { + sif::error << "CountingSemaphoreUsingTask: Max count bigger than " + "intial cout. Setting initial count to max count." << std::endl; + initCount = maxCount; + } handle = TaskManagement::getCurrentTaskHandle(); + while(currentCount != initCount) { + xTaskNotifyGive(handle); + currentCount++; + } } ReturnValue_t CountingSemaphoreUsingTask::acquire( diff --git a/osal/FreeRTOS/CountingSemaphUsingTask.h b/osal/FreeRTOS/CountingSemaphUsingTask.h index 7fc98e93..8c881d69 100644 --- a/osal/FreeRTOS/CountingSemaphUsingTask.h +++ b/osal/FreeRTOS/CountingSemaphUsingTask.h @@ -11,7 +11,7 @@ extern "C" { class CountingSemaphoreUsingTask: public SemaphoreIF { public: - CountingSemaphoreUsingTask(uint8_t count, uint8_t initCount); + CountingSemaphoreUsingTask(uint8_t maxCount, uint8_t initCount); ReturnValue_t acquire(uint32_t timeoutMs) override; ReturnValue_t release() override; @@ -19,8 +19,8 @@ public: private: TaskHandle_t handle; - uint8_t count = 0; - uint8_t initCount = 0; + const uint8_t maxCount; + uint8_t currentCount = 0; }; #endif /* FRAMEWORK_OSAL_FREERTOS_COUNTINGSEMAPHUSINGTASK_H_ */ diff --git a/osal/FreeRTOS/SemaphoreFactory.cpp b/osal/FreeRTOS/SemaphoreFactory.cpp index 6acdc7b7..05a898cd 100644 --- a/osal/FreeRTOS/SemaphoreFactory.cpp +++ b/osal/FreeRTOS/SemaphoreFactory.cpp @@ -40,18 +40,18 @@ SemaphoreIF* SemaphoreFactory::createBinarySemaphore(uint32_t argument) { } } -SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t count, +SemaphoreIF* SemaphoreFactory::createCountingSemaphore(uint8_t maxCount, uint8_t initCount, uint32_t argument) { if(argument == USE_REGULAR_SEMAPHORES) { - return new CountingSemaphore(count, initCount); + return new CountingSemaphore(maxCount, initCount); } else if(argument == USE_TASK_NOTIFICATIONS) { - return new CountingSemaphoreUsingTask(count, initCount); + return new CountingSemaphoreUsingTask(maxCount, initCount); } else { sif::warning << "SemaphoreFactory: Invalid argument, return regular" "binary semaphore" << std::endl; - return new CountingSemaphore(count, initCount); + return new CountingSemaphore(maxCount, initCount); } } diff --git a/tasks/SemaphoreFactory.h b/tasks/SemaphoreFactory.h index 971e97fa..75bbe25d 100644 --- a/tasks/SemaphoreFactory.h +++ b/tasks/SemaphoreFactory.h @@ -33,7 +33,7 @@ public: * @param argument Can be used to pass implementation specific information. * @return */ - SemaphoreIF* createCountingSemaphore(uint8_t count, uint8_t initCount, + SemaphoreIF* createCountingSemaphore(uint8_t maxCount, uint8_t initCount, uint32_t argument = 0); void deleteSemaphore(SemaphoreIF* mutex);