Rename to STA RTOS

This commit is contained in:
Henrik Stickann
2022-05-10 16:18:55 +02:00
parent 802999f2f6
commit 5e2296a028
23 changed files with 493 additions and 491 deletions

53
include/sta/rtos/defs.hpp Normal file
View File

@@ -0,0 +1,53 @@
/**
* @file
* @brief Constants and macros for use with CMSIS RTOS2.
*/
#ifndef STA_RTOS_DEFS_HPP
#define STA_RTOS_DEFS_HPP
/**
* @defgroup STA_RTOS RTOS
* @brief STA RTOS library.
*/
/**
* @defgroup STA_RTOS_API API
* @ingroup STA_RTOS
* @brief Public interface.
*/
/**
* @defgroup STA_RTOS_BuildConfig Build config
* @ingroup STA_RTOS
* @brief Build configuration options.
*/
/**
* @ingroup STA_RTOS_API
* @{
*/
// See limits defined in cmsis_os2.c
/**
* @brief Number of usable bits in task notification flags.
*/
#define STA_RTOS_MAX_BITS_TASK_NOTIFY 31U
/**
* @brief Number of usable bits in event group flags.
*/
#define STA_RTOS_MAX_BITS_EVENT_GROUPS 24U
/**
* @brief Mask for valid task notification flag bits.
*/
#define STA_RTOS_THREAD_FLAGS_VALID_BITS ( ( 1UL << STA_RTOS_MAX_BITS_TASK_NOTIFY ) - 1U )
/**
* @brief Mask for valid event group flag bits.
*/
#define STA_RTOS_EVENT_FLAGS_VALID_BITS ( ( 1UL << STA_RTOS_MAX_BITS_EVENT_GROUPS ) - 1U )
/** @} */
#endif // STA_RTOS_DEFS_HPP

View File

@@ -0,0 +1,85 @@
/**
* @file
* @brief Helper for easy system task setup in `<sta/config.hpp>`.
*/
#ifndef STA_RTOS_EASY_CONFIG_HPP
#define STA_RTOS_EASY_CONFIG_HPP
/**
* @defgroup STA_RTOS_EasyConfig Easy Config
* @ingroup STA_RTOS_BuildConfig
* @brief Helpers for easy RTOS module setup.
*
* Use this header only inside the <sta/config.hpp> header of your application.
*/
#ifdef DOXYGEN
/**
* @brief Don't warn about use of <rtos2/easy_config.hpp> outside of <sta/config.hpp>.
*
* @ingroup STA_RTOS_EasyConfig
*/
# define STA_RTOS_EASY_CONFIG_NO_WARNING
#endif // DOXYGEN
#if !defined(STA_CONFIG_HPP) && !defined(STA_RTOS_EASY_CONFIG_NO_WARNING)
#warning "Intended for use in <sta/config.hpp>"
#endif // !STA_CONFIG_HPP && !STA_RTOS_EASY_CONFIG_NO_WARNING
#ifdef DOXYGEN
/**
* @brief Enable all system tasks and required features.
*
* @ingroup STA_RTOS_EasyConfig
*/
# define STA_RTOS_EASY_CONFIG_SYSTEM_TASKS_ENABLE
#endif // DOXYGEN
#ifdef STA_RTOS_EASY_CONFIG_SYSTEM_TASKS_ENABLE
// Enable system events used by system tasks
# define STA_RTOS_SYSTEM_EVENT_ENABLE
// Enable system tasks
# define STA_RTOS_WATCHDOG_ENABLE
# define STA_RTOS_STARTUP_ENABLE
#endif // STA_RTOS_EASY_CONFIG_SYSTEM_TASKS_ENABLE
#define _STA_RTOS_CONCAT(a, b) a ## b
#define STA_RTOS_MAKE_HANDLE_NAME(name) _STA_RTOS_CONCAT(name, Handle)
#define STA_RTOS_MAKE_CALLBACK_NAME(name) _STA_RTOS_CONCAT(name, Callback)
#define STA_RTOS_MAKE_TASK_NAME(name) _STA_RTOS_CONCAT(name, Task)
#ifdef DOXYGEN
/**
* @brief Common base name used for watchdog timer handle and callback names.
*
* @ingroup STA_RTOS_EasyConfig
*/
# define STA_RTOS_EASY_CONFIG_WATCHDOG_TIMER_NAME
#endif // DOXYGEN
#ifdef STA_RTOS_EASY_CONFIG_WATCHDOG_TIMER_NAME
# define STA_RTOS_WATCHDOG_TIMER_HANDLE STA_RTOS_MAKE_HANDLE_NAME(STA_RTOS_EASY_CONFIG_WATCHDOG_TIMER_NAME)
# define STA_RTOS_WATCHDOG_TIMER_CALLBACK STA_RTOS_MAKE_CALLBACK_NAME(STA_RTOS_EASY_CONFIG_WATCHDOG_TIMER_NAME)
#endif // STA_RTOS_EASY_CONFIG_WATCHDOG_TIMER_NAME
#ifdef DOXYGEN
/**
* @brief Common base name used for watchdog task handle and entry function names.
*
* @ingroup STA_RTOS_EasyConfig
*/
# define STA_RTOS_EASY_CONFIG_WATCHDOG_NAME
#endif // DOXYGEN
#ifdef STA_RTOS_EASY_CONFIG_WATCHDOG_NAME
# define STA_RTOS_WATCHDOG_HANDLE STA_RTOS_MAKE_HANDLE_NAME(STA_RTOS_EASY_CONFIG_WATCHDOG_NAME)
# define STA_RTOS_WATCHDOG_ENTRY_FUNCTION STA_RTOS_MAKE_TASK_NAME(STA_RTOS_EASY_CONFIG_WATCHDOG_NAME)
#endif // STA_RTOS_EASY_CONFIG_WATCHDOG_NAME
#endif // STA_RTOS_EASY_CONFIG_HPP

