0% found this document useful (0 votes)
31 views12 pages

Topic10 RTOS-BinarySemaphores Mco556

This document discusses binary semaphores in FreeRTOS and their use for task synchronization. It explains that a binary semaphore allows a producer task or interrupt service routine to signal a consumer task. The consumer task will block until the semaphore is given, then resume execution. This can be used to defer interrupt processing from an ISR to a higher priority task. The lab objectives are to learn how to implement deferred processing using a software timer ISR and binary semaphore.

Uploaded by

Dark Matter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views12 pages

Topic10 RTOS-BinarySemaphores Mco556

This document discusses binary semaphores in FreeRTOS and their use for task synchronization. It explains that a binary semaphore allows a producer task or interrupt service routine to signal a consumer task. The consumer task will block until the semaphore is given, then resume execution. This can be used to defer interrupt processing from an ISR to a higher priority task. The lab objectives are to learn how to implement deferred processing using a software timer ISR and binary semaphore.

Uploaded by

Dark Matter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Seneca College – School of Electronics and Mechanical Engineering Technology (SEMET)

MCO556 – ARM® Microcontroller for Real Time


Embedded Applications

Topic 10 – Binary Semaphores

www.freertos.org

Author and Presenter: Benjamin Shefler


Copyright © 2020 by SEMET
Agenda:

• Additional RTOS task synchronization methods:


Counting Semaphores
Binary Semaphores
• Use of software timer ISR + binary semaphores for deferred processing
• Overview of the lab on semaphores

2
FreeRTOS Task Synchronization Methods

• Application tasks can be precisely synchronized by using a number of appropriate


task synchronization methods available in FreeRTOS.
• The synchronization methods like Mutexes, Queues, Event Groups, and Software
timers were introduced in the previous lecture.
• Another available task synchronization method is called Semaphore.
• A semaphore is used to notify another task about an event(s) and thus, the task
waiting for the event is able to run.
• There are two types of semaphores – Counting Semaphores and Binary
Semaphores.

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

A binary semaphore may be used in place of a mutex to arbitrate access to a


shared resource, however, there is a subtle but important difference:
• When using a mutex, a corresponding pair of “take” and “give” operations must
be called from within the same thread, which allows the kernel to know which
thread owns the mutex at any instant. Semaphores have no such notion of
ownership
• When using a binary semaphore, one task can “give” a semaphore and the other
task(s) can “take” the semaphore (producer → consumer) (as shown on the
previous slide). If the consumer task has the right priority, then the consumer
task will run right after the producer task and so, the two tasks will be contiguous

8
Software Timer ISR + Binary Semaphore → Deferred Processing

• The software timer produces interrupts after specified time intervals.


• The timer callback function (or ISR) should exit as quickly as it is practical.
• If the interrupt related processing is time consuming, it can be deferred from the
timer ISR to a task, which will do (or continue) the processing.
• In order to run the deferred task immediately after the ISR, the deferred task
must be assigned a higher priority over other tasks in the application, or the ISR
can use a binary semaphore to unblock the deferred task immediately after the
timer ISR.
• In this case, the processing of the ISR related things would appear just as if the
processing had been performed in the ISR itself.
• Keep in mind that the time spent on the ISR and deferred processing together
must be shorter then the programmed time interval, otherwise a new timer
interrupt will be asserted, but the deferred task has not finished its processing.

9
Programming Binary Semaphores for Task Deferring
Declare a semaphore in Definitions:
SemaphoreHandle_t xSemaphore_Task1;/*reference to semaphore*/

Create the semaphore in main:


xSemaphore_Task1 = xSemaphoreCreateBinary();

Give the semaphore to Task1 in software timer ISR (producer):


xSemaphoreGiveFromISR(xSemaphore_Task1, &xHigherPriorityTaskWoken);

Take the semaphore in Task1 (consumer):


xSemaphoreTake(xSemaphore_Task1, portMAX_DELAY); /*this line will
unblock Task1 and the task will start running*/

10
Lecture Summary

• Software Timers are used to execute functions at scheduled, fixed frequencies,


and are usually used with binary semaphores and queues.
• A Binary Semaphore allows a task to wait in the Blocked state for a single event to
occur.
• The Binary Semaphore unblocks a single task when the event occurs—the task
that is unblocked is the highest priority task that was waiting for the event.
• The Binary Semaphore can be used by an ISR to unblock an appointed task each
time a particular interrupt occurs, effectively synchronizing the task with the
interrupt.
• Note that it is possible to replace many binary semaphores with a single Event
Group.

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

You might also like