0
0
FreeRTOSprogramming~5 mins

vTaskDelayUntil() for precise timing in FreeRTOS - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: vTaskDelayUntil() for precise timing
O(n)
Understanding Time Complexity

We want to understand how the time spent by a FreeRTOS task using vTaskDelayUntil() changes as the number of task cycles grows.

Specifically, how does the delay function affect the task's execution timing over many cycles?

Scenario Under Consideration

Analyze the time complexity of the following FreeRTOS task code using vTaskDelayUntil().

void vTaskFunction( void *pvParameters ) {
    TickType_t xLastWakeTime;
    const TickType_t xFrequency = pdMS_TO_TICKS( 100 );

    xLastWakeTime = xTaskGetTickCount();

    for( ;; ) {
        // Perform task action here

        vTaskDelayUntil( &xLastWakeTime, xFrequency );
    }
}

This code runs a task that repeats an action every 100 milliseconds precisely using vTaskDelayUntil().

Identify Repeating Operations

Look at what repeats in this task.

  • Primary operation: The infinite for loop repeating the task action and delay.
  • How many times: The loop runs indefinitely, repeating every fixed time interval.
How Execution Grows With Input

The task runs once every 100 ms, so the number of executions grows linearly with time.

Input Size (n) - Number of cyclesApprox. Operations
1010 task actions and delays
100100 task actions and delays
10001000 task actions and delays

Pattern observation: As the number of cycles increases, the total operations increase directly in proportion.

Final Time Complexity

Time Complexity: O(n)

This means the total time spent grows directly with the number of task cycles executed.

Common Mistake

[X] Wrong: "Using vTaskDelayUntil() makes the task run instantly without waiting, so time doesn't grow with cycles."

[OK] Correct: The function actually delays the task to keep timing precise, so each cycle takes a fixed time, making total time grow with the number of cycles.

Interview Connect

Understanding how task delays affect timing helps you design real-time systems that behave predictably, a key skill in embedded programming.

Self-Check

What if we changed xFrequency to a variable value that changes each cycle? How would the time complexity change?