# Semaphore release in ISR using NIOS II

**URL:** <https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922>\
**Category:** Kernel\
**Created:** [February 13, 2011, 5:28pm UTC](https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922 "2011-02-13T17:28:35Z")\
**Posts on this page:** 7\
**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:** [February 13, 2011, 5:28pm UTC](https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922/1 "2011-02-13T17:28:35Z")

</div>

**[danyup](https://sourceforge.net/u/danyup/)** wrote on [Sunday, February 13, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/56ab79a3/#8e48):

Hello to all,  
I’m working on measurements over FreeRTOS for a thesis in Computer Engineering. I’m using FreeRTOS over NIOS II processor in an Altera FPGA and a custom made board.  
Here is what i’m trying to verify:  
A task is blocked on a semaphore, released by an hardware interrupt that fires once every 650us. I want to see if the task keep the same period of the interrupt and what is the latency of using this method (a task waiting for a semaphore) for interrupt management.  
When the Interrupt management task is executed alone everything is fine: i can see it starting once every 650 us. If I try it with other lower priority tasks I found that the task is launched once every 2ms, that is the RTOS tick period.  
It seems to me that the context switch that should wake the interrupt management task doesn’t occur until the intervention of FreeRTOS kernel, instead of being forced by the semaphore release function and the macro portEND\_SWITCHING\_ISR.  
Am I doing something wrong?  
Thanks for your help.  
Here’s my source code:

```
static void test_irq(void* context){
portBASE_TYPE highPrioWoken = pdFALSE;
IOWR(CONTATORE_AVALON_0_BASE, 6, 0);
xSemaphoreGiveFromISR(semaphore, &highPrioWoken);
portEND_SWITCHING_ISR(highPrioWoken);
}
static portTASK_FUNCTION( vSporadicTask, pvParameters ){
int i;
for(i = 0; i<NUMTEST; i++){
xSemaphoreTake(semaphore, portMAX_DELAY);
m_testBuffer[i] = alt_timestamp();
}
tst_put_open();
tst_put_string("name", "SporadicTest");
tst_put_int("numtest", NUMTEST);
tst_put_int("numtask", NUMTASK);
tst_put_ulong("TickFreq", alt_timestamp_freq());
tst_put_ulongbuffer("data", m_testBuffer, NUMTEST);
tst_put_close();
vTaskSuspend(NULL);
}
static portTASK_FUNCTION( vDummyTask, pvParameters ){
int m_dummyVar = 0;
for(;;){
m_dummyVar++;
}
}

```

Functions starting with tst\_put\_ are custom functions used to put the array containing the timestamps on standard output so that i can retrieve and analyse it. However they are called after the test is completed so they should not affect the correct behaviour of the task.

---

<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:** [February 13, 2011, 10:02pm UTC](https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922/2 "2011-02-13T22:02:21Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Sunday, February 13, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/56ab79a3/#8e32):

You don’t show the code for creating the tasks, but from your description, it sounds like vDummyTask and vSporadicTask are assigned to the same priority level, thus when the semaphore is given, vSporadicTask will not interrupt vDummyTask, but only be scheduled ready and take its turn at the next tick.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 13, 2011, 10:48pm UTC](https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922/3 "2011-02-13T22:48:44Z")

</div>

**[danyup](https://sourceforge.net/u/danyup/)** wrote on [Sunday, February 13, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/56ab79a3/#d9a4):

Uh… sorry for the missing source… However the sporadic task has an higher priority than the dummy task. That’s why I can’t explain what’s happening…

void startSporadicTest(){  
xTaskHandle handle=NULL;  
int basePrio = tskIDLE\_PRIORITY+1;  
vSemaphoreCreateBinary(semaphore);  
if(semaphore != NULL){  
xSemaphoreTake(semaphore, 0);  
}  
createDummyTasks(NUMTASK, basePrio);  
alt\_ic\_isr\_register(SYSID\_IRQ\_INTERRUPT\_CONTROLLER\_ID, CONTATORE\_AVALON\_0\_IRQ,test\_irq, NULL, NULL); //registering interrupt function  
IOWR(CONTATORE\_AVALON\_0\_BASE, 4, 1); //Enabling interrupt  
m\_testBuffer = pvPortMalloc( NUMTEST \* sizeof(alt\_u32) );  
if(!m\_testBuffer){  
printf(“Errore!! Impossibile creare il buffer m\_testBuffer\n”);  
}  
xTaskCreate(vSporadicTask, (signed char \*) “Test”, configMINIMAL\_STACK\_SIZE, NULL, basePrio+1, &handle);  
}

void createDummyTasks(int numtasks, int prio){  
int i;  
xTaskHandle m\_handle=NULL;  
for(i = 0; i \< numtasks; i++){  
xTaskCreate(vDummyTask, (signed char \*) “Dummy”, DUMMYSTACK, NULL, prio, &m\_handle);  
if(!m\_handle){  
printf(“Errore!! Memoria insufficiente per il task %d\n”, i); //That is out of memory  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 13, 2011, 10:50pm UTC](https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922/4 "2011-02-13T22:50:34Z")

</div>

**[danyup](https://sourceforge.net/u/danyup/)** wrote on [Sunday, February 13, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/56ab79a3/#c2a6):

I forgot the code tag… ☹  
Why can’t I edit the post?  
However thanks for your reply

---

<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:** [February 13, 2011, 11:47pm UTC](https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922/5 "2011-02-13T23:47:18Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Sunday, February 13, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/56ab79a3/#ec5c):

One thought, is configMAX\_PRIORITIES (in FreeRTOSConfig.H) at least 3? (Since you have 3 priorities, 0, 1, 2).

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [February 16, 2011, 9:00am UTC](https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922/6 "2011-02-16T09:00:40Z")

</div>

**[danyup](https://sourceforge.net/u/danyup/)** wrote on [Wednesday, February 16, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/56ab79a3/#1568):

Yes, it is… configMAX\_PRIORITIES is set to 5.  
Is there someone else who tried this kind of test? Even with different platform?

---

<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:** [February 17, 2011, 4:34am UTC](https://forums.freertos.org/t/semaphore-release-in-isr-using-nios-ii/1922/7 "2011-02-17T04:34:02Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, February 17, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/56ab79a3/#779c):

I will say that I have a somewhat similar structure in one of my projects, with a task that is woken up via a semaphore connected to a higher speed interrupt than the tick interrupt. and it runs fine. I will admit I don’t have the equivalent of your dummy task that is just wasting time.

My only suggestion would be (if possible) to trace with a debugger through the give and switch to see why the task isn’t starting.

The only thing I see that looks unusual to me is that the line m\_testbuffer = alt\_timestamp(); seems to keep setting the same variable each time, I would expect it to be m\_testbuffer\_ = alt\_timestamp(); or something like that.\_
