Choose an Inter-Task Communication Method in FreeRTOS
Intro
As you might have you know already, the EFS team in WARG is heavily using FreeRTOS to accomplish the multi-tasking needs in our projects, including the ZeroPilot. When one is working on a task with one of the Managers in ZeroPilot for example, they might need to transfer the processed data from their manager to another manager, or they might need to receive streams of raw data from an ISR. This type of communication that happens between task-to-task and ISR-to-task is called inter-task communication or inter-thread communication outside of the FreeRTOS context. This page is going to introduce some inter-task com methods available to you such that it can guide you to find the appropriate method to use in your scenario.
Options
Option 1 - Stream Buffer
Allows a stream of bytes to be passed from
ISR → Task
Task → Task
The length of the bytes can be totally arbitrary
Contents are passed by copy
Only single-reader single-writer scenarios***
Allows transmission complete callback
Option 2 - Queue
Allows messages to be passed from
ISR → Task
Task → Task
They are thread-safe FIFO buffers, meaning multiple reader/write fine
Contents are passed by copy
Allows a block time to be specified. When a task attempts to rad from an empty queue, the task can be blocked until the buffers are filled or time-out
Length is fixed
Option 3 - Task Notification
It is an event that sent directly to a task. No indirect buffers(queue, semaphore) during transmission
It can be used as a lightweight binary/counting semaphore, or 32-bit value direct sender, which is faster and cost less RAM than normal inter-task com
This functionality is only available after a certain release
Other Options
Binary Semaphore, Counting Semaphore, and Mutex. But they are more focused on task synchronization rather than message exchange
Global variables, static variables, and static functions are shared between tasks, but they are vulnerable to a race condition
You can also allocate a piece of memory on the heap, which is also shared between tasks, and you can free it when you don’t need it anymore. Functions are
pvPortMalloc()
andvPortFree
. Also need multi-thread protection