From 0f02168b25ef3b0b7459c8c31e80155b2379a7e1 Mon Sep 17 00:00:00 2001 From: dario Date: Sat, 23 Sep 2023 22:42:47 +0200 Subject: [PATCH 1/4] Added modifiable callback for timers --- include/sta/rtos/timer.hpp | 48 +++++++++++++++++++++++++++++++++++--- src/timer.cpp | 34 ++++++++++++++++++++------- 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/include/sta/rtos/timer.hpp b/include/sta/rtos/timer.hpp index b576078..2d0cf3d 100644 --- a/include/sta/rtos/timer.hpp +++ b/include/sta/rtos/timer.hpp @@ -9,6 +9,9 @@ #include #include +#include + + namespace sta { /** @@ -19,16 +22,55 @@ namespace sta class RtosTimer : public Timer { public: + /** + * @brief + * + * @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(); + /** + * @brief Initializes the timer with a callback that has no effect. + */ + RtosTimer(); + + ~RtosTimer(); + public: + /** + * @brief Change the timer's callback to a new function. + * + * @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); + + /** + * @brief Run the timer for a given number of milliseconds. + * + * @param millis The number of milliseconds to run the timer for. + */ void start(uint32_t millis) override; + + /** + * @brief Stop the timer. + */ void stop() override; + private: + static void timeoutHandler(void* arg) + { + RtosTimer* timer = static_cast(arg); + timer->callback_(timer->callbackArg_); + } + private: osTimerId_t timer_id_; /**< CMSIS RTOS2 Timer */ - osTimerAttr_t timer_attr_; /**< CMSIS RTOS2 Timer attributes */ + osTimerAttr_t timer_attr_; /**< CMSIS RTOS2 Timer attributes */ + + std::function callback_; /**< The callback to call on timeout. */ + void* callbackArg_; /**< The argument to pass to the callback. */ }; } // namespace sta -#endif // STA_RTOS_TIMER_HPP \ No newline at end of file +#endif // STA_RTOS_TIMER_HPP diff --git a/src/timer.cpp b/src/timer.cpp index 398cdd3..dbaab72 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -1,28 +1,44 @@ #include #include +#include + namespace sta { - RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) { - timer_attr_.name = "Timer"; - timer_attr_.attr_bits = osTimerOnce; - timer_attr_.cb_size = sizeof(osTimerAttr_t); + RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) + : timer_id_{NULL}, timer_attr_{.name="Timer", .attr_bits=osTimerOnce, .cb_size=sizeof(osTimerAttr_t)}, callback_{callback}, callbackArg_{arg} + { + // Pass an anonymous function as the callback which will invoke the currently registered callback. + timer_id_ = osTimerNew(timeoutHandler, osTimerOnce, arg, &timer_attr_); - timer_id_ = osTimerNew(callback, osTimerOnce, arg, &timer_attr_); + STA_ASSERT_MSG(timer_id_ != NULL, "Failed to create timer!"); } - RtosTimer::~RtosTimer() { + RtosTimer::RtosTimer() + : RtosTimer([](void *){}, NULL) + {} + + RtosTimer::~RtosTimer() + { osTimerDelete(timer_id_); } - void RtosTimer::start(uint32_t millis) { + void RtosTimer::setCallback(void (*callback)(void *arg), void *arg) + { + callback_ = callback; + callbackArg_ = arg; + } + + void RtosTimer::start(uint32_t millis) + { osStatus_t status = osTimerStart(timer_id_, millis); if (status != osOK) STA_DEBUG_PRINTLN("Timer start failed"); } - void RtosTimer::stop() { + void RtosTimer::stop() + { osStatus_t status = osTimerStop(timer_id_); if (status != osOK) STA_DEBUG_PRINTLN("Timer stop failed"); } -} // namespace sta \ No newline at end of file +} // namespace sta From 2948c9203e44a5126e6a0067522698964f0ee7c4 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 27 Sep 2023 00:06:08 +0200 Subject: [PATCH 2/4] Added fixes to timer implementation --- include/sta/rtos/timer.hpp | 12 +++++++----- src/timer.cpp | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/include/sta/rtos/timer.hpp b/include/sta/rtos/timer.hpp index 2d0cf3d..7507c3a 100644 --- a/include/sta/rtos/timer.hpp +++ b/include/sta/rtos/timer.hpp @@ -57,12 +57,13 @@ namespace sta */ void stop() override; + /** + * @returns True of the timmer is currently running. + */ + bool isRunning(); + private: - static void timeoutHandler(void* arg) - { - RtosTimer* timer = static_cast(arg); - timer->callback_(timer->callbackArg_); - } + static void timeoutHandler(void* arg); private: osTimerId_t timer_id_; /**< CMSIS RTOS2 Timer */ @@ -70,6 +71,7 @@ namespace sta std::function callback_; /**< The callback to call on timeout. */ void* callbackArg_; /**< The argument to pass to the callback. */ + bool running_; /**< The current state of the timer. Running or not? */ }; } // namespace sta diff --git a/src/timer.cpp b/src/timer.cpp index dbaab72..967396d 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -5,7 +5,11 @@ namespace sta { RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) - : timer_id_{NULL}, timer_attr_{.name="Timer", .attr_bits=osTimerOnce, .cb_size=sizeof(osTimerAttr_t)}, callback_{callback}, callbackArg_{arg} + : timer_id_{NULL}, + timer_attr_{.name="Timer", .attr_bits=osTimerOnce, /*.cb_size=sizeof(osTimerAttr_t)*/}, + callback_{callback}, + callbackArg_{arg}, + running_{false} { // Pass an anonymous function as the callback which will invoke the currently registered callback. timer_id_ = osTimerNew(timeoutHandler, osTimerOnce, arg, &timer_attr_); @@ -19,6 +23,8 @@ namespace sta { RtosTimer::~RtosTimer() { + if (isRunning()) stop(); + osTimerDelete(timer_id_); } @@ -32,13 +38,38 @@ namespace sta { { osStatus_t status = osTimerStart(timer_id_, millis); - if (status != osOK) STA_DEBUG_PRINTLN("Timer start failed"); + if (status != osOK) + { + STA_DEBUG_PRINTLN("Timer start failed"); + return; + } + + running_ = true; } void RtosTimer::stop() { osStatus_t status = osTimerStop(timer_id_); - if (status != osOK) STA_DEBUG_PRINTLN("Timer stop failed"); + if (status != osOK) + { + STA_DEBUG_PRINTLN("Timer stop failed"); + return; + } + + running_ = false; } + + bool RtosTimer::isRunning() + { + return running_; + } + + void RtosTimer::timeoutHandler(void* arg) + { + RtosTimer* timer = static_cast(arg); + timer->running_ = false; + timer->callback_(timer->callbackArg_); + } + } // namespace sta From 430b8b8947611c7572f24b673a9935b1a1356fb0 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 27 Sep 2023 09:54:40 +0200 Subject: [PATCH 3/4] Fixed timer starting --- src/system/watchdog.cpp | 1 + src/timer.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/system/watchdog.cpp b/src/system/watchdog.cpp index 373b349..1bf582d 100644 --- a/src/system/watchdog.cpp +++ b/src/system/watchdog.cpp @@ -2,6 +2,7 @@ #ifdef STA_RTOS_WATCHDOG_ENABLE #include +#include #include #include #include diff --git a/src/timer.cpp b/src/timer.cpp index 967396d..79c6361 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -12,7 +12,7 @@ namespace sta { running_{false} { // Pass an anonymous function as the callback which will invoke the currently registered callback. - timer_id_ = osTimerNew(timeoutHandler, osTimerOnce, arg, &timer_attr_); + timer_id_ = osTimerNew(timeoutHandler, osTimerOnce, this, &timer_attr_); STA_ASSERT_MSG(timer_id_ != NULL, "Failed to create timer!"); } From 6d444e7cfc2c8e9597270a42db46afe1066093d0 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 27 Sep 2023 12:39:03 +0200 Subject: [PATCH 4/4] Completely removed timer argument that caused crashes --- src/timer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/timer.cpp b/src/timer.cpp index 7c76b24..4f227b0 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -6,7 +6,7 @@ namespace sta { RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) : timer_id_{NULL}, - timer_attr_{.name="Timer", .attr_bits=osTimerOnce, /*.cb_size=sizeof(osTimerAttr_t)*/}, + timer_attr_{.name="Timer", .attr_bits=osTimerOnce}, callback_{callback}, callbackArg_{arg}, running_{false}