# Problem with semaphore time

**URL:** <https://forums.freertos.org/t/problem-with-semaphore-time/1159>\
**Category:** Kernel\
**Created:** [December 14, 2009, 10:32pm UTC](https://forums.freertos.org/t/problem-with-semaphore-time/1159 "2009-12-14T22:32:57Z")\
**Posts on this page:** 3\
**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:** [December 14, 2009, 10:32pm UTC](https://forums.freertos.org/t/problem-with-semaphore-time/1159/1 "2009-12-14T22:32:57Z")

</div>

**[gary10es](https://sourceforge.net/u/gary10es/)** wrote on [Monday, December 14, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/333f2b62/#2bb6):

Hello everybody, i have a problem with the time in communicate my interrupt and one task while a semaphore.  
My tick is 1mseg, and the frecuency of my micro in EVK1100 is 66Mhz.

the code is that  
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

/  
\_\_attribute\_\_((\_\_noinline\_\_))

static portBASE\_TYPE eic\_int\_handlerPRI\_NonNakedBehaviour( void )

{

portBASE\_TYPE retstatus,retstatus2;

portBASE\_TYPE xTaskWoken = pdFALSE;

Cont++;

if ( (F\_Count\_P==1) && (-Num\_Execute\_P==0))

{

F\_Count\_P=0;

/\* Because FreeRTOS is not supposed to run with nested interrupts, put all OS

&nbsp;&nbsp;&nbsp; calls in a critical section . \*/

&nbsp;&nbsp;&nbsp; portENTER\_CRITICAL();

retstatus=xSemaphoreGiveFromISR( xSemaphoreSendP, xTaskWoken );

portEXIT\_CRITICAL();

/\* The return value will be used by portEXIT\_SWITCHING\_ISR() to know if it

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; should perform a vTaskSwitchContext(). \*/

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; if (retstatus)  
&nbsp; xTaskWoken = pdTRUE;

}

/\* The return value will be used by portEXIT\_SWITCHING\_ISR() to know if it

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; should perform a vTaskSwitchContext(). \*/

&nbsp;&nbsp;&nbsp; // False because don’t change the task when the interrupt happened.!!!!!!

&nbsp;&nbsp;&nbsp; //Reset the Interrupt

eic\_clear\_interrupt\_line(&AVR32\_EIC, EXT\_INT0);

return (xTaskWoken);

}

and the task to unblock with this semaphore is this:  
static portTASK\_FUNCTION( vTxRxPCUTask, pvParameters )

{

const portTickType xTicksToWait=TIMEOUT/portTICK\_RATE\_MS; //Ponemos el timeout en mseg como cte.Se aplica a todos

unsigned short i,j;

unsigned short length\_aux;

\_dataword aux;

\_datadoubleword aux2;

( void ) pvParameters;

while(1)

{

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; …. // rest of code  
&nbsp;&nbsp;&nbsp; }

the time beetwen the semaphoreGive and the semaphore Take is about 70 microsecond, and i want to reduce this time

is there any possibilitty? need i use co-routine? can i use a flag in the interrupt&nbsp; and the task every moment see the flag ?

any idea ?  
thanks  
Oliver

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 15, 2009, 6:56pm UTC](https://forums.freertos.org/t/problem-with-semaphore-time/1159/2 "2009-12-15T18:56:41Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Tuesday, December 15, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/333f2b62/#2369):

I’m not sure what the effect of the critical section in the interrupt will be. Probably bad at a guess though as the macros used will be using variables that are part of the task context.

Make sure the task you want to wake is the highest priority to ensure it runs as soon as its unblocked. Are you context switching to it before leaving the interrupt?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [December 16, 2009, 9:20am UTC](https://forums.freertos.org/t/problem-with-semaphore-time/1159/3 "2009-12-16T09:20:54Z")

</div>

**[gary10es](https://sourceforge.net/u/gary10es/)** wrote on [Wednesday, December 16, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/333f2b62/#ab9f):

hello, this task is the highest priority, but when the interrupt awaken i not sure if need to change the context, because in that moment should all task are in blocking state.&nbsp; I´ll check&nbsp;&nbsp; retstatus=xSemaphoreGiveFromISR( xSemaphoreSendP, xTaskWoken );&nbsp;&nbsp; retstatus because the value can be true.  
Two question:

a) How can i do that this task run as soon as its unblocked?  
b) And what can i do to avoid context switching?

thanks  
Oliver
