From 40727728d06660c8b0448cf405f32d2b1634e6aa Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Sun, 16 Jun 2024 10:44:24 +0200 Subject: [PATCH 1/2] feat(sync): added periodicDelay function to threads --- include/sta/tacos/thread.hpp | 9 +++++++++ src/thread.cpp | 26 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/sta/tacos/thread.hpp b/include/sta/tacos/thread.hpp index 6344d55..a60064d 100644 --- a/include/sta/tacos/thread.hpp +++ b/include/sta/tacos/thread.hpp @@ -128,6 +128,14 @@ namespace sta */ void sleep(uint32_t ticks); + /** + * @brief Sleep until next period. Sets itself to WAITING if the watchdog is enabled, preventing + * the watchdog from restarting this thread. + * + * @param frequency + */ + void periodicDelay(uint32_t frequency); + #ifdef STA_CAN_BUS_ENABLE /** * @brief Set the ID of the CAN bus this thread is associated with. @@ -216,6 +224,7 @@ namespace sta uint32_t canID_; #endif // STA_TACOS_WATCHDOG_ENABLED bool terminate_; + uint32_t previous_tick_; }; } } diff --git a/src/thread.cpp b/src/thread.cpp index 61bf62a..bfb3058 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -13,7 +13,11 @@ #include #include +#include +// Include FreeRTOS configuration for the system tick rate. +typedef uint32_t TickType_t; +#include namespace sta { @@ -31,7 +35,8 @@ namespace sta CAN_queue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, canID_{0}, #endif // STA_CAN_BUS_ENABLE - terminate_{false} + terminate_{false}, + previous_tick_{0} { STA_ASSERT(stack_size >= 0); STA_ASSERT(cb_size >= 0); @@ -110,6 +115,25 @@ namespace sta osDelay(ticks); +#ifdef STA_TACOS_WATCHDOG_ENABLED + heartbeat(); +#endif // STA_TACOS_WATCHDOG_ENABLED + } + + void TacosThread::periodicDelay(uint32_t frequency){ + if (previous_tick_ == 0) + { + previous_tick_ = osKernelGetTickCount(); + } + + previous_tick_ += configTICK_RATE_HZ/frequency; + +#ifdef STA_TACOS_WATCHDOG_ENABLED + waiting(); +#endif // STA_TACOS_WATCHDOG_ENABLED + + osDelayUntil(previous_tick_); + #ifdef STA_TACOS_WATCHDOG_ENABLED heartbeat(); #endif // STA_TACOS_WATCHDOG_ENABLED From da7b98be44c4aea341d082a51517fe5633486326 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Mon, 17 Jun 2024 11:31:44 +0200 Subject: [PATCH 2/2] cleanup: cmsis-rtos for kernel frequency --- src/thread.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/thread.cpp b/src/thread.cpp index bfb3058..e13863d 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -15,10 +15,6 @@ #include #include -// Include FreeRTOS configuration for the system tick rate. -typedef uint32_t TickType_t; -#include - namespace sta { namespace tacos @@ -126,7 +122,7 @@ namespace sta previous_tick_ = osKernelGetTickCount(); } - previous_tick_ += configTICK_RATE_HZ/frequency; + previous_tick_ += osKernelGetTickFreq()/frequency; #ifdef STA_TACOS_WATCHDOG_ENABLED waiting();