avr-uart
AVR UART Library
uart.h
1 #ifndef UART_H
2 #define UART_H
3 
4 /************************************************************************
5 Title: Interrupt UART library with receive/transmit circular buffers
6 Author: Andy Gock
7 Software: AVR-GCC 4.1, AVR Libc 1.4
8 Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
9 License: GNU General Public License
10 Usage: see README.md and Doxygen manual
11 
12 Based on original library by Peter Fluery, Tim Sharpe, Nicholas Zambetti.
13 
14 https://github.com/andygock/avr-uart
15 
16 LICENSE:
17 
18  Copyright (C) 2012 Andy Gock
19  Copyright (C) 2006 Peter Fleury
20 
21  This program is free software; you can redistribute it and/or modify
22  it under the terms of the GNU General Public License as published by
23  the Free Software Foundation; either version 2 of the License, or
24  any later version.
25 
26  This program is distributed in the hope that it will be useful,
27  but WITHOUT ANY WARRANTY; without even the implied warranty of
28  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29  GNU General Public License for more details.
30 
31 ************************************************************************/
32 
33 /************************************************************************
34 uart_available, uart_flush, uart1_available, and uart1_flush functions
35 were adapted from the Arduino HardwareSerial.h library by Tim Sharpe on
36 11 Jan 2009. The license info for HardwareSerial.h is as follows:
37 
38  HardwareSerial.h - Hardware serial library for Wiring
39  Copyright (c) 2006 Nicholas Zambetti. All right reserved.
40 
41  This library is free software; you can redistribute it and/or
42  modify it under the terms of the GNU Lesser General Public
43  License as published by the Free Software Foundation; either
44  version 2.1 of the License, or (at your option) any later version.
45 
46  This library is distributed in the hope that it will be useful,
47  but WITHOUT ANY WARRANTY; without even the implied warranty of
48  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49  Lesser General Public License for more details.
50 
51  You should have received a copy of the GNU Lesser General Public
52  License along with this library; if not, write to the Free Software
53  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
54 ************************************************************************/
55 
81 #include <stdint.h>
82 #include <avr/io.h>
83 
84 #if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
85 #error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
86 #endif
87 
88 /*
89  * constants and macros
90  */
91 
92 /* Enable USART 1, 2, 3 as required */
93 /* Can be defined in compiler symbol setup with -D option (preferred) */
94 #ifndef USART0_ENABLED
95  #define USART0_ENABLED
96 #endif
97 //#define USART1_ENABLED
98 //#define USART2_ENABLED
99 //#define USART3_ENABLED
100 
101 /* Set size of receive and transmit buffers */
102 
103 #ifndef UART_RX0_BUFFER_SIZE
104  #define UART_RX0_BUFFER_SIZE 128
105 #endif
106 #ifndef UART_RX1_BUFFER_SIZE
107  #define UART_RX1_BUFFER_SIZE 128
108 #endif
109 #ifndef UART_RX2_BUFFER_SIZE
110  #define UART_RX2_BUFFER_SIZE 128
111 #endif
112 #ifndef UART_RX3_BUFFER_SIZE
113  #define UART_RX3_BUFFER_SIZE 128
114 #endif
115 
116 #ifndef UART_TX0_BUFFER_SIZE
117  #define UART_TX0_BUFFER_SIZE 128
118 #endif
119 #ifndef UART_TX1_BUFFER_SIZE
120  #define UART_TX1_BUFFER_SIZE 128
121 #endif
122 #ifndef UART_TX2_BUFFER_SIZE
123  #define UART_TX2_BUFFER_SIZE 128
124 #endif
125 #ifndef UART_TX3_BUFFER_SIZE
126  #define UART_TX3_BUFFER_SIZE 128
127 #endif
128 
129 /* Check buffer sizes are not too large for 8-bit positioning */
130 
131 #if (UART_RX0_BUFFER_SIZE > 256 & !defined(USART0_LARGE_BUFFER))
132  #error "Buffer too large, please use -DUSART0_LARGE_BUFFER switch in compiler options"
133 #endif
134 
135 #if (UART_RX1_BUFFER_SIZE > 256 & !defined(USART1_LARGE_BUFFER))
136  #error "Buffer too large, please use -DUSART1_LARGE_BUFFER switch in compiler options"
137 #endif
138 
139 #if (UART_RX2_BUFFER_SIZE > 256 & !defined(USART2_LARGE_BUFFER))
140  #error "Buffer too large, please use -DUSART2_LARGE_BUFFER switch in compiler options"
141 #endif
142 
143 #if (UART_RX3_BUFFER_SIZE > 256 & !defined(USART3_LARGE_BUFFER))
144  #error "Buffer too large, please use -DUSART3_LARGE_BUFFER switch in compiler options"
145 #endif
146 
147 /* Check buffer sizes are not too large for *_LARGE_BUFFER operation (16-bit positioning) */
148 
149 #if (UART_RX0_BUFFER_SIZE > 32768)
150  #error "Buffer too large, maximum allowed is 32768 bytes"
151 #endif
152 
153 #if (UART_RX1_BUFFER_SIZE > 32768)
154  #error "Buffer too large, maximum allowed is 32768 bytes"
155 #endif
156 
157 #if (UART_RX2_BUFFER_SIZE > 32768)
158  #error "Buffer too large, maximum allowed is 32768 bytes"
159 #endif
160 
161 #if (UART_RX3_BUFFER_SIZE > 32768)
162  #error "Buffer too large, maximum allowed is 32768 bytes"
163 #endif
164 
169 #define UART_BAUD_SELECT(baudRate,xtalCpu) (((xtalCpu)+8UL*(baudRate))/(16UL*(baudRate))-1UL)
170 
175 #define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) ((((xtalCpu)+4UL*(baudRate))/(8UL*(baudRate))-1)|0x8000)
176 
177 /* test if the size of the circular buffers fits into SRAM */
178 
179 #if defined(USART0_ENABLED) && ( (UART_RX0_BUFFER_SIZE+UART_TX0_BUFFER_SIZE) >= (RAMEND-0x60))
180  #error "size of UART_RX0_BUFFER_SIZE + UART_TX0_BUFFER_SIZE larger than size of SRAM"
181 #endif
182 
183 #if defined(USART1_ENABLED) && ( (UART_RX1_BUFFER_SIZE+UART_TX1_BUFFER_SIZE) >= (RAMEND-0x60))
184  #error "size of UART_RX1_BUFFER_SIZE + UART_TX1_BUFFER_SIZE larger than size of SRAM"
185 #endif
186 
187 #if defined(USART2_ENABLED) && ( (UART_RX2_BUFFER_SIZE+UART_RX2_BUFFER_SIZE) >= (RAMEND-0x60))
188  #error "size of UART_RX2_BUFFER_SIZE + UART_TX2_BUFFER_SIZE larger than size of SRAM"
189 #endif
190 
191 #if defined(USART3_ENABLED) && ( (UART_RX3_BUFFER_SIZE+UART_RX3_BUFFER_SIZE) >= (RAMEND-0x60))
192  #error "size of UART_RX3_BUFFER_SIZE + UART_TX3_BUFFER_SIZE larger than size of SRAM"
193 #endif
194 
195 /*
196 ** high byte error return code of uart_getc()
197 */
198 #define UART_FRAME_ERROR 0x0800
199 #define UART_OVERRUN_ERROR 0x0400
200 #define UART_BUFFER_OVERFLOW 0x0200
201 #define UART_NO_DATA 0x0100
203 /* Macros, to allow use of legacy names */
204 
206 #define uart_init(b) uart0_init(b)
207 
209 #define uart_getc() uart0_getc()
210 
212 #define uart_peek() uart0_peek()
213 
215 #define uart_putc(d) uart0_putc(d)
216 
218 #define uart_puts(s) uart0_puts(s)
219 
221 #define uart_puts_p(s) uart0_puts_p(s)
222 
224 #define uart_available() uart0_available()
225 
227 #define uart_flush() uart0_flush()
228 
229 /*
230 ** function prototypes
231 */
232 
238 extern void uart0_init(uint16_t baudrate);
239 
240 
265 extern uint16_t uart0_getc(void);
266 
293 extern uint16_t uart0_peek(void);
294 
300 extern void uart0_putc(uint8_t data);
301 
302 
313 extern void uart0_puts(const char *s);
314 
315 
327 extern void uart0_puts_p(const char *s);
328 
333 #define uart_puts_P(__s) uart0_puts_p(PSTR(__s))
334 
336 #define uart0_puts_P(__s) uart0_puts_p(PSTR(__s))
337 
342 extern uint16_t uart0_available(void);
343 
347 extern void uart0_flush(void);
348 
349 
351 extern void uart1_init(uint16_t baudrate);
352 
354 extern uint16_t uart1_getc(void);
355 
357 extern uint16_t uart1_peek(void);
358 
360 extern void uart1_putc(uint8_t data);
361 
363 extern void uart1_puts(const char *s);
364 
366 extern void uart1_puts_p(const char *s);
367 
369 #define uart1_puts_P(__s) uart1_puts_p(PSTR(__s))
370 
372 extern uint16_t uart1_available(void);
373 
375 extern void uart1_flush(void);
376 
377 
379 extern void uart2_init(uint16_t baudrate);
380 
382 extern uint16_t uart2_getc(void);
383 
385 extern uint16_t uart2_peek(void);
386 
388 extern void uart2_putc(uint8_t data);
389 
391 extern void uart2_puts(const char *s);
392 
394 extern void uart2_puts_p(const char *s);
395 
397 #define uart2_puts_P(__s) uart2_puts_p(PSTR(__s))
398 
400 extern uint16_t uart2_available(void);
401 
403 extern void uart2_flush(void);
404 
405 
407 extern void uart3_init(uint16_t baudrate);
408 
410 extern uint16_t uart3_getc(void);
411 
413 extern uint16_t uart3_peek(void);
414 
416 extern void uart3_putc(uint8_t data);
417 
419 extern void uart3_puts(const char *s);
420 
422 extern void uart3_puts_p(const char *s);
423 
425 #define uart3_puts_P(__s) uart3_puts_p(PSTR(__s))
426 
428 extern uint16_t uart3_available(void);
429 
431 extern void uart3_flush(void);
432 
435 #endif // UART_H
436 
uint16_t uart0_getc(void)
Get received byte from ringbuffer.
uint16_t uart3_getc(void)
Get received byte of USART3 from ringbuffer. (only available on selected ATmega)
void uart3_init(uint16_t baudrate)
Initialize USART3 (only available on selected ATmegas)
void uart0_puts_p(const char *s)
Put string from program memory to ringbuffer for transmitting via UART.
uint16_t uart2_getc(void)
Get received byte of USART2 from ringbuffer. (only available on selected ATmega)
void uart1_putc(uint8_t data)
Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) ...
void uart1_puts(const char *s)
Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) ...
uint16_t uart2_peek(void)
Peek at next byte in USART2 ringbuffer.
void uart3_puts(const char *s)
Put string to ringbuffer for transmitting via USART3 (only available on selected ATmega) ...
void uart3_puts_p(const char *s)
Put string from program memory to ringbuffer for transmitting via USART3 (only available on selected ...
void uart1_init(uint16_t baudrate)
Initialize USART1 (only available on selected ATmegas)
void uart2_flush(void)
Flush bytes waiting in receive buffer of USART2.
void uart2_init(uint16_t baudrate)
Initialize USART2 (only available on selected ATmegas)
void uart3_putc(uint8_t data)
Put byte to ringbuffer for transmitting via USART3 (only available on selected ATmega) ...
uint16_t uart3_available(void)
Return number of bytes waiting in the receive buffer of USART3.
uint16_t uart2_available(void)
Return number of bytes waiting in the receive buffer of USART2.
void uart0_init(uint16_t baudrate)
Initialize UART and set baudrate.
void uart0_puts(const char *s)
Put string to ringbuffer for transmitting via UART.
uint16_t uart0_peek(void)
Peek at next byte in ringbuffer.
void uart2_puts(const char *s)
Put string to ringbuffer for transmitting via USART2 (only available on selected ATmega) ...
void uart1_flush(void)
Flush bytes waiting in receive buffer of USART1.
void uart3_flush(void)
Flush bytes waiting in receive buffer of USART3.
uint16_t uart0_available(void)
Return number of bytes waiting in the receive buffer.
void uart1_puts_p(const char *s)
Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ...
uint16_t uart1_peek(void)
Peek at next byte in USART1 ringbuffer.
uint16_t uart3_peek(void)
Peek at next byte in USART3 ringbuffer.
uint16_t uart1_getc(void)
Get received byte of USART1 from ringbuffer. (only available on selected ATmega)
void uart0_flush(void)
Flush bytes waiting in receive buffer.
void uart2_puts_p(const char *s)
Put string from program memory to ringbuffer for transmitting via USART2 (only available on selected ...
void uart2_putc(uint8_t data)
Put byte to ringbuffer for transmitting via USART2 (only available on selected ATmega) ...
uint16_t uart1_available(void)
Return number of bytes waiting in the receive buffer of USART1.
void uart0_putc(uint8_t data)
Put byte to ringbuffer for transmitting via UART.