Array of Semaphore cannot receive semaphore from semaphoregivefromisr()

thegracious wrote on Friday, April 25, 2014:

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???

davedoors wrote on Friday, April 25, 2014:

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

thegracious wrote on Friday, April 25, 2014:

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.

thegracious wrote on Friday, April 25, 2014:

in the normal situation, i use sig_Pulse1 and sig_Pulse2 instead of sig_Pulse[channel1] and sig_Pulse[channel2]

rtel wrote on Friday, April 25, 2014:

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/FAQHelp.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

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++;
}
}

thegracious wrote on Saturday, April 26, 2014:

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.

thegracious wrote on Saturday, April 26, 2014:

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 );

woops_ wrote on Saturday, April 26, 2014:

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

thegracious wrote on Saturday, April 26, 2014:

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).

rtel wrote on Saturday, April 26, 2014:

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:

http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html

Regards.

thegracious wrote on Tuesday, April 29, 2014:

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!

rtel wrote on Tuesday, April 29, 2014:

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 web page.

Regards.

thegracious wrote on Wednesday, April 30, 2014:

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.

davedoors wrote on Wednesday, April 30, 2014:

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.

thegracious wrote on Wednesday, May 07, 2014:

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!