Subversion Repositories HomeAutomation

Rev

Rev 60 | Rev 127 | 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. ** function prototypes
  72. */
  73.  
  74. void UartInit(void);
  75.  
  76. /**
  77.    @brief   Initialize UART and set baudrate
  78.    @param   baudrate Specify baudrate using macro UART_BAUD_SELECT()
  79.    @return  none
  80. */
  81. extern void uart_init(unsigned int baudrate);
  82.  
  83.  
  84. /**
  85.  *  @brief   Get received byte from ringbuffer
  86.  *
  87.  * Returns in the lower byte the received character and in the
  88.  * higher byte the last receive error.
  89.  * UART_NO_DATA is returned when no data is available.
  90.  *
  91.  *  @param   void
  92.  *  @return  lower byte:  received byte from ringbuffer
  93.  *  @return  higher byte: last receive status
  94.  *           - \b 0 successfully received data from UART
  95.  *           - \b UART_NO_DATA          
  96.  *             <br>no receive data available
  97.  *           - \b UART_BUFFER_OVERFLOW  
  98.  *             <br>Receive ringbuffer overflow.
  99.  *             We are not reading the receive buffer fast enough,
  100.  *             one or more received character have been dropped
  101.  *           - \b UART_OVERRUN_ERROR    
  102.  *             <br>Overrun condition by UART.
  103.  *             A character already present in the UART UDR register was
  104.  *             not read by the interrupt handler before the next character arrived,
  105.  *             one or more received characters have been dropped.
  106.  *           - \b UART_FRAME_ERROR      
  107.  *             <br>Framing Error by UART
  108.  */
  109. extern unsigned int uart_getc(void);
  110.  
  111.  
  112. /**
  113.  *  @brief   Put byte to ringbuffer for transmitting via UART
  114.  *  @param   data byte to be transmitted
  115.  *  @return  none
  116.  */
  117. extern void uart_putc(unsigned char data);
  118.  
  119.  
  120. /**
  121.  *  @brief   Put string to ringbuffer for transmitting via UART
  122.  *
  123.  *  The string is buffered by the uart library in a circular buffer
  124.  *  and one character at a time is transmitted to the UART using interrupts.
  125.  *  Blocks if it can not write the whole string into the circular buffer.
  126.  *
  127.  *  @param   s string to be transmitted
  128.  *  @return  none
  129.  */
  130. extern void uart_puts(const char *s );
  131.  
  132.  
  133. /**
  134.  * @brief    Put string from program memory to ringbuffer for transmitting via UART.
  135.  *
  136.  * The string is buffered by the uart library in a circular buffer
  137.  * and one character at a time is transmitted to the UART using interrupts.
  138.  * Blocks if it can not write the whole string into the circular buffer.
  139.  *
  140.  * @param    s program memory string to be transmitted
  141.  * @return   none
  142.  * @see      uart_puts_P
  143.  */
  144. extern void uart_puts_p(const char *s );
  145.  
  146. /**
  147.  * @brief    Macro to automatically put a string constant into program memory
  148.  */
  149. #define uart_puts_P(__s)       uart_puts_p(P(__s))
  150.  
  151. /**
  152.  * @brief    Put integer to ringbuffer for transmitting via UART.
  153.  *
  154.  * The integer is converted to a string which is buffered by the uart
  155.  * library in a circular buffer and one character at a time is transmitted
  156.  * to the UART using interrupts.
  157.  *
  158.  * @param    value to transfer
  159.  * @return   none
  160.  * @see      uart_puts_p
  161.  */
  162. extern void uart_puti( int i );
  163.  
  164. /**
  165.  * @brief    Put nibble as hex to ringbuffer for transmit via UART.
  166.  *
  167.  * The lower nibble of the parameter is convertet to correspondig
  168.  * hex-char and put in a circular buffer and one character at a time
  169.  * is transmitted to the UART using interrupts.
  170.  *
  171.  * @param    value to transfer (byte, only lower nibble converted)
  172.  * @return   none
  173.  * @see      uart_putc
  174.  */
  175. extern void uart_puthex_nibble(const unsigned char b);
  176.  
  177. /**
  178.  * @brief    Put byte as hex to ringbuffer for transmit via UART.
  179.  *
  180.  * The upper and lower nibble of the parameter are convertet to
  181.  * correspondig hex-chars and put in a circular buffer and one
  182.  * character at a time is transmitted to the UART using interrupts.
  183.  *
  184.  * @param    value to transfer
  185.  * @return   none
  186.  * @see      uart_puthex_nibble
  187.  */
  188. extern void uart_puthex_byte(const unsigned char b);
  189.  
  190. /**
  191.  * @brief    Put byte as bin to ringbuffer for transmit via UART.
  192.  *
  193.  * @param    value to transfer
  194.  * @return   none
  195.  * @see      uart_putc
  196.  */
  197. extern void uart_putbin_byte(const unsigned char b);
  198.  
  199. /*@}*/
  200. #endif // UART_H
  201.  
  202.