# xSemaphoreGiveFromISR failed?

**URL:** <https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013>\
**Category:** Kernel\
**Created:** [October 30, 2020, 2:11am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013 "2020-10-30T02:11:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmz211](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/mmz211/32/639_2.png) [@mmz211](https://forums.freertos.org/u/mmz211)\
**Post date:** [October 30, 2020, 2:11am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/1 "2020-10-30T02:11:39Z")

</div>

Hi,

I’m using freertos 8.2.3, and I meet a problem of xSemaphore.

I used xSemaphore successful in one of my project. but when I use it the same way in another project, it failed to give semaphore. the latter project is derived from the former one. and I compared FreeRTOSConfig.h in the two project, they are the same.

the program stopped at xSemaphoreGiveFromISR. any other factor can influence semaphore?

the way I use xSemaphore is like this:

=================================  
in .h file

```
    SemaphoreHandle_t data_ready_sem;

```

in .c file

in one function:

```
data_ready_sem = xSemaphoreCreateBinary();
if( data_ready_sem == NULL ){
	ERROR_RESIN("data_ready_sem create failed\r\n");
}

// init some variable

xSemaphoreTake(data_ready_sem, portMAX_DELAY);

```

in timer fired handler:

```
    xSemaphoreGiveFromISR(data_ready_sem, &xHigherPriorityTaskWoken2);
```

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [October 30, 2020, 2:23am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/2 "2020-10-30T02:23:35Z")

</div>

What do you mean with a “timer fired handler”? Is that a hardware timer causing an ISR?  
Or is that a handler that is called from the timer module?  
And if so, I think you should use `xSemaphoreGive()` because the code runs in the context of the timer task.

Maybe the code stops in a `configASSERT()`, have you checked that?

> I’m using freertos 8.2.3

That version works perfectly, but it is recommended to use [the latest version](https://github.com/freertos/freertos) for new developments.

---

<div class="post-metadata">

**Author:** ![mmz211](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/mmz211/32/639_2.png) [@mmz211](https://forums.freertos.org/u/mmz211)\
**Post date:** [October 30, 2020, 7:53am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/3 "2020-10-30T07:53:27Z")

</div>

Hi, Hein Tibosch

Thanks for your reply.

I tried `xSemaphoreGive()` and it stopped at xSemaphoreTake(data\_ready\_sem, portMAX\_DELAY);

I dig into xSemaphoreTake and find it stopped at ‘portYIELD\_WITHIN\_API();’

I’ll dig into xSemaphoreGiveFromISR and try to find where the program stops.

---

<div class="post-metadata">

**Author:** ![mmz211](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/mmz211/32/639_2.png) [@mmz211](https://forums.freertos.org/u/mmz211)\
**Post date:** [October 30, 2020, 7:57am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/4 "2020-10-30T07:57:15Z")

</div>

if I use xSemaphoreGiveFromISR

it stops at  
portASSERT\_IF\_INTERRUPT\_PRIORITY\_INVALID();

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [October 30, 2020, 8:11am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/5 "2020-10-30T08:11:57Z")

</div>

Can you tell from where you call `xSemaphoreGiveFromISR()`?  
Is that from an ISR or from a normal task context?

---

<div class="post-metadata">

**Author:** ![mmz211](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/mmz211/32/639_2.png) [@mmz211](https://forums.freertos.org/u/mmz211)\
**Post date:** [October 30, 2020, 8:25am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/6 "2020-10-30T08:25:58Z")

</div>

Hi,

the mcu is atsams70 from atmel, which has contex v7 core.

`xSemaphoreGiveFromISR()`is call from  
void TC1\_Handler(void)  
which is a hardware timer handler.

the timer confige is like this:

‘void configure\_tc1(void)  
{  
uint32\_t ul\_perclk = sysclk\_get\_peripheral\_hz();  
uint32\_t ul\_rc\_time = 0;

```
/* Enable peripheral clock. */
pmc_enable_periph_clk(ID_TC1);

tc_init(TC0, TC1_CHANNEL,
TC_CMR_TCCLKS_TIMER_CLOCK2 | //MCK/8
TC_CMR_WAVE | //waveform mode
TC_CMR_WAVSEL_UP_RC | //count up, reset on RC match
TC_CMR_ACPA_CLEAR | //clear TIOA0 on match with RA
TC_CMR_ACPC_SET //Set TIOA0 on match with RC
);

ul_rc_time = (uint32_t)((float)ul_perclk/8.0/200000);

TC0->TC_CHANNEL[TC1_CHANNEL].TC_RA = ul_rc_time / 2;
TC0->TC_CHANNEL[TC1_CHANNEL].TC_RC = ul_rc_time;

/* Configure and enable interrupt on RC compare */
NVIC_EnableIRQ((IRQn_Type) ID_TC1);
tc_enable_interrupt(TC0, TC1_CHANNEL, TC_IER_CPCS);

```

}’

it is a hardware timer.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [October 30, 2020, 10:01am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/7 "2020-10-30T10:01:44Z")

</div>

Sounds like an interrupt priority issue of the timer. Did you call `NVIC_SetPriority` with an appropriate priority somewhere ?  
In addition please see this

> [@Program stuck at "configASSERT" whilst calling "vTaskNotifyGiveFromISR", might be caused by interrupt nesting](https://forums.freertos.org/t/program-stuck-at-configassert-whilst-calling-vtasknotifygivefromisr-might-be-caused-by-interrupt-nesting/11011/2):
>
> You’ve to take care and set interrupt priorities correctly i.e. logically less than or equal to configMAX\_SYSCALL\_INTERRUPT\_PRIORITY if your ISRs make use of the FreeRTOS - API. That’s all about it. You don’t really need to take care about nesting at least regarding FreeRTOS. To get interrupt prios right e.g. on Cortex-M3/4/7 please see [https://www.freertos.org/RTOS-Cortex-M3-M4.html](https://www.freertos.org/RTOS-Cortex-M3-M4.html)

and Richard’s follow up.

You should better not define global variables like `data_ready_sem` in a header file.  
Just declare it `extern SemaphoreHandle_t data_ready_sem;` and define it in one c-file if you want to use it (as a global variable) in multiple c-files later on.  
If it’s intended use is limited to a single c-file (e.g. the timer driver/task module) better define it as `static SemaphoreHandle_t data_ready_sem;` in this c-file.  
Just a hint and to avoid unexpected ‘multiple definitions’ errors if you include the header file in another c-file later on.

---

<div class="post-metadata">

**Author:** ![mmz211](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/mmz211/32/639_2.png) [@mmz211](https://forums.freertos.org/u/mmz211)\
**Post date:** [November 6, 2020, 7:00am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/8 "2020-11-06T07:00:49Z")

</div>

yes, It is a problem of NVIC\_SetPriority, I did not call this funciton as I’m not very clear with functions like NVIC\_\*.

any recommend documents/web page about these functions or about CMSIS?

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [November 6, 2020, 9:29am UTC](https://forums.freertos.org/t/xsemaphoregivefromisr-failed/11013/9 "2020-11-06T09:29:33Z")

</div>

Luckily only a small fraction of the CMSIS API is needed controlling the NVIC.  
See e.g. the related [ARM docs](https://arm-software.github.io/CMSIS_5/Core/html/group __NVIC__ gr.html) and the provided CMSIS (core) sources.
