FreeRTOS Tetris
Macros

Group that contains macros & functions to start, stop, reset and change the period of timers. More...

Collaboration diagram for Timer Control:

Macros

#define xTimerStart(xTimer, xTicksToWait)   xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )
 Start a timer that was previously created using the xTimerCreate() API function. If the timer had already been started and was already in the active state, then xTimerStart() has equivalent functionality to the xTimerReset() API function. More...
 
#define xTimerStop(xTimer, xTicksToWait)   xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP, 0U, NULL, ( xTicksToWait ) )
 Stop a timer that was previously started using either of the xTimerStart(), xTimerReset(), xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() or xTimerChangePeriodFromISR() API functions. More...
 
#define xTimerReset(xTimer, xTicksToWait)   xTimerGenericCommand( ( xTimer ), tmrCOMMAND_RESET, ( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )
 xTimerReset() re-starts a timer that was previously created using the xTimerCreate() API function. If the timer had already been started and was already in the active state, then xTimerReset() will cause the timer to re-evaluate its expiry time so that it is relative to when xTimerReset() was called. If the timer was in the dormant state then xTimerReset() has equivalent functionality to the xTimerStart() API function. More...
 
#define xTimerChangePeriod(xTimer, xNewPeriod, xTicksToWait)   xTimerGenericCommand( ( xTimer ), tmrCOMMAND_CHANGE_PERIOD, ( xNewPeriod ), NULL, ( xTicksToWait ) )
 Change the period of a timer that was previously created using the xTimerCreate() API function. More...
 

Detailed Description

Group that contains macros & functions to start, stop, reset and change the period of timers.

Macro Definition Documentation

◆ xTimerChangePeriod

#define xTimerChangePeriod (   xTimer,
  xNewPeriod,
  xTicksToWait 
)    xTimerGenericCommand( ( xTimer ), tmrCOMMAND_CHANGE_PERIOD, ( xNewPeriod ), NULL, ( xTicksToWait ) )

#include <timers.h>

Change the period of a timer that was previously created using the xTimerCreate() API function.

Timer functionality is provided by a timer service/daemon task. Many of the public FreeRTOS timer API functions send commands to the timer service task through a queue called the timer command queue. The timer command queue is private to the kernel itself and is not directly accessible to application code. The length of the timer command queue is set by the configTIMER_QUEUE_LENGTH configuration constant.

xTimerChangePeriod() can be called to change the period of an active or dormant state timer.

The configUSE_TIMERS configuration constant must be set to 1 for xTimerChangePeriod() to be available.

Parameters
xTimerThe handle of the timer that is having its period changed.
xNewPeriodThe new period for xTimer. Timer periods are specified in tick periods, so the constant portTICK_PERIOD_MS can be used to convert a time that has been specified in milliseconds. For example, if the timer must expire after 100 ticks, then xNewPeriod should be set to 100. Alternatively, if the timer must expire after 500ms, then xNewPeriod can be set to ( 500 / portTICK_PERIOD_MS ) provided configTICK_RATE_HZ is less than or equal to 1000.
xTicksToWaitSpecifies the time, in ticks, that the calling task should be held in the Blocked state to wait for the change period command to be successfully sent to the timer command queue, should the queue already be full when xTimerChangePeriod() was called. xTicksToWait is ignored if xTimerChangePeriod() is called before the scheduler is started.
Returns
pdFAIL will be returned if the change period command could not be sent to the timer command queue even after xTicksToWait ticks had passed. pdPASS will be returned if the command was successfully sent to the timer command queue. When the command is actually processed will depend on the priority of the timer service/daemon task relative to other tasks in the system. The timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY configuration constant.

Example usage:

// This function assumes xTimer has already been created. If the timer
// referenced by xTimer is already active when it is called, then the timer
// is deleted. If the timer referenced by xTimer is not active when it is
// called, then the period of the timer is set to 500ms and the timer is
// started.
void vAFunction( TimerHandle_t xTimer )
{
if( xTimerIsTimerActive( xTimer ) != pdFALSE ) // or more simply and equivalently "if( xTimerIsTimerActive( xTimer ) )"
{
// xTimer is already active - delete it.
xTimerDelete( xTimer );
}
else
{
// xTimer is not active, change its period to 500ms. This will also
// cause the timer to start. Block for a maximum of 100 ticks if the
// change period command cannot immediately be sent to the timer
// command queue.
if( xTimerChangePeriod( xTimer, 500 / portTICK_PERIOD_MS, 100 ) == pdPASS )
{
// The command was successfully sent.
}
else
{
// The command could not be sent, even after waiting for 100 ticks
// to pass. Take appropriate action here.
}
}
}