View File

@@ -0,0 +1,37 @@
/**
* @file
* @brief RTOS mutex implementation.
*/
#ifndef STA_RTOS_MUTEX_HPP
#define STA_RTOS_MUTEX_HPP
#include <sta/intf/mutex.hpp>
#include <cmsis_os2.h>
namespace sta
{
/**
* @brief Implementation of Mutex interface using CMSIS RTOS2.
*
* @ingroup STA_RTOS_API
*/
class RTOSMutex : public Mutex
{
public:
/**
* @param handle CMSIS RTOS2 mutex
*/
RTOSMutex(osMutexId_t * handle);
void acquire() override;
void release() override;
private:
osMutexId_t * handle_; /**< CMSIS RTOS2 mutex */
};
} // namespace sta
#endif // STA_RTOS_MUTEX_HPP

View File

@@ -0,0 +1,67 @@
/**
* @file
* @brief RTOS queue implementation.
*/
#ifndef STA_RTOS_QUEUE_HPP
#define STA_RTOS_QUEUE_HPP
#include <cmsis_os2.h>
#include <cstdint>
namespace sta
{
/**
* @brief Interface object for using CMSIS RTOS2 queues.
*
* @tparam Message type
*
* @ingroup STA_RTOS_API
*/
template <typename T>
class RTOSQueue
{
public:
using Message = T; /**< Queue message type */
public:
/**
* @param handle CMSIS RTOS2 queue handle
*/
RTOSQueue(osMessageQueueId_t * handle);
/**
* @brief Place message in queue.
*
* @param msg Message object
* @param timeout Timeout
* @return True on success
*/
bool put(const Message & msg, uint32_t timeout = osWaitForever);
/**
* @brief Take message from queue.
*
* @param[out] outMsg Message object destination
* @param timeout Timeout
* @return True on success
*/
bool get(Message * outMsg, uint32_t timeout = osWaitForever);
/**
* @brief Get number of messages in queue.
*
* @return Number of messages in queue
*/
uint32_t available() const;
private:
osMessageQueueId_t * handle_; /**< CMSIS RTOS2 queue handle */
};
} // namespace sta
#include <sta/rtos/queue.tpp>
#endif // STA_RTOS_QUEUE_HPP

View File

