wfrancis wrote on Tuesday, June 23, 2009:
/* We cannot write directly to the UART, so queue the character.
Block for a maximum of xBlockTime if there is no space in the
queue. It is ok to block within a critical section as each
task has it’s own critical section management. */
xReturn = xQueueSend( xTxChars_u1, &cOutChar, xBlockTime );
/* Depending on queue sizing and task prioritisation: While we
were blocked waiting to post interrupts were not disabled. It is
possible that the serial ISR has emptied the Tx queue, in which
case we need to start the Tx off again. */
if( lTHREEmpty1 == ( portLONG ) pdTRUE )
xQueueReceive( xTxChars_u1, &cOutChar, serNO_BLOCK );
lTHREEmpty1 = pdFALSE;
U1THR = cOutChar;
when posted a char to the uart , we think the uart is busy.
then we post the next char to a queue, then the task blocks, from a critical section?
isn’t the isr been disabled now?then how could the uart continue to transmit till the lTHREEmpty becomes true again?could it be safe if we set xBlockTime to 0?
Need know some detail about this, thank you!
richard_damon wrote on Tuesday, June 23, 2009:
The comments around the call to xQueueSend try to explain it. If a task blocks (like in xQueueSend if the queue is full), then even though we are in a critical section, when the schedule switches to another task, it will re-enable the interrupts. When the task does resume the interrupts will be disabled again (this is what is meant by "each task has its own critical section management"). So in a critical section, no interrupts (at least those that interact with the kernel) or other tasks run. If the task in the critical section blocks with a call to the FreeRTOS kernel, then until that task is started back up, in effect the critical section is paused (since the critical section is only for while that task runs).
wfrancis wrote on Thursday, January 05, 2012:
xReturn = xQueueSend( xTxChars_u1, &cOutChar, xBlockTime ); // A
if( lTHREEmpty1 == ( portLONG ) pdTRUE ) // B
xQueueReceive( xTxChars_u1, &cOutChar, serNO_BLOCK ); // C
lTHREEmpty1 = pdFALSE;
U1THR = cOutChar;
I am still not clear about this problem.
When we block at the queue, it will switch to other task, which may enable the interrupt.
And when the queue unblocks, we will return. Is the interrupt still enabled?
Can the serial continue to send chars with this interrupt or we need to resend a char to make the interrupt send a char?
If the THRE is not empty, Will the call to xSerialPutChar stop the serial from sending a char, because of entering and exiting critical section?
Could it be possible that after we send a char to the queue(at A), the interrupt empty the queue and make THRE empty, and so at B we will go into,
then at C when we wants to receive the char from the queue, it is already emptied by the interrupt?
What is the case that the code will go into B?
richard_damon wrote on Thursday, January 05, 2012:
Yes. When the task blocks, it will switch to another task which WILL reenable the interrupts (unless that task was also inside of a critical section). When we restart, interrupts will again be disabled. Enabling and disabling interrupts will itself normally not cause an issues with serial transmission. If the transmitter finishes sending a character will interrupts are disabled, the transmitter may temporarily stop (depending if it has a fifo buffer) for lack of data to send, but once interrupts are re-enabled, the transmitter interrupt will occur and the interrupt routine will send the next character.
For Q3, remember that the purpose of the critical section is to make sure no interrupt can be processed inside of it. Now, while the critical section is suspended while we are dismissed if the queue is full, the critical section will restart once the task is switched back too, which will be before the Send actually puts the character into the queue, so it is not possible for an interrupt to occur between the actual pushing of the character in A and the test in B or moving on to C, so the Receive will be certain to get a character.
I am not familiar with this example or even what processor it was for. I know that when I write serial drivers like this, I do NOT like to use a critical section like this, but instead, after pushing the character into the queue, check if the serial buffer has room for more characters, and if so, trigger the hardware to generate a serial transmitter interrupt, so the serial ISR will pull a character out of the queue and send it to the buffer (this isn’t always an option though, not all processors are as nice about having the software forcing hardware interrupts). The big advantage of this is that I don’t need to worry about a critical section which might impact other devices too, or worrying about edge cases in timing. At worse, I generate the interrupt when there turns out to not be room in the buffer or more characters in the queue, but these are naturally checked for in the ISR.