Practical FreeRTOS Task & Queue Design
January 5, 20249 min read
Tasks are not threads you spawn freely
On a microcontroller, every task costs a stack. Design a small, fixed set of tasks with clear responsibilities: an acquisition task per sensor class, a processing task, a comms task. Avoid spawning tasks dynamically in steady state.
Priorities and preemption
Assign priorities by deadline, not importance. The shortest-deadline work (e.g. servicing a radio interrupt's deferred handler) gets the highest priority. Keep ISRs tiny — defer real work to a task via a queue or task notification.
IPC and avoiding priority inversion
Use queues for data and notifications for signaling. When sharing a resource behind a mutex, enable priority inheritance so a low-priority holder is temporarily boosted, preventing a medium-priority task from starving a high-priority waiter.