# Array of Semaphore cannot receive semaphore from semaphoregivefromisr()

**URL:** <https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466>\
**Category:** Kernel\
**Created:** [April 25, 2014, 11:34am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466 "2014-04-25T11:34:53Z")\
**Posts on this page:** 15\
**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:** [April 25, 2014, 11:34am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/1 "2014-04-25T11:34:53Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Friday, April 25, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#4f32):

I create an array of semaphore (in my case is two elements).  
xSemaphoreHandle sig\_Pulse[NUMBER\_CHANNEL];  
and then initiate and take it  
vSemaphoreCreateBinary(sig\_Pulse[CHANNEL1]);  
xSemaphoreTake(sig\_Pulse[CHANNEL1],0);  
but the problem is that when the interrupt sending semaphore  
xSemaphoreGiveFromISR(sig\_Pulse[CHANNEL1], &higher\_priority\_task\_woken);  
portEND\_SWITCHING\_ISR(higher\_priority\_task\_woken);  
it not enable the other task, which is waiting for this semaphore  
xSemaphoreTake(sig\_Pulse[channel],portMAX\_DELAY);

but if I use the normal way with if…else then it work correctly  
if (channel == 0)  
{  
xSemaphoreTake(sig\_Pulse1,portMAX\_DELAY);  
}  
else  
{  
xSemaphoreTake(sig\_Pulse2,portMAX\_DELAY);  
}

This is both semaphore in 1 array.  
CHANNEL1 and CHANNEL2 are only the predefined keyword for number 0 and 1 and NUMBER\_CHANNEL is equal to 2.  
My board is sam3u4e-ek with atmel studio 6.2 beta compiler. and the kernel is free rtos 7.3.0  
please tell me where the problems is???

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 25, 2014, 12:10pm UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/2 "2014-04-25T12:10:19Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Friday, April 25, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#e635):

Is this the case for both semaphores in the array or just one? What is CHANNEL1 defined to?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 25, 2014, 12:20pm UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/3 "2014-04-25T12:20:02Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Friday, April 25, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#d6b3):

hello Dave,  
This is both semaphore in 1 array.  
CHANNEL1 and CHANNEL2 are only the predefined keyword for number 0 and 1.  
and NUMBER\_CHANNEL is equal to 2.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 25, 2014, 12:22pm UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/4 "2014-04-25T12:22:23Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Friday, April 25, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#c425):

in the normal situation, i use sig\_Pulse1 and sig\_Pulse2 instead of sig\_Pulse[channel1] and sig\_Pulse[channel2]

---

<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:** [April 25, 2014, 4:51pm UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/5 "2014-04-25T16:51:43Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, April 25, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#f42e):

I just put together and tried the following tiny application that gives two semaphores that reside in an array in the tick hook (which is called from an interrupt) and everything executed in the expected sequence. I’m not sure what your problem is, but maybe it is not the way you are using the semaphores, but how your interrupt priorities are defined (which is what probably 95% of support requests on Cortex-M devices, especially STM32 parts, turn out to be). If you were using FreeRTOS V8.0.0 (or maybe even V7.6.0) then you could defined configASSERT() and it would automatically call assert if you had the interrupt priorities wrong.

[http://www.freertos.org/a00110.html#configASSERT](http://www.freertos.org/a00110.html#configASSERT)  
[http://www.freertos.org/FAQHelp.html](http://www.freertos.org/FAQHelp.html)  
[http://www.freertos.org/RTOS-Cortex-M3-M4.html](http://www.freertos.org/RTOS-Cortex-M3-M4.html)

As an aside, xSemaphoreCreateBinary() is preferred over vSemaphoreCreateBinary() now:  
[http://www.freertos.org/xSemaphoreCreateBinary.html](http://www.freertos.org/xSemaphoreCreateBinary.html)

My test code follows:

```
SemaphoreHandle_t xSemaphores[2];

volatile uint32\_t ulTask1 = 0, ulTask2 = 0;

void main\_blinky( void )  
{  
/\* Create and take the semaphores. \*/  
vSemaphoreCreateBinary( xSemaphores[0] );  
vSemaphoreCreateBinary( xSemaphores[1] );  
xSemaphoreTake( xSemaphores[0], 0 );  
xSemaphoreTake( xSemaphores[1], 0 );

```
<span style="color: #888888">/* Create the tasks. */</span>
xTaskCreate( vTask1, <span style="background-color: #fff0f0">&quot;Task1&quot;</span>, configMINIMAL_STACK_SIZE, <span style="color: #007020">NULL</span>, tskIDLE_PRIORITY, <span style="color: #007020">NULL</span> );
xTaskCreate( vTask2, <span style="background-color: #fff0f0">&quot;Task2&quot;</span>, configMINIMAL_STACK_SIZE, <span style="color: #007020">NULL</span>, tskIDLE_PRIORITY, <span style="color: #007020">NULL</span> );

<span style="color: #888888">/* Start the tasks and timer running. */</span>
vTaskStartScheduler();

<span style="color: #008800; font-weight: bold">for</span>( ;; );

```

}  
/_-----------------------------------------------------------_/

void vApplicationTickHook( void )  
{  
static volatile uint32\_t uxCallCount = 0;

```
<span style="color: #888888">/* Increment call count, then give one semaphore when the call count</span>

```

reaches 100, and the other when the call count reaches 200. \*/  
uxCallCount++;

```
<span style="color: #008800; font-weight: bold">if</span>( uxCallCount <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">100</span> )
{
	xSemaphoreGiveFromISR( xSemaphores[<span style="color: #0000DD; font-weight: bold">0</span>], <span style="color: #007020">NULL</span> );
}

<span style="color: #008800; font-weight: bold">if</span>( uxCallCount <span style="color: #333333">==</span> <span style="color: #0000DD; font-weight: bold">200</span> )
{
	xSemaphoreGiveFromISR( xSemaphores[<span style="color: #0000DD; font-weight: bold">1</span>], <span style="color: #007020">NULL</span> );

	<span style="color: #888888">/* Start again. */</span>
	uxCallCount <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span>;
}

```

}  
/_-----------------------------------------------------------_/

void vTask1( void pvParameters )  
{  
for( ;; )  
{  
/ Wait for the semaphore, and increment the counter when  
 it is received. _/_  
xSemaphoreTake( xSemaphores[0], portMAX\_DELAY );  
ulTask1++;  
}  
}  
/-----------------------------------------------------------\*/

void vTask2( void pvParameters )  
{  
for( ;; )  
{  
/ Wait for the semaphore, and increment the counter when  
 it is received. \*/  
xSemaphoreTake( xSemaphores[1], portMAX\_DELAY );  
ulTask2++;  
}  
}
```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 26, 2014, 9:50am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/6 "2014-04-26T09:50:54Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Saturday, April 26, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#c215):

thank you Real Time Engineers ltd very much for the fast support.

I do define the same configASSERT() and set my hardware interrupt priority as configMAX\_SYSCALL\_INTERRUPT\_PRIORITY as already mentioned in Freertos page. But the error triggered hardfault problem instead of configASSERT().

Could you add the “portEND\_SWITCHING\_ISR” or taskyield() to the function to check that again?  
It is really weird because there must not be any difference between the 2 independent semaphore and those in an array. I would change to version 8.0 to check that again.  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 26, 2014, 10:02am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/7 "2014-04-26T10:02:54Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Saturday, April 26, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#946c):

I forgot one important thing.  
The created task take the argument as the number of semarphore. I mean:

```
void vTask2( void *pvParameters )
{
    uint8_t channel = (uint8_t) pvParameters
    for (;;)
    {
        xSemaphoreTake(sig_Pulse[channel],portMAX_DELAY);
        ...
    }
}
```

and the created function is: xTaskCreate( vTask2, "Task2", STACK\_SIZE, (void\*) channel, PRIORITY, &task\_id );

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 26, 2014, 10:09am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/8 "2014-04-26T10:09:01Z")

</div>

**[woops\_](https://sourceforge.net/u/woops_/)** wrote on [Saturday, April 26, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#e0a3):

Did you check the value of channel in vTask2 then? If it is wrong for some reason then it will break.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 26, 2014, 10:14am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/9 "2014-04-26T10:14:14Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Saturday, April 26, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#e0a3/00d7):

yes I do check it, and it does correctly (with warning in compiler) but the task take it correctly.  
(if it goes wrong then the 2 separate semaphore will going wrong too but it actually does right).

---

<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:** [April 26, 2014, 10:26am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/10 "2014-04-26T10:26:26Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Saturday, April 26, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#bd13):

> I do define the same configASSERT() and set my hardware interrupt priority as

You would need to be using FreeRTOS V7.6.0 or later for that to catch a priority misconfigurtion.

> Could you add the “portEND\_SWITCHING\_ISR” or taskyield() to the function to check that  
> again?

If the problem is related to the access of the semaphore then that would not make any difference, but I tried it all the same and it didn’t change the behaviour or my test application.

In your case I can only assume either an interrupt priority problem, a problem indexing into the array, or a problem creating the semaphores in the first place - although I understand you have checked those already.

> But the error triggered hardfault problem instead of configASSERT().

That has to be the biggest clue as to what the issue is. I would recommend stepping through the code until you see the hardfault occur, then look at what was being accessed immediately before the hardware (what triggered the hard fault). The following link may also help - but just stepping through the code would be easier:

> **[Debugging and diagnosing hard faults on ARM Cortex-M CPUs](https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html)**
>
> Debugging hard fault exceptions on ARM Cortex-M3 and ARM Cortex-M4 microcontrollers

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 29, 2014, 10:03am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/11 "2014-04-29T10:03:10Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Tuesday, April 29, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#bd13/ce47):

Thanks for your fast reply.

> But the error triggered hardfault problem instead of configASSERT().

I'm sorry about this, I misremembered. I already fix this problem (before I post this quesion).

I change the OS to version 8.0.0 and it does not make any difference at all. (I already change the to xSemaphoreCreateBinary() instead of vSemaphoreCreateBinary() too)  
I mean the two separate semaphore does work as normal but the semaphore in array does not work, no assert trigger found.  
The problem is that it does send semaphore but the other task can not take it and thus nothing work.

the waiting task, named Calculate1, does in ready status at the time the semaphore give but cannot take it. The waiting command is:  
xSemaphoreTake(sig\_Calculate[channel],portMAX\_DELAY);

I observe the semaphore via the function vQueueAddToRegistry().

any hint for me please!

---

<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:** [April 29, 2014, 10:12am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/12 "2014-04-29T10:12:23Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, April 29, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#1139):

Create a small a project as you can that still exhibits the problem, check it builds correctly on any machine (it does not rely on any absolute paths to tools, source files, linker scripts, etc), zip it up and send it to the ‘business contact’ email address on the [http://www.freertos.org/contact](http://www.freertos.org/contact) web page.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 30, 2014, 11:58am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/13 "2014-04-30T11:58:49Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Wednesday, April 30, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#1500):

I recheck my project and realize that the semaphore does send and receive but the problem is that it seem the array of semaphore make the “semaphore get” slower. And thus missing more than half of the semaphore give.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [April 30, 2014, 12:02pm UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/14 "2014-04-30T12:02:01Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Wednesday, April 30, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#a1da):

> it seem the array of semaphore make the “semaphore get” slower

How could it do that? Inside the function it just has a pointer to a semaphore, if the semaphore is stored in the same kind or RAM (rather than external RAM in one case and internal RAM in the other) it can’t make any difference to the speed.

Sounds like you should be using a counting semaphore.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 7, 2014, 9:59am UTC](https://forums.freertos.org/t/array-of-semaphore-cannot-receive-semaphore-from-semaphoregivefromisr/2466/15 "2014-05-07T09:59:21Z")

</div>

**[thegracious](https://sourceforge.net/u/thegracious/)** wrote on [Wednesday, May 07, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/6d5b4721/#379d):

thanks all for your support. Things now running as expected. The array of semaphore run as the two separate semaphore now. The reason is that some mistake in my code (I still dont know where), when I re-code it, step by step, everything goes right. Problem solved!