◆ xTimerReset

#define xTimerReset (   xTimer,
  xTicksToWait 
)    xTimerGenericCommand( ( xTimer ), tmrCOMMAND_RESET, ( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )

#include <timers.h>

xTimerReset() re-starts a timer that was previously created using the xTimerCreate() API function. If the timer had already been started and was already in the active state, then xTimerReset() will cause the timer to re-evaluate its expiry time so that it is relative to when xTimerReset() was called. If the timer was in the dormant state then xTimerReset() has equivalent functionality to the xTimerStart() API function.

Timer functionality is provided by a timer service/daemon task. Many of the public FreeRTOS timer API functions send commands to the timer service task through a queue called the timer command queue. The timer command queue is private to the kernel itself and is not directly accessible to application code. The length of the timer command queue is set by the configTIMER_QUEUE_LENGTH configuration constant.

Resetting a timer ensures the timer is in the active state. If the timer is not stopped, deleted, or reset in the mean time, the callback function associated with the timer will get called 'n' ticks after xTimerReset() was called, where 'n' is the timers defined period.

It is valid to call xTimerReset() before the scheduler has been started, but when this is done the timer will not actually start until the scheduler is started, and the timers expiry time will be relative to when the scheduler is started, not relative to when xTimerReset() was called.

The configUSE_TIMERS configuration constant must be set to 1 for xTimerReset() to be available.

Parameters
xTimerThe handle of the timer being reset/started/restarted.
xTicksToWaitSpecifies the time, in ticks, that the calling task should be held in the Blocked state to wait for the reset command to be successfully sent to the timer command queue, should the queue already be full when xTimerReset() was called. xTicksToWait is ignored if xTimerReset() is called before the scheduler is started.
Returns
pdFAIL will be returned if the reset command could not be sent to the timer command queue even after xTicksToWait ticks had passed. pdPASS will be returned if the command was successfully sent to the timer command queue. When the command is actually processed will depend on the priority of the timer service/daemon task relative to other tasks in the system, although the timers expiry time is relative to when xTimerStart() is actually called. The timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY configuration constant.

Example usage:

// When a key is pressed, an LCD back-light is switched on. If 5 seconds pass
// without a key being pressed, then the LCD back-light is switched off. In
// this case, the timer is a one-shot timer.
TimerHandle_t xBacklightTimer = NULL;
// The callback function assigned to the one-shot timer. In this case the
// parameter is not used.
void vBacklightTimerCallback( TimerHandle_t pxTimer )
{
// The timer expired, therefore 5 seconds must have passed since a key
// was pressed. Switch off the LCD back-light.
vSetBacklightState( BACKLIGHT_OFF );
}
// The key press event handler.
void vKeyPressEventHandler( char cKey )
{
// Ensure the LCD back-light is on, then reset the timer that is
// responsible for turning the back-light off after 5 seconds of
// key inactivity. Wait 10 ticks for the command to be successfully sent
// if it cannot be sent immediately.
vSetBacklightState( BACKLIGHT_ON );
if( xTimerReset( xBacklightTimer, 100 ) != pdPASS )
{
// The reset command was not executed successfully. Take appropriate
// action here.
}
// Perform the rest of the key processing here.
}
void main( void )
{
int32_t x;
// Create then start the one-shot timer that is responsible for turning
// the back-light off if no keys are pressed within a 5 second period.
xBacklightTimer = xTimerCreate( "BacklightTimer", // Just a text name, not used by the kernel.
( 5000 / portTICK_PERIOD_MS), // The timer period in ticks.
pdFALSE, // The timer is a one-shot timer.
0, // The id is not used by the callback so can take any value.
vBacklightTimerCallback // The callback function that switches the LCD back-light off.
);
if( xBacklightTimer == NULL )
{
// The timer was not created.
}
else
{
// Start the timer. No block time is specified, and even if one was
// it would be ignored because the scheduler has not yet been
// started.
if( xTimerStart( xBacklightTimer, 0 ) != pdPASS )
{
// The timer could not be set into the Active state.
}
}
// ...
// Create tasks here.
// ...
// Starting the scheduler will start the timer running as it has already
// been set into the active state.
// Should not reach here.
for( ;; );
}

◆ xTimerStart

#define xTimerStart (   xTimer,
  xTicksToWait 
)    xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START, ( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )

#include <timers.h>

Start a timer that was previously created using the xTimerCreate() API function. If the timer had already been started and was already in the active state, then xTimerStart() has equivalent functionality to the xTimerReset() API function.

