usart_driver.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* This file has been prepared for Doxygen automatic documentation generation.*/
  2. /*! \file *********************************************************************
  3. *
  4. * \brief XMEGA USART driver header file.
  5. *
  6. * This file contains the function prototypes and enumerator definitions
  7. * for various configuration parameters for the XMEGA USART driver.
  8. *
  9. * The driver is not intended for size and/or speed critical code, since
  10. * most functions are just a few lines of code, and the function call
  11. * overhead would decrease code performance. The driver is intended for
  12. * rapid prototyping and documentation purposes for getting started with
  13. * the XMEGA ADC module.
  14. *
  15. * For size and/or speed critical code, it is recommended to copy the
  16. * function contents directly into your application instead of making
  17. * a function call.
  18. *
  19. * \par Application note:
  20. * AVR1307: Using the XMEGA USART
  21. *
  22. * \par Documentation
  23. * For comprehensive code documentation, supported compilers, compiler
  24. * settings and supported devices see readme.html
  25. *
  26. * \author
  27. * Atmel Corporation: http://www.atmel.com \n
  28. * Support email: avr@atmel.com
  29. *
  30. * $Revision: 1694 $
  31. * $Date: 2008-07-29 14:21:58 +0200 (ti, 29 jul 2008) $ \n
  32. *
  33. * Copyright (c) 2008, Atmel Corporation All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions are met:
  37. *
  38. * 1. Redistributions of source code must retain the above copyright notice,
  39. * this list of conditions and the following disclaimer.
  40. *
  41. * 2. Redistributions in binary form must reproduce the above copyright notice,
  42. * this list of conditions and the following disclaimer in the documentation
  43. * and/or other materials provided with the distribution.
  44. *
  45. * 3. The name of ATMEL may not be used to endorse or promote products derived
  46. * from this software without specific prior written permission.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  49. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  50. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
  51. * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
  52. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  55. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  56. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  57. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. *****************************************************************************/
  59. #ifndef USART_DRIVER_H
  60. #define USART_DRIVER_H
  61. #include "avr_compiler.h"
  62. /* USART buffer defines. */
  63. /* \brief Receive buffer size: 2,4,8,16,32,64,128 or 256 bytes. */
  64. #define USART_RX_BUFFER_SIZE 4
  65. /* \brief Transmit buffer size: 2,4,8,16,32,64,128 or 256 bytes */
  66. #define USART_TX_BUFFER_SIZE 4
  67. /* \brief Receive buffer mask. */
  68. #define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
  69. /* \brief Transmit buffer mask. */
  70. #define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
  71. #if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
  72. #error RX buffer size is not a power of 2
  73. #endif
  74. #if ( USART_TX_BUFFER_SIZE & USART_TX_BUFFER_MASK )
  75. #error TX buffer size is not a power of 2
  76. #endif
  77. /* \brief USART transmit and receive ring buffer. */
  78. typedef struct USART_Buffer
  79. {
  80. /* \brief Receive buffer. */
  81. volatile uint8_t RX[USART_RX_BUFFER_SIZE];
  82. /* \brief Transmit buffer. */
  83. volatile uint8_t TX[USART_TX_BUFFER_SIZE];
  84. /* \brief Receive buffer head. */
  85. volatile uint8_t RX_Head;
  86. /* \brief Receive buffer tail. */
  87. volatile uint8_t RX_Tail;
  88. /* \brief Transmit buffer head. */
  89. volatile uint8_t TX_Head;
  90. /* \brief Transmit buffer tail. */
  91. volatile uint8_t TX_Tail;
  92. } USART_Buffer_t;
  93. /*! \brief Struct used when interrupt driven driver is used.
  94. *
  95. * Struct containing pointer to a usart, a buffer and a location to store Data
  96. * register interrupt level temporary.
  97. */
  98. typedef struct Usart_and_buffer
  99. {
  100. /* \brief Pointer to USART module to use. */
  101. USART_t * usart;
  102. /* \brief Data register empty interrupt level. */
  103. USART_DREINTLVL_t dreIntLevel;
  104. /* \brief Data buffer. */
  105. USART_Buffer_t buffer;
  106. } USART_data_t;
  107. /* Macros. */
  108. /*! \brief Macro that sets the USART frame format.
  109. *
  110. * Sets the frame format, Frame Size, parity mode and number of stop bits.
  111. *
  112. * \param _usart Pointer to the USART module
  113. * \param _charSize The character size. Use USART_CHSIZE_t type.
  114. * \param _parityMode The parity Mode. Use USART_PMODE_t type.
  115. * \param _twoStopBits Enable two stop bit mode. Use bool type.
  116. */
  117. #define USART_Format_Set(_usart, _charSize, _parityMode, _twoStopBits) \
  118. (_usart)->CTRLC = (uint8_t) _charSize | _parityMode | \
  119. (_twoStopBits ? USART_SBMODE_bm : 0)
  120. /*! \brief Set USART baud rate.
  121. *
  122. * Sets the USART's baud rate register.
  123. *
  124. * UBRR_Value : Value written to UBRR
  125. * ScaleFactor : Time Base Generator Scale Factor
  126. *
  127. * Equation for calculation of BSEL value in asynchronous normal speed mode:
  128. * If ScaleFactor >= 0
  129. * BSEL = ((I/O clock frequency)/(2^(ScaleFactor)*16*Baudrate))-1
  130. * If ScaleFactor < 0
  131. * BSEL = (1/(2^(ScaleFactor)*16))*(((I/O clock frequency)/Baudrate)-1)
  132. *
  133. * \note See XMEGA manual for equations for calculation of BSEL value in other
  134. * modes.
  135. *
  136. * \param _usart Pointer to the USART module.
  137. * \param _bselValue Value to write to BSEL part of Baud control register.
  138. * Use uint16_t type.
  139. * \param _bScaleFactor USART baud rate scale factor.
  140. * Use uint8_t type
  141. */
  142. #define USART_Baudrate_Set(_usart, _bselValue, _bScaleFactor) \
  143. (_usart)->BAUDCTRLA =(uint8_t)_bselValue; \
  144. (_usart)->BAUDCTRLB =(_bScaleFactor << USART_BSCALE0_bp)|(_bselValue >> 8)
  145. /*! \brief Enable USART receiver.
  146. *
  147. * \param _usart Pointer to the USART module
  148. */
  149. #define USART_Rx_Enable(_usart) ((_usart)->CTRLB |= USART_RXEN_bm)
  150. /*! \brief Disable USART receiver.
  151. *
  152. * \param _usart Pointer to the USART module.
  153. */
  154. #define USART_Rx_Disable(_usart) ((_usart)->CTRLB &= ~USART_RXEN_bm)
  155. /*! \brief Enable USART transmitter.
  156. *
  157. * \param _usart Pointer to the USART module.
  158. */
  159. #define USART_Tx_Enable(_usart) ((_usart)->CTRLB |= USART_TXEN_bm)
  160. /*! \brief Disable USART transmitter.
  161. *
  162. * \param _usart Pointer to the USART module.
  163. */
  164. #define USART_Tx_Disable(_usart) ((_usart)->CTRLB &= ~USART_TXEN_bm)
  165. /*! \brief Set USART RXD interrupt level.
  166. *
  167. * Sets the interrupt level on RX Complete interrupt.
  168. *
  169. * \param _usart Pointer to the USART module.
  170. * \param _rxdIntLevel Interrupt level of the RXD interrupt.
  171. * Use USART_RXCINTLVL_t type.
  172. */
  173. #define USART_RxdInterruptLevel_Set(_usart, _rxdIntLevel) \
  174. ((_usart)->CTRLA = ((_usart)->CTRLA & ~USART_RXCINTLVL_gm) | _rxdIntLevel)
  175. /*! \brief Set USART TXD interrupt level.
  176. *
  177. * Sets the interrupt level on TX Complete interrupt.
  178. *
  179. * \param _usart Pointer to the USART module.
  180. * \param _txdIntLevel Interrupt level of the TXD interrupt.
  181. * Use USART_TXCINTLVL_t type.
  182. */
  183. #define USART_TxdInterruptLevel_Set(_usart, _txdIntLevel) \
  184. (_usart)->CTRLA = ((_usart)->CTRLA & ~USART_TXCINTLVL_gm) | _txdIntLevel
  185. /*! \brief Set USART DRE interrupt level.
  186. *
  187. * Sets the interrupt level on Data Register interrupt.
  188. *
  189. * \param _usart Pointer to the USART module.
  190. * \param _dreIntLevel Interrupt level of the DRE interrupt.
  191. * Use USART_DREINTLVL_t type.
  192. */
  193. #define USART_DreInterruptLevel_Set(_usart, _dreIntLevel) \
  194. (_usart)->CTRLA = ((_usart)->CTRLA & ~USART_DREINTLVL_gm) | _dreIntLevel
  195. /*! \brief Set the mode the USART run in.
  196. *
  197. * Set the mode the USART run in. The default mode is asynchronous mode.
  198. *
  199. * \param _usart Pointer to the USART module register section.
  200. * \param _usartMode Selects the USART mode. Use USART_CMODE_t type.
  201. *
  202. * USART modes:
  203. * - 0x0 : Asynchronous mode.
  204. * - 0x1 : Synchronous mode.
  205. * - 0x2 : IrDA mode.
  206. * - 0x3 : Master SPI mode.
  207. */
  208. #define USART_SetMode(_usart, _usartMode) \
  209. ((_usart)->CTRLC = ((_usart)->CTRLC & (~USART_CMODE_gm)) | _usartMode)
  210. /*! \brief Check if data register empty flag is set.
  211. *
  212. * \param _usart The USART module.
  213. */
  214. #define USART_IsTXDataRegisterEmpty(_usart) (((_usart)->STATUS & USART_DREIF_bm) != 0)
  215. /*! \brief Put data (5-8 bit character).
  216. *
  217. * Use the macro USART_IsTXDataRegisterEmpty before using this function to
  218. * put data to the TX register.
  219. *
  220. * \param _usart The USART module.
  221. * \param _data The data to send.
  222. */
  223. #define USART_PutChar(_usart, _data) ((_usart)->DATA = _data)
  224. /*! \brief Checks if the RX complete interrupt flag is set.
  225. *
  226. * Checks if the RX complete interrupt flag is set.
  227. *
  228. * \param _usart The USART module.
  229. */
  230. #define USART_IsRXComplete(_usart) (((_usart)->STATUS & USART_RXCIF_bm) != 0)
  231. /*! \brief Get received data (5-8 bit character).
  232. *
  233. * This macro reads out the RX register.
  234. * Use the macro USART_RX_Complete to check if anything is received.
  235. *
  236. * \param _usart The USART module.
  237. *
  238. * \retval Received data.
  239. */
  240. #define USART_GetChar(_usart) ((_usart)->DATA)
  241. /* Functions for interrupt driven driver. */
  242. void USART_InterruptDriver_Initialize(USART_data_t * usart_data,
  243. USART_t * usart,
  244. USART_DREINTLVL_t dreIntLevel );
  245. void USART_InterruptDriver_DreInterruptLevel_Set(USART_data_t * usart_data,
  246. USART_DREINTLVL_t dreIntLevel);
  247. bool USART_TXBuffer_FreeSpace(USART_data_t * usart_data);
  248. bool USART_TXBuffer_PutByte(USART_data_t * usart_data, uint8_t data);
  249. bool USART_RXBufferData_Available(USART_data_t * usart_data);
  250. uint8_t USART_RXBuffer_GetByte(USART_data_t * usart_data);
  251. bool USART_RXComplete(USART_data_t * usart_data);
  252. void USART_DataRegEmpty(USART_data_t * usart_data);
  253. /* Functions for polled driver. */
  254. void USART_NineBits_PutChar(USART_t * usart, uint16_t data);
  255. uint16_t USART_NineBits_GetChar(USART_t * usart);
  256. #endif