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.
- 1 Suggest To Read
- 2 FreeRTOS Binary Semaphore from ISR in LPC2148
- 3 Introduction
- 4 API Used
- 5 Code
- 6 Output
Suggest To Read
- RTOS Basics – Part 1
- RTOS Basics – PART 2
- FreeRTOS Porting for LPC2148
- LPC2148 UART Tutorial
- LPC2148 Binary Semaphore
FreeRTOS Binary Semaphore from ISR 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 ()
- xSemaphoreTakeFromISR ()
The first three APIs we have seen already in our previous tutorials. So we will concentrate on Semaphore ISR 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.
If you are going to take semaphore from ISR please use below function.
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.
- 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.
- pdPASS :The semaphore was successfully taken (acquired).
- pdFAIL :The semaphore was not successfully taken because it was not available.
A version of xSemaphoreGive() that can be used in an ISR. Unlike xSemaphoreGive(), xSemaphoreGiveFromISR() does not permit a block time to be specified.
BaseType_t xSemaphoreGiveFromISR( SemaphoreHandle_t xSemaphore,
BaseType_t *pxHigherPriorityTaskWoken );
- xSemaphore : The semaphore being ‘given’. A semaphore is referenced by a variable of type SemaphoreHandle_t and must be explicitly created before
- *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.
- 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.
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.
void sendserial(unsigned char c);
void sendsserial(unsigned char *s);
void recsemaphore(void *q);
void recsemaphore(void *q)
sendsserial("Received Semaphore From ISR");
U0IER=0x01; //enables uart receive identification interrupt
VICIntSelect=0x0000; //selecting as irq
VICIntEnable|=0x0040; //enable uart1
VICVectAddr2=(unsigned long int)rx1; //assigning address
VICVectCntl2=0x26; //need to assign slot no of interrupt source which has been activated
void sendserial(unsigned char dat)
void sendsserial(unsigned char *sdat)
unsigned char r;
static portBASE_TYPE interrupttask;
sendsserial("Received Data : ");
sendsserial("Going to give semaphore");
sendsserial("Semaphore has given");
VICVectAddr = 0x00;