ENع
→The FreeRTOS scheduler
الدرس 39 · Bare metal versus an RTOS

The FreeRTOS tick and how it picks a task

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة11 خطوة5 أسئلةAdvanced
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 39 · تفاعلي
01 / 11

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. Tasks, 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 event
  3. Highest 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 runs
  4. vTaskDelay: 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 3
  5. The 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 ms
  6. Tick 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 ms
  7. Tick 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)
  8. 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 ms
  9. Back 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 job
  10. Nothing 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 priorities
  11. Tick 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

اختبار يتاح بعد الخطوة الأخيرة.