Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

uart.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.8.2.1 2007/07/01 11:14:38 peter Exp $
  7. Software: AVR-GCC 4.1, AVR Libc 1.4
  8. Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
  9. License: GNU General Public License
  10. Usage: see Doxygen manual
  11. LICENSE:
  12. Copyright (C) 2006 Peter Fleury
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. ************************************************************************/
  22. /**
  23. * @defgroup pfleury_uart UART Library
  24. * @code #include <uart.h> @endcode
  25. *
  26. * @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers.
  27. *
  28. * This library can be used to transmit and receive data through the built in UART.
  29. *
  30. * An interrupt is generated when the UART has finished transmitting or
  31. * receiving a byte. The interrupt handling routines use circular buffers
  32. * for buffering received and transmitted data.
  33. *
  34. * The UART_RX_BUFFER_SIZE and UART_TX_BUFFER_SIZE constants define
  35. * the size of the circular buffers in bytes. Note that these constants must be a power of 2.
  36. * You may need to adapt this constants to your target and your application by adding
  37. * CDEFS += -DUART_RX_BUFFER_SIZE=nn -DUART_RX_BUFFER_SIZE=nn to your Makefile.
  38. *
  39. * @note Based on Atmel Application Note AVR306
  40. * @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury
  41. */
  42. /**@{*/
  43. #if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
  44. #error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
  45. #endif
  46. /*
  47. ** constants and macros
  48. */
  49. /** @brief UART Baudrate Expression
  50. * @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz
  51. * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600
  52. */
  53. #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
  54. /** @brief UART Baudrate Expression for ATmega double speed mode
  55. * @param xtalcpu system clock in Mhz, e.g. 4000000L for 4Mhz
  56. * @param baudrate baudrate in bps, e.g. 1200, 2400, 9600
  57. */
  58. #define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) (((xtalCpu)/((baudRate)*8l)-1)|0x8000)
  59. /** Size of the circular receive buffer, must be power of 2 */
  60. #ifndef UART_RX_BUFFER_SIZE
  61. #define UART_RX_BUFFER_SIZE 32
  62. #endif
  63. /** Size of the circular transmit buffer, must be power of 2 */
  64. #ifndef UART_TX_BUFFER_SIZE
  65. #define UART_TX_BUFFER_SIZE 32
  66. #endif
  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. ** high byte error return code of uart_getc()
  73. */
  74. #define UART_FRAME_ERROR 0x0800 /* Framing Error by UART */
  75. #define UART_OVERRUN_ERROR 0x0400 /* Overrun condition by UART */
  76. #define UART_BUFFER_OVERFLOW 0x0200 /* receive ringbuffer overflow */
  77. #define UART_NO_DATA 0x0100 /* no receive data available */
  78. /*
  79. ** function prototypes
  80. */
  81. /**
  82. @brief Initialize UART and set baudrate
  83. @param baudrate Specify baudrate using macro UART_BAUD_SELECT()
  84. @return none
  85. */
  86. extern void uart_init(unsigned int baudrate);
  87. /**
  88. * @brief Get received byte from ringbuffer
  89. *
  90. * Returns in the lower byte the received character and in the
  91. * higher byte the last receive error.
  92. * UART_NO_DATA is returned when no data is available.
  93. *
  94. * @param void
  95. * @return lower byte: received byte from ringbuffer
  96. * @return higher byte: last receive status
  97. * - \b 0 successfully received data from UART
  98. * - \b UART_NO_DATA
  99. * <br>no receive data available
  100. * - \b UART_BUFFER_OVERFLOW
  101. * <br>Receive ringbuffer overflow.
  102. * We are not reading the receive buffer fast enough,
  103. * one or more received character have been dropped
  104. * - \b UART_OVERRUN_ERROR
  105. * <br>Overrun condition by UART.
  106. * A character already present in the UART UDR register was
  107. * not read by the interrupt handler before the next character arrived,
  108. * one or more received characters have been dropped.
  109. * - \b UART_FRAME_ERROR
  110. * <br>Framing Error by UART
  111. */
  112. extern unsigned int uart_getc(void);
  113. /**
  114. * @brief Put byte to ringbuffer for transmitting via UART
  115. * @param data byte to be transmitted
  116. * @return none
  117. */
  118. extern void uart_putc(unsigned char data);
  119. /**
  120. * @brief Put string to ringbuffer for transmitting via UART
  121. *
  122. * The string is buffered by the uart library in a circular buffer
  123. * and one character at a time is transmitted to the UART using interrupts.
  124. * Blocks if it can not write the whole string into the circular buffer.
  125. *
  126. * @param s string to be transmitted
  127. * @return none
  128. */
  129. extern void uart_puts(const char *s );
  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. * @brief Macro to automatically put a string constant into program memory
  144. */
  145. #define uart_puts_P(__s) uart_puts_p(PSTR(__s))
  146. /** @brief Initialize USART1 (only available on selected ATmegas) @see uart_init */
  147. extern void uart1_init(unsigned int baudrate);
  148. /** @brief Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */
  149. extern unsigned int uart1_getc(void);
  150. /** @brief Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */
  151. extern void uart1_putc(unsigned char data);
  152. /** @brief Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */
  153. extern void uart1_puts(const char *s );
  154. /** @brief Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */
  155. extern void uart1_puts_p(const char *s );
  156. /** @brief Macro to automatically put a string constant into program memory */
  157. #define uart1_puts_P(__s) uart1_puts_p(PSTR(__s))
  158. /**@}*/
  159. #endif // UART_H