Improved error handling during code generation
[cerebrum.git] / avr / uart.h
blob106541c5a16efc39da7cb023fcfafbf1a0daccd3
1 #ifndef UART_H
2 #define UART_H
3 /************************************************************************
4 Title: Interrupt UART library with receive/transmit circular buffers
5 Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
6 Fiddled around with by: jaseg
7 CAUTION! THIS FILE CURRENTLY ONLY WORKS WITH ATMega328p and similar devices because
8 only for these I was keen to change the signal names to proper interrupt vectors
9 File: $Id: uart.h,v 1.8.2.1 2007/07/01 11:14:38 peter Exp $
10 Software: AVR-GCC 4.1, AVR Libc 1.4
11 Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
12 License: GNU General Public License
13 Usage: see Doxygen manual
15 LICENSE:
16 Copyright (C) 2006 Peter Fleury
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 ************************************************************************/
30 /**
31 * @defgroup pfleury_uart UART Library
32 * @code #include <uart.h> @endcode
34 * @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers.
36 * This library can be used to transmit and receive data through the built in UART.
38 * An interrupt is generated when the UART has finished transmitting or
39 * receiving a byte. The interrupt handling routines use circular buffers
40 * for buffering received and transmitted data.
42 * The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define
43 * the size of the circular buffers in bytes. Note that these constants must be a power of 2.
44 * You may need to adapt this constants to your target and your application by adding
45 * CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.
47 * @note Based on Atmel Application Note AVR306
48 * @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury
51 /**@{*/
54 #if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
55 #error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
56 #endif
58 #include <avr/io.h>
62 ** constants and macros
65 /** @brief UART Baudrate Expression
66 * @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz
67 * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600
69 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
71 /** @brief UART Baudrate Expression for ATmega double speed mode
72 * @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz
73 * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600
75 #define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)
78 /** Size of the circular receive buffer, must be power of 2 */
79 #ifndef UART_RX_BUFFER_SIZE
80 #define UART_RX_BUFFER_SIZE 32
81 #endif
82 /** Size of the circular transmit buffer, must be power of 2 */
83 #ifndef UART_TX_BUFFER_SIZE
84 #define UART_TX_BUFFER_SIZE 32
85 #endif
87 /* test if the size of the circular buffers fits into SRAM */
88 #if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )
89 #error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"
90 #endif
92 /*
93 ** high byte error return code of uart_getc()
95 #define UART_FRAME_ERROR 0x0800 /* Framing Error by UART */
96 #define UART_OVERRUN_ERROR 0x0400 /* Overrun condition by UART */
97 #define UART_BUFFER_OVERFLOW 0x0200 /* receive ringbuffer overflow */
98 #define UART_NO_DATA 0x0100 /* no receive data available */
102 ** function prototypes
106 @brief Initialize UART and set baudrate
107 @param baudrate Specify baudrate using macro UART_BAUD_SELECT()
108 @return none
110 extern void uart_init(unsigned int baudrate);
114 * @brief Get received byte from ringbuffer
116 * Returns in the lower byte the received character and in the
117 * higher byte the last receive error.
118 * UART_NO_DATA is returned when no data is available.
120 * @param void
121 * @return lower byte: received byte from ringbuffer
122 * @return higher byte: last receive status
123 * - \b 0 successfully received data from UART
124 * - \b UART_NO_DATA
125 * <br>no receive data available
126 * - \b UART_BUFFER_OVERFLOW
127 * <br>Receive ringbuffer overflow.
128 * We are not reading the receive buffer fast enough,
129 * one or more received character have been dropped
130 * - \b UART_OVERRUN_ERROR
131 * <br>Overrun condition by UART.
132 * A character already present in the UART UDR register was
133 * not read by the interrupt handler before the next character arrived,
134 * one or more received characters have been dropped.
135 * - \b UART_FRAME_ERROR
136 * <br>Framing Error by UART
138 extern unsigned int uart_getc(void);
142 * @brief Put byte to ringbuffer for transmitting via UART
143 * @param data byte to be transmitted
144 * @return none
146 extern void uart_putc(unsigned char data);
148 extern void uart_putc_nonblocking(unsigned char data);
151 * @brief Put string to ringbuffer for transmitting via UART
153 * The string is buffered by the uart library in a circular buffer
154 * and one character at a time is transmitted to the UART using interrupts.
155 * Blocks if it can not write the whole string into the circular buffer.
157 * @param s string to be transmitted
158 * @return none
160 extern void uart_puts(const char *s );
164 * @brief Put string from program memory to ringbuffer for transmitting via UART.
166 * The string is buffered by the uart library in a circular buffer
167 * and one character at a time is transmitted to the UART using interrupts.
168 * Blocks if it can not write the whole string into the circular buffer.
170 * @param s program memory string to be transmitted
171 * @return none
172 * @see uart_puts_P
174 extern void uart_puts_p(const char *s );
177 * @brief Macro to automatically put a string constant into program memory
179 #define uart_puts_P(__s) uart_puts_p(PSTR(__s))
181 /**@}*/
184 #endif // UART_H