# FreeRTOS: xSemaphoreGiveFromISR() not functional from ISR with STM32F746NG

**URL:** <https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704>\
**Category:** Kernel\
**Created:** [March 4, 2019, 4:58am UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704 "2019-03-04T04:58:22Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 4, 2019, 4:58am UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/1 "2019-03-04T04:58:22Z")

</div>

**[sadakamath](https://sourceforge.net/u/sadakamath/)** wrote on [Monday, March 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#1501):

Hi,  
I see that xSemaphoreGiveFromISR() not functional from ISR with STM32F746NG.

HAL\_GPIO\_TogglePin is working fine when it is called in ISR. But, xSemaphoreGiveFromISR is not working.  
Is there any STM32F746NG configuration is missing to make it work with STM32F746NG?

NOTE:

1. Target board & CPU: STM32F746G-DISCO, STM32F746NG
2. FreeRTOS project is created from STM32CubeMx
3. Used TIM3 for Timebase Source and TIM2 for 1sec interrupt generation using internal clock
4. Refer below section for definition of configKERNEL\_INTERRUPT\_PRIORITY and configMAX\_SYSCALL\_INTERRUPT\_PRIORITY

References:

**main()**

void Thread2(void const \* argument)  
{  
uint8\_t txData[20] = “Hello from Thread2\r\n”;  
while(1)  
{  
if(xSemaphoreTake(myBinarySem01Handle, portMAX\_DELAY)) {  
HAL\_GPIO\_TogglePin(GPIOI,GPIO\_PIN\_1);  
HAL\_UART\_Transmit(&huart1, txData, 20, 5);  
osDelay(100);  
}  
}  
}

**Timer Interrupt Callback**

void HAL\_TIM\_PeriodElapsedCallback(TIM\_HandleTypeDef _htim)  
{  
if (htim-\>Instance == TIM3) {  
HAL\_IncTick(); //TIM3 used for Timebase Source  
}  
else if(htim-\>Instance == TIM2) {  
/_ HAL\_GPIO\_TogglePin(GPIOI,GPIO\_PIN\_1);  
LED blink working fine when below SemaphoreGiveFromISR commented \*/

```
  xSemaphoreGiveFromISR(myBinarySem01Handle,NULL);

```

}  
}

define NVIC\_PRIO\_BITS 4  
define configPRIO\_BITS 4  
define configLIBRARY\_LOWEST\_INTERRUPT\_PRIORITY 15  
define configLIBRARY\_MAX\_SYSCALL\_INTERRUPT\_PRIORITY 5

define configKERNEL\_INTERRUPT\_PRIORITY ( configLIBRARY\_LOWEST\_INTERRUPT\_PRIORITY \<\< (8 - configPRIO\_BITS) )  
define configMAX\_SYSCALL\_INTERRUPT\_PRIORITY ( configLIBRARY\_MAX\_SYSCALL\_INTERRUPT\_PRIORITY \<\< (8 - configPRIO\_BITS) )

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 4, 2019, 5:18am UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/2 "2019-03-04T05:18:41Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, March 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#1501/722f):

What makes you think it is not functional?

Have you created the semaphore? If so, please show that code to so we  
can see if you are using the handle correctly.

You are not using the second parameter to xSemaphoreGiveFromISR(), so  
the context switch will not happen until the next tick interrupt.

Using a direct to task notification in place of the semaphore will be  
much faster and use less RAM.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 4, 2019, 1:55pm UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/3 "2019-03-04T13:55:54Z")

</div>

**[sadakamath](https://sourceforge.net/u/sadakamath/)** wrote on [Monday, March 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#6cb8):

Thanks Richard, See my answers below -

What makes you think it is not functional?  
Ans: LED blink is not happening when I used xSemaphoreGiveFromISR() in interrupt callback.

Have you created the semaphore?  
Ans: Yes, see below -

/\* definition and creation of myBinarySem01 \*/  
osSemaphoreDef(myBinarySem01);  
myBinarySem01Handle = osSemaphoreCreate(osSemaphore(myBinarySem01), 1);

I have removed second parameter to xSemaphoreGiveFromISR() today while debugging LED blink issue. Second parameter was there in my original code.

Let me know if any clue?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 4, 2019, 3:55pm UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/4 "2019-03-04T15:55:09Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, March 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#6cb8/b6b3):

> //definition and creation of myBinarySem01 //  
> osSemaphoreDef(myBinarySem01);  
> myBinarySem01Handle = osSemaphoreCreate(osSemaphore(myBinarySem01), 1);

Unfortunately this is not one of our API functions so I don’t know what  
it is doing. Can you please try using xSemaphoreCreateBinary():

> **[RTOS binary semaphore API](https://www.freertos.org/xSemaphoreCreateBinary.html)**
>
> Describes the xSemaphoreCreateBinary() RTOS API function which is part of the RTOS semaphore API source code function set.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 5, 2019, 7:06am UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/5 "2019-03-05T07:06:30Z")

</div>

**[sadakamath](https://sourceforge.net/u/sadakamath/)** wrote on [Tuesday, March 05, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#ce2d):

Same issue persists with new code -

SemaphoreHandle\_t myBinarySem01Handle = 0;  
main()  
vSemaphoreCreateBinary(myBinarySem01Handle);

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 5, 2019, 5:11pm UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/6 "2019-03-05T17:11:06Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, March 05, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#ce2d/47ab):

Please cut down your project to the very minimum that demonstrates this  
problem (remove everything other than the code in question), zip it up,  
then attached the zip file to a post in this forum.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 5, 2019, 5:33pm UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/7 "2019-03-05T17:33:33Z")

</div>

**[sadakamath](https://sourceforge.net/u/sadakamath/)** wrote on [Tuesday, March 05, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#0ae0):

Thanks, Attacted entire Atollic TrueSTUDIO project as size is 826KB. Let me know if you need only main.c.

LED blink and UART transmit is working fine when I call in ISR callback.  
But, both are not working with xSemaphoreGiveFromISR()

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 5, 2019, 6:04pm UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/8 "2019-03-05T18:04:55Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, March 05, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#0ae0/46e8):

There is a lot more in this project that just the part that is causing  
the issue. Can you give me a clue where to look.

In the mean time, what does this line do?

osSemaphoreDef(myBinarySem01);

Looks like it _might_ declare a semaphore on the stack of main(), which  
won’t exist when the scheduler starts, but I’m not familiar with this  
API. Could be the issue though.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 6, 2019, 10:18am UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/9 "2019-03-06T10:18:26Z")

</div>

**[sadakamath](https://sourceforge.net/u/sadakamath/)** wrote on [Wednesday, March 06, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#dd13):

1. I have replaced semaphore declaration and vSemaphoreCreateBinary. But, same issue persists. see updated code below under “Reference” section -
2. Problamatic area is xSemaphoreGiveFromISR inside ISR callback. xSemaphoreGive is working fine when it is called outside ISR

//-------------Reference --------------//  
outside main()  
//osSemaphoreId myBinarySem01Handle;  
xSemaphoreHandle myBinarySem01Handle;

main()  
// osSemaphoreDef(myBinarySem01);  
// myBinarySem01Handle = osSemaphoreCreate(osSemaphore(myBinarySem01), 1);  
vSemaphoreCreateBinary(myBinarySem01Handle);

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [March 6, 2019, 2:01pm UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/10 "2019-03-06T14:01:59Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, March 06, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#dd13/3595):

Try the following:

1. Put a break point on the call to xSemaphoreGiveFromISR() inside  
HAL\_TIM\_PeriodElapsedCallback().

2. If the breakpoint isn’t hit, then that is the problem, otherwise if  
the breakpoint is hit inspect the variable myBinarySem01Handle in the  
debugger. It should be a pointer to a structure, but you might have to  
cast it to a xQUEUE\* to see the structure members (so you would add  
“((xQUEUE\*) myBinarySem01Handle” to the debugger’s watch window). Take  
a screen shot of the debugger so we can see the values inside the  
structure and post the screen shot here.

3. Step into the xSemaphoreGiveFromISR() function line by line in the  
debugger, and see what path it takes through the function.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 8, 2019, 11:35am UTC](https://forums.freertos.org/t/freertos-xsemaphoregivefromisr-not-functional-from-isr-with-stm32f746ng/7704/11 "2019-03-08T11:35:03Z")

</div>

**[sadakamath](https://sourceforge.net/u/sadakamath/)** wrote on [Friday, March 08, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/1c7439975c/#3049):

I’m able to resolve this issue.

I have moved 1 sec interrupt HAL\_TIM\_Base\_Start\_IT to just before osKernelStart(). It was earlier before the creation of Semaphore & tasks.

I din’t noticed earlier that vSemaphoreCreateBinary() calls for xSemaphoreGive()

Thanks for your support…!
