From f75f23d4e9da7509875454a2757f457b8e9c5493 Mon Sep 17 00:00:00 2001 From: dario Date: Tue, 19 Nov 2024 20:32:18 +0100 Subject: [PATCH] Moved system events to TACOS and re-introduced onManagerInit() and onStatemachineInit() for backwards compatibility --- include/sta/tacos/statemachine.hpp | 2 +- include/sta/tacos/system/events.hpp | 82 +++++++++++++++++++++++++++++ src/events.cpp | 53 +++++++++++++++++++ src/startup.cpp | 37 ++++++++++--- src/statemachine.cpp | 6 +-- src/thread.cpp | 5 +- 6 files changed, 169 insertions(+), 16 deletions(-) create mode 100644 include/sta/tacos/system/events.hpp create mode 100644 src/events.cpp diff --git a/include/sta/tacos/statemachine.hpp b/include/sta/tacos/statemachine.hpp index d9fd517..fc9c129 100644 --- a/include/sta/tacos/statemachine.hpp +++ b/include/sta/tacos/statemachine.hpp @@ -303,7 +303,7 @@ namespace sta * @ingroup tacos_statemachine */ STA_WEAK - void onStateTransition(uint16_t from, uint16_t to, uint32_t lockout){} + void onStateTransition(uint16_t from, uint16_t to, uint32_t lockout) {} } // namespace tacos } // namespace sta diff --git a/include/sta/tacos/system/events.hpp b/include/sta/tacos/system/events.hpp new file mode 100644 index 0000000..4ec0513 --- /dev/null +++ b/include/sta/tacos/system/events.hpp @@ -0,0 +1,82 @@ +/** + * @file + * @brief Implementation of system events. + */ +#ifndef STA_TACOS_SYSTEM_EVENTS_HPP +#define STA_TACOS_SYSTEM_EVENTS_HPP + + +/** + * @defgroup STA_RTOS_SysEvent System Events + * @ingroup STA_RTOS_API + * @brief System events. + * + * Check @ref STA_RTOS_BuildConfig for configuration options. + */ + + +#include + + +// System event flags +// + +/** + * @brief Startup system event flag. + * + * @ingroup STA_RTOS_SysEvent + */ +#define STA_TACOS_SYSTEM_EVENTS_STARTUP 0x100000U + + +namespace sta +{ + namespace tacos + { + /** + * @brief Initialize system events. + */ + void initSystemEvents(); + + + /** + * @brief Signal system events. + * + * @param flags System event flags + * + * @ingroup STA_RTOS_SysEvent + */ + void signalSystemEvents(uint32_t flags); + + /** + * @brief Wait for system events. + * + * @param flags System event flags + * @param options osFlagsWaitAll or osFlagsWaitAny (osFlagsNoClear always set) + * @param timeout Wait timeout (0 = instant, osWaitForever = infinite) + * + * @ingroup STA_RTOS_SysEvent + */ + void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout); + + + /** + * @brief Signal startup system event. + * + * @ingroup STA_RTOS_SysEvent + */ + void signalStartupEvent(); + + /** + * @brief Wait for startup system event. + * + * Blocking while waiting + * + * @ingroup STA_RTOS_SysEvent + */ + void waitForStartupEvent(); + } // namespace tacos +} // namespace sta + + +#endif // STA_RTOS_SYSTEM_EVENTS_HPP diff --git a/src/events.cpp b/src/events.cpp new file mode 100644 index 0000000..b6a34ff --- /dev/null +++ b/src/events.cpp @@ -0,0 +1,53 @@ +#include +#include + +#include + +#include +#include + + +namespace +{ + // Static memory for system events + StaticEventGroup_t systemEventControlBlock; + // Event handle + sta::RtosEvent * systemEvents = nullptr; +} + + +namespace sta +{ + namespace tacos + { + void initSystemEvents() + { + if (systemEvents == nullptr) + { + systemEvents = new sta::RtosEvent(); + } + } + + void signalSystemEvents(uint32_t flags) + { + STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized"); + systemEvents->set(flags); + } + + void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout) + { + STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized"); + systemEvents->peek(flags, timeout); + } + + void signalStartupEvent() + { + signalSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP); + } + + void waitForStartupEvent() + { + waitForSystemEvents(STA_TACOS_SYSTEM_EVENTS_STARTUP, osFlagsWaitAll, osWaitForever); + } + } // namespace tacos +} // namespace sta diff --git a/src/startup.cpp b/src/startup.cpp index 175cf54..fddf303 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -28,8 +28,7 @@ #include // rtos2-utils-specific includes. -#include -#include +#include // Tacos-specific includes. #include @@ -85,7 +84,7 @@ namespace sta { /** * @brief Function that is called before the statemachine task is started. Override it to - * adjust the statemachine to your specifications. + * adjust the statemachine to your specifications. Remains in TACOS for backwards compatibility, use startup() instead. * * @ingroup tacos_startup */ @@ -93,10 +92,34 @@ namespace sta void onStatemachineInit() {} + /** + * @brief Function that is called before the statemachine task is started. Override it to + * adjust the statemachine to your specifications. Remains in TACOS for backwards compatibility, use startup() instead. + * + * @ingroup tacos_startup + */ + STA_WEAK + void onManagerInit() + {} + + /** + * @brief Function that is called before the statemachine task is started. It serves as an entry point for the user to + * define the threads the statemachine should run. + * + * @ingroup tacos_startup + */ + STA_WEAK + void startup() + {} + void initStatemachine() { onStatemachineInit(); + onManagerInit(); + + startup(); + Statemachine::instance()->start(); } @@ -158,16 +181,14 @@ void startTACOS(void * arg) // Initialize HAL sta::initHAL(); - // Initialize RTOS system resources - sta::rtos::initSystem(); + // Initialize RTOS system events + sta::tacos::initSystemEvents(); // Call further initialization code sta::tacos::startupExtras(arg); // Wake threads -#ifdef STA_RTOS_SYSTEM_EVENTS_ENABLE - sta::rtos::signalStartupEvent(); -#endif // STA_RTOS_SYSTEM_EVENTS_ENABLE + sta::tacos::signalStartupEvent(); // Check if called from thread if (osThreadGetId() != nullptr) diff --git a/src/statemachine.cpp b/src/statemachine.cpp index e84c614..86077ad 100644 --- a/src/statemachine.cpp +++ b/src/statemachine.cpp @@ -131,12 +131,8 @@ namespace sta setLockoutTimer(lockout); } - // get heap stats at the end of the state transition - HeapStats_t stats; - vPortGetHeapStats(&stats); - // Execute the user-defined callback. - sta::tacos::onStateTransition(transition.from, transition.to, transition.lockout); + sta::tacos::onStateTransition(from, to, lockout); // Start all new tasks and stop all the tasks that aren't supposed to be running. updateThreads(); diff --git a/src/thread.cpp b/src/thread.cpp index 4b65bb1..ed5ed12 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -7,9 +7,10 @@ #include +#include + #include #include -#include #include #include @@ -63,7 +64,7 @@ namespace sta while (true) { // The thread has to wait until the system startup has been completed. - rtos::waitForStartupEvent(); + sta::tacos::waitForStartupEvent(); // Wait for a thread start flag. wait(STA_RTOS_THREAD_FLAG_START);