0
0
FreeRTOSprogramming~10 mins

Idle task and idle hook in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Idle task and idle hook
System starts FreeRTOS scheduler
All tasks blocked or suspended?
NoRun highest priority ready task
Yes
Run Idle Task
Idle Hook called?
YesExecute user Idle Hook function
No
Idle Task loops back waiting for tasks
The FreeRTOS scheduler runs tasks by priority. When no tasks are ready, the Idle Task runs. If an Idle Hook is defined, it runs inside the Idle Task loop.
Execution Sample
FreeRTOS
void vApplicationIdleHook(void) {
    // User code here
    // e.g., enter low power mode
}

// FreeRTOS runs idle task when no other task is ready
This code shows the Idle Hook function that runs inside the Idle Task when the system is idle.
Execution Table
StepConditionActionOutput
1Scheduler startedRun highest priority ready taskTask1 runs
2Task1 blocked or suspendedCheck other tasksNo ready tasks
3No ready tasksRun Idle TaskIdle Task runs
4Idle Hook defined?YesIdle Hook function called
5Idle Hook executes user codeUser code runse.g., low power mode entered
6Idle Hook endsIdle Task loopsWait for tasks to become ready
7New task readyScheduler switches to new taskNew task runs
💡 Idle Task runs continuously when no other tasks are ready; Idle Hook runs inside Idle Task if defined
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 7
Ready TasksTask1 readyNo ready tasksNo ready tasksNo ready tasksNew task ready
Idle Task StateNot runningNot runningRunningRunningNot running
Idle Hook StateNot runningNot runningNot runningRunningNot running
Key Moments - 3 Insights
Why does the Idle Task run only when no other tasks are ready?
Because FreeRTOS always runs the highest priority ready task. The Idle Task has the lowest priority and runs only when no other tasks are ready, as shown in execution_table row 3.
What happens if the Idle Hook is not defined?
The Idle Task runs an empty loop doing nothing. The Idle Hook function is only called if defined, as shown in execution_table row 4.
Can the Idle Hook be used to save power?
Yes, the Idle Hook is a good place to put code to enter low power mode, as shown in execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the Idle Hook function run?
AStep 7
BStep 2
CStep 4
DStep 1
💡 Hint
Check the 'Action' column in execution_table row 4 where Idle Hook is called.
According to variable_tracker, what is the state of 'Ready Tasks' after Step 3?
ANo ready tasks
BNew task ready
CTask1 ready
DIdle Task running
💡 Hint
Look at the 'Ready Tasks' row and the 'After Step 3' column in variable_tracker.
If a new task becomes ready at Step 7, what happens to the Idle Task?
AIdle Task continues running
BIdle Task suspends and new task runs
CIdle Hook runs again
DSystem resets
💡 Hint
See execution_table row 7 describing scheduler switching to new task.
Concept Snapshot
Idle Task runs when no other tasks are ready.
It has the lowest priority.
Idle Hook is a user function called inside Idle Task loop.
Use Idle Hook for low power or background work.
If no Idle Hook, Idle Task loops doing nothing.
Full Transcript
FreeRTOS runs tasks based on priority. When no tasks are ready, the Idle Task runs. The Idle Task has the lowest priority and runs continuously until a task becomes ready. If the user defines an Idle Hook function, FreeRTOS calls it inside the Idle Task loop. This hook is useful for running background code or entering low power mode. The execution table shows the scheduler starting, running tasks, then running the Idle Task and Idle Hook when no tasks are ready. Variables track which tasks are ready and the state of the Idle Task and Idle Hook. Key moments clarify why the Idle Task runs only when no other tasks are ready, what happens if the Idle Hook is not defined, and how the Idle Hook can save power. The visual quiz tests understanding of when the Idle Hook runs, the state of ready tasks, and what happens when a new task becomes ready.