From cf5466fe5546c4331889266956ed03cb0aad389b Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 15 Oct 2023 21:49:41 +0200 Subject: [PATCH 1/8] Changed execution order in ALPAKA initialization --- include/sta/rtos/debug.hpp | 28 ++++++++++++++++++++++++++++ src/system/startup.cpp | 6 +++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 include/sta/rtos/debug.hpp diff --git a/include/sta/rtos/debug.hpp b/include/sta/rtos/debug.hpp new file mode 100644 index 0000000..aa58cba --- /dev/null +++ b/include/sta/rtos/debug.hpp @@ -0,0 +1,28 @@ +/* + * debug.hpp + * + * Created on: Oct 15, 2023 + * Author: Dario + */ + +#ifndef RTOS2_UTILS_INCLUDE_STA_RTOS_DEBUG_HPP_ +#define RTOS2_UTILS_INCLUDE_STA_RTOS_DEBUG_HPP_ + +#include +#ifdef STA_DEBUGGING_ENABLED + +# define STA_DEBUG_THREADS() \ +{ \ + + +} + + +#else // !STA_DEBUGGING_ENABLED + +# define STA_DEBUG_THREADS() ((void)0) + + +#endif // STA_DEBUGGING_ENABLED + +#endif /* RTOS2_UTILS_INCLUDE_STA_RTOS_DEBUG_HPP_ */ diff --git a/src/system/startup.cpp b/src/system/startup.cpp index 9636bd7..f68decc 100644 --- a/src/system/startup.cpp +++ b/src/system/startup.cpp @@ -44,15 +44,15 @@ void startALPAKA(void * arg) { STA_ASSERT_MSG(osKernelGetState() != osKernelInactive, "Cannot call startALPAKA() before osKernelInitialize()"); - // Call further initialization code - sta::rtos::startupExtras(arg); - // Initialize HAL sta::initHAL(); // Initialize RTOS system resources sta::rtos::initSystem(); + // Call further initialization code + sta::rtos::startupExtras(arg); + // Wake threads sta::rtos::signalStartupEvent(); From d591560c95a4eae525cbbbdb4e4bc15642852473 Mon Sep 17 00:00:00 2001 From: dario Date: Sun, 15 Oct 2023 22:13:25 +0200 Subject: [PATCH 2/8] Added method for reading thread flags --- include/sta/rtos/thread.hpp | 5 +++++ src/thread.cpp | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/include/sta/rtos/thread.hpp b/include/sta/rtos/thread.hpp index 8378c87..f7d8f2d 100644 --- a/include/sta/rtos/thread.hpp +++ b/include/sta/rtos/thread.hpp @@ -81,6 +81,11 @@ namespace sta */ void notify(uint32_t flags); + /** + * @return Returns the flags that were set for this thread. + */ + uint32_t getFlags(); + /** * @brief Send termination request to thread. */ diff --git a/src/thread.cpp b/src/thread.cpp index 781f0d1..0694858 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -32,6 +32,12 @@ namespace sta osThreadFlagsSet(handle_.get(), flags); } + uint32_t RtosThread::getFlags() + { + STA_ASSERT(handle_.get() != nullptr); + + return osThreadFlagsGet(); + } void RtosThread::requestTermination() { From 87ef535471edfa76fd8409a3012bac6faa2eeb5a Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 23 Oct 2023 23:58:23 +0200 Subject: [PATCH 3/8] Fully changed timer to fully use std::function<> --- include/sta/rtos/thread.hpp | 5 +++++ include/sta/rtos/timer.hpp | 4 ++-- src/timer.cpp | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/sta/rtos/thread.hpp b/include/sta/rtos/thread.hpp index f7d8f2d..76a4bd1 100644 --- a/include/sta/rtos/thread.hpp +++ b/include/sta/rtos/thread.hpp @@ -39,6 +39,11 @@ */ #define STA_RTOS_THREAD_FLAG_TERMINATE ( 1UL << 30 ) +/** + * @brief Request thread start. + */ +#define STA_RTOS_THREAD_FLAG_START ( 1UL << 29 ) + #define STA_RTOS_THREAD_FLAGS_ERROR_CODE_BITS UINT32_C( 0x0000000F ) #define STA_RTOS_THREAD_FLAGS_ERROR_CODE_EXT_BITS UINT32_C( 0x7FFFFFF0 ) diff --git a/include/sta/rtos/timer.hpp b/include/sta/rtos/timer.hpp index d54cfeb..ac6714f 100644 --- a/include/sta/rtos/timer.hpp +++ b/include/sta/rtos/timer.hpp @@ -28,7 +28,7 @@ namespace sta * @param callback The callback function to call upon timer timeout. * @param arg The argument to pass to the callback function. */ - RtosTimer(void (*callback)(void *arg), void *arg); + RtosTimer(std::function, void *arg); /** * @brief Initializes the timer with a callback that has no effect. @@ -43,7 +43,7 @@ namespace sta * @param callback The callback function to call upon timer timeout. * @param arg The argument to pass to the callback function. */ - void setCallback(void (*callback)(void *arg), void *arg); + void setCallback(std::function callback, void *arg); /** * @brief Run the timer for a given number of milliseconds. diff --git a/src/timer.cpp b/src/timer.cpp index 4f227b0..7a7d6d3 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -4,7 +4,7 @@ namespace sta { - RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) + RtosTimer::RtosTimer(std::function callback, void *arg) : timer_id_{NULL}, timer_attr_{.name="Timer", .attr_bits=osTimerOnce}, callback_{callback}, @@ -28,7 +28,7 @@ namespace sta { osTimerDelete(timer_id_); } - void RtosTimer::setCallback(void (*callback)(void *arg), void *arg) + void RtosTimer::setCallback(std::function callback, void *arg) { callback_ = callback; callbackArg_ = arg; From 067a48c30953fb5ee2c9f442f9a93a062c16ac1e Mon Sep 17 00:00:00 2001 From: dario Date: Fri, 27 Oct 2023 12:43:13 +0200 Subject: [PATCH 4/8] Added boolean to encode termination requests; added start flag as system flag --- include/sta/rtos/thread.hpp | 16 +++++++++++++++- src/thread.cpp | 22 ++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/sta/rtos/thread.hpp b/include/sta/rtos/thread.hpp index 76a4bd1..713cf0b 100644 --- a/include/sta/rtos/thread.hpp +++ b/include/sta/rtos/thread.hpp @@ -14,7 +14,7 @@ /** * @brief Number of bits in task notification flags used internally. */ -#define STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY 1U +#define STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY 2U /** * @brief Number of user usable bits in task notification flags. */ @@ -91,10 +91,23 @@ namespace sta */ uint32_t getFlags(); + /** + * @brief Wait for certain thread flags. + * + * @return Returns the flags triggering the activation. + */ + uint32_t wait(uint32_t flags); + /** * @brief Send termination request to thread. */ void requestTermination(); + + /** + * @brief Returns true if this thread was requested to terminate. + */ + bool checkTerminationRequest(); + /** * @brief Forcibly terminate thread. */ @@ -109,6 +122,7 @@ namespace sta private: Handle handle_; + bool terminate_; }; } // namespace sta diff --git a/src/thread.cpp b/src/thread.cpp index 0694858..4c54e92 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -29,7 +29,16 @@ namespace sta STA_ASSERT(handle_.get() != nullptr); STA_ASSERT_MSG(IS_THREAD_SYS_FLAGS(flags), "Only system flags allowed"); - osThreadFlagsSet(handle_.get(), flags); + uint32_t rslt = osThreadFlagsSet(handle_.get(), flags); + + STA_ASSERT_MSG(rslt != osFlagsErrorUnknown, "Unknown thread flag error."); + STA_ASSERT_MSG(rslt != osFlagsErrorParameter, "Parameter thread_id is not a valid thread or flags has highest bit set."); + STA_ASSERT_MSG(rslt != osFlagsErrorResource, "The thread is in invalid state."); + } + + uint32_t RtosThread::wait(uint32_t flags) + { + return osThreadFlagsWait(flags, osFlagsWaitAny, osWaitForever); } uint32_t RtosThread::getFlags() @@ -41,7 +50,16 @@ namespace sta void RtosThread::requestTermination() { - sysNotify(STA_RTOS_THREAD_FLAG_TERMINATE); + // sysNotify(STA_RTOS_THREAD_FLAG_TERMINATE); + terminate_ = true; + } + + bool RtosThread::checkTerminationRequest() + { + bool temp = terminate_; + terminate_ = false; + + return temp; } void RtosThread::terminate() From 3900c7ba775c8d5d56273eeb8eac7775eae4e815 Mon Sep 17 00:00:00 2001 From: dario Date: Fri, 27 Oct 2023 14:31:38 +0200 Subject: [PATCH 5/8] Removed preliminary debug implementation --- include/sta/rtos/debug.hpp | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 include/sta/rtos/debug.hpp diff --git a/include/sta/rtos/debug.hpp b/include/sta/rtos/debug.hpp deleted file mode 100644 index aa58cba..0000000 --- a/include/sta/rtos/debug.hpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * debug.hpp - * - * Created on: Oct 15, 2023 - * Author: Dario - */ - -#ifndef RTOS2_UTILS_INCLUDE_STA_RTOS_DEBUG_HPP_ -#define RTOS2_UTILS_INCLUDE_STA_RTOS_DEBUG_HPP_ - -#include -#ifdef STA_DEBUGGING_ENABLED - -# define STA_DEBUG_THREADS() \ -{ \ - - -} - - -#else // !STA_DEBUGGING_ENABLED - -# define STA_DEBUG_THREADS() ((void)0) - - -#endif // STA_DEBUGGING_ENABLED - -#endif /* RTOS2_UTILS_INCLUDE_STA_RTOS_DEBUG_HPP_ */ From 0e96b2ec29fde1e56e12fc9f0dceb80012e0caf3 Mon Sep 17 00:00:00 2001 From: dario Date: Sat, 28 Oct 2023 20:12:27 +0200 Subject: [PATCH 6/8] Renamed checkTerminationRequest to avoid confusion about the actual behavior --- include/sta/rtos/thread.hpp | 6 ++++-- src/thread.cpp | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/sta/rtos/thread.hpp b/include/sta/rtos/thread.hpp index 713cf0b..5490026 100644 --- a/include/sta/rtos/thread.hpp +++ b/include/sta/rtos/thread.hpp @@ -104,9 +104,11 @@ namespace sta void requestTermination(); /** - * @brief Returns true if this thread was requested to terminate. + * @brief Resets the terminate bool to false. + * + * @return Returns the previous value of this variable. */ - bool checkTerminationRequest(); + bool resetTerminationRequest(); /** * @brief Forcibly terminate thread. diff --git a/src/thread.cpp b/src/thread.cpp index 4c54e92..a4b95a1 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -54,7 +54,7 @@ namespace sta terminate_ = true; } - bool RtosThread::checkTerminationRequest() + bool RtosThread::resetTerminationRequest() { bool temp = terminate_; terminate_ = false; From c609dc81cc70327948aa8173c9d6ad804ae7a642 Mon Sep 17 00:00:00 2001 From: dario Date: Tue, 7 Nov 2023 12:40:50 +0100 Subject: [PATCH 7/8] Added flags clear for thread and new constructor for queue. --- include/sta/rtos/queue.hpp | 5 +++++ include/sta/rtos/queue.tpp | 7 +++++++ include/sta/rtos/thread.hpp | 7 +++++++ src/thread.cpp | 9 +++++++++ 4 files changed, 28 insertions(+) diff --git a/include/sta/rtos/queue.hpp b/include/sta/rtos/queue.hpp index f731c91..3fb127e 100644 --- a/include/sta/rtos/queue.hpp +++ b/include/sta/rtos/queue.hpp @@ -34,6 +34,11 @@ namespace sta */ RtosQueue(osMessageQueueId_t handle); + /** + * @param length The maximum number of elements to be stored in the queue. + */ + RtosQueue(uint32_t length); + /** * @brief Place message in queue. * diff --git a/include/sta/rtos/queue.tpp b/include/sta/rtos/queue.tpp index 0ca5676..a92767c 100644 --- a/include/sta/rtos/queue.tpp +++ b/include/sta/rtos/queue.tpp @@ -21,6 +21,13 @@ namespace sta { STA_ASSERT(handle != nullptr); } + + template + RtosQueue::RtosQueue(uint32_t length) + : handle_{osMessageQueueNew(length, sizeof(Message), NULL)} + { + STA_ASSERT(handle_ != NULL); + } template bool RtosQueue::put(const Message & msg, uint32_t timeout /* = osWaitForever */) diff --git a/include/sta/rtos/thread.hpp b/include/sta/rtos/thread.hpp index 5490026..5d6c849 100644 --- a/include/sta/rtos/thread.hpp +++ b/include/sta/rtos/thread.hpp @@ -98,6 +98,13 @@ namespace sta */ uint32_t wait(uint32_t flags); + /** + * @brief Clears the thread flags set for this thread. + * + * @return Returns the thread flags that set before clearing. + */ + uint32_t clear(uint32_t flags); + /** * @brief Send termination request to thread. */ diff --git a/src/thread.cpp b/src/thread.cpp index a4b95a1..7e88251 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -41,6 +41,15 @@ namespace sta return osThreadFlagsWait(flags, osFlagsWaitAny, osWaitForever); } + uint32_t RtosThread::clear(uint32_t flags) + { + uint32_t setFlags = osThreadFlagsClear(flags); + + STA_ASSERT(setFlags != (uint32_t)osError); + + return setFlags; + } + uint32_t RtosThread::getFlags() { STA_ASSERT(handle_.get() != nullptr); From 6bcca7af856b3401a574dae43a362ee487d7b17a Mon Sep 17 00:00:00 2001 From: dario Date: Thu, 9 Nov 2023 18:20:48 +0100 Subject: [PATCH 8/8] Removed terminate flag from threads --- include/sta/rtos/thread.hpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/include/sta/rtos/thread.hpp b/include/sta/rtos/thread.hpp index 5d6c849..1e4d2b7 100644 --- a/include/sta/rtos/thread.hpp +++ b/include/sta/rtos/thread.hpp @@ -14,7 +14,7 @@ /** * @brief Number of bits in task notification flags used internally. */ -#define STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY 2U +#define STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY 1U /** * @brief Number of user usable bits in task notification flags. */ @@ -34,15 +34,10 @@ */ #define STA_RTOS_THREAD_FLAG_ERROR_CODE ( 1UL << 31 ) -/** - * @brief Request thread termination. - */ -#define STA_RTOS_THREAD_FLAG_TERMINATE ( 1UL << 30 ) - /** * @brief Request thread start. */ -#define STA_RTOS_THREAD_FLAG_START ( 1UL << 29 ) +#define STA_RTOS_THREAD_FLAG_START ( 1UL << 30 ) #define STA_RTOS_THREAD_FLAGS_ERROR_CODE_BITS UINT32_C( 0x0000000F )