127 views|1 replies
chenbingjy
Currently offline
|
The OP
Published on 2024-11-12 13:14
Only look at the author
This post is from Real-time operating system RTOS
Latest reply
I am just nature's porter.
1. **Introduction to binary semaphores**
- In FreeRTOS, binary semaphores are a mechanism for task synchronization and mutual exclusion. It has only two states: full (acquired) and empty (available). Binary semaphores can be used to protect shared resources or for synchronization between tasks.
2. **Steps to enable binary semaphores**
- **Create a binary semaphore**
- Use the `xSemaphoreCreateBinary()` function to create a binary semaphore. This function returns a handle of type `SemaphoreHandle_t`. For example:
```c
SemaphoreHandle_t binarySemaphore;
binarySemaphore = xSemaphoreCreateBinary();
if(binarySemaphore == NULL)
{
// Handle creation failure, which may be due to insufficient memory, etc.
// You can add error handling code here, such as printing error information, etc.
}
```
- **Give (release) semaphore**
- After a binary semaphore is created, it is not acquired by default (empty state). You can use the `xSemaphoreGive()` function to release a semaphore and make it full. For example, to release a semaphore in a task:
```c
void vTaskFunction(void *pvParameters)
{
// Other task code
// Release semaphoreif
(xSemaphoreGive(binarySemaphore)== pdTRUE)
{
// Processing of successful semaphore release
}
else
{
// Processing of failed semaphore release, which may be due to invalid semaphore, etc.
}
// Other code for the task
}
```
- **Get semaphore**
- Another task or interrupt service function (if the conditions are met) can use the `xSemaphoreTake()` function to get the semaphore. When the semaphore is full, the `xSemaphoreTake()` function will change the semaphore to empty state and allow the task to continue execution. For example:
```c
void anotherTaskFunction(void *pvParameters)
{
if(xSemaphoreTake(binarySemaphore, portMAX_DELAY)== pdTRUE)
{
// Successfully acquire the semaphore and execute the code segment protected by the semaphore
// For example, access shared resources, etc.
// After completion, the semaphore can be released again so that other tasks can acquire it
xSemaphoreGive(binarySemaphore);
}
else
{
// Handling failure to acquire the semaphore, which may be due to the semaphore being empty and waiting timeout, etc.
}
}
```
- **Notes**
- When using a semaphore in an interrupt service function, the `xSemaphoreGiveFromISR()` function should be used to release the semaphore instead of `xSemaphoreGive()`. Because functions that cause blocking cannot be called in an interrupt service function. The use of the `xSemaphoreGiveFromISR()` function is slightly more complicated, and it requires an additional parameter to indicate whether a task switch is required. For example:
```c
void vISRFunction(void)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(binarySemaphore, &xHigherPriorityTaskWoken);
if(xHigherPriorityTaskWoken == pdTRUE)
{
// Request task switching so that the high-priority task waiting for the semaphore can run as soon as possible
portYIELD_FROM_ISR();
}
}
```
- When using binary semaphores for task synchronization or mutual exclusion, ensure that the semaphores are initialized, released, and acquired correctly to avoid problems such as deadlock or resource contention. And the waiting time for semaphore acquisition should be set reasonably according to the specific application scenario, such as using `portMAX_DELAY` to indicate waiting until the semaphore is available, or using other shorter time values to achieve a limited waiting time.
Details
Published on 2024-11-13 09:36
| |
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
|
||
2
Published on 2024-11-13 09:36
Only look at the author
This post is from Real-time operating system RTOS
| ||
|
||
|
EEWorld Datasheet Technical Support
EEWorld
subscription
account
EEWorld
service
account
Automotive
development
circle
About Us Customer Service Contact Information Datasheet Sitemap LatestNews
Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190