(2) FreeRTOS mission control (1)

Table of Contents

1. Task delay vTaskDelay

2. Task delay vTaskDelayUntil

3. Task delay xTaskDelayUnti


1. Task delay vTaskDelay

//task. h

void vTaskDelay( const TickType_t xTicksToDelay);

INCLUDE_vTaskDelay must be defined as 1 for this function to be available. Delay the task by the given number of ticks. The actual time the task remains blocked depends on the tick frequency. The constant portTICK_PERIOD_MS along with the tick period resolution can be used to calculate the actual time from the tick frequency.

vTaskDelay() specifies the time at which the task wants to be unblocked, relative to the time vTaskDelay() is called. For example, if you specify a time block of 100 ticks, the task will unblock 100 ticks after vTaskDelay() is called. vTaskDelay() does not therefore provide a good way to control the frequency of periodic tasks, because the path through the code and other tasks and interrupt activity will affect how often vTaskDelay() is called, which in turn affects when the next task is executed.

Parameters:

xTicksToDelay The number of tick cycles the calling task should block.

Usage examples:

void vTaskFunction( void * pvParameters )
 {
 /* Block for 500ms. */
 const TickType_t xDelay = 500 / portTICK_PERIOD_MS;

     for( ;; )
     {
         /* Simply toggle the LED every 500ms, blocking between each toggle. */
         vToggleLED();
         vTaskDelay(xDelay);
     }
}

2. Task delay vTaskDelayUntil

//task. h

void vTaskDelayUntil( TickType_t *pxPreviousWakeTime,
                      const TickType_t xTimeIncrement );

INCLUDE_vTaskDelayUntil must be defined as 1 to use this function. Delay the 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 way: vTaskDelay() specifies the time at which the task wants to unblock, which is relative to the time vTaskDelay() is called, whereas vTaskDelayUntil () specifies the absolute time at which the task wants to be unblocked.

vTaskDelay() will cause a task to block for a specific number of ticks from the time vTaskDelay() is called. Therefore, it is difficult to use vTaskDelay() alone to generate a fixed execution frequency, because the time between the task unblocking after calling vTaskDelay() and the task calling vTaskDelay() again may not be fixed [the task may be called between two take different code paths, or may be interrupted or preempted a different number of times on each execution].

vTaskDelay() specifies the wake-up time relative to the time the function is called, and vTaskDelayUntil() specifies the absolute (exact) time it wants to unblock.

It should be noted that if vTaskDelayUntil() is used to specify a wake-up time that has elapsed, the function will return immediately (without blocking). Therefore, a task that is executed periodically using vTaskDelayUntil() must recalculate its required wake-up time if the periodic execution is stopped for any reason (for example, the task is suspended), causing the task to miss one or more periodic executions. This can be discovered by examining the variable passed by reference, which is the pxPreviousWakeTime parameter for the current tick count. However, this is not necessary in most use cases.

The constant portTICK_PERIOD_MS combined with the tick period resolution can be used to calculate the actual time from the tick frequency.

This function must not be called when vTaskSuspendAll() has been called to suspend the RTOS scheduler.

Parameters:

pxPreviousWakeTime Pointer to a variable, which is used to save the last task The time to unblock at a time. This variable must be initialized with the current time before it is used for the first time (see example below). After this, the variable is automatically updated in vTaskDelayUntil().
xTimeIncrement Periodic time period. The task will be unblocked at (*pxPreviousWakeTime + xTimeIncrement) time. Calling vTaskDelayUntil with the same xTimeIncrement parameter value will cause the task to execute at a fixed interval.

Example usage:

// Perform an action every 10 ticks.
 void vTaskFunction( void * pvParameters )
 {
 TickType_t xLastWakeTime;
 const TickType_t xFrequency = 10;

     // Initialise the xLastWakeTime variable with the current time.
     xLastWakeTime = xTaskGetTickCount();

     for( ;; )
     {
         // Wait for the next cycle.
         vTaskDelayUntil( & amp;xLastWakeTime, xFrequency );

         // Perform action here.
     }
 }

3. Task delay xTaskDelayUntil

//task. h

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

INCLUDE_xTaskDelayUntil must be defined as 1 for this function to be available. Delay the 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 way: 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 time specified in the pxPreviousWakeTime parameter. Blocks for the specified number of ticks. It is difficult to produce a fixed execution frequency using vTaskDelay() by itself, because the time between a task starting execution and the task calling vTaskDelay() may not be fixed [the task may take different code paths between calls, or Each execution may be interrupted or preempted a different number of times]. xTaskDelayUntil() can be used to generate a constant execution frequency.

vTaskDelay() specifies the wake-up time relative to when the function is called, while xTaskDelayUntil() specifies the absolute (exact) time it wants to unblock.

The macro pdMS_TO_TICKS() can be used to count the number of ticks in time in milliseconds with a resolution of one tick period.

Parameters:

pxPreviousWakeTime Pointer to a variable, which is used to save the last task The time to unblock at a time. This variable must be initialized with the current time before it is used for the first time (see example below). After this, the variable will be automatically updated in xTaskDelayUntil().
xTimeIncrement Periodic time period. The task will be unblocked at (*pxPreviousWakeTime + xTimeIncrement) time. Call xTaskDelayUntil with the same xTimeIncrement parameter value to execute at a fixed interval.

Return to:

A value that can be used to check whether the task is actually delayed: pdTRUE if the task is delayed, pdFALSE otherwise. If the next expected wake time has passed, the task will not be delayed.

Example usage:

// Perform an action every 10 ticks.
void vTaskFunction( void * pvParameters )
{
TickType_t xLastWakeTime;
const TickType_t xFrequency = 10;
BaseType_t xWasDelayed;

    // Initialise the xLastWakeTime variable with the current time.
    xLastWakeTime = xTaskGetTickCount ();
    for( ;; )
    {
        // Wait for the next cycle.
        xWasDelayed = xTaskDelayUntil( & amp;xLastWakeTime, xFrequency );

        // Perform action here. xWasDelayed value can be used to determine
        // whether a deadline was missed if the code here took too long.
    }
}