FreeRTOS – LPC2148 Binary Semaphore

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

  • binary,
  • counting, and
  • mutual‐exclusion (mutex) semaphores.

Binary Semaphore

  • 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?

Counting Semaphores

  • 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

  • ownership,
  • 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.

API Used

  1. xTaskCreate ()
  2. vTaskStartScheduler ()
  3. vTaskDelay ()
  4. vSemaphoreCreateBinary ()
  5. xSemaphoreTake ()
  6. xSemaphoreTakeFromISR ()
  7. xSemaphoreGive ()
  8. xSemaphoreGiveFromISR()

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.

vSemaphoreCreateBinary ()

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.

Return Value:


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.

xSemaphoreTake ()

‘Takes’ (or obtains) a semaphore that has previously been created using a call to vSemaphoreCreateBinary().

#include “FreeRTOS.h”
#include “semphr.h”

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.

Return Value:

  • 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

xSemaphoreGive ()

‘Gives’ (or releases) a semaphore that has previously been created using a call to vSemaphoreCreateBinary(), xSemaphoreCreateCounting() or xSemaphoreCreateMutex() – and has also been successfully ‘taken’.

#include “FreeRTOS.h”
#include “semphr.h”

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.

Return Value:

  • 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).


Without Semaphore:

Here prints are improper.

With Semaphore:

After adding semaphore, now its coming properly.

[Download This Project]

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!