4008-04-07 Release
[avr_bc100.git] / BaseMegaFirmware / GCC / uart.h
1 #ifndef UART_H\r
2 #define UART_H\r
3 /************************************************************************\r
4 Title:    Interrupt UART library with receive/transmit circular buffers\r
5 Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury\r
6 File:     $Id: uart.h,v 1.7.2.5 2005/08/14 11:25:41 Peter Exp $\r
7 Software: AVR-GCC 3.3\r
8 Hardware: any AVR with built-in UART, tested on AT90S8515 at 4 Mhz\r
9 Usage:    see Doxygen manual\r
10 ************************************************************************/\r
11 \r
12 /** \r
13  *  @defgroup pfleury_uart UART Library\r
14  *  @code #include <uart.h> @endcode\r
15  * \r
16  *  @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers. \r
17  *\r
18  *  This library can be used to transmit and receive data through the built in UART. \r
19  *\r
20  *  An interrupt is generated when the UART has finished transmitting or\r
21  *  receiving a byte. The interrupt handling routines use circular buffers\r
22  *  for buffering received and transmitted data.\r
23  *\r
24  *  The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define\r
25  *  the size of the circular buffers in bytes. Note that these constants must be a power of 2.\r
26  *  You may need to adapt this constants to your target and your application by adding \r
27  *  CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.\r
28  *\r
29  *  @note Based on Atmel Application Note AVR306\r
30  *  @author Peter Fleury pfleury@gmx.ch  http://jump.to/fleury\r
31  */\r
32  \r
33 /**@{*/\r
34 \r
35 \r
36 #if (__GNUC__ * 100 + __GNUC_MINOR__) < 304\r
37 #error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"\r
38 #endif\r
39 \r
40 \r
41 /*\r
42 ** constants and macros\r
43 */\r
44 \r
45 /** @brief  UART Baudrate Expression\r
46  *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz          \r
47  *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     \r
48  */\r
49 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)\r
50 \r
51 /** @brief  UART Baudrate Expression for ATmega double speed mode\r
52  *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz           \r
53  *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     \r
54  */\r
55 #define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)\r
56 \r
57 \r
58 /** Size of the circular receive buffer, must be power of 2 */\r
59 #ifndef UART_RX_BUFFER_SIZE\r
60 #define UART_RX_BUFFER_SIZE 32\r
61 #endif\r
62 /** Size of the circular transmit buffer, must be power of 2 */\r
63 #ifndef UART_TX_BUFFER_SIZE\r
64 #define UART_TX_BUFFER_SIZE 32\r
65 #endif\r
66 \r
67 /* test if the size of the circular buffers fits into SRAM */\r
68 #if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )\r
69 #error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"\r
70 #endif\r
71 \r
72 /* \r
73 ** high byte error return code of uart_getc()\r
74 */\r
75 #define UART_FRAME_ERROR      0x0800              /* Framing Error by UART       */\r
76 #define UART_OVERRUN_ERROR    0x0400              /* Overrun condition by UART   */\r
77 #define UART_BUFFER_OVERFLOW  0x0200              /* receive ringbuffer overflow */\r
78 #define UART_NO_DATA          0x0100              /* no receive data available   */\r
79 \r
80 \r
81 /*\r
82 ** function prototypes\r
83 */\r
84 \r
85 /**\r
86    @brief   Initialize UART and set baudrate \r
87    @param   baudrate Specify baudrate using macro UART_BAUD_SELECT()\r
88    @return  none\r
89 */\r
90 extern void uart_init(unsigned int baudrate);\r
91 \r
92 \r
93 /**\r
94  *  @brief   Get received byte from ringbuffer\r
95  *\r
96  * Returns in the lower byte the received character and in the \r
97  * higher byte the last receive error.\r
98  * UART_NO_DATA is returned when no data is available.\r
99  *\r
100  *  @param   void\r
101  *  @return  lower byte:  received byte from ringbuffer\r
102  *  @return  higher byte: last receive status\r
103  *           - \b 0 successfully received data from UART\r
104  *           - \b UART_NO_DATA           \r
105  *             <br>no receive data available\r
106  *           - \b UART_BUFFER_OVERFLOW   \r
107  *             <br>Receive ringbuffer overflow.\r
108  *             We are not reading the receive buffer fast enough, \r
109  *             one or more received character have been dropped \r
110  *           - \b UART_OVERRUN_ERROR     \r
111  *             <br>Overrun condition by UART.\r
112  *             A character already present in the UART UDR register was \r
113  *             not read by the interrupt handler before the next character arrived,\r
114  *             one or more received characters have been dropped.\r
115  *           - \b UART_FRAME_ERROR       \r
116  *             <br>Framing Error by UART\r
117  */\r
118 extern unsigned int uart_getc(void);\r
119 \r
120 \r
121 /**\r
122  *  @brief   Put byte to ringbuffer for transmitting via UART\r
123  *  @param   data byte to be transmitted\r
124  *  @return  none\r
125  */\r
126 extern void uart_putc(unsigned char data);\r
127 \r
128 \r
129 /**\r
130  *  @brief   Put string to ringbuffer for transmitting via UART\r
131  *\r
132  *  The string is buffered by the uart library in a circular buffer\r
133  *  and one character at a time is transmitted to the UART using interrupts.\r
134  *  Blocks if it can not write the whole string into the circular buffer.\r
135  * \r
136  *  @param   s string to be transmitted\r
137  *  @return  none\r
138  */\r
139 extern void uart_puts(const char *s );\r
140 \r
141 \r
142 /**\r
143  * @brief    Put string from program memory to ringbuffer for transmitting via UART.\r
144  *\r
145  * The string is buffered by the uart library in a circular buffer\r
146  * and one character at a time is transmitted to the UART using interrupts.\r
147  * Blocks if it can not write the whole string into the circular buffer.\r
148  *\r
149  * @param    s program memory string to be transmitted\r
150  * @return   none\r
151  * @see      uart_puts_P\r
152  */\r
153 extern void uart_puts_p(const char *s );\r
154 \r
155 /**\r
156  * @brief    Macro to automatically put a string constant into program memory\r
157  */\r
158 #define uart_puts_P(__s)       uart_puts_p(PSTR(__s))\r
159 \r
160 \r
161 \r
162 /** @brief  Initialize USART1 (only available on selected ATmegas) @see uart_init */\r
163 extern void uart1_init(unsigned int baudrate);\r
164 /** @brief  Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */\r
165 extern unsigned int uart1_getc(void);\r
166 /** @brief  Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */\r
167 extern void uart1_putc(unsigned char data);\r
168 /** @brief  Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */\r
169 extern void uart1_puts(const char *s );\r
170 /** @brief  Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */\r
171 extern void uart1_puts_p(const char *s );\r
172 /** @brief  Macro to automatically put a string constant into program memory */\r
173 #define uart1_puts_P(__s)       uart1_puts_p(PSTR(__s))\r
174 \r
175 /**@}*/\r
176 \r
177 #endif // UART_H \r
178 \r