Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# UART Driver/Function Library (uart.c)
[Drivers (for AVR peripherals)]

## Detailed Description

     #include "uart.h"

Overview
  This library provides both buffered and unbuffered transmit and receive functions for the AVR processor UART. Buffered access means that the UART can transmit and receive data in the "background", while your code continues executing. Also included are functions to initialize the UART, set the baud rate, flush the buffers, and check buffer status.

Note:
  For full text output functionality, you may wish to use the rprintf functions along with this driver.

About UART operations
  Most Atmel AVR-series processors contain one or more hardware UARTs (aka, serial ports). UART serial ports can communicate with other serial ports of the same type, like those used on PCs. In general, UARTs are used to communicate with devices that are RS-232 compatible (RS-232 is a certain kind of serial port).

  By far, the most common use for serial communications on AVR processors is for sending information and data to a PC running a terminal program. Here is an exmaple:

          uartInit();                 // initialize UART (serial port)
          uartSetBaudRate(9600);      // set UART speed to 9600 baud
          rprintfInit(uartSendByte);  // configure rprintf to use UART for output
          rprintf("Hello World\r\n"); // send "hello world" message via serial port

Warning:
  The CPU frequency (F_CPU) must be set correctly in `global.h` for the UART library to calculate correct baud rates. Furthermore, certain CPU frequencies will not produce exact baud rates due to integer frequency division round-off. See your AVR processor's datasheet for full details.

## Defines
#define | UART_DEFAULT_BAUD_RATE 9600
#define | UART_TX_BUFFER_SIZE 0x0040
#define | UART_RX_BUFFER_SIZE 0x0040
#define | UART_INTERRUPT_HANDLER SIGNAL
## Functions
void | uartInit (void)
void | uartInitBuffers (void)
void | uartSetRxHandler (void(*rx_func)(unsigned char c))
void | uartSetBaudRate (u32 baudrate)
cBuffer * | uartGetRxBuffer (void)
cBuffer * | uartGetTxBuffer (void)
void | uartSendByte (u08 data)
int | uartGetByte (void)
u08 | uartReceiveByte (u08 *data)
u08 | uartReceiveBufferIsEmpty (void)
void | uartFlushReceiveBuffer (void)
u08 | uartAddToTxBuffer (u08 data)
void | uartSendTxBuffer (void)
u08 | uartSendBuffer (char *buffer, u16 nBytes)

## Define Documentation

#define UART_DEFAULT_BAUD_RATE 9600

| Default uart baud rate. This is the default speed after a uartInit() command, and can be changed by using uartSetBaudRate(). Definition at line 64 of file uart.h.

#define UART_INTERRUPT_HANDLER SIGNAL

| Type of interrupt handler to use for uart interrupts. Value may be SIGNAL or INTERRUPT. Warning: Do not change unless you know what you're doing. Definition at line 94 of file uart.h.

#define UART_RX_BUFFER_SIZE 0x0040

| Number of bytes for uart receive buffer. Do not change this value in uart.h, but rather override it with the desired value defined in your project's global.h Definition at line 78 of file uart.h.

#define UART_TX_BUFFER_SIZE 0x0040

| Number of bytes for uart transmit buffer. Do not change this value in uart.h, but rather override it with the desired value defined in your project's global.h Definition at line 72 of file uart.h.

## Function Documentation

u08 uartAddToTxBuffer | ( | u08 | data | ) |

| Add byte to end of uart Tx buffer. Returns TRUE if successful, FALSE if failed (no room left in buffer). Definition at line 183 of file uart.c.

void uartFlushReceiveBuffer | ( | void |  | ) |

| Flushes (deletes) all data from receive buffer. Definition at line 161 of file uart.c.

int uartGetByte | ( | void |  | ) |

| Gets a single byte from the uart receive buffer. Returns the byte, or -1 if no byte is available (getchar-style). Definition at line 125 of file uart.c.

cBuffer* uartGetRxBuffer | ( | void |  | ) |

| Returns pointer to the receive buffer structure. Definition at line 100 of file uart.c.

cBuffer* uartGetTxBuffer | ( | void |  | ) |

| Returns pointer to the transmit buffer structure. Definition at line 107 of file uart.c.

void uartInit | ( | void |  | ) |

| Initializes uart. Note: After running this init function, the processor I/O pins that used for uart communications (RXD, TXD) are no long available for general purpose I/O. Definition at line 44 of file uart.c.

void uartInitBuffers | ( | void |  | ) |

| Initializes transmit and receive buffers. Automatically called from uartInit() Definition at line 66 of file uart.c.

u08 uartReceiveBufferIsEmpty | ( | void |  | ) |

| Returns TRUE/FALSE if receive buffer is empty/not-empty. Definition at line 170 of file uart.c.

u08 uartReceiveByte | ( | u08 * | data | ) |

| Gets a single byte from the uart receive buffer. Function returns TRUE if data was available, FALSE if not. Actual data is returned in variable pointed to by "data". Example usage:
        char myReceivedByte;
        uartReceiveByte( &myReceivedByte );
Definition at line 135 of file uart.c.

u08 uartSendBuffer | ( | char * | buffer,
|  | u16 | nBytes
| ) |

  | Sends a block of data via the uart using interrupt control. Parameters:
  | buffer | pointer to data to be sent
  | nBytes | length of data (number of bytes to sent)

void uartSendByte | ( | u08 | data | ) |

| Sends a single byte over the uart. Note: This function waits for the uart to be ready, therefore, consecutive calls to uartSendByte() will go only as fast as the data can be sent over the serial port. Definition at line 114 of file uart.c.

void uartSendTxBuffer | ( | void |  | ) |

| Begins transmission of the transmit buffer under interrupt control. Definition at line 190 of file uart.c.

void uartSetBaudRate | ( | u32 | baudrate | ) |

| Sets the uart baud rate. Argument should be in bits-per-second, like `uartSetBaudRate(9600)`; Definition at line 89 of file uart.c.

void uartSetRxHandler | ( | void(*)(unsigned char c) | rx_func | ) |

| Redirects received data to a user function. Definition at line 82 of file uart.c.