Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Serial_XMEGA.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief Serial USART Peripheral Driver (XMEGA)
  28. *
  29. * On-chip serial USART driver for the XMEGA AVR microcontrollers.
  30. *
  31. * \note This file should not be included directly. It is automatically included as needed by the USART driver
  32. * dispatch header located in LUFA/Drivers/Peripheral/Serial.h.
  33. */
  34. /** \ingroup Group_Serial
  35. * \defgroup Group_Serial_XMEGA Serial USART Peripheral Driver (XMEGA)
  36. *
  37. * \section Sec_Serial_XMEGA_ModDescription Module Description
  38. * On-chip serial USART driver for the XMEGA AVR microcontrollers.
  39. *
  40. * \note This file should not be included directly. It is automatically included as needed by the USART driver
  41. * dispatch header located in LUFA/Drivers/Peripheral/Serial.h.
  42. *
  43. * \section Sec_Serial_XMEGA_ExampleUsage Example Usage
  44. * The following snippet is an example of how this module may be used within a typical
  45. * application.
  46. *
  47. * \code
  48. * // Initialize the serial USART driver before first use, with 9600 baud (and no double-speed mode)
  49. * Serial_Init(&USARTD0, 9600, false);
  50. *
  51. * // Send a string through the USART
  52. * Serial_TxString(&USARTD0, "Test String\r\n");
  53. *
  54. * // Receive a byte through the USART
  55. * uint8_t DataByte = Serial_RxByte(&USARTD0);
  56. * \endcode
  57. *
  58. * @{
  59. */
  60. #ifndef __SERIAL_XMEGA_H__
  61. #define __SERIAL_XMEGA_H__
  62. /* Includes: */
  63. #include "../../../Common/Common.h"
  64. #include "../../Misc/TerminalCodes.h"
  65. #include <stdio.h>
  66. /* Enable C linkage for C++ Compilers: */
  67. #if defined(__cplusplus)
  68. extern "C" {
  69. #endif
  70. /* Preprocessor Checks: */
  71. #if !defined(__INCLUDE_FROM_SERIAL_H) && !defined(__INCLUDE_FROM_SERIAL_C)
  72. #error Do not include this file directly. Include LUFA/Drivers/Peripheral/Serial.h instead.
  73. #endif
  74. /* Private Interface - For use in library only: */
  75. #if !defined(__DOXYGEN__)
  76. /* External Variables: */
  77. extern FILE USARTSerialStream;
  78. /* Function Prototypes: */
  79. int Serial_putchar(char DataByte,
  80. FILE *Stream);
  81. int Serial_getchar(FILE *Stream);
  82. int Serial_getchar_Blocking(FILE *Stream);
  83. #endif
  84. /* Public Interface - May be used in end-application: */
  85. /* Macros: */
  86. /** Macro for calculating the baud value from a given baud rate when the \c U2X (double speed) bit is
  87. * not set.
  88. *
  89. * \param[in] Baud Target serial UART baud rate.
  90. *
  91. * \return Closest UBRR register value for the given UART frequency.
  92. */
  93. #define SERIAL_UBBRVAL(Baud) ((((F_CPU / 16) + (Baud / 2)) / (Baud)) - 1)
  94. /** Macro for calculating the baud value from a given baud rate when the \c U2X (double speed) bit is
  95. * set.
  96. *
  97. * \param[in] Baud Target serial UART baud rate.
  98. *
  99. * \return Closest UBRR register value for the given UART frequency.
  100. */
  101. #define SERIAL_2X_UBBRVAL(Baud) ((((F_CPU / 8) + (Baud / 2)) / (Baud)) - 1)
  102. /* Function Prototypes: */
  103. /** Transmits a given string located in program space (FLASH) through the USART.
  104. *
  105. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  106. * \param[in] FlashStringPtr Pointer to a string located in program space.
  107. */
  108. void Serial_SendString_P(USART_t* const USART,
  109. const char* FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
  110. /** Transmits a given string located in SRAM memory through the USART.
  111. *
  112. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  113. * \param[in] StringPtr Pointer to a string located in SRAM space.
  114. */
  115. void Serial_SendString(USART_t* const USART,
  116. const char* StringPtr) ATTR_NON_NULL_PTR_ARG(1);
  117. /** Transmits a given buffer located in SRAM memory through the USART.
  118. *
  119. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  120. * \param[in] Buffer Pointer to a buffer containing the data to send.
  121. * \param[in] Length Length of the data to send, in bytes.
  122. */
  123. void Serial_SendData(USART_t* const USART,
  124. const void* Buffer,
  125. uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
  126. /** Creates a standard character stream from the USART so that it can be used with all the regular functions
  127. * in the avr-libc \c <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf). The created
  128. * stream is bidirectional and can be used for both input and output functions.
  129. *
  130. * Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
  131. * fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
  132. * be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
  133. * line buffering.
  134. *
  135. * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed, if \c NULL, \c stdout
  136. * and \c stdin will be configured to use the USART.
  137. *
  138. * \pre The USART must first be configured via a call to \ref Serial_Init() before the stream is used.
  139. */
  140. void Serial_CreateStream(FILE* Stream);
  141. /** Identical to \ref Serial_CreateStream(), except that reads are blocking until the calling stream function terminates
  142. * the transfer.
  143. *
  144. * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed, if \c NULL, \c stdout
  145. * and \c stdin will be configured to use the USART.
  146. *
  147. * \pre The USART must first be configured via a call to \ref Serial_Init() before the stream is used.
  148. */
  149. void Serial_CreateBlockingStream(FILE* Stream);
  150. /* Inline Functions: */
  151. /** Initializes the USART, ready for serial data transmission and reception. This initializes the interface to
  152. * standard 8-bit, no parity, 1 stop bit settings suitable for most applications.
  153. *
  154. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  155. * \param[in] BaudRate Serial baud rate, in bits per second.
  156. * \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate.
  157. */
  158. static inline void Serial_Init(USART_t* const USART,
  159. const uint32_t BaudRate,
  160. const bool DoubleSpeed) ATTR_NON_NULL_PTR_ARG(1);
  161. static inline void Serial_Init(USART_t* const USART,
  162. const uint32_t BaudRate,
  163. const bool DoubleSpeed)
  164. {
  165. uint16_t BaudValue = (DoubleSpeed ? SERIAL_2X_UBBRVAL(BaudRate) : SERIAL_UBBRVAL(BaudRate));
  166. USART->BAUDCTRLB = (BaudValue >> 8);
  167. USART->BAUDCTRLA = (BaudValue & 0xFF);
  168. USART->CTRLC = (USART_CMODE_ASYNCHRONOUS_gc | USART_PMODE_DISABLED_gc | USART_CHSIZE_8BIT_gc);
  169. USART->CTRLB = (USART_RXEN_bm | USART_TXEN_bm | (DoubleSpeed ? USART_CLK2X_bm : 0));
  170. }
  171. /** Turns off the USART driver, disabling and returning used hardware to their default configuration.
  172. *
  173. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  174. */
  175. static inline void Serial_Disable(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  176. static inline void Serial_Disable(USART_t* const USART)
  177. {
  178. USART->CTRLA = 0;
  179. USART->CTRLB = 0;
  180. USART->CTRLC = 0;
  181. }
  182. /** Indicates whether a character has been received through the USART.
  183. *
  184. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  185. *
  186. * \return Boolean \c true if a character has been received, \c false otherwise.
  187. */
  188. static inline bool Serial_IsCharReceived(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  189. static inline bool Serial_IsCharReceived(USART_t* const USART)
  190. {
  191. return ((USART->STATUS & USART_RXCIF_bm) ? true : false);
  192. }
  193. /** Indicates whether there is hardware buffer space for a new transmit on the USART. This
  194. * function can be used to determine if a call to \ref Serial_SendByte() will block in advance.
  195. *
  196. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  197. *
  198. * \return Boolean \c true if a character can be queued for transmission immediately, \c false otherwise.
  199. */
  200. static inline bool Serial_IsSendReady(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  201. static inline bool Serial_IsSendReady(USART_t* const USART)
  202. {
  203. return (USART->STATUS & USART_DREIF_bm) ? true : false;
  204. }
  205. /** Indicates whether the hardware USART transmit buffer is completely empty, indicating all
  206. * pending transmissions have completed.
  207. *
  208. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  209. *
  210. * \return Boolean \c true if no characters are buffered for transmission, \c false otherwise.
  211. */
  212. static inline bool Serial_IsSendComplete(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
  213. static inline bool Serial_IsSendComplete(USART_t* const USART)
  214. {
  215. return (USART->STATUS & USART_TXCIF_bm) ? true : false;
  216. }
  217. /** Transmits a given byte through the USART.
  218. *
  219. * \note If no buffer space is available in the hardware USART, this function will block. To check if
  220. * space is available before calling this function, see \ref Serial_IsSendReady().
  221. *
  222. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  223. * \param[in] DataByte Byte to transmit through the USART.
  224. */
  225. static inline void Serial_SendByte(USART_t* const USART,
  226. const char DataByte) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  227. static inline void Serial_SendByte(USART_t* const USART,
  228. const char DataByte)
  229. {
  230. while (!(Serial_IsSendReady(USART)));
  231. USART->DATA = DataByte;
  232. }
  233. /** Receives the next byte from the USART.
  234. *
  235. * \param[in,out] USART Pointer to the base of the USART peripheral within the device.
  236. *
  237. * \return Next byte received from the USART, or a negative value if no byte has been received.
  238. */
  239. static inline int16_t Serial_ReceiveByte(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
  240. static inline int16_t Serial_ReceiveByte(USART_t* const USART)
  241. {
  242. if (!(Serial_IsCharReceived(USART)))
  243. return -1;
  244. USART->STATUS = USART_RXCIF_bm;
  245. return USART->DATA;
  246. }
  247. /* Disable C linkage for C++ Compilers: */
  248. #if defined(__cplusplus)
  249. }
  250. #endif
  251. #endif
  252. /** @} */