Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

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;
}
else
{
/* Disable transmit interrupt */
SCI0CR2_SCTIE = 0;
}
}

if( ( xTaskWokenByPost ) || ( xTaskWokenByTx ) )
{
portYIELD();
}
}

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.

Regards.

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 ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists