# Issue using the xSemaphoreGiveFromISR() function

**URL:** <https://forums.freertos.org/t/issue-using-the-xsemaphoregivefromisr-function/2844>\
**Category:** Kernel\
**Created:** [October 29, 2014, 11:36am UTC](https://forums.freertos.org/t/issue-using-the-xsemaphoregivefromisr-function/2844 "2014-10-29T11:36:09Z")\
**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:** [October 29, 2014, 11:36am UTC](https://forums.freertos.org/t/issue-using-the-xsemaphoregivefromisr-function/2844/1 "2014-10-29T11:36:09Z")

</div>

**[chris-itk](https://sourceforge.net/u/chris-itk/)** wrote on [Wednesday, October 29, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/7f57e76b/#9d8a):

Hi to all,  
I’ve got a problem regarding the use of the xSemaphoreGiveFromISR() function.  
I’m using it out of a callback function which is called within an ISR.

I use a binary semaphore as a signal for a completed spi reception.  
It has to be taken again immediately after it was given by the callback.

The problem is, that the binary semaphore is still “taken” after calling xSemaphoreGiveFromISR(), so my task blocks again.

Do you have any ideas how to fix my problem?

Here’s the callback function:

static void SPI\_Handler\_Callback(void)  
{  
static signed short xHigherPriorityTaskWoken;

```
if((0U != u8_SPI_Handler_Is_Initialized))
{
	xSemaphoreGiveFromISR(SPI_Handler_SPI_Signal, &xHigherPriorityTaskWoken);

	portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
return;

```

}

---

<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:** [October 29, 2014, 12:07pm UTC](https://forums.freertos.org/t/issue-using-the-xsemaphoregivefromisr-function/2844/2 "2014-10-29T12:07:33Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, October 29, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/7f57e76b/#8498):

What does the call to xSemaphoreGiveFromISR() return?

Do you have [configASSERT()](http://www.freertos.org/a00110.html#configASSERT) defined?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 29, 2014, 12:29pm UTC](https://forums.freertos.org/t/issue-using-the-xsemaphoregivefromisr-function/2844/3 "2014-10-29T12:29:29Z")

</div>

**[chris-itk](https://sourceforge.net/u/chris-itk/)** wrote on [Wednesday, October 29, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/7f57e76b/#8498/574f):

Thanks for your answer.  
No, I have not defined the configAssert().

The return value of xSemaphoreGiveFromISR() is pdPASS.

I debugged inside this routine and it reaches the line:

/\* Increment the lock count so the task that unlocks the queue  
knows that data was posted while it was locked. \*/  
++( pxQueue-\>xTxLock );

I’m not absolutely sure, but I think the semaphore is succesfully incremented after this line, isn’t it?

But when that’s true, why am I blocking the next time I try to take the same semaphore?

---

<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:** [October 29, 2014, 2:24pm UTC](https://forums.freertos.org/t/issue-using-the-xsemaphoregivefromisr-function/2844/4 "2014-10-29T14:24:41Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, October 29, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/7f57e76b/#8498/574f/aa0b):

Could you please post the code where you are receiving/taking the mutex.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 30, 2014, 3:33pm UTC](https://forums.freertos.org/t/issue-using-the-xsemaphoregivefromisr-function/2844/5 "2014-10-30T15:33:09Z")

</div>

**[chris-itk](https://sourceforge.net/u/chris-itk/)** wrote on [Thursday, October 30, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/7f57e76b/#99dd):

Here I’m doing a take just immediately after starting the spi transfere:

if(pdFALSE == xSemaphoreTake(SPI\_Handler\_SPI\_Signal, (SPI\_HANDLER\_SYNC\_TIMEOUT\_WAIT\_MS/portTICK\_RATE\_MS)))  
{  
ret = MD\_ERROR;  
SPI\_Handler\_Switch\_Chipselect(SPI\_Device\_Info, CS\_DISABLE);  
}

The Semaphore is then given again within my callback routine (Interrupt context):  
static void SPI\_Handler\_Callback(void)  
{  
signed short xHigherPriorityTaskWoken = pdFALSE;

```
if((0U != u8_SPI_Handler_Is_Initialized))
{
xSemaphoreGiveFromISR(SPI_Handler_SPI_Signal, &xHigherPriorityTaskWoken);
    portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
return;

```

}

I found out, that if I don’t request a Taskswitch within the Callback, the task comes up again at the next systick, which is after 1ms. But by using portYIELD\_FROM\_ISR() the task never comes up again. It seems to be a timing problem. If I do it step by step in debugger everything works fine. Maybe there is some kind of race condition (If I wait little, approx. 20µs, after starting the spi-transfere and before trying to take the semaphore, the problem also doesn’t occur?
