ELEC-C7222
Libraries for ELEC C7222 Course Work
Loading...
Searching...
No Matches
c7222::FreeRtosTimer Class Reference

Ownership-based C++ wrapper for FreeRTOS software timers. More...

#include <freertos_timer.hpp>

Inheritance diagram for c7222::FreeRtosTimer:
Inheritance graph
Collaboration diagram for c7222::FreeRtosTimer:
Collaboration graph

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
 
NonCopyableoperator= (const NonCopyable &)=delete
 
 NonCopyable (NonCopyable &&)=default
 
NonCopyableoperator= (NonCopyable &&)=default
 
- Public Member Functions inherited from c7222::NonMovable
 NonMovable (const NonMovable &)=default
 
NonMovableoperator= (const NonMovable &)=default
 
 NonMovable (NonMovable &&)=delete
 
NonMovableoperator= (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
 

Detailed Description

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:

  • RAII ownership: the class owns a timer handle and deletes it in the destructor, avoiding leaks and dangling handles.
  • Explicit initialization: construction is lightweight; actual RTOS resources are allocated in Initialize() (or the non-default constructor).
  • Move/Copy safety: inherits NonCopyableNonMovable to prevent copying timer handles or callbacks across objects.
  • Thin wrapper: keeps FreeRTOS semantics visible (ticks, start/stop/reset), rather than hiding them behind higher-level abstractions.

FreeRTOS timer summary and usage in this class:

  • FreeRTOS software timers run in the Timer Service Task context, not in interrupt context. Callbacks must be non-blocking and thread-safe with respect to the rest of the system.
  • Timers are created with a period in ticks and a type (one-shot or periodic). The scheduler triggers the callback when the timer expires.
  • This class stores a std::function<void(void*)> that is invoked by the C callback trampoline with a user-supplied argument. SetCallback() updates that function at runtime.
  • Start/Stop/Reset/ChangePeriod map directly to the underlying FreeRTOS APIs and accept optional ticks_to_wait parameters to control command queue blocking time.

Typical usage:

c7222::FreeRtosTimer timer("blink",
[](void* arg) {
(void)arg;
// periodic work
});
(void)timer.Start();
static std::uint32_t MsToTicks(std::uint32_t milliseconds)
Convert milliseconds to scheduler ticks.
Ownership-based C++ wrapper for FreeRTOS software timers.
Definition freertos_timer.hpp:71
bool Start(std::uint32_t ticks_to_wait=0, void *callback_arg=nullptr)
Start the timer.
@ kPeriodic
Periodic timer: fires repeatedly at the configured period.

Member Enumeration Documentation

◆ Type

enum class c7222::FreeRtosTimer::Type : uint8_t
strong
Enumerator
kOneShot 

One-shot timer: fires once then stops.

kPeriodic 

Periodic timer: fires repeatedly at the configured period.

Constructor & Destructor Documentation

◆ FreeRtosTimer() [1/2]

c7222::FreeRtosTimer::FreeRtosTimer ( )
default

Create an uninitialized timer wrapper.

The handle is null until Initialize() is called. This allows construction without allocating RTOS resources.

◆ FreeRtosTimer() [2/2]

c7222::FreeRtosTimer::FreeRtosTimer ( const char *  name,
std::uint32_t  period_ticks,
Type  type,
std::function< void(void *)>  callback = nullptr 
)

Create a FreeRTOS software timer.

Parameters
nameHuman-readable timer name.
period_ticksTimer period in ticks.
typeOne-shot or periodic.
callbackCallback invoked on expiry (optional).

◆ ~FreeRtosTimer()

c7222::FreeRtosTimer::~FreeRtosTimer ( )

Delete the timer if it was created.

This is safe to call even if the timer was never initialized.

Member Function Documentation

◆ ChangePeriod()

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.

Parameters
period_ticksNew period in ticks.
ticks_to_waitMaximum ticks to block if the timer command queue is full (0 = no wait).
Returns
true if the command was accepted, false otherwise.

◆ ChangePeriodFromISR()

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.

Returns
true if the command was accepted, false otherwise.

◆ Initialize()

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.

Parameters
nameHuman-readable timer name.
period_ticksTimer period in ticks.
typeOne-shot or periodic.
callbackCallback invoked on expiry (optional).
Returns
true if the timer was created successfully, false otherwise.
Here is the caller graph for this function:

◆ IsActive()

bool c7222::FreeRtosTimer::IsActive ( ) const

Check whether the timer is active.

Returns
true if the timer is currently running, false otherwise.

◆ IsValid()

bool c7222::FreeRtosTimer::IsValid ( ) const

Check if the timer handle is valid.

Returns
true if Initialize() succeeded and the handle is non-null.
Here is the caller graph for this function:

◆ Reset()

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.

Parameters
ticks_to_waitMaximum ticks to block if the timer command queue is full (0 = no wait).
Returns
true if the command was accepted, false otherwise.

◆ ResetFromISR()

bool c7222::FreeRtosTimer::ResetFromISR ( )

Reset the timer from ISR context (no immediate yield).

Any unblocked task will run on the next tick/schedule point.

Returns
true if the command was accepted, false otherwise.

◆ SetCallback()

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.

◆ Start()

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.

Parameters
ticks_to_waitMaximum ticks to block if the timer command queue is full (0 = no wait).
callback_argArgument passed to the timer callback on expiry.
Returns
true if the command was accepted, false otherwise.
Here is the caller graph for this function:

◆ StartFromISR()

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.

Parameters
callback_argArgument passed to the timer callback on expiry.
Returns
true if the command was accepted, false otherwise.
Here is the caller graph for this function:

◆ Stop()

bool c7222::FreeRtosTimer::Stop ( std::uint32_t  ticks_to_wait = 0)

Stop the timer.

Enqueues a stop command to the FreeRTOS timer service task.

Parameters
ticks_to_waitMaximum ticks to block if the timer command queue is full (0 = no wait).
Returns
true if the command was accepted, false otherwise.

◆ StopFromISR()

bool c7222::FreeRtosTimer::StopFromISR ( )

Stop the timer from ISR context (no immediate yield).

Any unblocked task will run on the next tick/schedule point.

Returns
true if the command was accepted, false otherwise.

Friends And Related Symbol Documentation

◆ FreeRtosTimerCallback

void FreeRtosTimerCallback ( void *  timer)
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.

Parameters
timerA pointer to the timer instance that has expired.

The documentation for this class was generated from the following file: