Subversion Repositories HomeAutomation

Rev

Rev 1576 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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