# SemaphoregivefromISR()

**URL:** <https://forums.freertos.org/t/semaphoregivefromisr/9640>\
**Category:** Libraries\
**Created:** [April 26, 2020, 7:43pm UTC](https://forums.freertos.org/t/semaphoregivefromisr/9640 "2020-04-26T19:43:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dhanush](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@Dhanush](https://forums.freertos.org/u/Dhanush)\
**Post date:** [April 26, 2020, 7:43pm UTC](https://forums.freertos.org/t/semaphoregivefromisr/9640/1 "2020-04-26T19:43:14Z")

</div>

Hi I am developing Interrupt based CAN for ARM cortex M4 and am facing issue inside the ISR ,I am using SemaphoreGivefromISR() from the ISR and it is not jumping to Semophoretake() function can anybody help me to solve this ?  
my ISR code added below

{  
BaseType\_t reschedule;  
reschedule=pdFALSE;  
PRINTF(“ENTERED handler \r\n\n”);  
if(xSemaphoreGiveFromISR(xSemaphore\_producer,&reschedule)==pdPASS)  
{  
PRINTF(“Semaphore\_producer passed\r\n”);  
}  
else  
{  
PRINTF("Failed to produce since semaphore is already available\r\n ");  
}  
portYIELD\_FROM\_ISR(reschedule);  
}

---

<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:** [April 26, 2020, 8:08pm UTC](https://forums.freertos.org/t/semaphoregivefromisr/9640/2 "2020-04-26T20:08:05Z")

</div>

Almost always using printf and friends in interrupt handlers causes any kind of problems.  
It starts with excessive stack usage (stack of ISRs is special and often limited) and usually it‘s not even safe to use it in ISRs. Also try to keep ISRs as short as possible to minimize the impact to other ISRs of your application.  
Remove the printf calls and very likely the problem is solved.

---

<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:** [April 26, 2020, 9:04pm UTC](https://forums.freertos.org/t/semaphoregivefromisr/9640/3 "2020-04-26T21:04:31Z")

</div>

Also, if the semaphore has already been given, then the function will get a failure code (I believe) from the GiveFromISR.

Also, that task waiting on the semaphore will only run next if it was higher in priority than the currently running task. If it isn’t, it will need to wait its turn.

---

<div class="post-metadata">

**Author:** ![Dhanush](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@Dhanush](https://forums.freertos.org/u/Dhanush)\
**Post date:** [April 27, 2020, 5:49am UTC](https://forums.freertos.org/t/semaphoregivefromisr/9640/4 "2020-04-27T05:49:24Z")

</div>

Hi Thank you for your valuable input ,my concern is execution is not exiting out of ISR ,even after successfully generating xSemaphoreGiveFromISR(); it is staying inside ISR for infinite times.

---

<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:** [April 27, 2020, 6:45am UTC](https://forums.freertos.org/t/semaphoregivefromisr/9640/5 "2020-04-27T06:45:54Z")

</div>

It ‘stays’ there probably due to fatal error or exception depending on how you handle these cases. Did you define configASSERT and the other debug options ?  
I’d strongly propose to do so for development.  
And is there a difference after removing the printf calls ?  
Did you ensure that FreeRTOS with your tasks is properly started, the semaphore(s) are created etc. before enabling the interrupts in hardware ?  
It’s a problem if an interrupt occurs before that / too early.  
Best is to arm the interrupt by the task handling it. Then it’s ensured that everything needed is running and properly initialized.

---

<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:** [April 27, 2020, 11:48am UTC](https://forums.freertos.org/t/semaphoregivefromisr/9640/6 "2020-04-27T11:48:39Z")

</div>

If your ISR doesn’t end, then it is likely that your PRINTF isn’t really usable inside an ISR.
