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

# UART Driver/Function Library for dual-UART processors (uart2.c)
[Drivers (for AVR peripherals)]

## Detailed Description

     #include "uart2.h"

Overview
  This is a UART driver for AVR-series processors with two hardware UARTs such as the mega161 and mega128. 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 UARTs, 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 UARTs (serial ports)
          uartSetBaudRate(0, 9600);   // set UART0 speed to 9600 baud
          uartSetBaudRate(1, 115200); // set UART1 speed to 115200 baud

          rprintfInit(uart0SendByte); // configure rprintf to use UART0 for output
          rprintf("Hello UART0\r\n"); // send "hello world" message via UART0

          rprintfInit(uart1SendByte); // configure rprintf to use UART1 for output
          rprintf("Hello UART1\r\n"); // send "hello world" message via UART1

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 | UART0_DEFAULT_BAUD_RATE 9600
| default baud rate for UART0
#define | UART1_DEFAULT_BAUD_RATE 9600
| default baud rate for UART1
#define | UART0_TX_BUFFER_SIZE 0x0010
| number of bytes for uart0 transmit buffer
#define | UART0_RX_BUFFER_SIZE 0x0080
| number of bytes for uart0 receive buffer
#define | UART1_TX_BUFFER_SIZE 0x0010
| number of bytes for uart1 transmit buffer
#define | UART1_RX_BUFFER_SIZE 0x0080
| number of bytes for uart1 receive buffer
#define | UART_INTERRUPT_HANDLER SIGNAL
#define | RXCIE RXCIE0
#define | TXCIE TXCIE0
#define | UDRIE UDRIE0
#define | RXEN RXEN0
#define | TXEN TXEN0
#define | CHR9 CHR90
#define | RXB8 RXB80
#define | TXB8 TXB80
#define | UBRR0L UBRR0
#define | UBRR1L UBRR1
## Functions
void | uartInit (void)
void | uart0Init (void)
| Initializes UART0 only.
void | uart1Init (void)
| Initializes UART1 only.
void | uart0InitBuffers (void)
void | uart1InitBuffers (void)
void | uartSetRxHandler (u08 nUart, void(*rx_func)(unsigned char c))
void | uartSetBaudRate (u08 nUart, u32 baudrate)
cBuffer * | uartGetRxBuffer (u08 nUart)
cBuffer * | uartGetTxBuffer (u08 nUart)
void | uartSendByte (u08 nUart, u08 data)
void | uart0SendByte (u08 data)
void | uart1SendByte (u08 data)
int | uart0GetByte (void)
int | uart1GetByte (void)
u08 | uartReceiveByte (u08 nUart, u08 *data)
u08 | uartReceiveBufferIsEmpty (u08 nUart)
void | uartFlushReceiveBuffer (u08 nUart)
void | uartAddToTxBuffer (u08 nUart, u08 data)
void | uart0AddToTxBuffer (u08 data)
void | uart1AddToTxBuffer (u08 data)
void | uartSendTxBuffer (u08 nUart)
u08 | uartSendBuffer (u08 nUart, char *buffer, u16 nBytes)
void | uartTransmitService (u08 nUart)
| interrupt service handlers
void | uartReceiveService (u08 nUart)

## Define Documentation

#define UART0_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 71 of file uart2.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 105 of file uart2.h.

## Function Documentation

void uart0AddToTxBuffer | ( | u08 | data | ) |

| AddToTxBuffer commands with the UART number hardcoded Use this with printfInit() - example: `printfInit(uart0AddToTxBuffer)`; Definition at line 248 of file uart2.c.

int uart0GetByte | ( | 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 191 of file uart2.c.

void uart0InitBuffers | ( | void |  | ) |

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

void uart0SendByte | ( | u08 | data | ) |

| SendByte commands with the UART number hardcoded Use these with printfInit() - example: `printfInit(uart0SendByte)`; Definition at line 179 of file uart2.c.

void uartAddToTxBuffer | ( | u08 | nUart,
|  | u08 | data
| ) |

| Add byte to end of uart Tx buffer. Definition at line 242 of file uart2.c.

void uartFlushReceiveBuffer | ( | u08 | nUart | ) |

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

cBuffer* uartGetRxBuffer | ( | u08 | nUart | ) |

| Returns pointer to the receive buffer structure. Definition at line 148 of file uart2.c.

cBuffer* uartGetTxBuffer | ( | u08 | nUart | ) |

| Returns pointer to the transmit buffer structure. Definition at line 154 of file uart2.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.

u08 uartReceiveBufferIsEmpty | ( | u08 | nUart | ) |

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

u08 uartReceiveByte | ( | u08 | nUart,
|  | 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(0, &myReceivedByte );
Definition at line 212 of file uart2.c.

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

| sends a buffer of length nBytes via the uart using interrupt control. Definition at line 266 of file uart2.c.

void uartSendByte | ( | u08 | nUart,
|  | u08 | data
| ) |

| Sends a single byte over the uart. Definition at line 160 of file uart2.c.

void uartSendTxBuffer | ( | u08 | nUart | ) |

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

void uartSetBaudRate | ( | u08 | nUart,
|  | u32 | baudrate
| ) |

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

void uartSetRxHandler | ( | u08 | nUart,
|  | void(*)(unsigned char c) | rx_func
| ) |

| Redirects received data to a user function. Definition at line 118 of file uart2.c.