|
ELEC-C7222
Libraries for ELEC C7222 Course Work
|
Ownership-based C++ wrapper for FreeRTOS software timers. More...
#include <freertos_timer.hpp>


Public Types | |
| enum class | Type : uint8_t { kOneShot , kPeriodic } |
Public Member Functions | |
| FreeRtosTimer ()=default | |
| Create an uninitialized timer wrapper. | |
| FreeRtosTimer (const char *name, std::uint32_t period_ticks, Type type, std::function< void(void *)> callback=nullptr) | |
| Create a FreeRTOS software timer. | |
| bool | Initialize (const char *name, std::uint32_t period_ticks, Type type, std::function< void(void *)> callback) |
| Initialize (or re-initialize) the timer wrapper. | |
| ~FreeRtosTimer () | |
| Delete the timer if it was created. | |
| bool | Start (std::uint32_t ticks_to_wait=0, void *callback_arg=nullptr) |
| Start the timer. | |
| bool | StartFromISR (void *callback_arg=nullptr) |
| Start the timer from ISR context (no immediate yield). | |
| bool | Stop (std::uint32_t ticks_to_wait=0) |
| Stop the timer. | |
| bool | StopFromISR () |
| Stop the timer from ISR context (no immediate yield). | |
| bool | Reset (std::uint32_t ticks_to_wait=0) |
| Reset the timer to start counting from zero. | |
| bool | ResetFromISR () |
| Reset the timer from ISR context (no immediate yield). | |
| bool | ChangePeriod (std::uint32_t period_ticks, std::uint32_t ticks_to_wait=0) |
| Change the timer period. | |
| bool | ChangePeriodFromISR (std::uint32_t period_ticks) |
| Change the timer period from ISR context (no immediate yield). | |
| void | SetCallback (std::function< void(void *)> callback) |
| Register or replace the timer callback. | |
| bool | IsValid () const |
| Check if the timer handle is valid. | |
| bool | IsActive () const |
| Check whether the timer is active. | |
Public Member Functions inherited from c7222::NonCopyable | |
| NonCopyable (const NonCopyable &)=delete | |
| NonCopyable & | operator= (const NonCopyable &)=delete |
| NonCopyable (NonCopyable &&)=default | |
| NonCopyable & | operator= (NonCopyable &&)=default |
Public Member Functions inherited from c7222::NonMovable | |
| NonMovable (const NonMovable &)=default | |
| NonMovable & | operator= (const NonMovable &)=default |
| NonMovable (NonMovable &&)=delete | |
| NonMovable & | operator= (NonMovable &&)=delete |
Friends | |
| void | FreeRtosTimerCallback (void *timer) |
| Internal use only. This function is invoked as a callback when the FreeRTOS timer expires. | |
Additional Inherited Members | |
Protected Member Functions inherited from c7222::NonCopyableNonMovable | |
| NonCopyableNonMovable ()=default | |
| ~NonCopyableNonMovable ()=default | |
Protected Member Functions inherited from c7222::NonCopyable | |
| NonCopyable ()=default | |
| ~NonCopyable ()=default | |
Protected Member Functions inherited from c7222::NonMovable | |
| NonMovable ()=default | |
| ~NonMovable ()=default | |
Ownership-based C++ wrapper for FreeRTOS software timers.
This class provides a small, ownership-based interface around a FreeRTOS software timer handle. It uses a single internal callback trampoline (FreeRtosTimerCallback) to bridge the C callback signature to a stored std::function<void(void*)> in the C++ object.
Design principles:
Initialize() (or the non-default constructor).NonCopyableNonMovable to prevent copying timer handles or callbacks across objects.FreeRTOS timer summary and usage in this class:
std::function<void(void*)> that is invoked by the C callback trampoline with a user-supplied argument. SetCallback() updates that function at runtime.ticks_to_wait parameters to control command queue blocking time.Typical usage:
|
strong |
|
default |
Create an uninitialized timer wrapper.
The handle is null until Initialize() is called. This allows construction without allocating RTOS resources.
| c7222::FreeRtosTimer::FreeRtosTimer | ( | const char * | name, |
| std::uint32_t | period_ticks, | ||
| Type | type, | ||
| std::function< void(void *)> | callback = nullptr |
||
| ) |
Create a FreeRTOS software timer.
| name | Human-readable timer name. |
| period_ticks | Timer period in ticks. |
| type | One-shot or periodic. |
| callback | Callback invoked on expiry (optional). |
| c7222::FreeRtosTimer::~FreeRtosTimer | ( | ) |
Delete the timer if it was created.
This is safe to call even if the timer was never initialized.
| bool c7222::FreeRtosTimer::ChangePeriod | ( | std::uint32_t | period_ticks, |
| std::uint32_t | ticks_to_wait = 0 |
||
| ) |
Change the timer period.
The new period takes effect after the command is processed by the timer service task.
| period_ticks | New period in ticks. |
| ticks_to_wait | Maximum ticks to block if the timer command queue is full (0 = no wait). |
| bool c7222::FreeRtosTimer::ChangePeriodFromISR | ( | std::uint32_t | period_ticks | ) |
Change the timer period from ISR context (no immediate yield).
Any unblocked task will run on the next tick/schedule point.
| bool c7222::FreeRtosTimer::Initialize | ( | const char * | name, |
| std::uint32_t | period_ticks, | ||
| Type | type, | ||
| std::function< void(void *)> | callback | ||
| ) |
Initialize (or re-initialize) the timer wrapper.
Allocates the underlying FreeRTOS timer and binds the callback trampoline. If the wrapper was previously initialized, the prior handle is deleted before creating a new one.
| name | Human-readable timer name. |
| period_ticks | Timer period in ticks. |
| type | One-shot or periodic. |
| callback | Callback invoked on expiry (optional). |

