Subversion Repositories HomeAutomation

Rev

Rev 127 | Go to most recent revision | Blame | 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. /**
  75.  *  @brief   Get received byte from ringbuffer
  76.  *
  77.  * Returns in the lower byte the received character and in the
  78.  * higher byte the last receive error.
  79.  * UART_NO_DATA is returned when no data is available.
  80.  *
  81.  *  @param   void
  82.  *  @return  lower byte:  received byte from ringbuffer
  83.  *  @return  higher byte: last receive status
  84.  *           - \b 0 successfully received data from UART
  85.  *           - \b UART_NO_DATA          
  86.  *             <br>no receive data available
  87.  *           - \b UART_BUFFER_OVERFLOW  
  88.  *             <br>Receive ringbuffer overflow.
  89.  *             We are not reading the receive buffer fast enough,
  90.  *             one or more received character have been dropped
  91.  *           - \b UART_OVERRUN_ERROR    
  92.  *             <br>Overrun condition by UART.
  93.  *             A character already present in the UART UDR register was
  94.  *             not read by the interrupt handler before the next character arrived,
  95.  *             one or more received characters have been dropped.
  96.  *           - \b UART_FRAME_ERROR      
  97.  *             <br>Framing Error by UART
  98.  */
  99. extern unsigned int uart_getc(void);
  100.  
  101.  
  102. /**
  103.  *  @brief   Put byte to ringbuffer for transmitting via UART
  104.  *  @param   data byte to be transmitted
  105.  *  @return  none
  106.  */
  107. extern void uart_putc(unsigned char data);
  108.  
  109.  
  110. /**
  111.  *  @brief   Put string to ringbuffer for transmitting via UART
  112.  *
  113.  *  The string is buffered by the uart library in a circular buffer
  114.  *  and one character at a time is transmitted to the UART using interrupts.
  115.  *  Blocks if it can not write the whole string into the circular buffer.
  116.  *
  117.  *  @param   s string to be transmitted
  118.  *  @return  none
  119.  */
  120. extern void uart_puts(const char *s );
  121.  
  122.  
  123. /**
  124.  * @brief    Put string from program memory to ringbuffer for transmitting via UART.
  125.  *
  126.  * The string is buffered by the uart library in a circular buffer
  127.  * and one character at a time is transmitted to the UART using interrupts.
  128.  * Blocks if it can not write the whole string into the circular buffer.
  129.  *
  130.  * @param    s program memory string to be transmitted
  131.  * @return   none
  132.  * @see      uart_puts_P
  133.  */
  134. extern void uart_puts_p(const char *s );
  135.  
  136. /**
  137.  * @brief    Macro to automatically put a string constant into program memory
  138.  */
  139. #define uart_puts_P(__s)       uart_puts_p(P(__s))
  140.  
  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.  
  167. /**
  168.  * @brief    Put byte as hex to ringbuffer for transmit via UART.
  169.  *
  170.  * The upper and lower nibble of the parameter are convertet to
  171.  * correspondig hex-chars and put in a circular buffer and one
  172.  * character at a time is transmitted to the UART using interrupts.
  173.  *
  174.  * @param    value to transfer
  175.  * @return   none
  176.  * @see      uart_puthex_nibble
  177.  */
  178. extern void uart_puthex_byte(const unsigned char b);
  179.  
  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.  
  189. /*@}*/
  190. #endif // UART_H
  191.  
  192.