The FreeRTOS tick and how it picks a task
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Every 1 ms a hardware timer interrupts the CPU: that is the FreeRTOS tick. In the tick interrupt FreeRTOS counts time, wakes tasks whose delay is over and decides which task runs next. Follow three tasks on an ESP32-C3 for six ticks and watch the rule at work: the highest-priority Ready task runs, equal priorities take turns each tick, Blocked tasks use no CPU, and the idle task fills the gaps. “FreeRTOS tasks and priorities” shows how to create the tasks; the OS lessons “Round robin and the time slice” and “Real-time scheduling and priority inversion” hold the general theory.
The tick: a 1 ms heartbeat
A hardware timer interrupts the CPU every tick. On our ESP32-C3 with the Arduino core the tick rate is 1000 Hz, so one tick is 1 ms. Each tick FreeRTOS adds 1 to the tick count and checks whether to switch tasks.
tick rate configTICK_RATE_HZ = 1000 -> 1 tick = 1 ms
tick ISR tick count += 1
wake tasks whose delay is over
pick again if needed
tick count ticks since the scheduler startedاقرأ الدرس خطوة بخطوة
The tick: a 1 ms heartbeat
A hardware timer interrupts the CPU every tick. On our ESP32-C3 with the Arduino core the tick rate is 1000 Hz, so one tick is 1 ms. Each tick FreeRTOS adds 1 to the tick count and checks whether to switch tasks.
tick rate configTICK_RATE_HZ = 1000 -> 1 tick = 1 ms tick ISR tick count += 1 wake tasks whose delay is over pick again if needed tick count ticks since the scheduler startedTasks, priorities and states
A task is a C function that loops forever, with a priority: higher numbers matter more. Running means on the CPU. Ready means it could run now. Blocked means waiting, using no CPU. The idle task, priority 0, is always ready.
task priority one job then sleeps sensor 2 0.2 ms 3 ticks display 1 2.5 ms 20 ticks logger 1 1.7 ms 20 ticks idle 0 (always ready) Running on the CPU (one core: one task) Ready could run now Blocked waiting for a tick or an eventHighest priority ready task runs
The scheduler has one rule: run the highest-priority task that is ready. At tick 0 all are ready, and sensor has priority 2, the highest. It goes onto the CPU. display and logger wait in the priority 1 list.
rule run the highest-priority Ready task tick 0 ready: sensor (2), display (1), logger (1), idle (0) -> sensor runsvTaskDelay: sensor blocks
sensor reads in 0.2 ms, then calls vTaskDelay(WAIT), which is 3 ticks. It moves to Blocked until the tick count reaches 0 + 3 = 3. Blocked tasks use no CPU, so the processor is free for others.
line 3 read(); 0.0 - 0.2 ms line 4 vTaskDelay(WAIT); WAIT = pdMS_TO_TICKS(3) = 3 ticks tick count 0 + 3 = 3 -> Blocked until tick 3The next priority down
The CPU must not sit empty, so the scheduler picks again at once. Priority 2 is now empty, so it takes the first task in the priority 1 list: display. It runs from 0.2 ms to the next tick.
priority 2 (empty: sensor is Blocked) priority 1 display, logger -> display runs display 0.2 ms - 1.0 msTick 1: equal priorities take turns
Tick 1. display and logger share priority 1, so the tick ends display's time slice: it goes back to Ready and logger runs. This is round robin with a time slice of one tick, 1 ms. display has not finished; it simply waits.
tick 1 time slicing: same priority, take turns display Running -> Ready (paused, state saved) logger Ready -> Running 1.0 - 2.0 ms time slice = one tick = 1 msTick 3: sensor wakes up
Tick 2 swaps them back: display runs again. Then comes tick 3. The tick interrupt sees that sensor's wait is over and moves it from Blocked to the priority 2 Ready list. Nobody else had to check the time.
tick 2 logger -> Ready, display -> Running 2.0 - 3.0 ms tick 3 tick count = 3 = sensor's wake tick sensor: Blocked -> Ready (priority 2)Preemption: sensor jumps in
sensor (2) now outranks display (1), so in the same tick interrupt FreeRTOS preempts display. A context switch saves display's registers on its own stack and loads sensor's. display will resume exactly where it stopped.
tick 3 ready priority 2 > running priority 1 -> preempt context switch save display's registers (its own stack) load sensor's registers sensor 0.0 ms - 0.2 msBack to sleep, turns go on
sensor reads, then blocks until tick 6. The priority 1 turn passes to logger, which finishes its job at 3.9 ms and blocks for 20 ticks, until tick 23. display gets the CPU back.
sensor read, vTaskDelay(WAIT) -> Blocked until 3 + 3 = 6 logger 3.2 ms - 3.9 ms, job done -> Blocked until 23 display 3.9 ms -> resumes its jobNothing ready: idle runs
display finishes at 4.6 ms and blocks until tick 24. Every task is now Blocked except idle, priority 0, so idle runs. A task that never blocked would starve every task below it.
display 3.9 ms - 4.6 ms, job done (2.5 ms in all) -> Blocked until 24 idle 4.6 ms - 6.0 ms (priority 0) a task that never blocks starves all lower prioritiesTick 6: and again
Tick 6: sensor wakes and preempts idle at once. So the sensor runs every 3 ms however busy the others are, while display and logger share the time left over. Priorities decide who runs; the tick decides when to look again.
tick 6 sensor: Blocked -> Ready -> Running (preempts idle) sensor starts at 0, 3, 6 ms ... every 3 ticks rule highest-priority Ready task runs; equal priorities share by time slice; Blocked tasks use no CPU
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01configTICK_RATE_HZ is 1000. A task calls vTaskDelay(pdMS_TO_TICKS(20)) when the tick count is 4. When does it become Ready again?
At 1000 Hz one tick is 1 ms, so pdMS_TO_TICKS(20) is 20 ticks. vTaskDelay counts from now: 4 + 20 = 24. That is exactly what happens to display in the figure (it blocks until tick 24).
02sensor (priority 2), display (priority 1) and the idle task are all Ready. Which one runs?
FreeRTOS always runs the highest-priority task that is Ready. Priority 2 beats priority 1 and 0.
03display and logger both have priority 1, both are Ready, and nothing higher is Ready. What happens at each tick?
With time slicing on, tasks of equal priority take turns: the tick ends one time slice and the scheduler picks the next Ready task of that priority (round robin).
04At tick 3 sensor (priority 2) leaves the Blocked state while display (priority 1) is running. What does FreeRTOS do?
With preemption on, a higher-priority task that becomes Ready takes the CPU straight away. A context switch saves display's registers on its own stack; display later resumes exactly where it stopped.
05Why must a high-priority task block (for example with vTaskDelay) instead of looping without pause?
The scheduler always picks the highest-priority Ready task, so a task that is always Ready starves everything below it. In the figure, idle only runs from 4.6 ms because every other task has blocked (logger until tick 23, display until 24).
المسرد
- Tick
- A periodic interrupt from a hardware timer. FreeRTOS uses it to count time and to decide whether to switch tasks. At configTICK_RATE_HZ = 1000 it comes every 1 ms.
- Tick count
- The number of tick interrupts since the scheduler started. Delays are measured in it.
- Task
- A C function that runs forever in its own loop, with its own stack and a priority. FreeRTOS switches between tasks.
- Ready, Running, Blocked
- Running: on the CPU now. Ready: could run, waiting for the CPU. Blocked: waiting for time to pass or an event, using no CPU.
- Preemption
- Taking the CPU from the running task as soon as a higher-priority task becomes Ready, without the running task's agreement.
- Time slicing (round robin)
- Tasks of equal priority take turns, one time slice (one tick period) each.
- Context switch
- Saving the registers of the task that leaves the CPU and loading those of the task that takes it, so each resumes where it stopped.
- Idle task
- A task FreeRTOS creates at priority 0. It runs only when no other task is Ready.
الملاحظات والمصادر
- The figure starts the clock at tick 0 with all three tasks already ready. On the Arduino core, setup() itself runs inside a task called loopTask at priority 1, so sensor really starts the moment it is created; loop() then deletes loopTask so it does not share priority 1.
- read(), draw() and save() stand for your own code. The figure assumes they need 0.2 ms, 2.5 ms and 1.7 ms of CPU time. A real job takes as long as its code does.
- vTaskDelay(3) counts ticks from the moment it is called, so the period is 3 ticks only because read() is short. For a steady period use vTaskDelayUntil(), which counts from the last wake time.
- Preemption is not only at ticks: if an interrupt or another task makes a higher-priority task ready, FreeRTOS can switch at once. A queue or semaphore given from an interrupt does this, covered in the next lessons.
- The ESP32-C3 has one core, and ESP-IDF says its FreeRTOS keeps the plain single-core rules, including round robin time slicing. On a two-core ESP32 each core picks its own task, so two tasks can run at once.
- R. Barry, Mastering the FreeRTOS Real Time Kernel, ch. 4: Task Management
- R. Barry, Mastering the FreeRTOS Real Time Kernel, ch. 8: Resource Management (each task's own stack and registers)
- Espressif ESP-IDF Programming Guide (ESP32-C3): FreeRTOS (IDF), single-core behaviour
- Espressif esp32-arduino-lib-builder: defconfig.common (CONFIG_FREERTOS_HZ=1000)
- Espressif arduino-esp32 core: main.cpp (loopTask at priority 1)
اختبار يتاح بعد الخطوة الأخيرة.