Merge remote-tracking branch 'origin/master'
[cerebrum.git] / uart.h
blob08a2fd2c00228e82ca239e1572fd155f5a09fda1
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
60 ** constants and macros
63 /** @brief UART Baudrate Expression
64 * @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz
65 * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600
67 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
69 /** @brief UART Baudrate Expression for ATmega double speed mode
70 * @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz
71 * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600
73 #define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)
76 /** Size of the circular receive buffer, must be power of 2 */
77 #ifndef UART_RX_BUFFER_SIZE
78 #define UART_RX_BUFFER_SIZE 32
79 #endif
80 /** Size of the circular transmit buffer, must be power of 2 */
81 #ifndef UART_TX_BUFFER_SIZE
82 #define UART_TX_BUFFER_SIZE 32
83 #endif
85 /* test if the size of the circular buffers fits into SRAM */
86 #if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )
87 #error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"
88 #endif
90 /*
91 ** high byte error return code of uart_getc()
93 #define UART_FRAME_ERROR 0x0800 /* Framing Error by UART */
94 #define UART_OVERRUN_ERROR 0x0400 /* Overrun condition by UART */
95 #define UART_BUFFER_OVERFLOW 0x0200 /* receive ringbuffer overflow */
96 #define UART_NO_DATA 0x0100 /* no receive data available */
100 ** function prototypes
104 @brief Initialize UART and set baudrate
105 @param baudrate Specify baudrate using macro UART_BAUD_SELECT()
106 @return none
108 extern void uart_init(unsigned int baudrate);
112 * @brief Get received byte from ringbuffer
114 * Returns in the lower byte the received character and in the
115 * higher byte the last receive error.
116 * UART_NO_DATA is returned when no data is available.
118 * @param void
119 * @return lower byte: received byte from ringbuffer
120 * @return higher byte: last receive status
121 * - \b 0 successfully received data from UART
122 * - \b UART_NO_DATA
123 * <br>no receive data available
124 * - \b UART_BUFFER_OVERFLOW
125 * <br>Receive ringbuffer overflow.
126 * We are not reading the receive buffer fast enough,
127 * one or more received character have been dropped
128 * - \b UART_OVERRUN_ERROR
129 * <br>Overrun condition by UART.
130 * A character already present in the UART UDR register was
131 * not read by the interrupt handler before the next character arrived,
132 * one or more received characters have been dropped.
133 * - \b UART_FRAME_ERROR
134 * <br>Framing Error by UART
136 extern unsigned int uart_getc(void);
140 * @brief Put byte to ringbuffer for transmitting via UART
141 * @param data byte to be transmitted
142 * @return none
144 extern void uart_putc(unsigned char data);
148 * @brief Put string to ringbuffer for transmitting via UART
150 * The string is buffered by the uart library in a circular buffer
151 * and one character at a time is transmitted to the UART using interrupts.
152 * Blocks if it can not write the whole string into the circular buffer.
154 * @param s string to be transmitted
155 * @return none
157 extern void uart_puts(const char *s );
161 * @brief Put string from program memory to ringbuffer for transmitting via UART.
163 * The string is buffered by the uart library in a circular buffer
164 * and one character at a time is transmitted to the UART using interrupts.
165 * Blocks if it can not write the whole string into the circular buffer.
167 * @param s program memory string to be transmitted
168 * @return none
169 * @see uart_puts_P
171 extern void uart_puts_p(const char *s );
174 * @brief Macro to automatically put a string constant into program memory
176 #define uart_puts_P(__s) uart_puts_p(PSTR(__s))
180 /** @brief Initialize USART1 (only available on selected ATmegas) @see uart_init */
181 extern void uart1_init(unsigned int baudrate);
182 /** @brief Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */
183 extern unsigned int uart1_getc(void);
184 /** @brief Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */
185 extern void uart1_putc(unsigned char data);
186 /** @brief Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */
187 extern void uart1_puts(const char *s );
188 /** @brief Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */
189 extern void uart1_puts_p(const char *s );
190 /** @brief Macro to automatically put a string constant into program memory */
191 #define uart1_puts_P(__s) uart1_puts_p(PSTR(__s))
193 /**@}*/
196 #endif // UART_H