4008-04-07 Release
[avr_bc100.git] / BaseMegaFirmware / GCC / uart.h
diff --git a/BaseMegaFirmware/GCC/uart.h b/BaseMegaFirmware/GCC/uart.h
new file mode 100644 (file)
index 0000000..ca9c460
--- /dev/null
@@ -0,0 +1,178 @@
+#ifndef UART_H\r
+#define UART_H\r
+/************************************************************************\r
+Title:    Interrupt UART library with receive/transmit circular buffers\r
+Author:   Peter Fleury <pfleury@gmx.ch>   http://jump.to/fleury\r
+File:     $Id: uart.h,v 1.7.2.5 2005/08/14 11:25:41 Peter Exp $\r
+Software: AVR-GCC 3.3\r
+Hardware: any AVR with built-in UART, tested on AT90S8515 at 4 Mhz\r
+Usage:    see Doxygen manual\r
+************************************************************************/\r
+\r
+/** \r
+ *  @defgroup pfleury_uart UART Library\r
+ *  @code #include <uart.h> @endcode\r
+ * \r
+ *  @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers. \r
+ *\r
+ *  This library can be used to transmit and receive data through the built in UART. \r
+ *\r
+ *  An interrupt is generated when the UART has finished transmitting or\r
+ *  receiving a byte. The interrupt handling routines use circular buffers\r
+ *  for buffering received and transmitted data.\r
+ *\r
+ *  The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define\r
+ *  the size of the circular buffers in bytes. Note that these constants must be a power of 2.\r
+ *  You may need to adapt this constants to your target and your application by adding \r
+ *  CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.\r
+ *\r
+ *  @note Based on Atmel Application Note AVR306\r
+ *  @author Peter Fleury pfleury@gmx.ch  http://jump.to/fleury\r
+ */\r
\r
+/**@{*/\r
+\r
+\r
+#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304\r
+#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"\r
+#endif\r
+\r
+\r
+/*\r
+** constants and macros\r
+*/\r
+\r
+/** @brief  UART Baudrate Expression\r
+ *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz          \r
+ *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     \r
+ */\r
+#define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)\r
+\r
+/** @brief  UART Baudrate Expression for ATmega double speed mode\r
+ *  @param  xtalcpu  system clock in Mhz, e.g. 4000000L for 4Mhz           \r
+ *  @param  baudrate baudrate in bps, e.g. 1200, 2400, 9600     \r
+ */\r
+#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)\r
+\r
+\r
+/** Size of the circular receive buffer, must be power of 2 */\r
+#ifndef UART_RX_BUFFER_SIZE\r
+#define UART_RX_BUFFER_SIZE 32\r
+#endif\r
+/** Size of the circular transmit buffer, must be power of 2 */\r
+#ifndef UART_TX_BUFFER_SIZE\r
+#define UART_TX_BUFFER_SIZE 32\r
+#endif\r
+\r
+/* test if the size of the circular buffers fits into SRAM */\r
+#if ( (UART_RX_BUFFER_SIZE+UART_TX_BUFFER_SIZE) >= (RAMEND-0x60 ) )\r
+#error "size of UART_RX_BUFFER_SIZE + UART_TX_BUFFER_SIZE larger than size of SRAM"\r
+#endif\r
+\r
+/* \r
+** high byte error return code of uart_getc()\r
+*/\r
+#define UART_FRAME_ERROR      0x0800              /* Framing Error by UART       */\r
+#define UART_OVERRUN_ERROR    0x0400              /* Overrun condition by UART   */\r
+#define UART_BUFFER_OVERFLOW  0x0200              /* receive ringbuffer overflow */\r
+#define UART_NO_DATA          0x0100              /* no receive data available   */\r
+\r
+\r
+/*\r
+** function prototypes\r
+*/\r
+\r
+/**\r
+   @brief   Initialize UART and set baudrate \r
+   @param   baudrate Specify baudrate using macro UART_BAUD_SELECT()\r
+   @return  none\r
+*/\r
+extern void uart_init(unsigned int baudrate);\r
+\r
+\r
+/**\r
+ *  @brief   Get received byte from ringbuffer\r
+ *\r
+ * Returns in the lower byte the received character and in the \r
+ * higher byte the last receive error.\r
+ * UART_NO_DATA is returned when no data is available.\r
+ *\r
+ *  @param   void\r
+ *  @return  lower byte:  received byte from ringbuffer\r
+ *  @return  higher byte: last receive status\r
+ *           - \b 0 successfully received data from UART\r
+ *           - \b UART_NO_DATA           \r
+ *             <br>no receive data available\r
+ *           - \b UART_BUFFER_OVERFLOW   \r
+ *             <br>Receive ringbuffer overflow.\r
+ *             We are not reading the receive buffer fast enough, \r
+ *             one or more received character have been dropped \r
+ *           - \b UART_OVERRUN_ERROR     \r
+ *             <br>Overrun condition by UART.\r
+ *             A character already present in the UART UDR register was \r
+ *             not read by the interrupt handler before the next character arrived,\r
+ *             one or more received characters have been dropped.\r
+ *           - \b UART_FRAME_ERROR       \r
+ *             <br>Framing Error by UART\r
+ */\r
+extern unsigned int uart_getc(void);\r
+\r
+\r
+/**\r
+ *  @brief   Put byte to ringbuffer for transmitting via UART\r
+ *  @param   data byte to be transmitted\r
+ *  @return  none\r
+ */\r
+extern void uart_putc(unsigned char data);\r
+\r
+\r
+/**\r
+ *  @brief   Put string to ringbuffer for transmitting via UART\r
+ *\r
+ *  The string is buffered by the uart library in a circular buffer\r
+ *  and one character at a time is transmitted to the UART using interrupts.\r
+ *  Blocks if it can not write the whole string into the circular buffer.\r
+ * \r
+ *  @param   s string to be transmitted\r
+ *  @return  none\r
+ */\r
+extern void uart_puts(const char *s );\r
+\r
+\r
+/**\r
+ * @brief    Put string from program memory to ringbuffer for transmitting via UART.\r
+ *\r
+ * The string is buffered by the uart library in a circular buffer\r
+ * and one character at a time is transmitted to the UART using interrupts.\r
+ * Blocks if it can not write the whole string into the circular buffer.\r
+ *\r
+ * @param    s program memory string to be transmitted\r
+ * @return   none\r
+ * @see      uart_puts_P\r
+ */\r
+extern void uart_puts_p(const char *s );\r
+\r
+/**\r
+ * @brief    Macro to automatically put a string constant into program memory\r
+ */\r
+#define uart_puts_P(__s)       uart_puts_p(PSTR(__s))\r
+\r
+\r
+\r
+/** @brief  Initialize USART1 (only available on selected ATmegas) @see uart_init */\r
+extern void uart1_init(unsigned int baudrate);\r
+/** @brief  Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */\r
+extern unsigned int uart1_getc(void);\r
+/** @brief  Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */\r
+extern void uart1_putc(unsigned char data);\r
+/** @brief  Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */\r
+extern void uart1_puts(const char *s );\r
+/** @brief  Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */\r
+extern void uart1_puts_p(const char *s );\r
+/** @brief  Macro to automatically put a string constant into program memory */\r
+#define uart1_puts_P(__s)       uart1_puts_p(PSTR(__s))\r
+\r
+/**@}*/\r
+\r
+#endif // UART_H \r
+\r