Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated Jul 2025

xTaskDelayUntil()

[Task Control]

task. h

1BaseType_t xTaskDelayUntil( TickType_t *pxPreviousWakeTime,
2 const TickType_t xTimeIncrement );

INCLUDE\_xTaskDelayUntil
must be defined as 1 for this function to be available. See the RTOS Configuration documentation for more information.

Delay a task until a specified time. This function can be used by periodic tasks to ensure a constant execution frequency.

This function differs from

vTaskDelay()
in one important aspect:
vTaskDelay()
will cause a task to block for the specified number of ticks from the time
vTaskDelay()
is called whereas
xTaskDelayUntil()
will cause a task to block for the specified number of ticks from the time specified in the
pxPreviousWakeTime
parameter. It is difficult to use
vTaskDelay()
by itself to generate a fixed execution frequency, as the time between a task starting to execute and that task calling
vTaskDelay()
may not be fixed [the task may take a different path through the code between calls, or may get interrupted or preempted a different number of times each time it executes].
xTaskDelayUntil()
can be used to generate a constant execution frequency.

Whereas

vTaskDelay()
specifies a wake time relative to the time at which the function is called,
xTaskDelayUntil()
specifies the absolute (exact) time at which it wishes to unblock.

The macro

pdMS\_TO\_TICKS()
can be used to calculate the number of ticks from a time specified in milliseconds with a resolution of one tick period.

Parameters:

  • pxPreviousWakeTime

    Pointer to a variable that holds the time at which the task was last unblocked. The variable must be initialised with the current time prior to its first use (see the example below). Following this the variable is automatically updated within

    xTaskDelayUntil()
    .

  • xTimeIncrement

    The cycle time period. The task will be unblocked at time

    (*pxPreviousWakeTime + xTimeIncrement)
    . Calling
    xTaskDelayUntil
    with the same
    xTimeIncrement
    parameter value will cause the task to execute with a fixed interval period.

Returns:

A value which can be used to check whether the task was actually delayed:

pdTRUE
if the task was delayed and
pdFALSE
otherwise. A task will not be delayed if the next expected wake time is in the past.

Example usage:

1
2// Perform an action every 10 ticks.
3void vTaskFunction( void * pvParameters )
4{
5TickType_t xLastWakeTime;
6const TickType_t xFrequency = 10;
7BaseType_t xWasDelayed;
8
9 // Initialise the xLastWakeTime variable with the current time.
10 xLastWakeTime = xTaskGetTickCount ();
11 for( ;; )
12 {
13 // Wait for the next cycle.
14 xWasDelayed = xTaskDelayUntil( &xLastWakeTime, xFrequency );
15
16 // Perform action here. xWasDelayed value can be used to determine
17 // whether a deadline was missed if the code here took too long.
18 }
19}
20