Software flow control

From InCircuit
Jump to: navigation, search

Flow-control in ICradio

The AVR based ICradio Modules support to signal that the buffer is nearly full by transmitting special chars to the device using software flow control. If the buffer reaches a high level, a XOFF-Character (0x13) is send over the uart. If the buffer is (nearly) empty again, a XON (0x11) is send.

Implementing flow control in a microcontroller environment

Enabling a Microcontroller-application to understand software-floatcontrol is quiet easy. While receiving data from the ICradio the stream has to be parsed for the corresponding characters. Using a dedicated flag the current state can then be set or cleared. By interpreting this flag in the sending routine, data-loss can be prevented.

The following C-code snippets will illustrate this and may be used directly in code used in conjunction with an AVR-Device. The flag will be defined as a global variable -- as it will be used from interrupt and non-interrupt context it should be a volatile type.

 static volatile uint8_t uart_send_allowed = 1;

Within the UART-Receive interrupt the following code will parse for the control-characters and set the flag accordingly. It is assumed that the last character received was saved in the locale variable data

 uint8_t data;
 ...
 switch(data) {
   case 0x11: uart_send_allowed = 1; break
   case 0x13: uart_send_allowed = 0; break;
   default:
   /* Do further data-parsing here */
 }

In the routine used to send a character, the following code will delay the execution until the flag is cleared

 ... uart_send_char(...) {
   while(!uart_send_allowed);
   /* Send the character out */
   ...
 }
Personal tools