RobotGuide

Hardware & electrical · topic E11 · 35 hours

RTOS (FreeRTOS / Zephyr)

An MCU never does just one thing: a 1 kHz current loop must run, CAN must be sent and received, sampling must happen, the host must be answered. Bare-metal while(1) cobles it together with state machines, and once tasks multiply they strangle each other. An RTOS answers three questions: who runs first, how long may it run, and how do shared resources queue up.

Core stage · Intermediate
System software · Hands-on Embedded & controls · Proficient

Tools
Tools

What to learn

  • Tasks and priorities: the ready/running/blocked three states, preemptive scheduling, time slices only among equal priorities
  • Sync primitives: queues carry data, semaphores carry events, mutexes guard resources — one sentence for each difference
  • Priority inversion and priority inheritance: a high-priority task blocked on a lock held by a low one while a middle one cuts in line — the interview favorite
  • Engineering details: how to size stacks, how to feed the watchdog, and why only FromISR versions of APIs may be called inside interrupts

The lesson · plain words

An RTOS (FreeRTOS/Zephyr) brings real task scheduling to the MCU: priority preemption, inter-task communication, software timers. From «big loop + flags» to an RTOS is the watershed of embedded engineering.

Preemptive scheduling and priority inversion

An RTOS preempts by priority: the instant a high-priority task becomes ready, lower ones yield immediately. Shared resources break that — watch the figure: the low-priority task holds the lock, the high-priority one is stuck, and the middle one finishes first. That is priority inversion (what killed Mars Pathfinder).

The fix is priority inheritance: the lock holder is temporarily boosted to the waiter's priority so it can finish its critical section fast. FreeRTOS mutexes inherit by default; bare binary semaphores do not — use the wrong type and this is what you get.

Interactive figure · drag it and see

What this figure shows: three tasks — high (servo), mid (telemetry), low (sampling) — on a shared timeline, with a toggle between bare semaphore and priority inheritance; in semaphore mode the high-priority task visibly waits on a lock held by the low one while the mid task finishes first, and inheritance shrinks the blocking window to 15 ms. Takeaway: use mutexes with priority inheritance; bare semaphores invert.

Task split: by «beat», not by «function»

The rule of thumb for splitting tasks: by timing beat and real-time class, not by functional module. The 1 kHz control task (highest priority), 100 Hz telemetry (middle), logging and comms (low, may block). Every task is one infinite loop + blocking wait — busy-waiting is forbidden.

Choosing inter-task communication: queue (data transfer, copy semantics), semaphore (event notification), mutex (protecting shared resources), task notification (fastest, one-to-one). Classic beginner mistake: «communicating» with global variables + flags — a breeding ground for race conditions.

Stacks, heaps, and the watchdog

Every task has its own stack, and overflow is the most common death (symptom: random hardfaults). Use xTaskGetStackHighWaterMark to check headroom, and over-allocate at creation. Heap allocation is banned inside real-time tasks (same discipline as D1) — allocate statically up front.

The watchdog is the last line of insurance: the feeding task must be the lowest priority — if it runs, every higher-priority task has yielded the CPU. When a task wedges, the watchdog triggers a reset; all field «reboot self-healing» rests on it.

FreeRTOS or Zephyr

FreeRTOS: small kernel, abundant documentation, ported to nearly every chip — the first choice for learning. Zephyr: device tree + driver model + Bluetooth stack in one full bundle, product-oriented, steep learning curve. Suggestion: learn scheduling principles on FreeRTOS, look at Zephyr when productizing.

How far to go: create three tasks at different priorities and explain the scheduling order; decouple an ADC task from the control task with a queue; explain priority inversion and inheritance clearly; configure a watchdog and demo a wedged task self-resetting.

Three questions — do you really get it?

1. The antidote to priority inversion is?

  • AMore tasks
  • BLower all priorities
  • CThe mutex's priority-inheritance mechanism
Show answer

C. The lock holder temporarily inherits the waiter's priority and rushes through its critical section.

2. The correct basis for splitting RTOS tasks is?

  • AFunctional modules
  • BTiming beat and real-time class
  • CSource file structure
Show answer

B. A 1 kHz control loop and 1 Hz logging want different things — that is where task boundaries naturally fall.

3. What priority should the watchdog-feeding task have?

  • AHighest
  • BMiddle
  • CLowest
Show answer

C. Lowest priority means it only gets fed when «every task has yielded the CPU normally» — exactly the signal you want to detect.

Keep going deeper

Links without a marker stay on this site; the rest point to the Chinese edition until its translation batch lands.

How it fails in the field

  • Calling blocking APIs (vTaskDelay, printf) inside an interrupt: the system dies at random
  • Stacks sized too small: overflow does not error out, it just produces «some task occasionally not running» — days of hunting

You have learned enough when

Run 4 tasks with different periods on an MCU, measure each task's actual period jitter, and reproduce priority inversion once.

The Chinese edition carries the full topic page for RTOS (FreeRTOS / Zephyr), including the complete outline, depth requirements per career track, and further derivations: robotguide.dev/k/E11/.