FreeRTOS-Cpp
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
FreeRTOS::StaticTimer Class Reference

Class that encapsulates the functionality of a FreeRTOS timer. More...

#include <Timer.hpp>

Inheritance diagram for FreeRTOS::StaticTimer:
Inheritance graph
[legend]
Collaboration diagram for FreeRTOS::StaticTimer:
Collaboration graph
[legend]

Public Member Functions

 StaticTimer (const StaticTimer &)=delete
 
StaticTimeroperator= (const StaticTimer &)=delete
 
- Public Member Functions inherited from FreeRTOS::TimerBase
 TimerBase (const TimerBase &)=delete
 
TimerBaseoperator= (const TimerBase &)=delete
 
virtual void timerEntry () final
 Function that acts as the entry point of the timer instance.
 
bool isValid () const
 Function that checks the value of the timer handle. This function should be called to ensure the timer was created successfully.
 
bool isActive () const
 Function that calls BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
 
bool start (const TickType_t blockTime=0) const
 Function that calls BaseType_t xTimerStart( TimerHandle_t xTimer, TickType_t xBlockTime )
 
bool startFromISR (bool &higherPriorityTaskWoken) const
 Function that calls BaseType_t xTimerStartFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken )
 
bool startFromISR () const
 Function that calls BaseType_t xTimerStartFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken )
 
bool stop (const TickType_t blockTime=0) const
 Function that calls BaseType_t xTimerStop( TimerHandle_t xTimer, TickType_t xBlockTime )
 
bool stopFromISR (bool &higherPriorityTaskWoken) const
 Function that calls BaseType_t xTimerStopFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken )
 
bool stopFromISR () const
 Function that calls BaseType_t xTimerStopFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken )
 
bool changePeriod (const TickType_t newPeriod, const TickType_t blockTime=0) const
 Function that calls BaseType_t xTimerChangePeriod( TimerHandle_t xTimer, TickType_t xNewPeriod, TickType_t xBlockTime )
 
bool changePeriodFromISR (bool &higherPriorityTaskWoken, const TickType_t newPeriod) const
 Function that calls BaseType_t xTimerChangePeriodFromISR( TimerHandle_t xTimer, TickType_t xNewPeriod, BaseType_t *pxHigherPriorityTaskWoken )
 
bool changePeriodFromISR (const TickType_t newPeriod) const
 Function that calls BaseType_t xTimerChangePeriodFromISR( TimerHandle_t xTimer, TickType_t xNewPeriod, BaseType_t *pxHigherPriorityTaskWoken )
 
bool deleteTimer (const TickType_t blockTime=0)
 Function that calls BaseType_t xTimerDelete( TimerHandle_t xTimer, TickType_t xBlockTime )
 
bool reset (const TickType_t blockTime=0) const
 Function that calls BaseType_t xTimerReset( TimerHandle_t xTimer, TickType_t xBlockTime )
 
bool resetFromISR (bool &higherPriorityTaskWoken) const
 Function that calls BaseType_t xTimerResetFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken )
 
bool resetFromISR () const
 Function that calls BaseType_t xTimerResetFromISR( TimerHandle_t xTimer, BaseType_t *pxHigherPriorityTaskWoken )
 
void setReloadMode (const bool autoReload) const
 Function that calls void vTimerSetReloadMode( TimerHandle_t xTimer, const UBaseType_t xAutoReload )
 
const char * getName () const
 Function that calls const char * pcTimerGetName( TimerHandle_t xTimer )
 
TickType_t getPeriod () const
 Function that calls TickType_t xTimerGetPeriod( TimerHandle_t xTimer )
 
TickType_t getExpiryTime () const
 Function that calls TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer )
 
bool getReloadMode () const
 Function that calls UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer )
 
void setDeleteBlockTime (const TickType_t deleteBlockTime=0)
 Set the delete block time. This value is used when the destructor calls deleteTimer().
 
TickType_t getDeleteBlockTime () const
 Set the delete block time. This value is used when the destructor calls deleteTimer().
 

Protected Member Functions

 StaticTimer (const TickType_t period, const bool autoReload=false, const char *name="", const TickType_t deleteBlockTime=0)
 Construct a new StaticTimer object by calling TimerHandle_t xTimerCreateStatic( const char * const pcTimerName, const TickType_t xTimerPeriod, const UBaseType_t xAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction StaticTimer_t *pxTimerBuffer )
 
 StaticTimer (StaticTimer &&) noexcept=default
 
StaticTimeroperator= (StaticTimer &&) noexcept=default
 
- Protected Member Functions inherited from FreeRTOS::TimerBase
virtual void timerFunction ()=0
 Abstraction function that acts as the entry point of the timer callback for the user.
 

Private Attributes

StaticTimer_t staticTimer
 

Additional Inherited Members

- Static Public Member Functions inherited from FreeRTOS::TimerBase
static void * operator new (size_t)=delete
 
static void * operator new[] (size_t)=delete
 
