FREERTOS: help with resolution for Binary-Semaphore initial state bug
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-16 6:54 AM
Hello Experts,
i am facing a similar issue on the STM32F407G discovery board. Is there any solution or a temporary fix for this iisue?
Changing the initial state of the semaphore to Depleted does nothing in the semaphore definition and creation:
In freertos.c, inside the function MX_FREERTOS_Init(). i think the second function parameter should be zero
I am trying a simple GPIO interrupt to task signaling, but the task always takes a semaphore without the GPIO interrupt the first time. I have attached the complete freertos.c file for context. This is the console screenshot to highlight the bug:
I tried to manually change:
myBinarySem01Handle = osSemaphoreCreate(osSemaphore(myBinarySem01), 1);
to
myBinarySem01Handle = osSemaphoreCreate(osSemaphore(myBinarySem01), 0);
Now the task does not take the semaphore during start up, but the moment the GPIO interrupt occurs(push-button), the whole system hangs in an asset failure:
Is there any solution to this problem? I have attached my freertos.c file as well.
Solved! Go to Solution.
- Labels:
-
STM32F4 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-16 1:16 PM - edited ‎2025-05-16 1:42 PM
Is config_USE_COUNTING_SEMAPHORES defined as 1 ?
Maybe, change osSemaphoreCreate like following:
...........
if (count == 1) {
return xSemaphoreCreateBinaryStatic( semaphore_def->controlblock );
}
else if (count == 0) {
sema = xSemaphoreCreateBinaryStatic( semaphore_def->controlblock );
if (!sema) return sema;
osSemaphoreWait(sema, 0); //immediately take it
return sema;
}
else {
..........
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-16 9:00 AM
If the 2nd parameter is 0, osSemaphoreCreate likely returned NULL handle (myBinarySem01Handle ), this is why you assert in xQueueGiveFromISR. Please see the source in cmsis_os.c and you will understand.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-16 12:40 PM
Yes, thanks for pointing it out. Looking at osSemaphoreCreate function i realized it was returning NULL.
This brings up the question how do we change the semaphores initial state to depleted/empty?
I also tried to use the native FreeRTOS APIs (xSemaphoreCreateBinary) which creates the semaphore in an 'empty' state, this worked for me, so i am pretty confident there is some issue in the cmsis implementation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-16 1:16 PM - edited ‎2025-05-16 1:42 PM
Is config_USE_COUNTING_SEMAPHORES defined as 1 ?
Maybe, change osSemaphoreCreate like following:
...........
if (count == 1) {
return xSemaphoreCreateBinaryStatic( semaphore_def->controlblock );
}
else if (count == 0) {
sema = xSemaphoreCreateBinaryStatic( semaphore_def->controlblock );
if (!sema) return sema;
osSemaphoreWait(sema, 0); //immediately take it
return sema;
}
else {
..........
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-18 11:32 PM
Thanks , you solution worked. But i used it in a different way, I did not want to modify the HAL function as it overwrites every time there is a change in the .ioc file
So i did something like this:
osSemaphoreId bin_sem_id;
osSemaphoreDef(bin_sem);
bin_sem_id = osSemaphoreCreate(osSemaphore(bin_sem), 1);
if (bin_sem_id != NULL)
{
osSemaphoreWait(bin_sem_id, 0); // Take the semaphore immediately (no blocking)
// Now the semaphore has a count of 0 and is unavailable.
}
else
{
// Semaphore creation failed
}
