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

Plz, help me to understand the serial example

Posted by francis wang on June 23, 2009

portENTER_CRITICAL();
......
/* 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;
}
....
portEXIT_CRITICAL();


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!

RE: Plz, help me to understand the serial example

Posted by Richard Damon on 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).

RE: Plz, help me to understand the serial example

Posted by francis wang on January 5, 2012
Hello!

portENTER_CRITICAL();
......
xReturn = xQueueSend( xTxChars_u1, &cOutChar, xBlockTime ); // A

if( lTHREEmpty1 == ( portLONG ) pdTRUE ) // B
{
xQueueReceive( xTxChars_u1, &cOutChar, serNO_BLOCK ); // C
lTHREEmpty1 = pdFALSE;
U1THR = cOutChar;
}
....
portEXIT_CRITICAL();

I am still not clear about this problem.
Q1:
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?

Q2:
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?

Q3:
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?

Thanks!

RE: Plz, help me to understand the serial example

Posted by Richard Damon on January 5, 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.


[ 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