TaskWoken flag in xQueueReceiveFromISR()

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();     } }

TaskWoken flag in xQueueReceiveFromISR()

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.

TaskWoken flag in xQueueReceiveFromISR()

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.

TaskWoken flag in xQueueReceiveFromISR()

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?

TaskWoken flag in xQueueReceiveFromISR()

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.

TaskWoken flag in xQueueReceiveFromISR()

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?

TaskWoken flag in xQueueReceiveFromISR()

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.