Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 29 | jimmy | 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 | File: not valid see "Extension" |
||
| 7 | ($Id: uart.h,v 1.7.2.1 2003/12/27 20:39:14 peter Exp $) |
||
| 8 | Software: AVR-GCC 3.3/3.4 |
||
| 9 | Hardware: any AVR with built-in UART, tested on AT90S8515 at 4 Mhz and |
||
| 10 | ATmega16 at 1.8, 3.6, 4 and 8 MHz |
||
| 11 | 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 | ************************************************************************/ |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @defgroup pfleury_uart UART Library |
||
| 18 | * @code #include <uart.h> @endcode |
||
| 19 | * |
||
| 20 | * @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers. |
||
| 21 | * |
||
| 22 | * This library can be used to transmit and receive data through the built in UART. |
||
| 23 | * |
||
| 24 | * An interrupt is generated when the UART has finished transmitting or |
||
| 25 | * receiving a byte. The interrupt handling routines use circular buffers |
||
| 26 | * for buffering received and transmitted data. |
||
| 27 | * |
||
| 28 | * The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define |
||
| 29 | * the size of the circular buffers in bytes. Note that these constants must be a power of 2. |
||
| 30 | * You may need to adapt this size to your target and your application. |
||
| 31 | * |
||
| 32 | * @note Based on Atmel Application Note AVR306 |
||
| 33 | * @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury |
||
| 34 | */ |
||
| 35 | |||
| 36 | /*@{*/ |
||
| 37 | |||
| 38 | #if (__GNUC__ * 100 + __GNUC_MINOR__) < 303 |
||
| 39 | #error "This library requires AVR-GCC 3.3 or later, update to newer AVR-GCC compiler !" |
||
| 40 | #endif |
||
| 41 | |||
| 42 | |||
| 43 | /* |
||
| 44 | ** constants and macros |
||
| 45 | */ |
||
| 46 | |||
| 47 | /** @brief UART Baudrate Expression |
||
| 48 | * @param xtalcpu system clock in Mhz |
||
| 49 | * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600 |
||
| 50 | */ |
||
| 51 | #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) |
||
| 52 | |||
| 53 | |||
| 54 | /** Size of the circular receive buffer, must be power of 2 */ |
||
| 55 | #define UART_RX_BUFFER_SIZE 32 |
||
| 56 | /** Size of the circular transmit buffer, must be power of 2 */ |
||
| 57 | #define UART_TX_BUFFER_SIZE 32 |
||
| 58 | |||
| 59 | |||
| 60 | #ifndef P |
||
| 61 | #define P(s) ({static const char c[] __attribute__ ((progmem)) = s;c;}) |
||
| 62 | #endif |
||
| 63 | |||
| 64 | |||
| 65 | /* |
||
| 66 | ** high byte error return code of uart_getc() |
||
| 67 | */ |
||
| 68 | #define UART_FRAME_ERROR 0x0800 /* Framing Error by UART */ |
||
| 69 | #define UART_OVERRUN_ERROR 0x0400 /* Overrun condition by UART */ |
||
| 70 | #define UART_BUFFER_OVERFLOW 0x0200 /* receive ringbuffer overflow */ |
||
| 71 | #define UART_NO_DATA 0x0100 /* no receive data available */ |
||
| 72 | |||
| 73 | |||
| 74 | /* |
||
| 75 | ** function prototypes |
||
| 76 | */ |
||
| 77 | |||
| 78 | /** |
||
| 79 | @brief Initialize UART and set baudrate |
||
| 80 | @param baudrate Specify baudrate using macro UART_BAUD_SELECT() |
||
| 81 | @return none |
||
| 82 | */ |
||
| 83 | extern void uart_init(unsigned int baudrate); |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @brief Get received byte from ringbuffer |
||
| 88 | * |
||
| 89 | * Returns in the lower byte the received character and in the |
||
| 90 | * higher byte the last receive error. |
||
| 91 | * UART_NO_DATA is returned when no data is available. |
||
| 92 | * |
||
| 93 | * @param void |
||
| 94 | * @return lower byte: received byte from ringbuffer |
||
| 95 | * @return higher byte: last receive status |
||
| 96 | * - \b 0 successfully received data from UART |
||
| 97 | * - \b UART_NO_DATA |
||
| 98 | * <br>no receive data available |
||
| 99 | * - \b UART_BUFFER_OVERFLOW |
||
| 100 | * <br>Receive ringbuffer overflow. |
||
| 101 | * We are not reading the receive buffer fast enough, |
||
| 102 | * one or more received character have been dropped |
||
| 103 | * - \b UART_OVERRUN_ERROR |
||
| 104 | * <br>Overrun condition by UART. |
||
| 105 | * A character already present in the UART UDR register was |
||
| 106 | * not read by the interrupt handler before the next character arrived, |
||
| 107 | * one or more received characters have been dropped. |
||
| 108 | * - \b UART_FRAME_ERROR |
||
| 109 | * <br>Framing Error by UART |
||
| 110 | */ |
||
| 111 | extern unsigned int uart_getc(void); |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @brief Put byte to ringbuffer for transmitting via UART |
||
| 116 | * @param data byte to be transmitted |
||
| 117 | * @return none |
||
| 118 | */ |
||
| 119 | extern void uart_putc(unsigned char data); |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @brief Put string to ringbuffer for transmitting via UART |
||
| 124 | * |
||
| 125 | * The string is buffered by the uart library in a circular buffer |
||
| 126 | * and one character at a time is transmitted to the UART using interrupts. |
||
| 127 | * Blocks if it can not write the whole string into the circular buffer. |
||
| 128 | * |
||
| 129 | * @param s string to be transmitted |
||
| 130 | * @return none |
||
| 131 | */ |
||
| 132 | extern void uart_puts(const char *s ); |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @brief Put string from program memory to ringbuffer for transmitting via UART. |
||
| 137 | * |
||
| 138 | * The string is buffered by the uart library in a circular buffer |
||
| 139 | * and one character at a time is transmitted to the UART using interrupts. |
||
| 140 | * Blocks if it can not write the whole string into the circular buffer. |
||
| 141 | * |
||
| 142 | * @param s program memory string to be transmitted |
||
| 143 | * @return none |
||
| 144 | * @see uart_puts_P |
||
| 145 | */ |
||
| 146 | extern void uart_puts_p(const char *s ); |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @brief Macro to automatically put a string constant into program memory |
||
| 150 | */ |
||
| 151 | #define uart_puts_P(__s) uart_puts_p(P(__s)) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @brief Put integer to ringbuffer for transmitting via UART. |
||
| 155 | * |
||
| 156 | * The integer is converted to a string which is buffered by the uart |
||
| 157 | * library in a circular buffer and one character at a time is transmitted |
||
| 158 | * to the UART using interrupts. |
||
| 159 | * |
||
| 160 | * @param value to transfer |
||
| 161 | * @return none |
||
| 162 | * @see uart_puts_p |
||
| 163 | */ |
||
| 164 | extern void uart_puti( int i ); |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @brief Put nibble as hex to ringbuffer for transmit via UART. |
||
| 168 | * |
||
| 169 | * The lower nibble of the parameter is convertet to correspondig |
||
| 170 | * hex-char and put in a circular buffer and one character at a time |
||
| 171 | * is transmitted to the UART using interrupts. |
||
| 172 | * |
||
| 173 | * @param value to transfer (byte, only lower nibble converted) |
||
| 174 | * @return none |
||
| 175 | * @see uart_putc |
||
| 176 | */ |
||
| 177 | extern void uart_puthex_nibble(const unsigned char b); |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @brief Put byte as hex to ringbuffer for transmit via UART. |
||
| 181 | * |
||
| 182 | * The upper and lower nibble of the parameter are convertet to |
||
| 183 | * correspondig hex-chars and put in a circular buffer and one |
||
| 184 | * character at a time is transmitted to the UART using interrupts. |
||
| 185 | * |
||
| 186 | * @param value to transfer |
||
| 187 | * @return none |
||
| 188 | * @see uart_puthex_nibble |
||
| 189 | */ |
||
| 190 | extern void uart_puthex_byte(const unsigned char b); |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @brief Put byte as bin to ringbuffer for transmit via UART. |
||
| 194 | * |
||
| 195 | * @param value to transfer |
||
| 196 | * @return none |
||
| 197 | * @see uart_putc |
||
| 198 | */ |
||
| 199 | extern void uart_putbin_byte(const unsigned char b); |
||
| 200 | |||
| 201 | /*@}*/ |
||
| 202 | #endif // UART_H |
||
| 203 |