# binary semaphore, ISR not yielding to switch

**URL:** <https://forums.freertos.org/t/binary-semaphore-isr-not-yielding-to-switch/296>\
**Category:** Kernel\
**Created:** [February 1, 2008, 4:34pm UTC](https://forums.freertos.org/t/binary-semaphore-isr-not-yielding-to-switch/296 "2008-02-01T16:34:30Z")\
**Posts on this page:** 5\
**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:** [February 1, 2008, 4:34pm UTC](https://forums.freertos.org/t/binary-semaphore-isr-not-yielding-to-switch/296/1 "2008-02-01T16:34:30Z")

</div>

**[cjb2002](https://sourceforge.net/u/cjb2002/)** wrote on [Friday, February 01, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/0f1698d2/#7c5f):

hi everybody,

here I am coding an ISR to tell uIP when to read data from an ethernet MAC.

here is the ISR:

-----------------------------------  
void \_\_attribute\_\_ ((naked))&nbsp; emac\_interrupt( void ) ;

void&nbsp;&nbsp;&nbsp; emac\_int\_not\_naked(void)  
{

&nbsp;&nbsp;&nbsp; volatile u32 regValue = 0;

&nbsp;&nbsp;&nbsp; static portBASE\_TYPE xTaskWoken = pdFALSE;

&nbsp;&nbsp;&nbsp; //leemos int status  
&nbsp;&nbsp;&nbsp; regValue = MAC\_INTSTATUS;

&nbsp;&nbsp;&nbsp; do  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue == 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue & MAC\_INT\_RXOVERRUN)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m\_nic\_write(MAC\_INTCLEAR, MAC\_INT\_RXOVERRUN);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue & MAC\_INT\_RXERROR)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m\_nic\_write(MAC\_INTCLEAR, MAC\_INT\_RXERROR);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue & MAC\_INT\_RXFINISHED)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m\_nic\_write(MAC\_INTCLEAR, MAC\_INT\_RXFINISHED);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //p\_priv-\>stats.rx\_erros++;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //while ( MAC\_RXPRODUCEINDEX != (MAC\_RXCONSUMEINDEX - 1) );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue & MAC\_INT\_RXDONE)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m\_nic\_write(MAC\_INTCLEAR, MAC\_INT\_RXDONE);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //wake up!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xTaskWoken = xSemaphoreGiveFromISR( xETH\_RX\_Sem, xTaskWoken );

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue & MAC\_INT\_TXUNDERRUN)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m\_nic\_write(MAC\_INTCLEAR, MAC\_INT\_TXUNDERRUN);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue & MAC\_INT\_TXERROR)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m\_nic\_write(MAC\_INTCLEAR, MAC\_INT\_TXERROR);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue & MAC\_INT\_TXFINISHED)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m\_nic\_write(MAC\_INTCLEAR, MAC\_INT\_TXFINISHED);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (regValue & MAC\_INT\_TXDONE)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m\_nic\_write(MAC\_INTCLEAR, MAC\_INT\_TXDONE);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; } while (0);

&nbsp;&nbsp;&nbsp; VICVectAddr = 0;&nbsp;&nbsp;&nbsp; //clear

&nbsp;&nbsp;&nbsp; if( xTaskWoken&nbsp; )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; portYIELD\_FROM\_ISR();&nbsp;&nbsp;&nbsp; //sip, hay que levantar la tarea!  
&nbsp;&nbsp;&nbsp; }  
}

void \_\_attribute\_\_ ((naked))&nbsp; emac\_interrupt( void )  
{  
&nbsp;&nbsp;&nbsp; /\* Save the context of the interrupted task. \*/  
&nbsp;&nbsp;&nbsp; portSAVE\_CONTEXT();

&nbsp;&nbsp;&nbsp; /\* Call the handler.&nbsp; This must be a separate function to ensure the  
&nbsp;&nbsp;&nbsp; stack frame is correctly set up. \*/  
&nbsp;&nbsp;&nbsp; emac\_int\_not\_naked();

&nbsp;&nbsp;&nbsp; /\* Restore the context of whichever task will run next. \*/  
&nbsp;&nbsp;&nbsp; portRESTORE\_CONTEXT();  
}  
--------------------------------------------------

so then I just do this in the UIP task:

…  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( xSemaphoreTake( xETH\_RX\_Sem, portMAX\_DELAY ) != pdTRUE )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("B\n");  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; something();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
…

it doesn’t matter if portMAX\_DELAY is configured to undefinitely block or not.  
I tested it and portYIELD\_FROM\_ISR() is called, but the task is not woken, and there are lower priorities task running, so I don’t know what’s wrong.

I’m using a LPC2468 in THUMB mode, but tried purely ARM mode and it’s the same behaviour.

any ideas?

thanks!

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 1, 2008, 4:35pm UTC](https://forums.freertos.org/t/binary-semaphore-isr-not-yielding-to-switch/296/2 "2008-02-01T16:35:39Z")

</div>

**[cjb2002](https://sourceforge.net/u/cjb2002/)** wrote on [Friday, February 01, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/0f1698d2/#7c5f/4e38):

oh, I just forgot, I’m creating the semaphore as a binary one, just before enabling the EMAC interrupt in the uip task.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 4, 2008, 11:29am UTC](https://forums.freertos.org/t/binary-semaphore-isr-not-yielding-to-switch/296/3 "2008-02-04T11:29:05Z")

</div>

**[cjb2002](https://sourceforge.net/u/cjb2002/)** wrote on [Monday, February 04, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/0f1698d2/#7c5f/c1b1):

finally, the problem was the static declaration of xTaskWoken and initialization in the same line. Quite subtle, but enough to avoid it from doing things right.

thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 4, 2008, 7:35pm UTC](https://forums.freertos.org/t/binary-semaphore-isr-not-yielding-to-switch/296/4 "2008-02-04T19:35:43Z")

</div>

**[imajeff](https://sourceforge.net/u/imajeff/)** wrote on [Monday, February 04, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/0f1698d2/#7c5f/aa0e):

Do I understand correctly that the problem is you are not running proper "startup code" in order to initialize global variables on powerup? Why not?

It is just not C without proper startup code.

(sorry, I don’t mean to start an off-topic discussion about GCC)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 5, 2008, 10:18pm UTC](https://forums.freertos.org/t/binary-semaphore-isr-not-yielding-to-switch/296/5 "2008-02-05T22:18:31Z")

</div>

**[cjb2002](https://sourceforge.net/u/cjb2002/)** wrote on [Tuesday, February 05, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/0f1698d2/#7c5f/241a):

Not actually. GCC is doing right and the startup code is too.

By declaring  
&nbsp;&nbsp; static portBASE\_TYPE xTaskWoken = pdFALSE;

it means that at startup it will contain pdFALSE, but as for the ISR routine implies, it will see it just as:  
&nbsp;&nbsp; static portBASE\_TYPE xTaskWoken;

only that it will be initialized to pdFALSE at startup.

Since I never do a xTaskWoken = pdFALSE; statement in the ISR, it will never get back to pdFALSE except when calling xSemaphoreGiveFromISR, which never returned false in my current implementation.

it’s amazing how a single statement-splitting changes meanings:

static portBASE\_TYPE xTaskWoken;  
xTaskWoken = pdFALSE;

but it’s the way it is meant to be, as what C regards.

hope I made it clear,

Carlos
