# STM32 xSemaphoreGive Interrupt infinite Loop 

**URL:** <https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671>\
**Category:** Kernel\
**Created:** [January 6, 2011, 8:43am UTC](https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671 "2011-01-06T08:43:49Z")\
**Posts on this page:** 7\
**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:** [January 6, 2011, 8:43am UTC](https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671/1 "2011-01-06T08:43:49Z")

</div>

**[irvind](https://sourceforge.net/u/irvind/)** wrote on [Thursday, January 06, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/77bcee4c/#8e69):

Hi,

Currently I am using STM32 Cortex M3, compiler IAR 4.x & FreeRTOS v6.1.0.

I have 2 interrupt task for the moment, interrupt from CAN bus & periodic software interrupt.

```
void vPeriodicTask( void *pvParameters )
{
       
  portTickType xLastExecutionTime;
  xLastExecutionTime = xTaskGetTickCount();
  
  for(;;)
  {
    xSemaphoreGive(SEM_INT);
    vTaskDelayUntil( &xLastExecutionTime, 10000/portTICK_RATE_MS);
  }
}
void USB_LP_CAN_RX0_IRQHandler(void)
{
   
   CAN_Receive(CAN_FIFO0, &RxMessage);
  
   portBASE_TYPE xHighxHigherPriorityTaskWoken = pdFALSE;
   xSemaphoreGiveFromISR( SEM_CAN_INT, &xHighxHigherPriorityTaskWoken);
   portEND_SWITCHING_ISR(xHighxHigherPriorityTaskWoken);
           
}

```

Then if I put a break point at xSemaphoreGive(SEM\_INT); under periodic task. I found that it looks like execute this line for so many times before xSemaphoreTake( SEM\_INT, portMAX\_DELAY); been unblocked. The way I test it press F5 when process stop at break point, my expectation is it will execute next line when I press F5. Am I make wrong assumption?

On the other hand, my program freeze at this line for

```
( pxIterator = ( xListItem * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext ) 

```

after 1-2 hour running. Turned on stack overflow check but catch nothing. Non-of the interrupt firing before startscheduler execute.

Could you please advise this? Thank you so much for your support.

---

<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:** [January 6, 2011, 9:19am UTC](https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671/2 "2011-01-06T09:19:18Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, January 06, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/77bcee4c/#176e):

Have you taken account of point 3 on the following page?&nbsp; Your symptoms would suggest not.

> **[FreeRTOS - Open Source RTOS Kernel for small embedded systems](https://www.freertos.org/FAQHelp.html)**
>
> FreeRTOS is a portable, open source, mini Real Time kernel. A free RTOS for small embedded systems

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 6, 2011, 10:29am UTC](https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671/3 "2011-01-06T10:29:42Z")

</div>

**[irvind](https://sourceforge.net/u/irvind/)** wrote on [Thursday, January 06, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/77bcee4c/#72e9):

Hi Richard,

I saw point 3 before , but I don’t think I have using any API in ISR. My interrupt handler task as below, please advise.

```
void USB_LP_CAN_RX0_IRQHandler(void)
{
   
   CAN_Receive(CAN_FIFO0, &RxMessage);
  
   portBASE_TYPE xHighxHigherPriorityTaskWoken = pdFALSE;
   xSemaphoreGiveFromISR( SEM_CAN_INT, &xHighxHigherPriorityTaskWoken);
   portEND_SWITCHING_ISR(xHighxHigherPriorityTaskWoken);
           
}

```

But I doubt if I really understand well on interrupt priority parts, below are the code I use to enable CAN interrupt.

```
NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN_RX0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

```

My FreeRTOSConfig.h setting as default.

```
/* This is the raw value as per the Cortex-M3 NVIC. Values can be 255
(lowest) to 0 (1?) (highest). */
#define configKERNEL_INTERRUPT_PRIORITY 255
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 191 /* equivalent to 0xb0, or priority 11. */
/* This is the value being used as per the ST library which permits 16
priority values, 0 to 15. This must correspond to the
configKERNEL_INTERRUPT_PRIORITY setting. Here 15 corresponds to the lowest
NVIC value of 255. */
#define configLIBRARY_KERNEL_INTERRUPT_PRIORITY	15

```

The priority mentioned in point 3 refered to NVIC\_IRQChannelPreemptionPriority or create task priority? Which is priority 3 for my task.

Please advise.

Thank you so much for your reply.

---

<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:** [January 6, 2011, 10:42am UTC](https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671/4 "2011-01-06T10:42:38Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, January 06, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/77bcee4c/#f689):

> I saw point 3 before , but I don’t think I have using any API in ISR.

You are calling xSemaphoreGiveFromISR() from the ISR, so you are using a FreeRTOS API function in the ISR.&nbsp; Therefore you \*must\* set your interrupt priorities correctly otherwise you will get the exact symptom you are describing.

> NVIC\_InitStructure.NVIC\_IRQChannel=USB\_LP\_CAN\_RX0\_IRQChannel;  
> NVIC\_InitStructure.NVIC\_IRQChannelPreemptionPriority = 0;  
> NVIC\_InitStructure.NVIC\_IRQChannelSubPriority = 0;  
> NVIC\_InitStructure.NVIC\_IRQChannelCmd = ENABLE;  
> NVIC\_Init(&NVIC\_InitStructure);

You are setting the interrupt to the highest possible priority, this is exactly what the linked warning is telling you not to do, and will result in the behaviour you are describing.

There are lots of other posts on this subject.&nbsp; Please learn about the MCU before learning about FreeRTOS.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 7, 2011, 7:11am UTC](https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671/5 "2011-01-07T07:11:01Z")

</div>

**[irvind](https://sourceforge.net/u/irvind/)** wrote on [Friday, January 07, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/77bcee4c/#a2c3):

Hi Richard,

Thanks for your reply.

Understand that almost 95% support request on this issue. I go through both purchased FreeRTOS both ebooks but still not fully understand on interrupt nesting priority configuration with FreeRTOS implemented.

```
#define configKERNEL_INTERRUPT_PRIORITY 255
#define configMAX_SYSCALL_INTERRUPT_PRIORITY 191 /* equivalent to 0xb0, or priority 11. */

```

Understand that KERNEL\_INTTERRUPT\_PRIORITY advice to remain at lowest which is 255. But MAX\_SYSCALL\_INTERRUPT\_PRIORITY why set to priority 11? Is this value set for demo or we can change it accordingly to our requirement?

So far I am not facing problem without using RTOS, so I am lack of knowledge in interrupt priority configuration. So I wish that you could guide me on this issue.

From the purchased ebooks, it mentioned that priority of ISR that using FreeRTOS API should between MAX\_SYSCALL\_PRIORITY & KERNEL\_INT\_PRIORITY. Is this limit to API that end with “FromISR” or all RTOS API?

If I set my MAX\_SYSCALL\_PRIORITY to 0, then KERNEL\_PRIORITY as default 255. Is that mean my NVIC priority of ISR that using FreeRTOS API can use any priority between 0-255? But changing MAX\_SYSCALL\_PRIORITY to highest will causing any problem?

Please advise me on this. Thanks in advanced.

Regards.

---

<div class="post-metadata">

**Author:** ![quartus0](https://avatars.discourse-cdn.com/v4/letter/q/bbce88/32.png) [@quartus0](https://forums.freertos.org/u/quartus0)\
**Post date:** [March 3, 2011, 7:43am UTC](https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671/6 "2011-03-03T07:43:25Z")

</div>

**[quartus0](https://sourceforge.net/u/quartus0/)** wrote on [Thursday, March 03, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/77bcee4c/#b38c):

Hi.  
I have similar problem with you.  
Did you find out the solution ?  
If so, could you please let me know ?

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [March 3, 2011, 2:47pm UTC](https://forums.freertos.org/t/stm32-xsemaphoregive-interrupt-infinite-loop/2671/7 "2011-03-03T14:47:03Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, March 03, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/77bcee4c/#8c5a):

irvind,  
The only problem that I know that setting MAX\_SYSCALL\_PRIORITY to 0 will cause is that it then becomes impossible to have an interrupt (that doesn’t call FreeRTOS) , at a priority above MAX\_SYSCALL\_PRIORITY so that it doesn’t get delayed by critical sections in the kernel and user code. (Non-maskable interrupts/traps still can not call the FreeRTOS API)

quartus,  
If you have the same problem, then you probably have the same solution. Any interrupt function that calls FreeRTOS functions must use the FromISR versions, and must have a priority lower (which on some processors is a smaller number and other is a larger number).
