This is a generic question on the use of Semas.
RTOS documentation states:
A binary semaphore need not be given back once obtained, so task synchronisation can be implemented by one task/interrupt continuously ‘giving’ the semaphore while another continuously ‘takes’ the semaphore. This is demonstrated by the sample code on the xSemaphoreGiveFromISR() documentation page.
Who owns the sema as it is created? Is it the task that created it?
I have seen uses of this in the code, where the creating task explicitily takes the sema right after creation by calling the xSemaphoreTake and at other places, it just creates the sema.
Is there a rule to this?
Now the real question -
Let us say that a sema is shared between interrupt and a task.
Task creates the sema and waits on the sema for new data to arrive that is signaled by the ISR routine using the giveFromIsr call.
Before the ISR signals the availability of data with the givefromISR, who owns the sema? If the task owns the sema, it will always be looking at the data which is not there and so it constantly keeps looking for data (as it owns the sema) instead of waiting for data using the sema. If this is correct then it defeats the purpose of sema.
Is this correcty understanding?
Now a real example from the demo code:
I am looking at the web server code using uiP stack for the ARM7_LPC2368_Eclipse port FreeRtos release ver 5.2
Looking at the ARM_LPC2368_Eclipse code for uIP Stack in uip_task.c
xEMACSemaphore semaphore - is declared in emac.c and it is declared as external in uIP_task.c
1. This semaphore is created twice by calling vSemaphoreCreateBinary( xEMACSemaphore ), once in uIP_task.c and once in emac.c
Is it OK to create it twice?
2. Once it is created, uip_task waits on this for new data to arrive with the following call in forever loop:
xSemaphoreTake( xEMACSemaphore, configTICK_RATE_HZ / 2 );
3. This semaphore is given in an ISR routine with the:
xSemaphoreGiveFromISR( xEMACSemaphore, &xHigherPriorityTaskWoken );
4. This sema is not given back by the uip_task anywhere.
when the uip_task goes into wait by xSemaphoreTake(…), it will always be available even if the interrupt routine has not indicated arrival of new data with the givefromISR call.
Does this mean that the system is always polling for the data as opposed to waiting for the sema and then looking for the data?
3. No where in the code I see the sema being released by the uip_task.