From a4a4851704e2097cb234c297f884e582aea535f8 Mon Sep 17 00:00:00 2001 From: Henrik Stickann <4376396-Mithradir@users.noreply.gitlab.com> Date: Sat, 9 Apr 2022 21:15:27 +0200 Subject: [PATCH] Rework structure and configuration --- include/sta/{os2.hpp => os2/defs.hpp} | 6 +-- include/sta/os2/startup.hpp | 29 +++++++++++++ include/sta/os2/system_event.hpp | 59 +++++++++++++++++++++++++++ include/sta/os2/watchdog.hpp | 44 ++++++++++++++++++++ include/sta/system_event.hpp | 52 ----------------------- include/sta/watchdog.hpp | 26 ------------ src/os2/startup.cpp | 50 +++++++++++++++++++++++ src/os2/system_event.cpp | 41 +++++++++++++++++++ src/os2/watchdog/heartbeat.cpp | 46 +++++++++++++++++++++ src/{ => os2}/watchdog/watchdog.cpp | 26 ++++++++---- src/startup.cpp | 44 -------------------- src/system_event.cpp | 38 ----------------- src/watchdog/heartbeat.cpp | 38 ----------------- 13 files changed, 289 insertions(+), 210 deletions(-) rename include/sta/{os2.hpp => os2/defs.hpp} (79%) create mode 100644 include/sta/os2/startup.hpp create mode 100644 include/sta/os2/system_event.hpp create mode 100644 include/sta/os2/watchdog.hpp delete mode 100644 include/sta/system_event.hpp delete mode 100644 include/sta/watchdog.hpp create mode 100644 src/os2/startup.cpp create mode 100644 src/os2/system_event.cpp create mode 100644 src/os2/watchdog/heartbeat.cpp rename src/{ => os2}/watchdog/watchdog.cpp (51%) delete mode 100644 src/startup.cpp delete mode 100644 src/system_event.cpp delete mode 100644 src/watchdog/heartbeat.cpp diff --git a/include/sta/os2.hpp b/include/sta/os2/defs.hpp similarity index 79% rename from include/sta/os2.hpp rename to include/sta/os2/defs.hpp index 3fcb761..5da075d 100644 --- a/include/sta/os2.hpp +++ b/include/sta/os2/defs.hpp @@ -1,5 +1,5 @@ -#ifndef STA_OS2_HPP -#define STA_OS2_HPP +#ifndef STA_OS2_DEFS_HPP +#define STA_OS2_DEFS_HPP // See limits defined in cmsis_os2.c @@ -10,4 +10,4 @@ #define STA_OS2_EVENT_FLAGS_VALID_BITS ((1UL << STA_OS2_MAX_BITS_EVENT_GROUPS) - 1U) -#endif // STA_OS2_HPP +#endif // STA_OS2_DEFS_HPP diff --git a/include/sta/os2/startup.hpp b/include/sta/os2/startup.hpp new file mode 100644 index 0000000..4c66f11 --- /dev/null +++ b/include/sta/os2/startup.hpp @@ -0,0 +1,29 @@ +/** + * @brief Implementation of startup system task. + * + * Define STA_OS2_STARTUP_ENABLE in to enable. + * + * Configuration: + * STA_OS2_STARTUP_ENTRY_FUNCTION: Name of task entry function (default: startupTask) + */ +#ifndef STA_OS2_STARTUP_HPP +#define STA_OS2_STARTUP_HPP + +#include +#ifdef STA_OS2_STARTUP_ENABLE + + +namespace sta +{ + /** + * @brief Extra initialization run at start of startup task. + * + * May be overridden by application if required. + */ + void startupExtras(void * argument); +} // namespace sta + + +#endif // STA_OS2_STARTUP_ENABLE + +#endif // STA_OS2_STARTUP_HPP diff --git a/include/sta/os2/system_event.hpp b/include/sta/os2/system_event.hpp new file mode 100644 index 0000000..182f086 --- /dev/null +++ b/include/sta/os2/system_event.hpp @@ -0,0 +1,59 @@ +/** + * @brief Implementation of system events. + * + * Define STA_OS2_SYSTEM_EVENT_ENABLE in to enable. + * + * Set STA_OS2_SYSTEM_EVENT_HANDLE to name of global handle variable. + * Defaults to `systemEventHandle`. + */ +#ifndef STA_OS2_SYSTEM_EVENT_HPP +#define STA_OS2_SYSTEM_EVENT_HPP + +#include +#ifdef STA_OS2_SYSTEM_EVENT_ENABLE + +#include + + +// System event flags +// + +#define STA_SYSTEM_EVENT_STARTUP 0x100000U + + +namespace sta +{ + /** + * @brief Signal system events. + * + * @param flags System event flags + */ + 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) + */ + void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout); + + + /** + * @brief Signal startup system event. + */ + void signalStartupEvent(); + + /** + * @brief Wait for startup system event. + * + * Blocking while waiting + */ + void waitForStartupEvent(); +} // namespace sta + + +#endif // STA_OS2_SYSTEM_EVENT_ENABLE + +#endif // STA_OS2_SYSTEM_EVENT_HPP diff --git a/include/sta/os2/watchdog.hpp b/include/sta/os2/watchdog.hpp new file mode 100644 index 0000000..8ef5989 --- /dev/null +++ b/include/sta/os2/watchdog.hpp @@ -0,0 +1,44 @@ +/** + * @brief Implementation of watchdog system task. + * + * Define STA_OS2_WATCHDOG_ENABLE in to enable. + * + * Configuration: + * STA_OS2_WATCHDOG_TIMER_PERIOD: Period of heartbeat timer (default: 1000) + * STA_OS2_WATCHDOG_TIMER_HANDLE: Name of global timer handle variable (default: heartbeatHandle) + * STA_OS2_WATCHDOG_TIMER_CALLBACK: Name of timer callback function (default: heartbeatCallback) + * + * STA_OS2_WATCHDOG_HANDLE: Name of global task handle variable (default: watchdogHandle) + * STA_OS2_WATCHDOG_ENTRY_FUNCTION: Name of task entry function (default: watchdogTask) + */ +#ifndef STA_OS2_WATCHDOG_HPP +#define STA_OS2_WATCHDOG_HPP + +#include +#ifdef STA_OS2_WATCHDOG_ENABLE + +#include + + +// Watchdog task flags +// + +#define STA_WATCHDOG_FLAG_HEARTBEAT 0x00001000U + + +namespace sta +{ + /** + * @brief Start heartbeat timer for watchdog. + */ + void startWatchdogTimer(); + /** + * @brief Send notification to watchdog task. + */ + void notifyWatchdog(uint32_t flags); +} // namespace sta + + +#endif // STA_OS2_WATCHDOG_ENABLE + +#endif // STA_OS2_WATCHDOG_HPP diff --git a/include/sta/system_event.hpp b/include/sta/system_event.hpp deleted file mode 100644 index 68e82b9..0000000 --- a/include/sta/system_event.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef STA_SYSTEM_EVENT_HPP -#define STA_SYSTEM_EVENT_HPP - -#include - -#ifdef STA_SYSTEM_EVENT_ENABLE - -#include - - -// System event flags -// - -#define STA_SYSTEM_EVENT_STARTUP 0x100000U - - -namespace sta -{ - /** - * @brief Signal system events - * - * @param flags System event flags - */ - void signalSystemEvents(uint32_t flags); - - /** - * @brief Wait for system event. - * - * @param flags System event flags - * @param options osFlagsWaitAll or osFlagsWaitAny (osFlagsNoClear always set) - * @param timeout Wait timeout (0 = instant, osWaitForever = infinite) - */ - void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout); - - - /** - * @brief Signal startup system event - */ - void signalStartupEvent(); - - /** - * @brief Wait for startup system event. - * - * Blocking while waiting - */ - void waitForStartupEvent(); -} // namespace sta - - -#endif // STA_SYSTEM_EVENT_ENABLE - -#endif // STA_SYSTEM_EVENT_HPP diff --git a/include/sta/watchdog.hpp b/include/sta/watchdog.hpp deleted file mode 100644 index def86b2..0000000 --- a/include/sta/watchdog.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef STA_WATCHDOG_HPP -#define STA_WATCHDOG_HPP - -#include - -#ifdef STA_WATCHDOG_ENABLE - -#include - - -// Watchdog task flags -// - -#define STA_WATCHDOG_FLAG_HEARTBEAT 0x00001000U - - -namespace sta -{ - void startWatchdogTimer(); - void notifyWatchdog(uint32_t flags); -} // namespace sta - - -#endif // STA_WATCHDOG_ENABLE - -#endif // STA_WATCHDOG_HPP diff --git a/src/os2/startup.cpp b/src/os2/startup.cpp new file mode 100644 index 0000000..dd72dcf --- /dev/null +++ b/src/os2/startup.cpp @@ -0,0 +1,50 @@ +#include +#ifdef STA_OS2_STARTUP_ENABLE + +#include +#include +#include + +#include + +#include + + +#ifndef STA_OS2_STARTUP_ENTRY_FUNCTION +# define STA_OS2_STARTUP_ENTRY_FUNCTION startupTask +#endif // !STA_OS2_STARTUP_ENTRY_FUNCTION + + +namespace sta +{ + // Provide weak implementation to allow overriding + STA_WEAK + void startupExtras(void *) + {} +} // namespace sta + + +// Declare with C linkage +extern "C" +{ + void STA_OS2_STARTUP_ENTRY_FUNCTION(void * arg) + { + // Call further initialization code + sta::startupExtras(arg); + + +#ifdef STA_WATCHDOG_ENABLE + // Start timers + sta::startWatchdogTimer(); +#endif + + // Wake tasks + sta::signalStartupEvent(); + + // Terminate task + osThreadExit(); + } +} + + +#endif // STA_OS2_STARTUP_ENABLE diff --git a/src/os2/system_event.cpp b/src/os2/system_event.cpp new file mode 100644 index 0000000..868e7c8 --- /dev/null +++ b/src/os2/system_event.cpp @@ -0,0 +1,41 @@ +#include + +#ifdef STA_OS2_SYSTEM_EVENT_ENABLE + +#include + + +#ifndef STA_OS2_SYSTEM_EVENT_HANDLE +# define STA_OS2_SYSTEM_EVENT_HANDLE systemEventHandle +#endif // !STA_OS2_SYSTEM_EVENT_HANDLE + +// Access handle from freertos.c +extern osEventFlagsId_t STA_OS2_SYSTEM_EVENT_HANDLE; + + +namespace sta +{ + void signalSystemEvents(uint32_t flags) + { + osEventFlagsSet(STA_OS2_SYSTEM_EVENT_HANDLE, flags); + } + + void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout) + { + osEventFlagsWait(STA_OS2_SYSTEM_EVENT_HANDLE, flags, options | osFlagsNoClear, timeout); + } + + + void signalStartupEvent() + { + signalSystemEvents(STA_SYSTEM_EVENT_STARTUP); + } + + void waitForStartupEvent() + { + waitForSystemEvents(STA_SYSTEM_EVENT_STARTUP, osFlagsWaitAll, osWaitForever); + } +} // namespace sta + + +#endif // STA_SYSTEM_EVENT_ENABLE diff --git a/src/os2/watchdog/heartbeat.cpp b/src/os2/watchdog/heartbeat.cpp new file mode 100644 index 0000000..a71fe40 --- /dev/null +++ b/src/os2/watchdog/heartbeat.cpp @@ -0,0 +1,46 @@ +#include + +#ifdef STA_OS2_WATCHDOG_ENABLE + +#include + + +#ifndef STA_OS2_WATCHDOG_TIMER_PERIOD +# define STA_OS2_WATCHDOG_TIMER_PERIOD 1000 +#endif // !STA_OS2_WATCHDOG_TIMER_PERIOD + +#ifndef STA_OS2_WATCHDOG_TIMER_HANDLE +# define STA_OS2_WATCHDOG_TIMER_HANDLE heartbeatHandle +#endif // !STA_OS2_WATCHDOG_TIMER_HANDLE + +#ifndef STA_OS2_WATCHDOG_TIMER_CALLBACK +# define STA_OS2_WATCHDOG_TIMER_CALLBACK heartbeatCallback +#endif // !STA_OS2_WATCHDOG_TIMER_CALLBACK + + +// Access handles from freertos.c +extern osTimerId_t STA_OS2_WATCHDOG_TIMER_HANDLE; + + +namespace sta +{ + void startWatchdogTimer() + { + osTimerStart(STA_OS2_WATCHDOG_TIMER_HANDLE, STA_OS2_WATCHDOG_TIMER_PERIOD); + } +} // namespace sta + + +// Declare with C linkage +extern "C" +{ + void STA_OS2_WATCHDOG_TIMER_CALLBACK(void *) + { + // Notify watchdog task to send heartbeat message + // Required because blocking in a timer callback is not allowed + sta::notifyWatchdog(STA_WATCHDOG_FLAG_HEARTBEAT); + } +} + + +#endif // STA_OS2_WATCHDOG_ENABLE diff --git a/src/watchdog/watchdog.cpp b/src/os2/watchdog/watchdog.cpp similarity index 51% rename from src/watchdog/watchdog.cpp rename to src/os2/watchdog/watchdog.cpp index 9441ddd..80a3c5d 100644 --- a/src/watchdog/watchdog.cpp +++ b/src/os2/watchdog/watchdog.cpp @@ -1,18 +1,26 @@ -#include +#include -#ifdef STA_WATCHDOG_ENABLE +#ifdef STA_OS2_WATCHDOG_ENABLE -#include -#include -#include +#include +#include #include #include -// Access handles from freertos.c -extern osThreadId_t watchdogHandle; +#ifndef STA_OS2_WATCHDOG_HANDLE +# define STA_OS2_WATCHDOG_HANDLE watchdogHandle +#endif // !STA_OS2_WATCHDOG_HANDLE + +#ifndef STA_OS2_WATCHDOG_ENTRY_FUNCTION +# define STA_OS2_WATCHDOG_ENTRY_FUNCTION watchdogTask +#endif // !STA_OS2_WATCHDOG_ENTRY_FUNCTION + + +// Access handle from freertos.c +extern osThreadId_t STA_OS2_WATCHDOG_HANDLE; namespace sta @@ -31,7 +39,7 @@ namespace sta // Declare with C linkage extern "C" { - STA_WEAK void watchdogTask(void * arg) + void STA_OS2_WATCHDOG_ENTRY_FUNCTION(void * arg) { sta::waitForStartupEvent(); @@ -47,4 +55,4 @@ extern "C" } -#endif // STA_WATCHDOG_ENABLE +#endif // STA_OS2_WATCHDOG_ENABLE diff --git a/src/startup.cpp b/src/startup.cpp deleted file mode 100644 index 4c67e0a..0000000 --- a/src/startup.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include - -#ifdef STA_STARTUP_TASK_ENABLE - -#include -#include -#include - -#include - -#include - - -namespace sta -{ - // Weak noop to be overridden by application if required - STA_WEAK void startupTaskExtras(void *) {} -} // namespace sta - - -// Declare with C linkage -extern "C" -{ - void startupTask(void * arg) - { - // Call further initialization code - sta::startupTaskExtras(arg); - - -#ifdef STA_WATCHDOG_ENABLE - // Start timers - sta::startWatchdogTimer(); -#endif - - // Wake tasks - sta::signalStartupEvent(); - - // Terminate task - osThreadExit(); - } -} - - -#endif // STA_STARTUP_TASK_ENABLE diff --git a/src/system_event.cpp b/src/system_event.cpp deleted file mode 100644 index 419f6e3..0000000 --- a/src/system_event.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include - -#ifdef STA_SYSTEM_EVENT_ENABLE - -#include - - -// Access handles from freertos.c -extern osEventFlagsId_t systemEventHandle; - - -namespace sta -{ - void signalSystemEvents(uint32_t flags) - { - osEventFlagsSet(systemEventHandle, flags); - } - - - void waitForSystemEvents(uint32_t flags, uint32_t options, uint32_t timeout) - { - osEventFlagsWait(systemEventHandle, flags, options | osFlagsNoClear, timeout); - } - - - void signalStartupEvent() - { - signalSystemEvents(STA_SYSTEM_EVENT_STARTUP); - } - - void waitForStartupEvent() - { - waitForSystemEvents(STA_SYSTEM_EVENT_STARTUP, osFlagsWaitAll, osWaitForever); - } -} // namespace sta - - -#endif // STA_SYSTEM_EVENT_ENABLE diff --git a/src/watchdog/heartbeat.cpp b/src/watchdog/heartbeat.cpp deleted file mode 100644 index 778a489..0000000 --- a/src/watchdog/heartbeat.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include - -#ifdef STA_WATCHDOG_ENABLE - -#include - - -#ifndef STA_WATCHDOG_TIMER_PERIOD -# define STA_WATCHDOG_TIMER_PERIOD 1000 -#endif // STA_WATCHDOG_TIMER_PERIOD - - -// Access handles from freertos.c -extern osTimerId_t heartbeatHandle; - - -namespace sta -{ - void startWatchdogTimer() - { - osTimerStart(heartbeatHandle, STA_WATCHDOG_TIMER_PERIOD); - } -} // namespace sta - - -// Declare with C linkage -extern "C" -{ - void heartbeatCallback(void *) - { - // Notify watchdog task to send heartbeat message - // Required because blocking in a timer callback is not allowed - sta::notifyWatchdog(STA_WATCHDOG_FLAG_HEARTBEAT); - } -} - - -#endif // STA_WATCHDOG_ENABLE