FreeRTOS – LPC2148 Binary Semaphore in ISR

In our previous tutorials we have seen How to use simple Binary Semaphore. Today we will learn the FreeRTOS Binary Semaphore from ISR in LPC2148. Lets start.

Suggest To Read

FreeRTOS Binary Semaphore from ISR in LPC2148

Introduction

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.

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

Mutexes

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.

Notes

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. xSemaphoreTakeFromISR ()
  6. xSemaphoreGiveFromISR()

The first three APIs we have seen already in our previous tutorials. So we will concentrate on Semaphore ISR 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 );

Parameters:

  • xSemaphore : Variable of type SemaphoreHandle_t that will store the handle of the semaphore being created.

Return Value:

None.

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.

xSemaphoreTakeFromISR ()

If you are going to take semaphore from ISR please use below function.

#include “FreeRTOS.h”
#include “queue.h”

BaseType_t xSemaphoreTakeFromISR( SemaphoreHandle_t xSemaphore,

                                signed BaseType_t *pxHigherPriorityTaskWoken );

A version of xSemaphoreTake() that can be called from an ISR. Unlike xSemaphoreTake(),
xSemaphoreTakeFromISR() does not permit a block time to be specified.

Parameters:

  • xSemaphore : The semaphore being ‘taken’. A semaphore is referenced by a variable of type SemaphoreHandle_t and must be explicitly created before being used.
  • pxHigherPriorityTaskWoken : It is possible (although unlikely, and dependent on the semaphore type) that a semaphore will have one or more tasks blocked on it waiting to give the semaphore. Calling xSemaphoreTakeFromISR() will make a task that was blocked waiting to give the semaphore leave the Blocked state. If calling the API function causes a task to leave the Blocked state, and the unblocked task has a priority equal to or higher than the currently executing task (the task that was interrupted), then, internally, the API function will set *pxHigherPriorityTaskWoken to pdTRUE. If xSemaphoreTakeFromISR() sets *pxHigherPriorityTaskWoken to pdTRUE, then a context switch should be performed before the interrupt is exited. This will ensure that the interrupt returns directly to the highest priority Ready state task.

Return Value:

  • pdPASS :The semaphore was successfully taken (acquired).
  • pdFAIL :The semaphore was not successfully taken because it was not available.

xSemaphoreGiveFromISR()

A version of xSemaphoreGive() that can be used in an ISR. Unlike xSemaphoreGive(), xSemaphoreGiveFromISR() does not permit a block time to be specified.

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

BaseType_t xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore,
                       BaseType_t *pxHigherPriorityTaskWoken );

Parameters:

  • xSemaphore : The semaphore being ‘given’. A semaphore is referenced by a variable of type SemaphoreHandle_t and must be explicitly created before
    being used.
  • *pxHigherPriorityTaskWoken : It is possible that a single semaphore will have one or more tasks blocked on it waiting for the semaphore to become available. Calling xSemaphoreGiveFromISR() can make the semaphore available, and so cause such a task to leave the Blocked state. If calling xSemaphoreGiveFromISR() causes a task to leave the Blocked state, and the unblocked task has a priority higher than or equal to the currently executing task (the task that was interrupted), then, internally, xSemaphoreGiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE. If xSemaphoreGiveFromISR() sets this value to pdTRUE, then a context switch should be performed before the interrupt is exited. This will ensure that the interrupt returns directly to the highest priority Ready state task.

Return Value:

  • pdTRUE : The call to xSemaphoreGiveFromISR() was successful.
  • errQUEUE_FULL : If a semaphore is already available, it cannot be given, and xSemaphoreGiveFromISR() will return errQUEUE_FULL.

Code

Here i’ve created one task. Whenever UART receives data from user, it gives semaphore to task. That task waits until it gets semaphore. If it gets semaphore from ISR then it will prints in UART.  You can download the full project here.

Output

lpc2148-freertos-semaphore-from-isr FreeRTOS - LPC2148 Binary Semaphore in ISR

[Download This Project]

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

Click Here to Download App!