Problem using a minary semaphore

barbercolman wrote on Wednesday, August 18, 2010:

I have a serial communications task and a display task. The serial task creates a binary semaphore at task initialization
    vSemaphoreCreateBinary( xPacketReceivedSemaphore );
The receive interrupt gives the semaphore after a packet is received.
                        xSemaphoreGiveFromISR( xPacketReceivedSemaphore, &xHigherPriorityTaskWoken );
The serial task sends a new request for a packet of data after receiving a packet or one second without receiving a packet
        if( xSemaphoreTake( xPacketReceivedSemaphore, (1000 / portTICK_RATE_MS) ) == pdTRUE ){
This process works well as long as communications is started before the display task starts. If the display task before the first packet is received the serial task get to the xSemaphoretake and never returns.

Any idea what is happening? This processor is not starved. the vApplicationIdleHook is being called. I am using freeRtos 6.03 and a PIC32MX.

richard_damon wrote on Wednesday, August 18, 2010:

The first question is what does the display task have anything to do with the serial operation?

Second, the take you have shown will return after the second (unless you have corrupted something or the time has stopped). If a packet isn’t sent and the semaphore raised, it will return pdFALSE and not go into the statements controlled by the if, so that clause should only process the previous packet, and after the if, should be the statement to request the next one…

barbercolman wrote on Thursday, August 19, 2010:

It is a very good question. It should not, but it does.

When it works a new packet is sent as soon as the last packet is returned. If a packet is not responded to a new packet will be sent after the 1 second delay.

This does work but only when a packet has been sent and responded to before the graphics task has started. It does not make sense to me. I think my next step is to look in task.c and see why the communications task is not running.