From 08fec8e3e9fbebacf560675a7ed9c247ac382ae3 Mon Sep 17 00:00:00 2001 From: dario Date: Tue, 2 Jan 2024 00:56:22 +0100 Subject: [PATCH 1/2] Updated id handling for mutex and signal, sleep() for thread. --- include/sta/rtos/mutex.hpp | 11 +++++++++-- include/sta/rtos/signal.hpp | 4 ++-- include/sta/rtos/thread.hpp | 8 ++++++++ src/mutex.cpp | 16 +++++++++++++--- src/signal.cpp | 10 +++++----- src/thread.cpp | 4 ++++ 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/include/sta/rtos/mutex.hpp b/include/sta/rtos/mutex.hpp index c60f34c..263ff0d 100644 --- a/include/sta/rtos/mutex.hpp +++ b/include/sta/rtos/mutex.hpp @@ -23,13 +23,20 @@ namespace sta /** * @param handle CMSIS RTOS2 mutex */ - RtosMutex(osMutexId_t * handle); + RtosMutex(osMutexId_t handle); + + /** + * @brief Construct a new Rtos Mutex object + * + * @param name The name of the mutex. + */ + RtosMutex(const char* name); void acquire() override; void release() override; private: - osMutexId_t * handle_; /**< CMSIS RTOS2 mutex */ + osMutexId_t handle_; /**< CMSIS RTOS2 mutex */ }; } // namespace sta diff --git a/include/sta/rtos/signal.hpp b/include/sta/rtos/signal.hpp index ea54513..1c9625f 100644 --- a/include/sta/rtos/signal.hpp +++ b/include/sta/rtos/signal.hpp @@ -23,7 +23,7 @@ namespace sta /** * @param semaphore CMSIS RTOS2 semaphore */ - RtosSignal(osSemaphoreId_t * semaphore); + RtosSignal(osSemaphoreId_t semaphore); void notify() override; bool peek() override; @@ -31,7 +31,7 @@ namespace sta void wait() override; private: - osSemaphoreId_t * semaphore_; /**< CMSIS RTOS2 semaphore */ + osSemaphoreId_t semaphore_; /**< CMSIS RTOS2 semaphore */ }; } // namespace sta diff --git a/include/sta/rtos/thread.hpp b/include/sta/rtos/thread.hpp index 0013ccb..8aeffa4 100644 --- a/include/sta/rtos/thread.hpp +++ b/include/sta/rtos/thread.hpp @@ -74,6 +74,14 @@ namespace sta */ RtosThread(const Handle & handle); + /** + * @brief Sets the thread to BLOCKED for a given number of ticks. + * Other threads can run in the meantime. + * + * @param ticks The number of ticks before setting the thread to READY again. + */ + void sleep(uint32_t ticks); + /** * @brief Send user notification flags to thread. * diff --git a/src/mutex.cpp b/src/mutex.cpp index faa5067..8252d7e 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -1,19 +1,29 @@ #include +#include + namespace sta { - RtosMutex::RtosMutex(osMutexId_t * handle) + RtosMutex::RtosMutex(osMutexId_t handle) : handle_{handle} {} + RtosMutex::RtosMutex(const char* name) + { + osMutexAttr_t attribs = { .name = "uartMutex"}; + handle_ = osMutexNew(&attribs); + + STA_ASSERT(handle_ != NULL); + } + void RtosMutex::acquire() { - osMutexAcquire(*handle_, osWaitForever); + osMutexAcquire(handle_, osWaitForever); } void RtosMutex::release() { - osMutexRelease(*handle_); + osMutexRelease(handle_); } } // namespace sta diff --git a/src/signal.cpp b/src/signal.cpp index 7bd50fe..6047088 100644 --- a/src/signal.cpp +++ b/src/signal.cpp @@ -3,27 +3,27 @@ namespace sta { - RtosSignal::RtosSignal(osSemaphoreId_t * semaphore) + RtosSignal::RtosSignal(osSemaphoreId_t semaphore) : semaphore_{semaphore} {} void RtosSignal::notify() { - osSemaphoreRelease(*semaphore_); + osSemaphoreRelease(semaphore_); } bool RtosSignal::peek() { - return (osSemaphoreGetCount(*semaphore_) != 0); + return (osSemaphoreGetCount(semaphore_) != 0); } bool RtosSignal::test() { - return (osSemaphoreAcquire(*semaphore_, 0) == osOK); + return (osSemaphoreAcquire(semaphore_, 0) == osOK); } void RtosSignal::wait() { - osSemaphoreAcquire(*semaphore_, osWaitForever); + osSemaphoreAcquire(semaphore_, osWaitForever); } } // namespace sta diff --git a/src/thread.cpp b/src/thread.cpp index 3b1927b..1850d83 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -17,6 +17,10 @@ namespace sta terminate_{false} {} + void RtosThread::sleep(uint32_t ticks) + { + osDelay(ticks); + } void RtosThread::notify(uint32_t flags) { From aacf39a32c62a708f74012cd6a7b62745aa9d706 Mon Sep 17 00:00:00 2001 From: dario Date: Tue, 2 Jan 2024 11:48:52 +0100 Subject: [PATCH 2/2] Changed mutex constructor to properly use the provided name --- src/mutex.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mutex.cpp b/src/mutex.cpp index 8252d7e..854b356 100644 --- a/src/mutex.cpp +++ b/src/mutex.cpp @@ -11,7 +11,7 @@ namespace sta RtosMutex::RtosMutex(const char* name) { - osMutexAttr_t attribs = { .name = "uartMutex"}; + osMutexAttr_t attribs = { .name = name}; handle_ = osMutexNew(&attribs); STA_ASSERT(handle_ != NULL);