Moved system events to TACOS and re-introduced onManagerInit() and onStatemachineInit() for backwards compatibility

This commit is contained in:
dario 2024-11-19 20:32:18 +01:00 committed by CarlWachter
parent f6952ccd32
commit f75f23d4e9
6 changed files with 169 additions and 16 deletions

View File

@ -303,7 +303,7 @@ namespace sta
* @ingroup tacos_statemachine * @ingroup tacos_statemachine
*/ */
STA_WEAK 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 tacos
} // namespace sta } // namespace sta

View File

@ -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 <cstdint>
// 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

53
src/events.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <sta/tacos/system/events.hpp>
#include <sta/rtos/event.hpp>
#include <sta/debug/assert.hpp>
#include <cmsis_os2.h>
#include <FreeRTOS.h>
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

View File

@ -28,8 +28,7 @@
#include <sta/lang.hpp> #include <sta/lang.hpp>
// rtos2-utils-specific includes. // rtos2-utils-specific includes.
#include <sta/rtos/system/startup.hpp> #include <sta/tacos/system/events.hpp>
#include <sta/rtos/system/events.hpp>
// Tacos-specific includes. // Tacos-specific includes.
#include <sta/tacos/c_api/startup.h> #include <sta/tacos/c_api/startup.h>
@ -85,7 +84,7 @@ namespace sta
{ {
/** /**
* @brief Function that is called before the statemachine task is started. Override it to * @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 * @ingroup tacos_startup
*/ */
@ -93,10 +92,34 @@ namespace sta
void onStatemachineInit() 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() void initStatemachine()
{ {
onStatemachineInit(); onStatemachineInit();
onManagerInit();
startup();
Statemachine::instance()->start(); Statemachine::instance()->start();
} }
@ -158,16 +181,14 @@ void startTACOS(void * arg)
// Initialize HAL // Initialize HAL
sta::initHAL(); sta::initHAL();
// Initialize RTOS system resources // Initialize RTOS system events
sta::rtos::initSystem(); sta::tacos::initSystemEvents();
// Call further initialization code // Call further initialization code
sta::tacos::startupExtras(arg); sta::tacos::startupExtras(arg);
// Wake threads // Wake threads
#ifdef STA_RTOS_SYSTEM_EVENTS_ENABLE sta::tacos::signalStartupEvent();
sta::rtos::signalStartupEvent();
#endif // STA_RTOS_SYSTEM_EVENTS_ENABLE
// Check if called from thread // Check if called from thread
if (osThreadGetId() != nullptr) if (osThreadGetId() != nullptr)

View File

@ -131,12 +131,8 @@ namespace sta
setLockoutTimer(lockout); setLockoutTimer(lockout);
} }
// get heap stats at the end of the state transition
HeapStats_t stats;
vPortGetHeapStats(&stats);
// Execute the user-defined callback. // 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. // Start all new tasks and stop all the tasks that aren't supposed to be running.
updateThreads(); updateThreads();

View File

@ -7,9 +7,10 @@
#include <sta/tacos/thread.hpp> #include <sta/tacos/thread.hpp>
#include <sta/tacos/system/events.hpp>
#include <sta/debug/assert.hpp> #include <sta/debug/assert.hpp>
#include <sta/debug/debug.hpp> #include <sta/debug/debug.hpp>
#include <sta/rtos/system/events.hpp>
#include <functional> #include <functional>
#include <cstring> #include <cstring>
@ -63,7 +64,7 @@ namespace sta
while (true) while (true)
{ {
// The thread has to wait until the system startup has been completed. // The thread has to wait until the system startup has been completed.
rtos::waitForStartupEvent(); sta::tacos::waitForStartupEvent();
// Wait for a thread start flag. // Wait for a thread start flag.
wait(STA_RTOS_THREAD_FLAG_START); wait(STA_RTOS_THREAD_FLAG_START);