In our previous tutorials we have seen How to create the task using FreeRTOS API. Today we will see the FreeRTOS Binary Semaphore Tutorial in LPC2148. Lets start.
Suggest To Read
FreeRTOS Binary Semaphore Tutorial in LPC2148
Before we starting this tutorial we should know about the semaphore. If you go through the RTOS Basics two parts you can go ahead.For others I will give you the small introduction about semaphore.
A semaphore (sometimes called a semaphore token) is a kernel object that one or more threads of execution can acquire or release for the purposes of synchronization or mutual exclusion. This is useful for when two or more task accessing same resource. A binary semaphore used for synchronization does not need to be ‘given’ back after it has been successfully ‘taken’ (obtained).
A kernel can support many different types of semaphores, including
- counting, and
- mutual‐exclusion (mutex) semaphores.
- Similar to mutex
- Can have a value 1 or 0
- Whenever a task asks for semaphore, the OS checks if the semaphore’s value is 1
- If so, the call succeeds and the value is set to 0
- Else, the task is blocked
Binary semaphores are treated as global resources,
- They are shared among all tasks that need them.
- Making the semaphore a global resource allows any task to release it, even if the task did not initially acquire it?
- Semaphores with an initial value greater than 1
- can give multiple tasks simultaneous access to a shared resource, unlike a mutex
- Priority inheritance, therefore, cannot be implemented
Are powerful tools for synchronizing access to shared resources. A mutual exclusion (mutex) semaphore is a special binary semaphore that supports
- recursive access,
- task deletion safety, and
- one or more protocols for avoiding problems inherent to mutual exclusion.
Binary semaphores and mutexes are very similar, but do have some subtle differences. Mutexes include a priority inheritance mechanism, binary semaphores do not. This makes binary semaphores the better choice for implementing synchronization (between tasks or between tasks and an interrupt), and mutexes the better choice for implementing simple mutual exclusion.
- xTaskCreate ()
- vTaskStartScheduler ()
- vTaskDelay ()
- vSemaphoreCreateBinary ()
- xSemaphoreTake ()
- xSemaphoreTakeFromISR ()
- xSemaphoreGive ()
The first three APIs we have seen already in our previous tutorials. And Semaphore in ISR APIs we will in next tutorials. So we will concentrate on Semaphore APIs.
NOTE: The vSemaphoreCreateBinary() macro remains in the source code to ensure backward compatibility, but it should not be used in new designs. Use the xSemaphoreCreateBinary() function instead.
A macro that creates a binary semaphore. A semaphore must be explicitly created before it
can be used.
void vSemaphoreCreateBinary( SemaphoreHandle_t xSemaphore );
- xSemaphore : Variable of type SemaphoreHandle_t that will store the handle of the semaphore being created.
If, following a call to vSemaphoreCreateBinary(), xSemaphore is equal to NULL, then the semaphore cannot be created because there is insufficient heap memory available for FreeRTOS to allocate the semaphore data structures. In all other cases, xSemaphore will hold the handle of the created semaphore.
‘Takes’ (or obtains) a semaphore that has previously been created using a call to vSemaphoreCreateBinary().
BaseType_t xSemaphoreTake( SemaphoreHandle_t xSemaphore,
TickType_t xTicksToWait );
- xSemaphore : The semaphore being ‘taken’. A semaphore is referenced by a variable of type SemaphoreHandle_t and must be explicitly created before being used.
- xTicksToWait : The maximum amount of time the task should remain in the Blocked state to wait for the semaphore to become available, if the semaphore is not available immediately. If xTicksToWait is zero, then xSemaphoreTake() will return immediately if the semaphore is not available.
- pdPASS : Returned only if the call to xSemaphoreTake() was successful in obtaining the semaphore.
- pdFAIL : Returned if the call to xSemaphoreTake() did not successfully obtain the semaphore
‘Gives’ (or releases) a semaphore that has previously been created using a call to vSemaphoreCreateBinary(), xSemaphoreCreateCounting() or xSemaphoreCreateMutex() – and has also been successfully ‘taken’.
BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore );
- xSemaphore : The Semaphore being ‘given’. A semaphore is referenced by a variable of type SemaphoreHandle_t and must be explicitly created before being used.
- pdPASS :The semaphore give was successful.
- pdFAIL :The semaphore give was not successful.
Here i’m creating two tasks with same priority. These two tasks will contentiously prints data in serial terminal. Here Common resource is UART. In output section I showed difference with semaphore and without semaphore. Go through the Code. Here i’m adding only the main file. If you want to download the full project, please visit here (GitHub).
void task1(void *q);
void task2(void *a);
void task1(void *q)
void task2(void *a)
Here prints are improper.
After adding semaphore, now its coming properly.