Subversion Repositories HomeAutomation

Rev

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