Sync. between task and isr / binary semaphore

jdpkam wrote on Thursday, September 29, 2011:


I have a question regarding synchronization between a task and an interrupt-routine using xSemaphoreTake and xSemaphoreGiveFromISR(…, …). My semaphore is created using the vSemaphoreCreateBinary() wrapper.

Data flow:

My task receives messages with data that are to be sent via. an uart. The uart write routine call the xSemaphoreTake(…,portMAX_DELAY) and triggers the TX interrupt to start sending data. Afterwards the task returns to “listening” for new messages (while the TX interrupt routine is active). After the last byte is sent in the interrupt context the  xSemaphoreGiveFromISR() routine is called.

The idea is, that if a new messages arrives to the task while we still are transmitting data, the task will be suspended until the xSemaphoreGiveFromISR() is called from the interrupt routine.

Unfortunately this seems to crash my system and I cannot figure out why. Have I misunderstood the properties of the binary semaphore? Is it not possible to call xSemaphoreTake() multiple times on the binary semaphore to “stall” a task (if the semaphore is already taken)?

Using FreeRTOS 6.0.0 on a NEC 78K0R 1167 @ 14,7 MHz using IAR 4.70.

Jonas, DK

rtel wrote on Friday, September 30, 2011:

If I interpret your email correctly, your UART task is doing something like this:

for( ;; )
____/* Listen for an incoming message. */

____/* A message was received.  Process it and generate a reply. */

____/* Wait until previous Tx has completed. */
____xSemaphoreTake( … );

___/* Send generated reply. */

Is that correct?

That should be ok, provided ListenForUARTMessage() can queue messages up, in case more than one message is received while the task is blocked on xSemaphoreTake().

Have you done the usual things like checking for stack overflows in the tasks?


jdpkam wrote on Wednesday, October 12, 2011:


As it turned out, it was my lazy programming, rather than an errored FreeRTOS, that was to blame for my problem. I actually re-programmed the UART hardware whilst transferring data (due to the new data transfer request) – this for some reason crashes the system. Back to reading the uC UserGuide I guess…

Jonas, DK

gridsense wrote on Friday, October 14, 2011:

I your Tx ISR; do you use “taskYIELD_FROM_ISR(xHigherPriorityTaskWoken)” after  xSemaphoreGiveFromISR(…, …) ?