# taskYIELD() causes hang inPIC24f interupt

**URL:** <https://forums.freertos.org/t/taskyield-causes-hang-inpic24f-interupt/1157>\
**Category:** Kernel\
**Created:** [November 16, 2010, 6:46pm UTC](https://forums.freertos.org/t/taskyield-causes-hang-inpic24f-interupt/1157 "2010-11-16T18:46:33Z")\
**Posts on this page:** 4\
**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:** [November 16, 2010, 6:46pm UTC](https://forums.freertos.org/t/taskyield-causes-hang-inpic24f-interupt/1157/1 "2010-11-16T18:46:33Z")

</div>

**[s-obrien](https://sourceforge.net/u/s-obrien/)** wrote on [Tuesday, November 16, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/3342c469/#04e8):

Per the FreeRTOS documentation to handle API calls inside of an interrupt I need to taskYIELD if necessary, I have the following ISR:

void \_\_attribute\_\_ ((\_\_interrupt\_\_, no\_auto\_psv)) \_T4Interrupt(void)  
{  
&nbsp;&nbsp; portBASE\_TYPE xHigherPriorityTaskWoken = pdFALSE;  
&nbsp;&nbsp; IFS1bits.T4IF = 0;  
&nbsp;&nbsp;  
&nbsp;&nbsp; xQueueSendToBackFromISR(queue\_name, &queue\_data, &xHigherPriorityTaskWoken);

&nbsp;&nbsp; if(xHigherPriorityTaskWoken == pdTRUE)  
&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; taskYIELD(); // Force context switch  
&nbsp;&nbsp; }

Everytime xHigherPriorityTaskWoken is true and the taskYIELD() executes, my processor resets.&nbsp; I’ve debugged to determine this is correct.&nbsp; If I comment out the taskYIELD, everything works smoothly.

The only thing I can think is that the priority of this interrupt is 7 and the tick timer has priority 1, but I’m not disabling any interrupts and per the RTOS docs this should be the correct method to use.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 16, 2010, 8:07pm UTC](https://forums.freertos.org/t/taskyield-causes-hang-inpic24f-interupt/1157/2 "2010-11-16T20:07:31Z")

</div>

**[jorick23](https://sourceforge.net/u/jorick23/)** wrote on [Tuesday, November 16, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/3342c469/#ad9c):

The correct function to use to switch tasks inside an interrupt is vTaskSwitchContext, not taskYield.&nbsp; So your code should look like this:

&nbsp;&nbsp; if(xHigherPriorityTaskWoken == pdTRUE)  
&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vTaskSwitchContext(); // Force context switch  
&nbsp;&nbsp; }

---

<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:** [November 16, 2010, 9:23pm UTC](https://forums.freertos.org/t/taskyield-causes-hang-inpic24f-interupt/1157/3 "2010-11-16T21:23:02Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, November 16, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/3342c469/#01a4):

I think in this case calling taskYIELD() is the correct thing to do - but this must be done as the very last thing in the ISR routine.&nbsp; Is it the last line of executable code?

FreeRTOS\Demo\PIC24\_MPLAB\serial\serial.c has an example ISR.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 16, 2010, 10:04pm UTC](https://forums.freertos.org/t/taskyield-causes-hang-inpic24f-interupt/1157/4 "2010-11-16T22:04:32Z")

</div>

**[s-obrien](https://sourceforge.net/u/s-obrien/)** wrote on [Tuesday, November 16, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/3342c469/#6ad5):

I’ve solved the problem.&nbsp; It turns out I was getting a stack overflow on my IDLE task.&nbsp; I had been assuming that the stack used for interrupts was from the task executing when the interrupt occurred which wouldn’t have caused any stack issues.

Could someone shed some light on what consumes the IDLE task’s stack during operation?&nbsp; (I have no user defined IDLE hook or anything that should be used during idle time).

(It looks like taskYIELD() is correct, when I switched it out with vTaskSwitchContext() I got strange behavior)