static void * operator new (size_t, void *ptr)
 
static void * operator new[] (size_t, void *ptr)
 

Detailed Description

Class that encapsulates the functionality of a FreeRTOS timer.

Timer Timer.hpp <FreeRTOS/Timer.hpp>

Each software timer requires a small amount of RAM that is used to hold the timer's state. If a timer is created using FreeRTOS::Timer() then this RAM is automatically allocated from the FreeRTOS heap. If a software timer is created this class then the RAM is included in the object so it can be statically allocated at compile time. See the Static Vs Dynamic allocation page for more information.

Note
This class is not intended to be instantiated by the user. The user should create a class that derives from this class and implement timerFunction().
Warning
This class contains the timer data structure, so any instance of this class or class derived from this class should be persistent (not declared on the stack of another function).

Constructor & Destructor Documentation

◆ StaticTimer()

FreeRTOS::StaticTimer::StaticTimer ( const TickType_t  period,
const bool  autoReload = false,
const char *  name = "",
const TickType_t  deleteBlockTime = 0 
)
inlineexplicitprotected

Construct a new StaticTimer object by calling TimerHandle_t xTimerCreateStatic( const char * const pcTimerName, const TickType_t xTimerPeriod, const UBaseType_t xAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction StaticTimer_t *pxTimerBuffer )

Timer.hpp

See also
https://www.freertos.org/xTimerCreateStatic.html
Note
When calling xTimerCreateStatic the constructor passes the this pointer as the pvTimerID argument. This pointer is used so that the interface function callTimerFunction() can invoke timerFunction() for this instance of the class.
Timers are created in the dormant state. The start(), reset(), startFromISR(), resetFromISR(), changePeriod() and changePeriodFromISR() API functions can all be used to transition a timer into the active state.
Parameters
periodThe period of the timer. The period is specified in ticks, and the macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds to a time specified in ticks. For example, if the timer must expire after 100 ticks, then simply set period to 100. Alternatively, if the timer must expire after 500ms, then set period to pdMS_TO_TICKS( 500 ). pdMS_TO_TICKS() can only be used if configTICK_RATE_HZ is less than or equal to 1000. The timer period must be greater than 0.
autoReloadIf autoReload is set to true, then the timer will expire repeatedly with a frequency set by the period parameter. If autoReload is set to false, then the timer will be a one-shot and enter the dormant state after it expires.
nameA human readable text name that is assigned to the timer. This is done purely to assist debugging. The RTOS kernel itself only ever references a timer by its handle, and never by its name.
deleteBlockTimeSpecifies the time, in ticks, that the calling task should be held in the Blocked state to wait for the delete command to be successfully sent to the timer command queue, should the queue already be full when the destructor is called. deleteBlockTime is ignored if the destructor is called before the RTOS scheduler is started or if the timer has already been deleted by deleteTimer() before the destructor is called. This value can be updated by calling setDeleteBlockTime().

Example Usage

#include <FreeRTOS/Kernel.hpp>
#include <FreeRTOS/Timer.hpp>
constexpr size_t numberOfTimers = 5;
class MyTimer : public FreeRTOS::StaticTimer {
public:
MyTimer() : FreeRTOS::StaticTimer(0, true, "Timer") {}
void timerFunction() final;
private:
uint8_t count = 0;
};
// Define a callback function that will be used by multiple timer instances. The
// callback function does nothing but count the number of times the associated
// timer expires, and stop the timer once the timer has expired 10 times.
void MyTimer::timerFunction() {
constexpr uint8_t maxExpiryCountBeforeStopping = 10;
// Optionally do something if the pxTimer parameter is NULL.
configASSERT(!isValid());
// Increment the count, then test to see if the timer has expired
// maxExpiryCountBeforeStopping yet.
count++;
// If the timer has expired 10 times then stop it from running.
if (count++ >= maxExpiryCountBeforeStopping) {
// Do not use a block time if calling a timer API function from a timer
// callback function, as doing so could cause a deadlock!
stop();
}
}
// An array of FreeRTOS::StaticTimer objects, which are used to store the state
// of each created timer.
static MyTimer xTimers[numberOfTimers];
int aFunction() {
// Initialize then start some timers. Starting the timers before the RTOS
// scheduler has been started means the timers will start running immediately
// that the RTOS scheduler starts.
for (size_t x = 0; x < numberOfTimers; x++) {
if (xTimers[x].isValid()) {
xTimers[x].changePeriod((100 * x) + 100);
if (!xTimers[x].start()) {
// The timer could not be set into the Active state.
}
} else {
// The timer was not created.
}
}
// Create tasks here.
// Starting the RTOS scheduler will start the timers running as they have
// already been set into the active state.
// Should not reach here.
for (;;) {
;
}
}
Class that encapsulates the functionality of a FreeRTOS timer.
Definition Timer.hpp:868
void startScheduler()
Function that calls vTaskStartScheduler()
Definition Kernel.hpp:293

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