0
0
FreeRTOSprogramming~10 mins

vTaskDelayUntil() for precise timing in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - vTaskDelayUntil() for precise timing
Start: Get current tick count
Calculate next wake time = last wake time + period
Call vTaskDelayUntil(&lastWakeTime, period)
Task blocks until next wake time
Task resumes exactly at next wake time
Update lastWakeTime to next wake time
Back to start for next cycle
The function delays a task until a precise tick count, ensuring consistent periodic execution by updating the last wake time each cycle.
Execution Sample
FreeRTOS
TickType_t xLastWakeTime = xTaskGetTickCount();
const TickType_t xFrequency = 100;

for(;;) {
  vTaskDelayUntil(&xLastWakeTime, xFrequency);
  // Task code here
}
This code runs a task periodically every 100 ticks using vTaskDelayUntil for precise timing.
Execution Table
IterationxLastWakeTime (before delay)xFrequencyNext Wake TimevTaskDelayUntil ActionTask StatexLastWakeTime (after delay)
110001001100Delay until tick 1100Blocked until 11001100
211001001200Delay until tick 1200Blocked until 12001200
312001001300Delay until tick 1300Blocked until 13001300
413001001400Delay until tick 1400Blocked until 14001400
514001001500Delay until tick 1500Blocked until 15001500
Exit15001001600Delay until tick 1600Stopped (example end)1600
💡 Execution stops after 5 iterations for demonstration; normally runs indefinitely.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
xLastWakeTime100011001200130014001500
xFrequency100100100100100100
Key Moments - 3 Insights
Why do we use vTaskDelayUntil instead of vTaskDelay for periodic tasks?
vTaskDelayUntil uses a fixed reference time (xLastWakeTime) to calculate the next wake time, ensuring consistent intervals even if the task execution time varies. This is shown in the execution_table where xLastWakeTime increments by xFrequency each iteration.
What happens if the task takes longer than the period to execute?
If the task runs longer than the period, vTaskDelayUntil will not delay (or delay zero ticks) because the next wake time is already passed. This keeps the task on schedule without drifting, as seen by the fixed increments in xLastWakeTime.
Why do we update xLastWakeTime after each delay?
Updating xLastWakeTime to the next wake time keeps the timing reference fixed and prevents drift. The execution_table shows xLastWakeTime increasing by xFrequency each iteration, maintaining precise timing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is xLastWakeTime before the delay in iteration 3?
A1300
B1100
C1200
D1000
💡 Hint
Check the 'xLastWakeTime (before delay)' column in row for iteration 3.
At which iteration does the task delay until tick 1400?
AIteration 4
BIteration 5
CIteration 2
DIteration 3
💡 Hint
Look at the 'Next Wake Time' column in the execution_table.
If xFrequency changed to 200, how would xLastWakeTime change after iteration 2?
AIt would be 1200
BIt would be 1400
CIt would be 1300
DIt would be 1500
💡 Hint
xLastWakeTime increments by xFrequency each iteration; double the frequency doubles the increment.
Concept Snapshot
vTaskDelayUntil(&lastWakeTime, period) delays a task until a fixed tick count.
Use a TickType_t variable to store last wake time.
Each call adds period to lastWakeTime for precise periodic timing.
Prevents drift unlike vTaskDelay which delays relative to now.
Ideal for tasks needing consistent intervals.
Full Transcript
This visual execution trace shows how vTaskDelayUntil works in FreeRTOS to create precise periodic task timing. The task stores the last wake time in a variable and delays until the next scheduled tick count. Each iteration adds the fixed period to the last wake time, ensuring the task runs exactly every period ticks without drifting. The execution table tracks the last wake time before and after each delay, the next wake time, and the task state. Key moments clarify why vTaskDelayUntil is preferred over vTaskDelay for periodic tasks and how it handles tasks that run longer than the period. The quiz tests understanding of timing values and effects of changing the period. This method is essential for real-time applications requiring consistent timing.