FreeRTOS is a type of real-time operating system. It is similar to all RTOS, having a serious problem with inter-task communication and resource sharing.
In RTOS, inter-task communication is not like passing variables between normal functions. Normal functions can pass the data to a global variable or pass it to another function by returning the value.
If the global variable is being used by RTOS, the data might be written by higher priority task before the lower priority task read it. This will corrupt the data.
The hardware resources have the same problem. For example, consider a task sending a string to a PC through the UART port. However, it has been interrupted by another higher priority task that also sends data through the same UART port. At the end, the data will appear between the string. Therefore, inter-task communication is important for the coordination of tasks.
There are few solution dealing with inter-task communication.
- Queue – It is a temporary storage for the data that need to transfer between tasks. The length of the queue is depend on the rate of push and pull of the queue.
- Semaphore – It can be divided into two types. Binary semaphore and counting semaphore.
- Binary semaphore – It is similar to a queue that contains only a binary data. It functions like a signal that indicates that the data is ready. It is useful to synchronize data between tasks or interrupts.
- Counting semaphore – It is similar to binary semaphore, but the length of queue is more than one. It is useful in counting events. For example, it can be used for counting the available resources (eg. memory) or the data that need to be processed by another task.
- Mutex (Mutual Exclusion) – It is similar to a binary semaphore. However, mutex supports priority inheritance mechanism. Therefore, mutex is able to keep a low priority task from using the resource while another high priority task attempts to access the same resource. The mutex is very useful in resource sharing.
The solutions above is used to solve the data transfer problem between RTOS tasks. They might create other problem like priority inversion. Therefore, special care must be taken to use these solution in RTOS.