When to activate
- Designing task architecture for RTOS-based firmware
- Choosing IPC mechanisms (queues, semaphores, events)
- Optimizing task priorities and stack sizes
- Debugging priority inversion, deadlocks, or stack overflows
- Migrating from bare-metal to RTOS
When NOT to use
- For bare-metal super-loop firmware (no RTOS)
- For Linux-based embedded systems
- For high-level application architecture
Instructions
- Task decomposition. One task per responsibility. Sensor task, network task, UI task. Minimize task count (each = RAM overhead).
- Priority assignment. Rate Monotonic: shorter period = higher priority. Critical tasks above all. Idle task for power saving.
- IPC selection. Queues (producer/consumer), Semaphores (resource counting), Mutexes (shared resource), Events (flag signaling).
- Stack sizing. Measure with watermark pattern (fill stack with known value, measure unused). Add 25% margin.
- Avoid pitfalls. No dynamic allocation after init. No printf in ISR. Short ISRs (set flag, process in task). Priority inheritance for mutexes.
- Power management. Tickless idle.
vTaskSuspend unused tasks. configUSE_TICKLESS_IDLE = 1.
- Debugging. Stack overflow detection. Task runtime monitoring. Idle task hook for CPU usage calculation.
Example
RTOS Task Design: Sensor Gateway
Tasks (by priority):
1. RadioTask (pri=5, stack=2KB, period=event-driven) — BLE receive/send
2. SensorTask (pri=4, stack=1KB, period=100ms) — BME280 read + filter
3. DataTask (pri=3, stack=1.5KB, period=event-driven) — aggregate + store
4. CloudTask (pri=2, stack=4KB, period=event-driven) — MQTT publish
5. UITask (pri=1, stack=2KB, period=500ms) — LED + button handling
6. IdleTask (pri=0) — WFI for power saving
IPC:
SensorTask → DataTask: xQueue (sensor readings)
RadioTask → DataTask: xQueue (commands from phone)
DataTask → CloudTask: xQueue (batched data)
All → UITask: xEventGroup (status flags)
Total RAM: 10.5KB tasks + 2KB system = 12.5KB (MCU has 64KB)
1---2name: rtos-patterns3description: RTOS task design — FreeRTOS/Zephyr scheduling, IPC mechanisms, resource management, and real-time guarantees4allowed-tools: [Read, Write, Grep]5effort: high6metadata:7 source: https://skillmd.com/skills/uitbreidenos/rtos-patterns8---910## When to activate1112- Designing task architecture for RTOS-based firmware13- Choosing IPC mechanisms (queues, semaphores, events)14- Optimizing task priorities and stack sizes15- Debugging priority inversion, deadlocks, or stack overflows16- Migrating from bare-metal to RTOS1718## When NOT to use1920- For bare-metal super-loop firmware (no RTOS)21- For Linux-based embedded systems22- For high-level application architecture2324## Instructions25261. **Task decomposition.** One task per responsibility. Sensor task, network task, UI task. Minimize task count (each = RAM overhead).272. **Priority assignment.** Rate Monotonic: shorter period = higher priority. Critical tasks above all. Idle task for power saving.283. **IPC selection.** Queues (producer/consumer), Semaphores (resource counting), Mutexes (shared resource), Events (flag signaling).294. **Stack sizing.** Measure with watermark pattern (fill stack with known value, measure unused). Add 25% margin.305. **Avoid pitfalls.** No dynamic allocation after init. No printf in ISR. Short ISRs (set flag, process in task). Priority inheritance for mutexes.316. **Power management.** Tickless idle. `vTaskSuspend` unused tasks. `configUSE_TICKLESS_IDLE = 1`.327. **Debugging.** Stack overflow detection. Task runtime monitoring. Idle task hook for CPU usage calculation.3334## Example3536```37RTOS Task Design: Sensor Gateway3839Tasks (by priority):40 1. RadioTask (pri=5, stack=2KB, period=event-driven) — BLE receive/send41 2. SensorTask (pri=4, stack=1KB, period=100ms) — BME280 read + filter42 3. DataTask (pri=3, stack=1.5KB, period=event-driven) — aggregate + store43 4. CloudTask (pri=2, stack=4KB, period=event-driven) — MQTT publish44 5. UITask (pri=1, stack=2KB, period=500ms) — LED + button handling45 6. IdleTask (pri=0) — WFI for power saving4647IPC:48 SensorTask → DataTask: xQueue (sensor readings)49 RadioTask → DataTask: xQueue (commands from phone)50 DataTask → CloudTask: xQueue (batched data)51 All → UITask: xEventGroup (status flags)5253Total RAM: 10.5KB tasks + 2KB system = 12.5KB (MCU has 64KB)54```