@@ -0,0 +1,41 @@
#ifndef STA_RTOS_QUEUE_TPP
#define STA_RTOS_QUEUE_TPP
#ifndef STA_RTOS_QUEUE_HPP
# error "Internal header. Use <sta/rtos/queue.hpp> instead."
#endif // !STA_RTOS_QUEUE_HPP
#include <sta/assert.hpp>
namespace sta
{
template <typename T>
RTOSQueue<T>::RTOSQueue(osMessageQueueId_t * handle)
: handle_{handle}
{
STA_ASSERT(handle != nullptr);
}
template <typename T>
bool RTOSQueue<T>::put(const Message & msg, uint32_t timeout /* = osWaitForever */)
{
return (osOK == osMessageQueuePut(*handle_, &msg, 0, timeout));
}
template <typename T>
bool RTOSQueue<T>::get(Message * outMsg, uint32_t timeout /* = osWaitForever */)
{
uint8_t prio;
return (osOK == osMessageQueueGet(*handle_, outMsg, &prio, timeout));
}
template <typename T>
uint32 RTOSQueue<T>::available() const
{
return osMessageQueueGetCount(*handle_);
}
} // namespace sta
#endif // STA_RTOS_QUEUE_TPP

View File

@@ -0,0 +1,39 @@
/**
* @file
* @brief RTOS signal implementation.
*/
#ifndef STA_RTOS_SIGNAL_HPP
#define STA_RTOS_SIGNAL_HPP
#include <sta/intf/signal.hpp>
#include <cmsis_os2.h>
namespace sta
{
/**
* @brief Implementation of Signal interface using CMSIS RTOS2.
*
* @ingroup STA_RTOS_API
*/
class RTOSSignal : public Signal
{
public:
/**
* @param semaphore CMSIS RTOS2 semaphore
*/
RTOSSignal(osSemaphoreId_t * semaphore);
void notify() override;
bool peek() override;
bool test() override;
void wait() override;
private:
osSemaphoreId_t * semaphore_; /**< CMSIS RTOS2 semaphore */
};
} // namespace sta
#endif // STA_RTOS_SIGNAL_HPP

View File

@@ -0,0 +1,57 @@
/**
* @file
* @brief Implementation of startup system task.
*/
#ifndef STA_RTOS_STARTUP_HPP
#define STA_RTOS_STARTUP_HPP
/**
* @defgroup STA_RTOS_Startup Startup task
* @ingroup STA_RTOS_API
* @brief Startup system task.
*
* Check @ref STA_RTOS_BuildConfig for configuration options.
*/
#ifdef DOXYGEN
/**
* @brief Enable module.
*
* @ingroup STA_RTOS_BuildConfig
*/
# define STA_RTOS_STARTUP_ENABLE
#endif // DOXYGEN
/**
* @def STA_RTOS_STARTUP_ENTRY_FUNCTION
* @brief Set name of startup task entry function.
*
* @ingroup STA_RTOS_BuildConfig
*/
#ifndef STA_RTOS_STARTUP_ENTRY_FUNCTION
# define STA_RTOS_STARTUP_ENTRY_FUNCTION startupTask
#endif // !STA_RTOS_STARTUP_ENTRY_FUNCTION
#include <sta/config.hpp>
#ifdef STA_RTOS_STARTUP_ENABLE
namespace sta
{
/**
* @brief Extra initialization run at start of startup task.
*
* May be overridden by application if required.
*
* @ingroup STA_RTOS_Startup
*/
void startupExtras(void * argument);
} // namespace sta
#endif // STA_RTOS_STARTUP_ENABLE
#endif // STA_RTOS_STARTUP_HPP

View File

