1269 |
kakl |
1 |
/*! \file uart2.h \brief Dual UART driver with buffer support. */ |
|
|
2 |
//***************************************************************************** |
|
|
3 |
// |
|
|
4 |
// File Name : 'uart2.h' |
|
|
5 |
// Title : Dual UART driver with buffer support |
|
|
6 |
// Author : Pascal Stang - Copyright (C) 2000-2002 |
|
|
7 |
// Created : 11/20/2000 |
|
|
8 |
// Revised : 07/04/2004 |
|
|
9 |
// Version : 1.0 |
|
|
10 |
// Target MCU : ATMEL AVR Series |
|
|
11 |
// Editor Tabs : 4 |
|
|
12 |
// |
|
|
13 |
// This code is distributed under the GNU Public License |
|
|
14 |
// which can be found at http://www.gnu.org/licenses/gpl.txt |
|
|
15 |
// |
|
|
16 |
/// \ingroup driver_avr |
|
|
17 |
/// \defgroup uart2 UART Driver/Function Library for dual-UART processors (uart2.c) |
|
|
18 |
/// \code #include "uart2.h" \endcode |
|
|
19 |
/// \par Overview |
|
|
20 |
/// This is a UART driver for AVR-series processors with two hardware |
|
|
21 |
/// UARTs such as the mega161 and mega128. This library provides both |
|
|
22 |
/// buffered and unbuffered transmit and receive functions for the AVR |
|
|
23 |
/// processor UART. Buffered access means that the UART can transmit |
|
|
24 |
/// and receive data in the "background", while your code continues |
|
|
25 |
/// executing. Also included are functions to initialize the UARTs, |
|
|
26 |
/// set the baud rate, flush the buffers, and check buffer status. |
|
|
27 |
/// |
|
|
28 |
/// \note For full text output functionality, you may wish to use the rprintf |
|
|
29 |
/// functions along with this driver. |
|
|
30 |
/// |
|
|
31 |
/// \par About UART operations |
|
|
32 |
/// Most Atmel AVR-series processors contain one or more hardware UARTs |
|
|
33 |
/// (aka, serial ports). UART serial ports can communicate with other |
|
|
34 |
/// serial ports of the same type, like those used on PCs. In general, |
|
|
35 |
/// UARTs are used to communicate with devices that are RS-232 compatible |
|
|
36 |
/// (RS-232 is a certain kind of serial port). |
|
|
37 |
/// \par |
|
|
38 |
/// By far, the most common use for serial communications on AVR processors |
|
|
39 |
/// is for sending information and data to a PC running a terminal program. |
|
|
40 |
/// Here is an exmaple: |
|
|
41 |
/// \code |
|
|
42 |
/// uartInit(); // initialize UARTs (serial ports) |
|
|
43 |
/// uartSetBaudRate(0, 9600); // set UART0 speed to 9600 baud |
|
|
44 |
/// uartSetBaudRate(1, 115200); // set UART1 speed to 115200 baud |
|
|
45 |
/// |
|
|
46 |
/// rprintfInit(uart0SendByte); // configure rprintf to use UART0 for output |
|
|
47 |
/// rprintf("Hello UART0\r\n"); // send "hello world" message via UART0 |
|
|
48 |
/// |
|
|
49 |
/// rprintfInit(uart1SendByte); // configure rprintf to use UART1 for output |
|
|
50 |
/// rprintf("Hello UART1\r\n"); // send "hello world" message via UART1 |
|
|
51 |
/// \endcode |
|
|
52 |
/// |
|
|
53 |
/// \warning The CPU frequency (F_CPU) must be set correctly in \c global.h |
|
|
54 |
/// for the UART library to calculate correct baud rates. Furthermore, |
|
|
55 |
/// certain CPU frequencies will not produce exact baud rates due to |
|
|
56 |
/// integer frequency division round-off. See your AVR processor's |
|
|
57 |
/// datasheet for full details. |
|
|
58 |
// |
|
|
59 |
//***************************************************************************** |
|
|
60 |
//@{ |
|
|
61 |
|
|
|
62 |
#ifndef UART2_H |
|
|
63 |
#define UART2_H |
|
|
64 |
|
|
|
65 |
#include "global.h" |
|
|
66 |
#include "buffer.h" |
|
|
67 |
|
|
|
68 |
//! Default uart baud rate. |
|
|
69 |
/// This is the default speed after a uartInit() command, |
|
|
70 |
/// and can be changed by using uartSetBaudRate(). |
|
|
71 |
#define UART0_DEFAULT_BAUD_RATE 9600 ///< default baud rate for UART0 |
|
|
72 |
#define UART1_DEFAULT_BAUD_RATE 9600 ///< default baud rate for UART1 |
|
|
73 |
|
|
|
74 |
// buffer memory allocation defines |
|
|
75 |
// buffer sizes |
|
|
76 |
#ifndef UART0_TX_BUFFER_SIZE |
|
|
77 |
#define UART0_TX_BUFFER_SIZE 0x0010 ///< number of bytes for uart0 transmit buffer |
|
|
78 |
#endif |
|
|
79 |
#ifndef UART0_RX_BUFFER_SIZE |
|
|
80 |
#define UART0_RX_BUFFER_SIZE 0x0080 ///< number of bytes for uart0 receive buffer |
|
|
81 |
#endif |
|
|
82 |
#ifndef UART1_TX_BUFFER_SIZE |
|
|
83 |
#define UART1_TX_BUFFER_SIZE 0x0010 ///< number of bytes for uart1 transmit buffer |
|
|
84 |
#endif |
|
|
85 |
#ifndef UART1_RX_BUFFER_SIZE |
|
|
86 |
#define UART1_RX_BUFFER_SIZE 0x0080 ///< number of bytes for uart1 receive buffer |
|
|
87 |
#endif |
|
|
88 |
|
|
|
89 |
// define this key if you wish to use |
|
|
90 |
// external RAM for the UART buffers |
|
|
91 |
//#define UART_BUFFER_EXTERNAL_RAM |
|
|
92 |
#ifdef UART_BUFFER_EXTERNAL_RAM |
|
|
93 |
// absolute address of uart0 buffers |
|
|
94 |
#define UART0_TX_BUFFER_ADDR 0x1000 |
|
|
95 |
#define UART0_RX_BUFFER_ADDR 0x1100 |
|
|
96 |
// absolute address of uart1 buffers |
|
|
97 |
#define UART1_TX_BUFFER_ADDR 0x1200 |
|
|
98 |
#define UART1_RX_BUFFER_ADDR 0x1300 |
|
|
99 |
#endif |
|
|
100 |
|
|
|
101 |
//! Type of interrupt handler to use for uart interrupts. |
|
|
102 |
/// Value may be SIGNAL or INTERRUPT. |
|
|
103 |
/// \warning Do not change unless you know what you're doing. |
|
|
104 |
#ifndef UART_INTERRUPT_HANDLER |
|
|
105 |
#define UART_INTERRUPT_HANDLER SIGNAL |
|
|
106 |
#endif |
|
|
107 |
|
|
|
108 |
// compatibility for the mega161 |
|
|
109 |
#ifndef RXCIE |
|
|
110 |
#define RXCIE RXCIE0 |
|
|
111 |
#define TXCIE TXCIE0 |
|
|
112 |
#define UDRIE UDRIE0 |
|
|
113 |
#define RXEN RXEN0 |
|
|
114 |
#define TXEN TXEN0 |
|
|
115 |
#define CHR9 CHR90 |
|
|
116 |
#define RXB8 RXB80 |
|
|
117 |
#define TXB8 TXB80 |
|
|
118 |
#endif |
|
|
119 |
#ifndef UBRR0L |
|
|
120 |
#define UBRR0L UBRR0 |
|
|
121 |
#define UBRR1L UBRR1 |
|
|
122 |
#endif |
|
|
123 |
|
|
|
124 |
// functions |
|
|
125 |
|
|
|
126 |
//! Initializes UARTs. |
|
|
127 |
/// \note After running this init function, the processor |
|
|
128 |
/// I/O pins that used for uart communications (RXD, TXD) |
|
|
129 |
/// are no long available for general purpose I/O. |
|
|
130 |
void uartInit(void); |
|
|
131 |
|
|
|
132 |
//! Initializes UART0 only. |
|
|
133 |
void uart0Init(void); |
|
|
134 |
|
|
|
135 |
//! Initializes UART1 only. |
|
|
136 |
void uart1Init(void); |
|
|
137 |
|
|
|
138 |
//! Initializes transmit and receive buffers. |
|
|
139 |
/// Automatically called from uartInit() |
|
|
140 |
void uart0InitBuffers(void); |
|
|
141 |
void uart1InitBuffers(void); |
|
|
142 |
|
|
|
143 |
//! Redirects received data to a user function. |
|
|
144 |
/// |
|
|
145 |
void uartSetRxHandler(u08 nUart, void (*rx_func)(unsigned char c)); |
|
|
146 |
|
|
|
147 |
//! Sets the uart baud rate. |
|
|
148 |
/// Argument should be in bits-per-second, like \c uartSetBaudRate(9600); |
|
|
149 |
void uartSetBaudRate(u08 nUart, u32 baudrate); |
|
|
150 |
|
|
|
151 |
//! Returns pointer to the receive buffer structure. |
|
|
152 |
/// |
|
|
153 |
cBuffer* uartGetRxBuffer(u08 nUart); |
|
|
154 |
|
|
|
155 |
//! Returns pointer to the transmit buffer structure. |
|
|
156 |
/// |
|
|
157 |
cBuffer* uartGetTxBuffer(u08 nUart); |
|
|
158 |
|
|
|
159 |
//! Sends a single byte over the uart. |
|
|
160 |
/// |
|
|
161 |
void uartSendByte(u08 nUart, u08 data); |
|
|
162 |
|
|
|
163 |
//! SendByte commands with the UART number hardcoded |
|
|
164 |
/// Use these with printfInit() - example: \c printfInit(uart0SendByte); |
|
|
165 |
void uart0SendByte(u08 data); |
|
|
166 |
void uart1SendByte(u08 data); |
|
|
167 |
|
|
|
168 |
//! Gets a single byte from the uart receive buffer. |
|
|
169 |
/// Returns the byte, or -1 if no byte is available (getchar-style). |
|
|
170 |
int uart0GetByte(void); |
|
|
171 |
int uart1GetByte(void); |
|
|
172 |
|
|
|
173 |
//! Gets a single byte from the uart receive buffer. |
|
|
174 |
/// Function returns TRUE if data was available, FALSE if not. |
|
|
175 |
/// Actual data is returned in variable pointed to by "data". |
|
|
176 |
/// Example usage: |
|
|
177 |
/// \code |
|
|
178 |
/// char myReceivedByte; |
|
|
179 |
/// uartReceiveByte(0, &myReceivedByte ); |
|
|
180 |
/// \endcode |
|
|
181 |
u08 uartReceiveByte(u08 nUart, u08* data); |
|
|
182 |
|
|
|
183 |
//! Returns TRUE/FALSE if receive buffer is empty/not-empty. |
|
|
184 |
/// |
|
|
185 |
u08 uartReceiveBufferIsEmpty(u08 nUart); |
|
|
186 |
|
|
|
187 |
//! Flushes (deletes) all data from receive buffer. |
|
|
188 |
/// |
|
|
189 |
void uartFlushReceiveBuffer(u08 nUart); |
|
|
190 |
|
|
|
191 |
//! Add byte to end of uart Tx buffer. |
|
|
192 |
/// |
|
|
193 |
void uartAddToTxBuffer(u08 nUart, u08 data); |
|
|
194 |
|
|
|
195 |
//! AddToTxBuffer commands with the UART number hardcoded |
|
|
196 |
/// Use this with printfInit() - example: \c printfInit(uart0AddToTxBuffer); |
|
|
197 |
void uart0AddToTxBuffer(u08 data); |
|
|
198 |
void uart1AddToTxBuffer(u08 data); |
|
|
199 |
|
|
|
200 |
//! Begins transmission of the transmit buffer under interrupt control. |
|
|
201 |
/// |
|
|
202 |
void uartSendTxBuffer(u08 nUart); |
|
|
203 |
|
|
|
204 |
//! sends a buffer of length nBytes via the uart using interrupt control. |
|
|
205 |
/// |
|
|
206 |
u08 uartSendBuffer(u08 nUart, char *buffer, u16 nBytes); |
|
|
207 |
|
|
|
208 |
//! interrupt service handlers |
|
|
209 |
void uartTransmitService(u08 nUart); |
|
|
210 |
void uartReceiveService(u08 nUart); |
|
|
211 |
|
|
|
212 |
#endif |
|
|
213 |
|