FreeRTOS TUTORIAL #4 -> Using Counting Semaphore
This is 4th tutorial in the series of FreeRTOS, and today we are going to see How to use Counting semaphore in FreeRTOS. You can browse other tutorial related to FreeRTOS HERE.
From now onward, I am not going to use the CMSIS API anymore, and instead I will use the FreeRTOS functions directly. This will help you understand the process more clearly, and you can use the same functions across different microcontrollers, that supports FreeRTOS.
Counting semaphore can be used to control the access to the resource. To obtain control of a resource, a task must first obtain a semaphore. Thus decrementing the semaphore count value. When the count value reaches zero there are no free resources. When a task finishes with the resource it ‘gives’ the semaphore back and thus incrementing the semaphore count value.
There is nothing much to do in the set up part. Just enable the FreeRTOS, and enable the counting Semaphore.
As I mentioned above, I am not going to use the CMSIS anymore, that’s why I have commented it out in the include code below. Also we have to manually include all the FreeRTOS related files. I am also including stdlib.h, and string.h, and you will see their functions as we progress forward.
Next we need to create handlers for the Tasks, and the semaphore.
I have defined 4 handlers for 4 different Tasks, and CountingSem is the handler for counting semaphore.
- Inside the main function, first of all we need to create the counting semaphore.
- xSemaphoreCreateCounting takes 2 parameters, First is the maximum number of count, second is the initial value. I have created a semaphore with 3 tokens, and initial number of tokens available will be 0.
- If there is some error, and Semaphore couldn’t create, it will return NULL, or else it will return some other value.
After creating the Semaphore, we have to create the Tasks. As you can see above, FOUR Tasks have been created with different priorities, 3 being Highest. Following are the arguments to the xTaskCreate
- Here HPT_TASK is the function, where the task code is written
- “HPT” is just a name of this task. This name is not used anywhere in the program.
- 128 is the stack size
- NULL indicates that I am not passing any argument to the Task
- 3 is the Priority of the Task
- &HPThandler is the handler of the Task
At the end, Start the Scheduler with vTaskStartScheduler()
- As the control enters the HPT, 3 tokens of the counting semaphore will be released
- The task will acquire the Semaphore, and the token available will decrease by 1
- After completing the execution, the task will go in the suspension, without releasing the Semaphore
We have to write the similar functions for the MPT, LPT, and VLPT Tasks. You can check them after downloading the code at the end.
I have also implemented UART to receive the data from the serial console. On receiving character ‘r’ the following will take place:-
- First, we must define a xHigherPriorityTaskWoken and initialize it to the pdFALSE
- Than, give the semaphore using xSemaphoreGiveFromISR. This takes the xHigherPriorityTaskWoken as the parameter
- If a Higher Priority Task has preempted the Low Priority Task, from which we entered the ISR, than a context switch should be performed, and the interrupt safe API function will set *pxHigherPriorityTaskWoken to pdTRUE
- portEND_SWITCHING_ISR will perform the context switching and the control will go to the High Priority Task.
- As the control enters HPT, three semaphores were released and therefore there are 3 tokens available in the beginning. HPT Task takes the semaphore, accesses the resource, and go into the suspension, without releasing the semaphore.
- The control enters the MPT Task now. There are 2 tokens available now. MPT Task takes the semaphore, accesses the resource, and go into the suspension, without releasing the semaphore.
- The control enters the LPT Task. There is only 1 token available. LPT Task takes the semaphore, accesses the resource, and go into the suspension, without releasing the semaphore.
- The VLPT runs and try to acquire the semaphore. There is no token available, so it waits for the Semaphore to become available. The waiting time is forever.
- LPT comes out from the suspension, and preempts the VLPT, and waits for the semaphore.
- When MPT comes out from the suspension, it will preempt the LPT
- HPT wakes up, and preempts MPT, and waits for the semaphore to become available.
- When the character ‘r’ is received, 3 tokens will be released from the ISR. HPT have the Highest priority among the waiting Tasks and therefore it will get the semaphore first
- MPT will acquire the semaphore next, run and go back in the suspension
- LPT will get the semaphore, and it will also go in the suspension
- At this point, VLPT is still waiting for the semaphore. LPT will wake up, as it has least delay, and it will try to take the semaphore. Token available is 0, so it have to wait forever for the semaphore
- MPT will wake up and it will preempt the LPT, and waits for the semaphore
- HPT will preempt the MPT, and now all three Tasks along with VLPT are waiting for the semaphore to be released.
- STM32 Ethernet #10 HTTP Server (Basic) using LWIP NETCONN (RTOS) May 18, 2022
- STM32 Ethernet #9 TCP Server and Client using LWIP NETCONN (RTOS) April 26, 2022
- STM32 Ethernet #8 UDP Client using LWIP NETCONN (RTOS) April 16, 2022
- STM32 Ethernet #7 UDP Server using LWIP NETCONN (RTOS) April 9, 2022
- GPS (Neo 6M) with STM32 March 9, 2022
- BME280 with STM32 January 8, 2022