@@ -0,0 +1,96 @@
/**
* @file
* @brief Implementation of system events.
*/
#ifndef STA_RTOS_SYSTEM_EVENT_HPP
#define STA_RTOS_SYSTEM_EVENT_HPP
/**
* @defgroup STA_RTOS_SysEvent System Events
* @ingroup STA_RTOS_API
* @brief System events.
*
* Check @ref STA_RTOS_BuildConfig for configuration options.
*/
#ifdef DOXYGEN
/**
* @brief Enable module.
*
* @ingroup STA_RTOS_BuildConfig
*/
# define STA_RTOS2_SYSTEM_EVENT_ENABLE
#endif // DOXYGEN
/**
* @def STA_RTOS_SYSTEM_EVENT_HANDLE
* @brief Set variable name of event flags handle.
*
* @ingroup STA_RTOS_BuildConfig
*/
#ifndef STA_RTOS_SYSTEM_EVENT_HANDLE
# define STA_RTOS_SYSTEM_EVENT_HANDLE systemEventHandle
#endif // !STA_RTOS_SYSTEM_EVENT_HANDLE
#include <sta/config.hpp>
#ifdef STA_RTOS_SYSTEM_EVENT_ENABLE
#include <cstdint>
// System event flags
//
/**
* @brief Startup system event flag.
*
* @ingroup STA_RTOS_SysEvent
*/
#define STA_SYSTEM_EVENT_STARTUP 0x100000U
namespace sta
{
/**
* @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 sta
#endif // STA_RTOS_SYSTEM_EVENT_ENABLE
#endif // STA_RTOS_SYSTEM_EVENT_HPP

View File

@@ -0,0 +1,126 @@
/**
* @file
* @brief Implementation of watchdog system task.
*/
#ifndef STA_RTOS_WATCHDOG_HPP
#define STA_RTOS_WATCHDOG_HPP
/**
* @defgroup STA_RTOS_Watchdog Watchdog task
* @ingroup STA_RTOS_API
* @brief Watchdog system task.
*
* Check @ref STA_RTOS_BuildConfig for configuration options.
*/
#ifdef DOXYGEN
/**
* @brief Enable module.
*
* @ingroup STA_RTOS_BuildConfig
*/
# define STA_RTOS_WATCHDOG_ENABLE
#endif // DOXYGEN
/**
* @def STA_RTOS_WATCHDOG_TIMER_PERIOD
* @brief Set period in ticks of heartbeat timer.
*
* @ingroup STA_RTOS_BuildConfig
*/
#ifndef STA_RTOS_WATCHDOG_TIMER_PERIOD
# define STA_RTOS_WATCHDOG_TIMER_PERIOD 1000
#endif // !STA_RTOS_WATCHDOG_TIMER_PERIOD
/**
* @def STA_RTOS_WATCHDOG_TIMER_HANDLE
* @brief Set variable name of heartbeat timer handle.
*
* @ingroup STA_RTOS_BuildConfig
*/
#ifndef STA_RTOS_WATCHDOG_TIMER_HANDLE
# define STA_RTOS_WATCHDOG_TIMER_HANDLE heartbeatHandle
#endif // !STA_RTOS_WATCHDOG_TIMER_HANDLE
/**
* @def STA_RTOS_WATCHDOG_TIMER_CALLBACK
* @brief Set name of heartbeat timer callback function.
*
* @ingroup STA_RTOS_BuildConfig
*/
#ifndef STA_RTOS_WATCHDOG_TIMER_CALLBACK
# define STA_RTOS_WATCHDOG_TIMER_CALLBACK heartbeatCallback
#endif // !STA_RTOS_WATCHDOG_TIMER_CALLBACK
/**
* @def STA_RTOS_WATCHDOG_HANDLE
* @brief Set variable name of watchdog task handle.
*
* @ingroup STA_RTOS_BuildConfig
*/
#ifndef STA_RTOS_WATCHDOG_HANDLE
# define STA_RTOS_WATCHDOG_HANDLE watchdogHandle
#endif // !STA_RTOS_WATCHDOG_HANDLE
/**
* @def STA_RTOS_WATCHDOG_ENTRY_FUNCTION
* @brief Set name of watchdog task entry function.
*
* @ingroup STA_RTOS_BuildConfig
*/
#ifndef STA_RTOS_WATCHDOG_ENTRY_FUNCTION
# define STA_RTOS_WATCHDOG_ENTRY_FUNCTION watchdogTask
#endif // !STA_RTOS_WATCHDOG_ENTRY_FUNCTION
#include <sta/config.hpp>
#ifdef STA_RTOS_WATCHDOG_ENABLE
#include <cstdint>
// Watchdog task flags
//
/**
* @brief Watchdog heartbeat flag.
*
* @ingroup STA_RTOS_Watchdog
*/
#define STA_WATCHDOG_FLAG_HEARTBEAT 0x00001000U
namespace sta
{
/**
* @brief Start heartbeat timer for watchdog.
*
* @ingroup STA_RTOS_Watchdog
*/
void startWatchdogTimer();
/**
* @brief Send notification to watchdog task.
*
* @ingroup STA_RTOS_Watchdog
*/
void notifyWatchdog(uint32_t flags);
/**
* @brief Handler for watchdog events.
*
* Must be implemented by application.
*
* @param arg Watchdog task argument
* @param flags Event flags
*
* @ingroup STA_RTOS_Watchdog
*/
void watchdogEventHandler(void * arg, uint32_t flags);
} // namespace sta
#endif // STA_RTOS_WATCHDOG_ENABLE
#endif // STA_RTOS2_WATCHDOG_HPP