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 guarantees4---56## When to activate78- Designing task architecture for RTOS-based firmware9- Choosing IPC mechanisms (queues, semaphores, events)10- Optimizing task priorities and stack sizes11- Debugging priority inversion, deadlocks, or stack overflows12- Migrating from bare-metal to RTOS1314## When NOT to use1516- For bare-metal super-loop firmware (no RTOS)17- For Linux-based embedded systems18- For high-level application architecture1920## Instructions21221. **Task decomposition.** One task per responsibility. Sensor task, network task, UI task. Minimize task count (each = RAM overhead).232. **Priority assignment.** Rate Monotonic: shorter period = higher priority. Critical tasks above all. Idle task for power saving.243. **IPC selection.** Queues (producer/consumer), Semaphores (resource counting), Mutexes (shared resource), Events (flag signaling).254. **Stack sizing.** Measure with watermark pattern (fill stack with known value, measure unused). Add 25% margin.265. **Avoid pitfalls.** No dynamic allocation after init. No printf in ISR. Short ISRs (set flag, process in task). Priority inheritance for mutexes.276. **Power management.** Tickless idle. `vTaskSuspend` unused tasks. `configUSE_TICKLESS_IDLE = 1`.287. **Debugging.** Stack overflow detection. Task runtime monitoring. Idle task hook for CPU usage calculation.2930## Example3132```33RTOS Task Design: Sensor Gateway3435Tasks (by priority):36 1. RadioTask (pri=5, stack=2KB, period=event-driven) — BLE receive/send37 2. SensorTask (pri=4, stack=1KB, period=100ms) — BME280 read + filter38 3. DataTask (pri=3, stack=1.5KB, period=event-driven) — aggregate + store39 4. CloudTask (pri=2, stack=4KB, period=event-driven) — MQTT publish40 5. UITask (pri=1, stack=2KB, period=500ms) — LED + button handling41 6. IdleTask (pri=0) — WFI for power saving4243IPC:44 SensorTask → DataTask: xQueue (sensor readings)45 RadioTask → DataTask: xQueue (commands from phone)46 DataTask → CloudTask: xQueue (batched data)47 All → UITask: xEventGroup (status flags)4849Total RAM: 10.5KB tasks + 2KB system = 12.5KB (MCU has 64KB)50```