Updated Apr 2025

xTimerStartFromISR

[Timer API]

timers.h

1 BaseType_t xTimerStartFromISR
2 (
3 TimerHandle_t xTimer,
4 BaseType_t *pxHigherPriorityTaskWoken
5 );

A version of xTimerStart() that can be called from an interrupt service routine.

Parameters:

  • xTimer

    The handle of the timer being started/restarted.

  • pxHigherPriorityTaskWoken

    The timer service/daemon task spends most of its time in the Blocked state, waiting for messages to arrive on the timer command queue. Calling xTimerStartFromISR() writes a message to the timer command queue, so has the potential to transition the timer service/daemon task out of the Blocked state. If calling xTimerStartFromISR() causes the timer service/daemon task to leave the Blocked state, and the timer service/daemon task has a priority equal to or greater than the currently executing task (the task that was interrupted), then *pxHigherPriorityTaskWoken will get set to pdTRUE internally within the xTimerStartFromISR() function. If xTimerStartFromISR() sets this value to pdTRUE, then a context switch should be performed before the interrupt exits.

Returns:

pdFAIL will be returned if the start command could not be sent to the timer command queue. 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 xTimerStartFromISR() is actually called. The timer service/daemon task priority is set by the configTIMER_TASK_PRIORITY configuration constant.

Example usage:

1/* This scenario assumes xBacklightTimer has already been created. When a
2 key is pressed, an LCD back-light is switched on. If 5 seconds pass
3 without a key being pressed, then the LCD back-light is switched off. In
4 this case, the timer is a one-shot timer, and unlike the example given for
5 the xTimerReset() function, the key press event handler is an interrupt
6 service routine. */
7
8/* The callback function assigned to the one-shot timer. In this case the
9 parameter is not used. */
10void vBacklightTimerCallback( TimerHandle_t pxTimer )
11{
12 /* The timer expired, therefore 5 seconds must have passed since a key
13 was pressed. Switch off the LCD back-light. */
14 vSetBacklightState( BACKLIGHT_OFF );
15}
16
17/* The key press interrupt service routine. */
18void vKeyPressEventInterruptHandler( void )
19{
20BaseType_t xHigherPriorityTaskWoken = pdFALSE;
21
22 /* Ensure the LCD back-light is on, then restart the timer that is
23 responsible for turning the back-light off after 5 seconds of
24 key inactivity. This is an interrupt service routine so can only
25 call FreeRTOS API functions that end in "FromISR". */
26 vSetBacklightState( BACKLIGHT_ON );
27
28 /* xTimerStartFromISR() or xTimerResetFromISR() could be called here
29 as both cause the timer to re-calculate its expiry time.
30 xHigherPriorityTaskWoken was initialised to pdFALSE when it was
31 declared (in this function). */
32 if( xTimerStartFromISR( xBacklightTimer,
33 &xHigherPriorityTaskWoken ) != pdPASS )
34 {
35 /* The start command was not executed successfully. Take appropriate
36 action here. */
37 }
38
39 /* Perform the rest of the key processing here. */
40
41 /* If xHigherPriorityTaskWoken equals pdTRUE, then a context switch
42 should be performed. The syntax required to perform a context switch
43 from inside an ISR varies from port to port, and from compiler to
44 compiler. Inspect the demos for the port you are using to find the
45 actual syntax required. */
46 if( xHigherPriorityTaskWoken != pdFALSE )
47 {
48 /* Call the interrupt safe yield function here (actual function
49 depends on the FreeRTOS port being used). */
50 }
51}