Timer functionality is provided by a timer service/daemon task. Many of the public FreeRTOS timer API functions send commands to the timer service task through a queue called the timer command queue. The timer command queue is private to the kernel itself and is not directly accessible to application code. The length of the timer command queue is set by the configTIMER_QUEUE_LENGTH configuration constant.

Starting a timer ensures the timer is in the active state. If the timer is not stopped, deleted, or reset in the mean time, the callback function associated with the timer will get called 'n' ticks after xTimerStart() was called, where 'n' is the timers defined period.

It is valid to call xTimerStart() before the scheduler has been started, but when this is done the timer will not actually start until the scheduler is started, and the timers expiry time will be relative to when the scheduler is started, not relative to when xTimerStart() was called.

The configUSE_TIMERS configuration constant must be set to 1 for xTimerStart() to be available.

Parameters
xTimerThe handle of the timer being started/restarted.
xTicksToWaitSpecifies the time, in ticks, that the calling task should be held in the Blocked state to wait for the start command to be successfully sent to the timer command queue, should the queue already be full when xTimerStart() was called. xTicksToWait is ignored if xTimerStart() is called before the scheduler is started.
Returns
pdFAIL will be returned if the start command could not be sent to the timer command queue even after xTicksToWait ticks had passed. pdPASS will be returned if the command was successfully sent to the timer command queue. When the command is actually processed will depend on the priority of the timer service/daemon task relative to other tasks in the system, although the timers expiry time is relative to when xTimerStart() is actually called. The timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY configuration constant.

Example usage:

See the xTimerCreate() API function example usage scenario.

◆ xTimerStop

#define xTimerStop (   xTimer,
  xTicksToWait 
)    xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP, 0U, NULL, ( xTicksToWait ) )

#include <timers.h>

Stop a timer that was previously started using either of the xTimerStart(), xTimerReset(), xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() or xTimerChangePeriodFromISR() API functions.

Timer functionality is provided by a timer service/daemon task. Many of the public FreeRTOS timer API functions send commands to the timer service task through a queue called the timer command queue. The timer command queue is private to the kernel itself and is not directly accessible to application code. The length of the timer command queue is set by the configTIMER_QUEUE_LENGTH configuration constant.

Stopping a timer ensures the timer is not in the active state.

The configUSE_TIMERS configuration constant must be set to 1 for xTimerStop() to be available.

Parameters
xTimerThe handle of the timer being stopped.
xTicksToWaitSpecifies the time, in ticks, that the calling task should be held in the Blocked state to wait for the stop command to be successfully sent to the timer command queue, should the queue already be full when xTimerStop() was called. xTicksToWait is ignored if xTimerStop() is called before the scheduler is started.
Returns
pdFAIL will be returned if the stop command could not be sent to the timer command queue even after xTicksToWait ticks had passed. pdPASS will be returned if the command was successfully sent to the timer command queue. When the command is actually processed will depend on the priority of the timer service/daemon task relative to other tasks in the system. The timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY configuration constant.

Example usage:

See the xTimerCreate() API function example usage scenario.

xTimerDelete
#define xTimerDelete(xTimer, xTicksToWait)
Delete a timer that was previously created using the xTimerCreate() API function.
Definition: timers.h:454
xTimerStart
#define xTimerStart(xTimer, xTicksToWait)
Start a timer that was previously created using the xTimerCreate() API function. If the timer had alr...
Definition: timers.h:508
TimerHandle_t
void * TimerHandle_t
Type by which software timers are referenced. For example, a call to xTimerCreate() returns an TimerH...
Definition: timers.h:154
xTimerReset
#define xTimerReset(xTimer, xTicksToWait)
xTimerReset() re-starts a timer that was previously created using the xTimerCreate() API function....
Definition: timers.h:671
portTICK_PERIOD_MS
#define portTICK_PERIOD_MS
The tick period in ms.
Definition: portmacro.h:110
vTaskStartScheduler
void vTaskStartScheduler(void) PRIVILEGED_FUNCTION
Starts the real time kernel tick processing. After calling the kernel has control over which tasks ar...
Definition: tasks.c:1709
xTimerCreate
TimerHandle_t xTimerCreate(const char *const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void *const pvTimerID, TimerCallbackFunction_t pxCallbackFunction) PRIVILEGED_FUNCTION
Creates a new software timer instance, and returns a handle by which the created software timer can b...
xTimerIsTimerActive
BaseType_t xTimerIsTimerActive(TimerHandle_t xTimer) PRIVILEGED_FUNCTION
Queries a timer to see if it is active or dormant.
xTimerChangePeriod
#define xTimerChangePeriod(xTimer, xNewPeriod, xTicksToWait)
Change the period of a timer that was previously created using the xTimerCreate() API function.
Definition: timers.h:748