0
0
FreeRTOSprogramming~10 mins

Tick timer and scheduler in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Tick timer and scheduler
System starts
Tick Timer Interrupt
Increment Tick Count
Check if Scheduler Needed
Call Scheduler
Select Next Task
Switch Context
Run Next Task
Back to Tick Timer Interrupt
The tick timer triggers an interrupt regularly, increments the tick count, and calls the scheduler to decide if a task switch is needed.
Execution Sample
FreeRTOS
void vTickISR() {
  tick_count++;
  if (scheduler_needed()) {
    vTaskSwitchContext();
  }
}
This code runs on each tick interrupt, updates the tick count, and switches tasks if needed.
Execution Table
StepTick CountScheduler Needed?ActionCurrent TaskNext Task
10NoIncrement tick_count to 1Task ATask A
21YesCall scheduler and switchTask ATask B
32NoIncrement tick_count to 3Task BTask B
43YesCall scheduler and switchTask BTask C
54NoIncrement tick_count to 5Task CTask C
65YesCall scheduler and switchTask CTask A
Exit6-Stop simulation--
💡 Simulation stops after 6 ticks for demonstration.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
tick_count0123456
current_taskTask ATask ATask BTask BTask CTask CTask A
next_taskTask ATask ATask BTask BTask CTask CTask A
Key Moments - 2 Insights
Why does the scheduler sometimes not switch tasks even though the tick count increases?
Because the scheduler only switches tasks when 'scheduler_needed()' returns Yes, as shown in steps 2, 4, and 6 in the execution_table.
What happens if the scheduler switches tasks?
The current task changes to the next task selected by the scheduler, as seen in steps 2, 4, and 6 where current_task updates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the tick_count at step 4?
A3
B4
C2
D5
💡 Hint
Check the Tick Count column at step 4 in the execution_table.
At which step does the scheduler NOT switch tasks despite the tick increment?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look for steps where 'Scheduler Needed?' is No in the execution_table.
If the scheduler always returned No, what would happen to current_task over time?
AIt would keep switching tasks.
BIt would cause an error.
CIt would stay the same task.
DIt would reset to Task A every tick.
💡 Hint
Refer to variable_tracker to see how current_task changes only when scheduler_needed() is Yes.
Concept Snapshot
Tick timer triggers an interrupt regularly.
Each tick increments a global tick count.
Scheduler checks if a task switch is needed.
If yes, it switches context to the next task.
If no, current task continues running.
This cycle repeats continuously.
Full Transcript
In FreeRTOS, the tick timer is a hardware timer that generates interrupts at fixed intervals. Each interrupt runs the tick ISR, which increments the system tick count. After incrementing, the scheduler checks if a higher priority task is ready to run. If so, it switches context to that task. Otherwise, the current task continues. This process repeats, allowing multitasking by switching tasks on each tick when needed.