Topic10 RTOS-BinarySemaphores Mco556
Topic10 RTOS-BinarySemaphores Mco556
www.freertos.org
2
FreeRTOS Task Synchronization Methods
3
FreeRTOS Task Synchronization Methods Diagram
4
A Binary Semaphore as a Synchronization Primitive
• A semaphore is similar to a mutex, but is different in that a semaphore is typically
used to manage events rather than to arbitrate access to a shared resource.
• A semaphore provides a mechanism for one thread to signal another thread.
• The first thread is thought of as the “producer” and the second thread as the
“consumer”, although no actual data is produced.
• In many cases the producer is an ISR and the consumer is a thread that is waiting
for the interrupt to occur.
• When a binary semaphore is used, the consumer task remains in the Blocked
state until the producer “gives” the semaphore to the consumer. After that, the
consumer task is able to run, and when the task is done, it enters the Blocked
state again and waits until the producer “gives” a new semaphore.
• The best use of a binary semaphore is to synchronize a short ISR with the longer
interrupt deferred processing task. If the highest priority is assigned to the
consumer task, then the whole procedure of processing the interrupt related
things becomes contiguous.
5
Semaphore Diagram
6
A Counting Semaphore
• A Counting Semaphore is different from a binary semaphore in that the
counting semaphore contains a count of events and it provides the same
mechanism for one thread to signal another thread.
• Semaphores’ count is incremented by the producer and decremented by
the consumer.
• When the count is 0, the consumer is blocked until the producer performs
a post (or give) that increments the count. When the count is N, the
producer is blocked until the consumer performs a pend (or take) to
decrement the count.
• The semaphore has no knowledge of which resource is allocated, only how
many.
• A binary semaphore is a special case in which the count can only be 0 or 1
and is more commonly used than a “counting semaphore”.
7
Using Binary Semaphores vs Using Mutexes
8
Software Timer ISR + Binary Semaphore → Deferred Processing
9
Programming Binary Semaphores for Task Deferring
Declare a semaphore in Definitions:
SemaphoreHandle_t xSemaphore_Task1;/*reference to semaphore*/
10
Lecture Summary
11
Overview of the Lab on Binary Semaphores
Lab Objectives:
• Learn the operation of a simple RTOS-based example application, which utilizes
Software Timer Interrupts and a task synchronization method called BINARY
SEMAPHORE.
• Learn how to start processing in a short software timer call back function (ISR)
and continue that processing in another task (deferred processing).
• Develop multitasking applications by modifying the existing example where two
tasks and two semaphores, and hardware interrupts (by button SW2) are used.
12