00001 /*! \file uart2.h \brief Dual UART driver with buffer support. */ 00002 //***************************************************************************** 00003 // 00004 // File Name : 'uart2.h' 00005 // Title : Dual UART driver with buffer support 00006 // Author : Pascal Stang - Copyright (C) 2000-2002 00007 // Created : 11/20/2000 00008 // Revised : 07/04/2004 00009 // Version : 1.0 00010 // Target MCU : ATMEL AVR Series 00011 // Editor Tabs : 4 00012 // 00013 // This code is distributed under the GNU Public License 00014 // which can be found at http://www.gnu.org/licenses/gpl.txt 00015 // 00016 /// \ingroup driver_avr 00017 /// \defgroup uart2 UART Driver/Function Library for dual-UART processors (uart2.c) 00018 /// \code #include "uart2.h" \endcode 00019 /// \par Overview 00020 /// This is a UART driver for AVR-series processors with two hardware 00021 /// UARTs such as the mega161 and mega128. This library provides both 00022 /// buffered and unbuffered transmit and receive functions for the AVR 00023 /// processor UART. Buffered access means that the UART can transmit 00024 /// and receive data in the "background", while your code continues 00025 /// executing. Also included are functions to initialize the UARTs, 00026 /// set the baud rate, flush the buffers, and check buffer status. 00027 /// 00028 /// \note For full text output functionality, you may wish to use the rprintf 00029 /// functions along with this driver. 00030 /// 00031 /// \par About UART operations 00032 /// Most Atmel AVR-series processors contain one or more hardware UARTs 00033 /// (aka, serial ports). UART serial ports can communicate with other 00034 /// serial ports of the same type, like those used on PCs. In general, 00035 /// UARTs are used to communicate with devices that are RS-232 compatible 00036 /// (RS-232 is a certain kind of serial port). 00037 /// \par 00038 /// By far, the most common use for serial communications on AVR processors 00039 /// is for sending information and data to a PC running a terminal program. 00040 /// Here is an exmaple: 00041 /// \code 00042 /// uartInit(); // initialize UARTs (serial ports) 00043 /// uartSetBaudRate(0, 9600); // set UART0 speed to 9600 baud 00044 /// uartSetBaudRate(1, 115200); // set UART1 speed to 115200 baud 00045 /// 00046 /// rprintfInit(uart0SendByte); // configure rprintf to use UART0 for output 00047 /// rprintf("Hello UART0\r\n"); // send "hello world" message via UART0 00048 /// 00049 /// rprintfInit(uart1SendByte); // configure rprintf to use UART1 for output 00050 /// rprintf("Hello UART1\r\n"); // send "hello world" message via UART1 00051 /// \endcode 00052 /// 00053 /// \warning The CPU frequency (F_CPU) must be set correctly in \c global.h 00054 /// for the UART library to calculate correct baud rates. Furthermore, 00055 /// certain CPU frequencies will not produce exact baud rates due to 00056 /// integer frequency division round-off. See your AVR processor's 00057 /// datasheet for full details. 00058 // 00059 //***************************************************************************** 00060 //@{ 00061 00062 #ifndef UART2_H 00063 #define UART2_H 00064 00065 #include "global.h" 00066 #include "buffer.h" 00067 00068 //! Default uart baud rate. 00069 /// This is the default speed after a uartInit() command, 00070 /// and can be changed by using uartSetBaudRate(). 00071 #define UART0_DEFAULT_BAUD_RATE 9600 ///< default baud rate for UART0 00072 #define UART1_DEFAULT_BAUD_RATE 9600 ///< default baud rate for UART1 00073 00074 // buffer memory allocation defines 00075 // buffer sizes 00076 #ifndef UART0_TX_BUFFER_SIZE 00077 #define UART0_TX_BUFFER_SIZE 0x0010 ///< number of bytes for uart0 transmit buffer 00078 #endif 00079 #ifndef UART0_RX_BUFFER_SIZE 00080 #define UART0_RX_BUFFER_SIZE 0x0080 ///< number of bytes for uart0 receive buffer 00081 #endif 00082 #ifndef UART1_TX_BUFFER_SIZE 00083 #define UART1_TX_BUFFER_SIZE 0x0010 ///< number of bytes for uart1 transmit buffer 00084 #endif 00085 #ifndef UART1_RX_BUFFER_SIZE 00086 #define UART1_RX_BUFFER_SIZE 0x0080 ///< number of bytes for uart1 receive buffer 00087 #endif 00088 00089 // define this key if you wish to use 00090 // external RAM for the UART buffers 00091 //#define UART_BUFFER_EXTERNAL_RAM 00092 #ifdef UART_BUFFER_EXTERNAL_RAM 00093 // absolute address of uart0 buffers 00094 #define UART0_TX_BUFFER_ADDR 0x1000 00095 #define UART0_RX_BUFFER_ADDR 0x1100 00096 // absolute address of uart1 buffers 00097 #define UART1_TX_BUFFER_ADDR 0x1200 00098 #define UART1_RX_BUFFER_ADDR 0x1300 00099 #endif 00100 00101 //! Type of interrupt handler to use for uart interrupts. 00102 /// Value may be SIGNAL or INTERRUPT. 00103 /// \warning Do not change unless you know what you're doing. 00104 #ifndef UART_INTERRUPT_HANDLER 00105 #define UART_INTERRUPT_HANDLER SIGNAL 00106 #endif 00107 00108 // compatibility for the mega161 00109 #ifndef RXCIE 00110 #define RXCIE RXCIE0 00111 #define TXCIE TXCIE0 00112 #define UDRIE UDRIE0 00113 #define RXEN RXEN0 00114 #define TXEN TXEN0 00115 #define CHR9 CHR90 00116 #define RXB8 RXB80 00117 #define TXB8 TXB80 00118 #endif 00119 #ifndef UBRR0L 00120 #define UBRR0L UBRR0 00121 #define UBRR1L UBRR1 00122 #endif 00123 00124 // functions 00125 00126 //! Initializes UARTs. 00127 /// \note After running this init function, the processor 00128 /// I/O pins that used for uart communications (RXD, TXD) 00129 /// are no long available for general purpose I/O. 00130 void uartInit(void); 00131 00132 //! Initializes UART0 only. 00133 void uart0Init(void); 00134 00135 //! Initializes UART1 only. 00136 void uart1Init(void); 00137 00138 //! Initializes transmit and receive buffers. 00139 /// Automatically called from uartInit() 00140 void uart0InitBuffers(void); 00141 void uart1InitBuffers(void); 00142 00143 //! Redirects received data to a user function. 00144 /// 00145 void uartSetRxHandler(u08 nUart, void (*rx_func)(unsigned char c)); 00146 00147 //! Sets the uart baud rate. 00148 /// Argument should be in bits-per-second, like \c uartSetBaudRate(9600); 00149 void uartSetBaudRate(u08 nUart, u32 baudrate); 00150 00151 //! Returns pointer to the receive buffer structure. 00152 /// 00153 cBuffer* uartGetRxBuffer(u08 nUart); 00154 00155 //! Returns pointer to the transmit buffer structure. 00156 /// 00157 cBuffer* uartGetTxBuffer(u08 nUart); 00158 00159 //! Sends a single byte over the uart. 00160 /// 00161 void uartSendByte(u08 nUart, u08 data); 00162 00163 //! SendByte commands with the UART number hardcoded 00164 /// Use these with printfInit() - example: \c printfInit(uart0SendByte); 00165 void uart0SendByte(u08 data); 00166 void uart1SendByte(u08 data); 00167 00168 //! Gets a single byte from the uart receive buffer. 00169 /// Returns the byte, or -1 if no byte is available (getchar-style). 00170 int uart0GetByte(void); 00171 int uart1GetByte(void); 00172 00173 //! Gets a single byte from the uart receive buffer. 00174 /// Function returns TRUE if data was available, FALSE if not. 00175 /// Actual data is returned in variable pointed to by "data". 00176 /// Example usage: 00177 /// \code 00178 /// char myReceivedByte; 00179 /// uartReceiveByte(0, &myReceivedByte ); 00180 /// \endcode 00181 u08 uartReceiveByte(u08 nUart, u08* data); 00182 00183 //! Returns TRUE/FALSE if receive buffer is empty/not-empty. 00184 /// 00185 u08 uartReceiveBufferIsEmpty(u08 nUart); 00186 00187 //! Flushes (deletes) all data from receive buffer. 00188 /// 00189 void uartFlushReceiveBuffer(u08 nUart); 00190 00191 //! Add byte to end of uart Tx buffer. 00192 /// 00193 void uartAddToTxBuffer(u08 nUart, u08 data); 00194 00195 //! AddToTxBuffer commands with the UART number hardcoded 00196 /// Use this with printfInit() - example: \c printfInit(uart0AddToTxBuffer); 00197 void uart0AddToTxBuffer(u08 data); 00198 void uart1AddToTxBuffer(u08 data); 00199 00200 //! Begins transmission of the transmit buffer under interrupt control. 00201 /// 00202 void uartSendTxBuffer(u08 nUart); 00203 00204 //! sends a buffer of length nBytes via the uart using interrupt control. 00205 /// 00206 u08 uartSendBuffer(u08 nUart, char *buffer, u16 nBytes); 00207 00208 //! interrupt service handlers 00209 void uartTransmitService(u08 nUart); 00210 void uartReceiveService(u08 nUart); 00211 00212 #endif 00213