# Problem using semaphores

**URL:** <https://forums.freertos.org/t/problem-using-semaphores/961>\
**Category:** Kernel\
**Created:** [November 3, 2011, 9:09pm UTC](https://forums.freertos.org/t/problem-using-semaphores/961 "2011-11-03T21:09:51Z")\
**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 3, 2011, 9:09pm UTC](https://forums.freertos.org/t/problem-using-semaphores/961/1 "2011-11-03T21:09:51Z")

</div>

**[massel74](https://sourceforge.net/u/massel74/)** wrote on [Thursday, November 03, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/29e162c4/#2225):

Currently I’m struggling with the usage of semaphore given from an ISR.&nbsp; My problem is as follow.

There is a gatekeeper task which receives a queue and triggers a SPI burst transfer (via DTC). The data transfer controller issues an interrupt when the transfer has finished. This interrupt gives the semaphore on which the gatekeeper task is waiting for. (gatekeeper in blocked state)  
This sounds very easy but unfortunately it doesn’t work on my hardware (Renesas SH7216).  
The facts are:  
- the interrupt is definitely issued (I’m calling a output toggle funktion in the ISR -\> measured with oscilloscope)  
- the semaphore is given in the ISR (xSemaphoreGiveFromISR return value is 1, E10A debugger used)  
- if I put a wait loop just before the xSemaphoreTake, means try to take the semaphore when the semaphore is already given -\> everthing is working fine (but this is not the sense of semaphores 😉

I don’t have no further ideas how to debug this problem. Is there something I should look to to find the problem.

#pragma interrupt INT\_RSPI\_SPTXI  
void INT\_RSPI\_SPTXI (void)  
{  
long xHigherPriorityTaskWoken;  
extern xSemaphoreHandle xBinarySemaphore\_SPITX;

RSPI.SPCR.BIT.SPTIE &= 0; //Transmit interrupt disable  
RSPI.SPSR.BYTE &= 0xDF;

xHigherPriorityTaskWoken = pdFALSE;

xSemaphoreGiveFromISR(xBinarySemaphore\_SPITX, &xHigherPriorityTaskWoken);

if (xHigherPriorityTaskWoken = pdPASS)  
{  
portYIELD\_FROM\_ISR( xHigherPriorityTaskWoken );  
}

}

void prvSPIGatekeeperTask( void \*pvParameters)  
{  
xSPIStruct xReceivedStructure;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uint32\_t i;

init\_rspi();

xSemaphoreTake( xBinarySemaphore\_SPIRX, portMAX\_DELAY); //take semaphore because initial value is 1

for( ;; )  
{  
xQueueReceive(xSPIQueue,&xReceivedStructure,portMAX\_DELAY); //blocked state until queue entry

setSPIBasefreq(xReceivedStructure.SPIBaseFreq); // set SPI base frequency  
SetRSPI\_CMD0(xReceivedStructure.SPIcmd); // set SPI command

if(RSPI.SPSR.BIT.MIDLE==1) // if SPI idle  
{  
//send and receive SPI message  
dtc\_SPI\_io(xReceivedStructure.pDatatoTransmit, xReceivedStructure.pDatatoReceive, xReceivedStructure.cnt\_DatatoTransceive);  
SetSPIfunction(0xF8); //enable SPI and interrupts

/\*for(i=0; i\<2000; i++) //if I enable this nop loop everything is working fine because the semaphore  
{&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // is already given when xSemaphoreTake is reached (not a workaround!!!)  
nop();  
}\*/

xSemaphoreTake( xBinarySemaphore\_SPIRX, portMAX\_DELAY); //blocked state until SPI interrupt occured  
}

}

The Semaphore is created in main funktion with:

vSemaphoreCreateBinary( xBinarySemaphore\_SPIRX );

Many thanks in advance.

Martin

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 3, 2011, 9:20pm UTC](https://forums.freertos.org/t/problem-using-semaphores/961/2 "2011-11-03T21:20:41Z")

</div>

**[massel74](https://sourceforge.net/u/massel74/)** wrote on [Thursday, November 03, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/29e162c4/#31ac):

oh sorry, I just saw that I put the wrong ISR in the previous post.

here is the correct one

#pragma interrupt INT\_RSPI\_SPRXI  
void INT\_RSPI\_SPRXI (void)  
{  
long xHigherPriorityTaskWoken;  
extern xSemaphoreHandle xBinarySemaphore\_SPIRX;

RSPI.SPCR.BIT.SPRIE &= 0; //Receive interrupt disable  
RSPI.SPSR.BYTE &= 0x7F;

xHigherPriorityTaskWoken = pdFALSE;

debugSPI=xSemaphoreGiveFromISR(xBinarySemaphore\_SPIRX, &xHigherPriorityTaskWoken);  
[//PB.DR.BIT.B9=~PB.DR.BIT.B9](https://PB.DR.BIT.B9=~PB.DR.BIT.B9);  
if (xHigherPriorityTaskWoken = pdPASS)  
{  
portYIELD\_FROM\_ISR( xHigherPriorityTaskWoken );  
}

}

Thanks,  
Martin

---

<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:** [November 4, 2011, 1:42am UTC](https://forums.freertos.org/t/problem-using-semaphores/961/3 "2011-11-04T01:42:26Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Friday, November 04, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/29e162c4/#f9f4):

One thing I see, but that doesn’t explain the problem, is that you are using = instead of == in testing xHigherPriorityTaskWoken.

Could you have the ISR at a wrong priority, and it is corrupting the system variables?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 4, 2011, 5:33pm UTC](https://forums.freertos.org/t/problem-using-semaphores/961/4 "2011-11-04T17:33:59Z")

</div>

**[massel74](https://sourceforge.net/u/massel74/)** wrote on [Friday, November 04, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/29e162c4/#2f2c):

My problem is solved. It was a bad idea to call the ISR directly without the wrapper.  
Sometimes it’s realy better to read the documentation more carefully. 🙂  
Of course I also fixed the stupid mistake in the xHigherPriorityWoken testing code.

Thanks,  
Martin
