Loading header...

RTOS Fundamentals

A real-time operating system lets firmware run multiple tasks with defined priorities, blocking primitives, and time services. It can simplify complex products, but it also introduces scheduling, stack, and concurrency responsibilities.

Learning Objectives

By the end of this lesson, you should be able to explain tasks, priorities, preemption, queues, semaphores, mutexes, tick timing, and the main reasons to choose or avoid an RTOS.

Task Model

flowchart TB ISR[Interrupts] --> Q[Queues and semaphores] Q --> T1[Sensor task] Q --> T2[Communication task] Q --> T3[Control task] T1 --> S[Shared services] T2 --> S T3 --> S

Each task has its own stack and usually runs an infinite loop. Tasks block while waiting for time, messages, or events, allowing other tasks to run.

Priority and Preemption

A preemptive RTOS runs the highest-priority ready task. If a high-priority task becomes ready, it can interrupt a lower-priority task. This improves responsiveness but means shared resources need protection.

Queues, Semaphores, and Mutexes

Primitive Use
Queue pass data between tasks
Binary semaphore signal an event
Counting semaphore count repeated events or resources
Mutex protect a shared resource with ownership
Event group wait for bitwise event combinations

Stack Sizing

Each task stack must cover worst-case function calls, local variables, library calls, and interrupt or port requirements.

$$
RAM_{tasks}=\sum stack_i + RAM_{kernel}
$$

$$
RAM_{total}=RAM_{tasks}+RAM_{buffers}
$$

Measure high-water marks during stress tests and keep margin.

Worked Example: Sensor Gateway

A sensor gateway might use a high-priority control task at 10 ms, a sensor task triggered by timer, a communication task handling packets, and a low-priority logging task writing flash. Sensor ISR posts a semaphore; the sensor task reads data and sends a queue message.

Priority Inversion

Priority inversion happens when a high-priority task waits for a mutex held by a low-priority task while a medium-priority task keeps running. Use RTOS mutexes with priority inheritance and keep mutex hold time short.

Common Mistakes

  • Assigning priorities by importance instead of deadline.
  • Too-small task stacks.
  • Using queues for large data copies instead of pointers with ownership rules.
  • Calling non-ISR-safe APIs from interrupts.
  • Holding a mutex while waiting on slow I/O.

Summary

An RTOS provides tasks, scheduling, synchronization, and timing services. It is powerful when complexity justifies it, but it demands careful priority design, stack measurement, and shared-resource discipline.

Further Reading

  • FreeRTOS Kernel Book and API reference.
  • Arm CMSIS-RTOS documentation.
  • Jean Labrosse, "MicroC/OS" real-time concepts.

Mind Map

mindmap root((RTOS Fundamentals)) Core concept Highest ready task runs Tasks block on events Kernel manages time Applications Gateways Motor control UI Wireless stacks Logging systems Calculations Task RAM sum stacks CPU load Tick period Deadline margin Design rules Priorities by deadline Measure stack Use right primitive Keep mutex short Practical checks High water marks ISR API rules Queue depth Priority inversion Common mistakes Tiny stacks Wrong priorities Blocking with mutex ISR unsafe calls