RTOS
An RTOS is a lightweight operating system designed for embedded systems where timing is critical. It provides:
- Task Management: Scheduling multiple tasks (threads) with priorities.
- Timing Guarantees: Predictable response times (real-time behavior).
- Inter-Task Communication: Tools like semaphores, queues, and mutexes.
Examples: FreeRTOS, Zephyr, RTEMS.
RTOS vs. No RTOS (Bare-Metal裸机): Key Differences
- With RTOS:
- How It Runs:
Your code is split into tasks (like threads in Java). The RTOS scheduler decides which task runs when, based on priorities and timing needs.
- Compilation:
You write in C/C++ (typically), compile to machine code, and link with the RTOS library. The resulting binary runs on the microcontroller (MCU) hardware, with the RTOS managing execution.
- Analogy:
Think of it like a JVM or OS threading model—your Java threads rely on the JVM scheduler, but here, the RTOS schedules directly on the hardware.
- Practical Example
A drone uses FreeRTOS—Task 1 reads IMU every 10 ms, Task 2 adjusts motors, Task 3 sends telemetry. All run “simultaneously.”
- Without RTOS (Bare-Metal):
- How It Runs:
- Startup:
MCU resets, jumps to a vector table (defined in your code), then runs main().
No OS bootloader—just your binary burned into flash.
-
Execution:
Your code runs directly on the MCU with no scheduler or OS layer. It’s a single, continuous program (often a “super loop” or interrupt-driven).
Super Loop: A while(1) loop handles all tasks sequentially.
Pros: Simple, predictable for small systems.
Cons: Blocking calls (e.g., delay(1000)) stall everything.
Interrupts: Hardware interrupts (e.g., timers, UART) break the loop for urgent events.
Pros: Fast response to hardware.
Cons: You manage priority and nesting manually.
- Compilation:
You write in C/C++, compile to machine code, and it runs from reset without any intermediary. You control everything.
- Analogy:
Imagine writing a single-threaded Java program with no JVM—just raw machine code executing sequentially on the CPU.
With Java, the JVM abstracts hardware and schedules bytecode. Without an RTOS, there’s no abstraction—you write machine code (via C) that directly manipulates registers and memory. No “virtual machine” or OS to lean on.
- Practical Example
A thermostat polls temperature every second and toggles a relay in a loop—simple, no RTOS needed.
FreeRTOS
Free Real-Time Operating System is a lightweight OS for microcontrollers