# Using semaphore problem

**URL:** <https://forums.freertos.org/t/using-semaphore-problem/716>\
**Category:** Kernel\
**Created:** [May 13, 2009, 12:18pm UTC](https://forums.freertos.org/t/using-semaphore-problem/716 "2009-05-13T12:18:45Z")\
**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:** [May 13, 2009, 12:18pm UTC](https://forums.freertos.org/t/using-semaphore-problem/716/1 "2009-05-13T12:18:45Z")

</div>

**[grzegorzw](https://sourceforge.net/u/grzegorzw/)** wrote on [Wednesday, May 13, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/1f9787ab/#8b0a):

I init semaphore before scheduler starts:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; vSemaphoreCreateBinary( xSPI0Semaphore );

in running task I take semaphore:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xSemaphoreTake( xSPI0Semaphore, portMAX\_DELAY );

WHAT IS WRONG:  
Semaphore is taken in first call, without giving anywhere before. So first taking procedure is not blocking.

TRY:  
I check semaphore before taking:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q = uxQueueMessagesWaiting( xSPI0Semaphore );

and q value is "1", but should be "0".

Please help

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 13, 2009, 12:27pm UTC](https://forums.freertos.org/t/using-semaphore-problem/716/2 "2009-05-13T12:27:55Z")

</div>

**anonymous** wrote on [Wednesday, May 13, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/1f9787ab/#8b0a/8452):

This is the documented behaviour: binary semaphores are initially released. You might want to do a xSemaphoreTake(xSPIOSemaphore, 0) just after creating the semaphore if you want it to be taken before the scheduler starts (the 0 doesn’t matter here, since you \*will\* get the semaphore immediately).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 14, 2009, 7:22am UTC](https://forums.freertos.org/t/using-semaphore-problem/716/3 "2009-05-14T07:22:42Z")

</div>

**[grzegorzw](https://sourceforge.net/u/grzegorzw/)** wrote on [Thursday, May 14, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/1f9787ab/#8b0a/3184):

Thank you for your help. I have still a problem with using semaphore.

I wrote a task:

void vSPI0Task( void \*pvParameters )  
{  
&nbsp;&nbsp;&nbsp; struct tspiIO spiIO;  
&nbsp;&nbsp;&nbsp; portBASE\_TYPE q;

&nbsp;&nbsp;&nbsp; xSemaphoreTake( xSPI0Semaphore, 0 );

&nbsp;&nbsp;&nbsp; for( ;; )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xQueueReceive( xSPIInputQueue, &spiIO, portMAX\_DELAY );

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPI\_AsyncWriteFirst( AT91C\_BASE\_SPI0, spiIO.DataToSend , spiIO.DataToSendLen, spiIO.WriteDummy );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPI\_AsyncReadNext( AT91C\_BASE\_SPI0, spiIO.ReceivedData, spiIO.ReceivedDataLen, spiIO.ReadDummy );

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AT91C\_BASE\_SPI0-\>SPI\_IER = AT91C\_SPI\_RXBUFF;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PDC\_EnableWriteAndRead((AT91S\_PDC\*)((\_\_u32)(AT91C\_BASE\_SPI0) + 0x100));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xSemaphoreTake( xSPI0Semaphore, portMAX\_DELAY );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xQueueSendToBack( xSPIOutputQueue, &spiIO, portMAX\_DELAY );  
&nbsp;&nbsp;&nbsp; }  
}

And a interrupt subroutine (I am using YAGARTO; interrupts without nesting):

void \_\_attribute\_\_ ((interrupt ("IRQ")))SPI0\_irq\_handler( void )  
{  
&nbsp;&nbsp;&nbsp; volatile \_\_u32 spiSr;  
&nbsp;&nbsp;&nbsp; portBASE\_TYPE xHigherPriorityTaskWoken;

&nbsp;&nbsp;&nbsp; spiSr = AT91C\_BASE\_SPI0-\>SPI\_SR;  
&nbsp;&nbsp;&nbsp; xHigherPriorityTaskWoken = pdFALSE;  
&nbsp;&nbsp;&nbsp; if ( spiSr & AT91C\_SPI\_RXBUFF )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AT91C\_BASE\_SPI0-\>SPI\_IDR = AT91C\_SPI\_RXBUFF;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xSemaphoreGiveFromISR( xSPI0Semaphore, &xHigherPriorityTaskWoken );  
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; AT91C\_BASE\_AIC-\>AIC\_ISR;  
&nbsp;&nbsp;&nbsp; AT91C\_BASE\_AIC-\>AIC\_EOICR = 0;

&nbsp;&nbsp;&nbsp; if( xHigherPriorityTaskWoken == pdTRUE )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; portYIELD\_FROM\_ISR();  
&nbsp;&nbsp;&nbsp; }  
}  
&nbsp;&nbsp;&nbsp;  
The problem lies in the fact that when the task is blocked in “taking semaphore state”, interrupt can’t give it and the task is “blocked permanently”. In debug mode (step run), when the interrupt comes before “taking semaphore state” and the semaphore is given, program works.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 14, 2009, 9:03am UTC](https://forums.freertos.org/t/using-semaphore-problem/716/4 "2009-05-14T09:03:44Z")

</div>

**[grzegorzw](https://sourceforge.net/u/grzegorzw/)** wrote on [Thursday, May 14, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/1f9787ab/#8b0a/5f13):

I found a solution:

void vSPI0\_Wrapper() \_\_attribute\_\_((naked));  
void vSPI0\_Handler();

void vSPI0\_Wrapper( void )  
{  
&nbsp;&nbsp;&nbsp; portSAVE\_CONTEXT();  
&nbsp;&nbsp;&nbsp; vSPI0\_Handler();  
&nbsp;&nbsp;&nbsp; portRESTORE\_CONTEXT();

}

void vSPI0\_Handler( void )  
{  
&nbsp;&nbsp;&nbsp; volatile \_\_u32 spiSr;  
&nbsp;&nbsp;&nbsp; portBASE\_TYPE xHigherPriorityTaskWoken;

&nbsp;&nbsp;&nbsp; spiSr = AT91C\_BASE\_SPI0-\>SPI\_SR;  
&nbsp;&nbsp;&nbsp; xHigherPriorityTaskWoken = pdFALSE;  
&nbsp;&nbsp;&nbsp; if ( spiSr & AT91C\_SPI\_RXBUFF )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AT91C\_BASE\_SPI0-\>SPI\_IDR = AT91C\_SPI\_RXBUFF;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xSemaphoreGiveFromISR( xSPI0Semaphore, &xHigherPriorityTaskWoken );  
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; AT91C\_BASE\_AIC-\>AIC\_ISR;  
&nbsp;&nbsp;&nbsp; AT91C\_BASE\_AIC-\>AIC\_EOICR = 0;

&nbsp;&nbsp;&nbsp; if( xHigherPriorityTaskWoken == pdTRUE )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; portYIELD\_FROM\_ISR();  
&nbsp;&nbsp;&nbsp; }  
}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 14, 2009, 2:34pm UTC](https://forums.freertos.org/t/using-semaphore-problem/716/5 "2009-05-14T14:34:37Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, May 14, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/1f9787ab/#8b0a/3184/8da3):

You need to write your interrupt service routine as detailed on the documentation page and as detailed by the provided examples.&nbsp; See the "interrupt service routines" section of [http://www.freertos.org/portlpc2106.html](http://www.freertos.org/portlpc2106.html) for a start.
