Quality RTOS & Embedded Software

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


TaskWoken flag in xQueueReceiveFromISR()

Posted by Jeff Smith on December 1, 2005
I see the demo using two xTaskWokenBy... flags to track whether the serial interrupt handler has woken a thread for TX or RX separately. It then yields if either-or have woken a thread.

I wonder why I can't use just one flag? Will it mess up something if I send a true into xQueueReceiveFromISR() when it actually had only woken a task from xQueueSendFromISR() ?

This is the code I referr to. It is from serial.c in the CodeWarrior/HCS12 demo. Sorry about bad formatting.
__interrupt void vCOM0_ISR( void )
volatile unsigned portCHAR ucByte, ucStatus;
portBASE_TYPE xTaskWokenByPost = pdFALSE, xTaskWokenByTx = pdFALSE;

/* What caused the interrupt? */
ucStatus = SCI0SR1;

if( ucStatus & serOVERRUN_INTERRUPT )
/* The interrupt was caused by an overrun. Clear the error by reading
the data register. */
ucByte = SCI0DRL;

if( ucStatus & serRX_INTERRUPT )
/* The interrupt was caused by a character being received.
Read the received byte. */
ucByte = SCI0DRL;

/* Post the character onto the queue of received characters - noting
whether or not this wakes a task. */
xTaskWokenByPost = xQueueSendFromISR( xRxedChars, ( void * ) &ucByte, pdFALSE );

if( ( ucStatus & serTX_INTERRUPT ) && ( SCI0CR2_SCTIE ) )
/* The interrupt was caused by a character being transmitted. */
if( xQueueReceiveFromISR( xCharsForTx, ( void * ) &ucByte, &xTaskWokenByTx ) == pdTRUE )
/* Clear the SCRF bit. */
SCI0DRL = ucByte;
/* Disable transmit interrupt */

if( ( xTaskWokenByPost ) || ( xTaskWokenByTx ) )

RE: TaskWoken flag in xQueueReceiveFromISR()

Posted by Jeff Smith on December 1, 2005
Although I'm the one who asked the question, I think I can see the answer now (I still lack some understanding of the task management queues)

Looking in queue.c, if I used only one "xTaskWoken" variable for both Tx and Rx in the same ISR, it could mess things up.

For example, first I call `xTaskWoken = xQueueSendFromISR(xRxedChars, &ucByte, pdFALSE)` which would still work as expected. It sets xTaskWoken and wake a task waiting to receive from that queue.

Next I call `xQueueReceiveFromISR( xCharsForTx, &ucByte, &xTaskWoken )`. I looks like in there it wakes the next task that is waiting to send to this queue if it was full. That task would never be wake on this event because I passed in pdTRUE after waking the task from the irrelevant queue.

So I do need to keep track of Tx and Rx in separate flags. Let me know if I missed something, though.

RE: TaskWoken flag in xQueueReceiveFromISR()

Posted by Richard on December 2, 2005
This is exactly correct.

The xQueueReceiveFromISR() mechanism is such to ensure only one task is woken by the ISR. If xTaskWoken is initially passed in as false and a task is woken it gets automatically set to true. The next call (if there is one within the same interrupt which is possible in some drivers) it is passed in as true, preventing any other tasks being woken.

Using just a single variables means it could be incorrectly set to true on the first call.


RE: TaskWoken flag in xQueueReceiveFromISR()

Posted by Nobody/Anonymous on December 2, 2005
Because there can be two events in one ISR (i.e. Rx and Tx at the same time), then what happens when two tasks of the same priority were waiting, one for Tx and another for Rx? Which one will start first?

RE: TaskWoken flag in xQueueReceiveFromISR()

Posted by Nobody/Anonymous on December 2, 2005
As tasks are unblocked they are placed at the back of the queue of tasks that are ready to run at that priority. Therefore they will execute in the order they were unblocked. If you want one to be guaranteed to execute first then it requires a higher priority.

RE: TaskWoken flag in xQueueReceiveFromISR()

Posted by Jeff Smith on December 2, 2005
I hope that's correct, that the first one woken is the first one to executed.

In practice however, adding tasks is not working that way. When I add multiple tasks of the same priority, the *last* one added of highest priority runs first because pxCurrentTCB points to it when the scheduler starts. Is that intended?

RE: TaskWoken flag in xQueueReceiveFromISR()

Posted by Nobody/Anonymous on December 2, 2005
Before starting the scheduler it is another matter. The lines:

if( pxCurrentTCB->uxPriority <= uxPriority )
____pxCurrentTCB = ( volatile tskTCB * volatile ) pxNewTCB;

mean that prior to the scheduler being started - if a task has a priority greater than OR EQUAL to the currently highest priority task, then it will be the 'current' task when the scheduler is started.

Therefore if your highest priority is 3 - and you create two tasks of priority 3 - the task created second will be the task to execute first.

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

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

Latest News

FreeRTOS v10.2.0 is available for immediate download. MIT licensed, and including RISC-V and ARMv8-M (Cortex-M33) demos.

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

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

Cadence Tensilica Cortes

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