Working global events implementation

This commit is contained in:
dario 2025-06-08 15:19:18 +02:00
parent 92525d790e
commit 939bbc6883
2 changed files with 31 additions and 27 deletions

View File

@ -16,31 +16,33 @@
#include <cstdint>
#include <cmsis_os2.h>
// Custom event flags for the user (bits 0-13).
//
#define STA_TACOS_CUSTOM_EVENT(k) (0x1U << k)
// A mask representing all custom flags available for the user.
#define STA_TACOS_CUSTOM_EVENT_MASK (0x7FFFU)
// System event flags
//
/**
* @brief Startup system event flag.
* @brief Startup system event flag (bits 14-23).
*
* @ingroup STA_RTOS_SysEvent
*/
#define STA_TACOS_SYSTEM_EVENTS_STARTUP (0x01U << 23)
#define STA_TACOS_EVENT_SYSTEM_MASK (0xFFFE0000U >> 1)
#define STA_TACOS_EVENT_TIMER_MASK (0x7FC00000U >> 1)
#define STA_TACOS_SYSTEM_EVENTS_STARTUP (0x01U << 30)
#define STA_TACOS_TICK_100_Hz (0x01U << 29)
#define STA_TACOS_TICK_50_Hz (0x01U << 28)
#define STA_TACOS_TOCK_50_Hz (0x01U << 27)
#define STA_TACOS_TICK_20_Hz (0x01U << 26)
#define STA_TACOS_TOCK_20_Hz (0x01U << 25)
#define STA_TACOS_TICK_10_Hz (0x01U << 24)
#define STA_TACOS_TOCK_10_Hz (0x01U << 23)
#define STA_TACOS_TICK_1_Hz (0x01U << 22)
#define STA_TACOS_TOCK_1_Hz (0x01U << 21)
#define STA_TACOS_TICK_100_Hz (0x01U << 22)
#define STA_TACOS_TICK_50_Hz (0x01U << 21)
#define STA_TACOS_TOCK_50_Hz (0x01U << 20)
#define STA_TACOS_TICK_20_Hz (0x01U << 19)
#define STA_TACOS_TICK_10_Hz (0x01U << 18)
#define STA_TACOS_TOCK_10_Hz (0x01U << 17)
#define STA_TACOS_TICK_1_Hz (0x01U << 16)
#define STA_TACOS_TOCK_1_Hz (0x01U << 15)
namespace sta
@ -72,7 +74,7 @@ namespace sta
*
* @ingroup STA_RTOS_SysEvent
*/
void wait(uint32_t flags, uint32_t options, uint32_t timeout);
void wait(uint32_t flags, uint32_t timeout = osWaitForever);
/**
* @brief Signal startup system event.

View File

@ -10,7 +10,7 @@
namespace
{
// Event handle
// Event handle for system events.
sta::RtosEvent * systemEvents = nullptr;
// Tick timer handle
@ -35,10 +35,11 @@ namespace sta
if (tickTimer == nullptr)
{
// Initialize the a timer that sets tick event flags.
tickTimer = new sta::RtosTimer([](void * args) {
uint32_t flags = 0x00;
flags |= STA_TACOS_TOCK_50_Hz;
flags |= STA_TACOS_TICK_100_Hz;
if (tickCounter % 20 == 0) {
flags |= STA_TACOS_TICK_50_Hz;
@ -48,17 +49,15 @@ namespace sta
if (tickCounter % 50 == 0) {
flags |= STA_TACOS_TICK_20_Hz;
} else if (tickCounter % 25 == 0) {
flags |= STA_TACOS_TOCK_20_Hz;
}
if (tickCounter % 100) {
if (tickCounter % 100 == 0) {
flags |= STA_TACOS_TICK_10_Hz;
} else if (tickCounter % 50 == 0) {
flags |= STA_TACOS_TOCK_10_Hz;
}
if (tickCounter % 1000) {
if (tickCounter % 1000 == 0) {
flags |= STA_TACOS_TICK_1_Hz;
} else if (tickCounter % 500 == 0) {
flags |= STA_TACOS_TOCK_1_Hz;
@ -66,8 +65,8 @@ namespace sta
tickCounter = (tickCounter + 10) % 1000;
signal(flags);
systemEvents->clear(STA_TACOS_EVENT_TIMER_MASK);
systemEvents->set(flags);
systemEvents->clear(flags);
}, nullptr, true);
tickTimer->start(10);
@ -77,10 +76,13 @@ namespace sta
void signal(uint32_t flags)
{
STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized");
STA_ASSERT_MSG((flags & ~STA_TACOS_CUSTOM_EVENT_MASK) == 0, "Invalid custom flags (available: bits 0 to 14)");
systemEvents->set(flags);
systemEvents->clear(flags);
}
void wait(uint32_t flags, uint32_t options, uint32_t timeout)
void wait(uint32_t flags, uint32_t timeout /* = osWaitForever */)
{
STA_ASSERT_MSG(systemEvents != nullptr, "System events not initialized");
systemEvents->peek(flags, timeout);
@ -88,12 +90,12 @@ namespace sta
void signalStartup()
{
signal(STA_TACOS_SYSTEM_EVENTS_STARTUP);
systemEvents->set(STA_TACOS_SYSTEM_EVENTS_STARTUP);
}
void waitForStartup()
{
wait(STA_TACOS_SYSTEM_EVENTS_STARTUP, osFlagsWaitAll, osWaitForever);
wait(STA_TACOS_SYSTEM_EVENTS_STARTUP);
}
} // namespace events
} // namespace tacos