Rev 142 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 142 | Rev 149 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | #ifndef UART_H |
1 | #ifndef UART_H |
| 2 | #define UART_H |
2 | #define UART_H |
| 3 | /************************************************************************ |
3 | /************************************************************************ |
| 4 | Title: Interrupt UART library with receive/transmit circular buffers |
4 | Title: Interrupt UART library with receive/transmit circular buffers |
| 5 | Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury |
5 | Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury |
| 6 | File: not valid see "Extension" |
- | |
| 7 | |
6 | File: $Id: uart.h,v 1.7.2.5 2005/08/14 11:25:41 Peter Exp $ |
| 8 | Software: |
7 | Software: AVR-GCC 3.3 |
| 9 | Hardware: |
8 | Hardware: any AVR with built-in UART, tested on AT90S8515 at 4 Mhz |
| 10 | ATmega16 at 1.8, 3.6, 4 and 8 MHz |
- | |
| 11 | Usage: see Doxygen manual |
9 | Usage: see Doxygen manual |
| 12 | Extensions:uart_puti, uart_puthex_nibble, uart_puthex_byte, |
- | |
| 13 | uart_putbin_byte by M.Thomas 9/2004 and 1/2005 |
- | |
| 14 | ************************************************************************/ |
10 | ************************************************************************/ |
| 15 | - | ||
| 16 | #include <uart_cfg.h> |
- | |
| 17 | 11 | ||
| 18 | /** |
12 | /** |
| 19 | * @defgroup pfleury_uart UART Library |
13 | * @defgroup pfleury_uart UART Library |
| 20 | * @code #include <uart.h> @endcode |
14 | * @code #include <uart.h> @endcode |
| 21 | * |
15 | * |
| Line 27... | Line 21... | ||
| 27 | * receiving a byte. The interrupt handling routines use circular buffers |
21 | * receiving a byte. The interrupt handling routines use circular buffers |
| 28 | * for buffering received and transmitted data. |
22 | * for buffering received and transmitted data. |
| 29 | * |
23 | * |
| 30 | * The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define |
24 | * The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define |
| 31 | * the size of the circular buffers in bytes. Note that these constants must be a power of 2. |
25 | * the size of the circular buffers in bytes. Note that these constants must be a power of 2. |
| 32 | * You may need to adapt this |
26 | * You may need to adapt this constants to your target and your application by adding |
| - | 27 | * CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile. |
|
| 33 | * |
28 | * |
| 34 | * @note Based on Atmel Application Note AVR306 |
29 | * @note Based on Atmel Application Note AVR306 |
| 35 | * @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury |
30 | * @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury |
| 36 | */ |
31 | */ |
| 37 | 32 | ||
| 38 | /*@{*/ |
33 | /**@{*/ |
| 39 | 34 | ||
| - | 35 | ||
| 40 | #if (__GNUC__ * 100 + __GNUC_MINOR__) < |
36 | #if (__GNUC__ * 100 + __GNUC_MINOR__) < 304 |
| 41 | #error "This library requires AVR-GCC 3. |
37 | #error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !" |
| 42 | #endif |
38 | #endif |
| 43 | 39 | ||
| 44 | 40 | ||
| 45 | /* |
41 | /* |
| 46 | ** constants and macros |
42 | ** constants and macros |
| 47 | */ |
43 | */ |
| 48 | 44 | ||
| 49 | /** @brief UART Baudrate Expression |
45 | /** @brief UART Baudrate Expression |
| 50 | * @param xtalcpu system clock in |
46 | * @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz |
| 51 | * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600 |
47 | * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600 |
| 52 | */ |
48 | */ |
| 53 | #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) |
49 | #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) |
| 54 | 50 | ||
| - | 51 | /** @brief UART Baudrate Expression for ATmega double speed mode |
|
| - | 52 | * @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz |
|
| - | 53 | * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600 |
|
| - | 54 | */ |
|
| - | 55 | #define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000) |
|
| - | 56 | ||
| - | 57 | ||
| - | 58 | /** Size of the circular receive buffer, must be power of 2 */ |
|
| - | 59 | #ifndef UART_RX_BUFFER_SIZE |
|
| - | 60 | #define UART_RX_BUFFER_SIZE 32 |
|
| - | 61 | #endif |
|
| - | 62 | /** Size of the circular transmit buffer, must be power of 2 */ |
|
| - | 63 | #ifndef UART_TX_BUFFER_SIZE |
|
| - | 64 | #define UART_TX_BUFFER_SIZE 32 |
|
| - | 65 | #endif |
|
| 55 | 66 | ||
| 56 |
|
67 | /* test if the size of the circular buffers fits into SRAM */ |
| 57 | |
68 | #if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) ) |
| - | 69 | //#error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM" |
|
| 58 | #endif |
70 | #endif |
| 59 | - | ||
| 60 | 71 | ||
| 61 | /* |
72 | /* |
| 62 | ** high byte error return code of uart_getc() |
73 | ** high byte error return code of uart_getc() |
| 63 | */ |
74 | */ |
| 64 | #define UART_FRAME_ERROR 0x0800 /* Framing Error by UART */ |
75 | #define UART_FRAME_ERROR 0x0800 /* Framing Error by UART */ |
| 65 | #define UART_OVERRUN_ERROR 0x0400 /* Overrun condition by UART */ |
76 | #define UART_OVERRUN_ERROR 0x0400 /* Overrun condition by UART */ |
| 66 | #define UART_BUFFER_OVERFLOW 0x0200 /* receive ringbuffer overflow */ |
77 | #define UART_BUFFER_OVERFLOW 0x0200 /* receive ringbuffer overflow */ |
| 67 | #define UART_NO_DATA 0x0100 /* no receive data available */ |
78 | #define UART_NO_DATA 0x0100 /* no receive data available */ |
| 68 | 79 | ||
| 69 | 80 | ||
| - | 81 | /* |
|
| - | 82 | ** function prototypes |
|
| - | 83 | */ |
|
| 70 | 84 | ||
| - | 85 | /** |
|
| - | 86 | @brief Initialize UART and set baudrate |
|
| - | 87 | @param baudrate Specify baudrate using macro UART_BAUD_SELECT() |
|
| - | 88 | @return none |
|
| - | 89 | */ |
|
| 71 |
|
90 | extern void uart_init(unsigned int baudrate); |
| 72 | 91 | ||
| 73 | 92 | ||
| 74 | /** |
93 | /** |
| 75 | * @brief Get received byte from ringbuffer |
94 | * @brief Get received byte from ringbuffer |
| 76 | * |
95 | * |
| Line 134... | Line 153... | ||
| 134 | extern void uart_puts_p(const char *s ); |
153 | extern void uart_puts_p(const char *s ); |
| 135 | 154 | ||
| 136 | /** |
155 | /** |
| 137 | * @brief Macro to automatically put a string constant into program memory |
156 | * @brief Macro to automatically put a string constant into program memory |
| 138 | */ |
157 | */ |
| 139 | #define uart_puts_P(__s) uart_puts_p( |
158 | #define uart_puts_P(__s) uart_puts_p(PSTR(__s)) |
| - | 159 | ||
| 140 | 160 | ||
| 141 | /** |
- | |
| 142 | * @brief Put integer to ringbuffer for transmitting via UART. |
- | |
| 143 | * |
- | |
| 144 | * The integer is converted to a string which is buffered by the uart |
- | |
| 145 | * library in a circular buffer and one character at a time is transmitted |
- | |
| 146 | * to the UART using interrupts. |
- | |
| 147 | * |
- | |
| 148 | * @param value to transfer |
- | |
| 149 | * @return none |
- | |
| 150 | * @see uart_puts_p |
- | |
| 151 | */ |
- | |
| 152 | extern void uart_puti( int i ); |
- | |
| 153 | - | ||
| 154 | /** |
- | |
| 155 | * @brief Put nibble as hex to ringbuffer for transmit via UART. |
- | |
| 156 | * |
- | |
| 157 | * The lower nibble of the parameter is convertet to correspondig |
- | |
| 158 | * hex-char and put in a circular buffer and one character at a time |
- | |
| 159 | * is transmitted to the UART using interrupts. |
- | |
| 160 | * |
- | |
| 161 | * @param value to transfer (byte, only lower nibble converted) |
- | |
| 162 | * @return none |
- | |
| 163 | * @see uart_putc |
- | |
| 164 | */ |
- | |
| 165 | extern void uart_puthex_nibble(const unsigned char b); |
- | |
| 166 | 161 | ||
| 167 | /** |
- | |
| - | 162 | /** @brief Initialize USART1 (only available on selected ATmegas) @see uart_init */ |
|
| - | 163 | extern void uart1_init(unsigned int baudrate); |
|
| 168 | |
164 | /** @brief Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */ |
| 169 | * |
- | |
| 170 |
|
165 | extern unsigned int uart1_getc(void); |
| - | 166 | /** @brief Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */ |
|
| 171 |
|
167 | extern void uart1_putc(unsigned char data); |
| 172 | |
168 | /** @brief Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */ |
| 173 | * |
- | |
| 174 |
|
169 | extern void uart1_puts(const char *s ); |
| - | 170 | /** @brief Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */ |
|
| 175 |
|
171 | extern void uart1_puts_p(const char *s ); |
| - | 172 | /** @brief Macro to automatically put a string constant into program memory */ |
|
| 176 |
|
173 | #define uart1_puts_P(__s) uart1_puts_p(PSTR(__s)) |
| - | 174 | ||
| 177 | |
175 | /**@}*/ |
| 178 | extern void uart_puthex_byte(const unsigned char b); |
- | |
| 179 | 176 | ||
| 180 | /** |
- | |
| 181 | * @brief Put byte as bin to ringbuffer for transmit via UART. |
- | |
| 182 | * |
- | |
| 183 | * @param value to transfer |
- | |
| 184 | * @return none |
- | |
| 185 | * @see uart_putc |
- | |
| 186 | */ |
- | |
| 187 | extern void uart_putbin_byte(const unsigned char b); |
- | |
| 188 | 177 | ||
| 189 | /*@}*/ |
- | |
| 190 | #endif // UART_H |
178 | #endif // UART_H |
| 191 | 179 | ||