# FreeRTOS help with the time to active semapho

**URL:** <https://forums.freertos.org/t/freertos-help-with-the-time-to-active-semapho/5522>\
**Category:** Kernel\
**Created:** [January 18, 2010, 7:43am UTC](https://forums.freertos.org/t/freertos-help-with-the-time-to-active-semapho/5522 "2010-01-18T07:43:23Z")\
**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:** [January 18, 2010, 7:43am UTC](https://forums.freertos.org/t/freertos-help-with-the-time-to-active-semapho/5522/1 "2010-01-18T07:43:23Z")

</div>

**[gary10es](https://sourceforge.net/u/gary10es/)** wrote on [Monday, January 18, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/f7d13a51/#1baa):

Hello everybody

I need to improve the time to activate a Semaphore from the ISR. I don’t known what solution to apply.&nbsp; The time is about 70 microsecond, maybe because there is a change of task.

Can i implement a different semaphore more fast?  
Is there another alternative?

Another question. If the interrupt can´t switch the context, can i delete this sentences&nbsp; portENTER\_SWITCHING\_ISR(); ?

Thank  
Oliver

Code:

/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* INTERRUPT \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
\_attribute\_\_((\_\_noinline\_\_))  
static portBASE\_TYPE eic\_int\_handler\_NonNakedBehaviour( void )  
{  
&nbsp; portBASE\_TYPE retstatus;  
&nbsp; portBASE\_TYPE xTaskWoken = pdFALSE;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* Because FreeRTOS is not supposed to run with nested interrupts, put all OS  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; calls in a critical section . \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; portENTER\_CRITICAL();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; retstatus=xSemaphoreGiveFromISR( xSemaphoreReceived, xTaskWoken );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; portEXIT\_CRITICAL();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* The return value will be used by portEXIT\_SWITCHING\_ISR() to know if it  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; should perform a vTaskSwitchContext(). \*/

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; eic\_clear\_interrupt\_line(&AVR32\_EIC, EXT\_INT2);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (xTaskWoken);  
}

\_\_attribute\_\_((\_\_naked\_\_))  
void eic\_int\_handler( void )  
{  
&nbsp;&nbsp; /\* This ISR can cause a context switch, so the first statement must be a  
&nbsp;&nbsp;&nbsp;&nbsp; call to the portENTER\_SWITCHING\_ISR() macro.&nbsp; This must be BEFORE any  
&nbsp;&nbsp;&nbsp;&nbsp; variable declarations. \*/  
&nbsp;&nbsp;&nbsp;&nbsp; portENTER\_SWITCHING\_ISR();

&nbsp;&nbsp;&nbsp;&nbsp; eic\_int\_handler\_NonNakedBehaviour();

&nbsp;&nbsp;&nbsp;&nbsp; /\* Exit the ISR.&nbsp; If a task was woken then a context switch will occur. \*/  
&nbsp;&nbsp;&nbsp;&nbsp; portEXIT\_SWITCHING\_ISR();  
}

/\*\*\*\*\*\*\*\*\*\*\*\*\* TASK \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/  
static portTASK\_FUNCTION( vTxRxPCUTask, pvParameters )  
{

( void ) pvParameters;

&nbsp;&nbsp; //first thing to do Take the Semaphore  
&nbsp;&nbsp; xSemaphoreTake(xSemaphoreReceived,portMAX\_DELAY);

&nbsp;&nbsp; while(1)  
&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp; //wait for ISR free the semaphore  
&nbsp;&nbsp;&nbsp;&nbsp; xSemaphoreTake(xSemaphoreReceived,portMAX\_DELAY);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Rest of the code  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; …….  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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:** [January 18, 2010, 9:59am UTC](https://forums.freertos.org/t/freertos-help-with-the-time-to-active-semapho/5522/2 "2010-01-18T09:59:30Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Monday, January 18, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/f7d13a51/#f482):

I don’t recognize this code, but generally taskENTER\_CRITICAL() and taskEXIT\_CRITICAL() should not be called from an interrupt. You might consider implementing portSET\_INTERRUPT\_MASK\_FROM\_ISR() and portCLEAR\_INTERRUPT\_MASK\_FROM\_ISR() which are intended for this type of scenario. If they are not defined then they are just removed using #defines.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 18, 2010, 10:26am UTC](https://forums.freertos.org/t/freertos-help-with-the-time-to-active-semapho/5522/3 "2010-01-18T10:26:17Z")

</div>

**[gary10es](https://sourceforge.net/u/gary10es/)** wrote on [Monday, January 18, 2010](https://sourceforge.net/p/freertos/discussion/382005/thread/f7d13a51/#d124):

I have an AVR32&nbsp; in EVK1100. In all example always appear these sentences.

If&nbsp; i don´t change of&nbsp; task in the interrupt, and i don´t call any function of S.O i have deleted this portENTER\_CRITICAL(); and portEXIT\_CRITICAL();. It´s work! but i don’t known if i can delete portENTER\_SWITCHING\_ISR(); and portEXIT\_SWITCHING\_ISR(); too. may i delete this sentences?

By the other hand, i want to improve the time beetwen the semaphore from it is given to it is taken  
Can i create a little code for to comunicate the interrupt with task as soon as posible. this is my problem i have wasted 70 microsecond.

Thanks  
Oliver

Thanks  
Oscar