| bool c7222::FreeRtosTimer::IsActive | ( | ) | const |
Check whether the timer is active.
| bool c7222::FreeRtosTimer::IsValid | ( | ) | const |
Check if the timer handle is valid.
Initialize() succeeded and the handle is non-null. 
| bool c7222::FreeRtosTimer::Reset | ( | std::uint32_t | ticks_to_wait = 0 | ) |
Reset the timer to start counting from zero.
For periodic timers, this restarts the period. For one-shot timers, this arms the timer again.
| ticks_to_wait | Maximum ticks to block if the timer command queue is full (0 = no wait). |
| bool c7222::FreeRtosTimer::ResetFromISR | ( | ) |
Reset the timer from ISR context (no immediate yield).
Any unblocked task will run on the next tick/schedule point.
| void c7222::FreeRtosTimer::SetCallback | ( | std::function< void(void *)> | callback | ) |
Register or replace the timer callback.
The callback runs in the FreeRTOS timer service task context. It must be short, non-blocking, and thread-safe with respect to shared resources. Passing nullptr clears the callback.
| bool c7222::FreeRtosTimer::Start | ( | std::uint32_t | ticks_to_wait = 0, |
| void * | callback_arg = nullptr |
||
| ) |
Start the timer.
Enqueues a start command to the FreeRTOS timer service task.
| ticks_to_wait | Maximum ticks to block if the timer command queue is full (0 = no wait). |
| callback_arg | Argument passed to the timer callback on expiry. |

| bool c7222::FreeRtosTimer::StartFromISR | ( | void * | callback_arg = nullptr | ) |
Start the timer from ISR context (no immediate yield).
This variant does not expose higher_priority_task_woken; any unblocked task will run on the next tick/schedule point.
| callback_arg | Argument passed to the timer callback on expiry. |

| bool c7222::FreeRtosTimer::Stop | ( | std::uint32_t | ticks_to_wait = 0 | ) |
Stop the timer.
Enqueues a stop command to the FreeRTOS timer service task.
| ticks_to_wait | Maximum ticks to block if the timer command queue is full (0 = no wait). |
| bool c7222::FreeRtosTimer::StopFromISR | ( | ) |
Stop the timer from ISR context (no immediate yield).
Any unblocked task will run on the next tick/schedule point.
|
friend |
Internal use only. This function is invoked as a callback when the FreeRTOS timer expires.
This function should not be used directly by the user. It is intended to be called by the FreeRTOS timer infrastructure to handle timer expiration events. Ensure that any logic that needs to be executed on timer expiry is encapsulated within the FreeRtosTimer class and its associated callback mechanism.
| timer | A pointer to the timer instance that has expired. |