Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Sync. between task and isr / binary semaphore

Posted by Jonas Poulsen on 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

RE: Sync. between task and isr / binary semaphore

Posted by Richard on 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?


RE: Sync. between task and isr / binary semaphore

Posted by Jonas Poulsen on 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

RE: Sync. between task and isr / binary semaphore

Posted by Samuel on October 14, 2011
I your Tx ISR; do you use "taskYIELD_FROM_ISR(xHigherPriorityTaskWoken)" after xSemaphoreGiveFromISR(…, …) ?

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner