0
0
FreeRTOSprogramming~10 mins

vTaskDelay() for periodic tasks in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - vTaskDelay() for periodic tasks
Task starts
Execute task code
Call vTaskDelay(delay)
Task blocked for delay ticks
Delay ends, task ready
Scheduler runs task again
Back to Execute task code
The task runs its code, then calls vTaskDelay to pause for a fixed time, making the task periodic by repeating this cycle.
Execution Sample
FreeRTOS
void Task(void *pvParameters) {
  for(;;) {
    // Do work
    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}
This task runs forever, doing work then delaying for 100 milliseconds each cycle.
Execution Table
StepActionTask StateDelay Time Left (ticks)Scheduler Behavior
1Task starts and runs codeRunning0Task runs
2Calls vTaskDelay(100)Blocked100Task blocked, scheduler runs other tasks
3Tick interrupt decrements delayBlocked99Task still blocked
4Tick interrupt decrements delayBlocked50Task still blocked
5Tick interrupt decrements delayBlocked1Task still blocked
6Tick interrupt decrements delay to 0Ready0Task ready to run
7Scheduler runs task againRunning0Task runs again
8Task repeats loopRunning0Task runs
9Calls vTaskDelay(100)Blocked100Task blocked again
10Execution continues similarly...Blocked/ReadyVariesCycle repeats
💡 Task never exits; it loops forever with periodic delays.
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 9Final
Task StateRunningBlockedReadyBlockedRepeats
Delay Time Left (ticks)01000100Cycles
Key Moments - 3 Insights
Why does the task become blocked after calling vTaskDelay?
Because vTaskDelay tells the scheduler to pause the task for the specified ticks, so it moves from Running to Blocked state as shown in step 2 of the execution_table.
How does the task become ready again after delay?
Each tick interrupt reduces the delay count. When it reaches zero (step 6), the task state changes from Blocked to Ready, so the scheduler can run it again.
Does vTaskDelay guarantee exact periodic timing?
vTaskDelay delays for at least the specified ticks, but actual timing depends on scheduler and other tasks. The task runs periodically but may have small timing variations.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Task State immediately after calling vTaskDelay?
ARunning
BReady
CBlocked
DSuspended
💡 Hint
Check Step 2 in the execution_table where vTaskDelay is called.
At which step does the delay time left reach zero, making the task ready again?
AStep 3
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the Delay Time Left column in the execution_table.
If the delay passed to vTaskDelay is doubled, how does the Delay Time Left change at Step 2?
AIt doubles
BIt stays the same
CIt halves
DIt becomes zero
💡 Hint
Refer to the Delay Time Left value at Step 2 in the execution_table.
Concept Snapshot
vTaskDelay(delayTicks) pauses a task for delayTicks ticks.
Task state changes from Running to Blocked during delay.
Delay counts down each tick until zero.
When zero, task becomes Ready and runs again.
Used to create periodic tasks with fixed wait times.
Full Transcript
This visual trace shows how vTaskDelay creates periodic tasks in FreeRTOS. The task runs its code, then calls vTaskDelay with a delay in ticks. This moves the task to Blocked state, pausing it. Each tick interrupt reduces the delay count. When the delay reaches zero, the task becomes Ready and the scheduler runs it again. This cycle repeats forever, making the task periodic. The execution table tracks task state and delay time step-by-step. Key moments explain why the task blocks and how it becomes ready again. The quiz tests understanding of task states and delay timing. This helps beginners see how vTaskDelay controls task timing visually.