Serial_AVR8.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2017.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2017 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 (AVR8)
  28. *
  29. * On-chip serial USART driver for the 8-bit 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_AVR8 Serial USART Peripheral Driver (AVR8)
  36. *
  37. * \section Sec_Serial_AVR8_ModDescription Module Description
  38. * On-chip serial USART driver for the 8-bit 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_AVR8_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(9600, false);
  50. *
  51. * // Send a string through the USART
  52. * Serial_SendString("Test String\r\n");
  53. *
  54. * // Send a raw byte through the USART
  55. * Serial_SendByte(0xDC);
  56. *
  57. * // Receive a byte through the USART (or -1 if no data received)
  58. * int16_t DataByte = Serial_ReceiveByte();
  59. * \endcode
  60. *
  61. * @{
  62. */
  63. #ifndef __SERIAL_AVR8_H__
  64. #define __SERIAL_AVR8_H__
  65. /* Includes: */
  66. #include "../../../Common/Common.h"
  67. #include "../../Misc/TerminalCodes.h"
  68. #include <stdio.h>
  69. /* Enable C linkage for C++ Compilers: */
  70. #if defined(__cplusplus)
  71. extern "C" {
  72. #endif
  73. /* Preprocessor Checks: */
  74. #if !defined(__INCLUDE_FROM_SERIAL_H) && !defined(__INCLUDE_FROM_SERIAL_C)
  75. #error Do not include this file directly. Include LUFA/Drivers/Peripheral/Serial.h instead.
  76. #endif
  77. /* Private Interface - For use in library only: */
  78. #if !defined(__DOXYGEN__)
  79. /* External Variables: */
  80. extern FILE USARTSerialStream;
  81. /* Function Prototypes: */
  82. int Serial_putchar(char DataByte,
  83. FILE *Stream);
  84. int Serial_getchar(FILE *Stream);
  85. int Serial_getchar_Blocking(FILE *Stream);
  86. #endif
  87. /* Public Interface - May be used in end-application: */
  88. /* Macros: */
  89. /** Macro for calculating the baud value from a given baud rate when the \c U2X (double speed) bit is
  90. * not set.
  91. *
  92. * \param[in] Baud Target serial UART baud rate.
  93. *
  94. * \return Closest UBRR register value for the given UART frequency.
  95. */
  96. #define SERIAL_UBBRVAL(Baud) ((((F_CPU / 16) + (Baud / 2)) / (Baud)) - 1)
  97. /** Macro for calculating the baud value from a given baud rate when the \c U2X (double speed) bit is
  98. * set.
  99. *
  100. * \param[in] Baud Target serial UART baud rate.
  101. *
  102. * \return Closest UBRR register value for the given UART frequency.
  103. */
  104. #define SERIAL_2X_UBBRVAL(Baud) ((((F_CPU / 8) + (Baud / 2)) / (Baud)) - 1)
  105. /* Function Prototypes: */
  106. /** Transmits a given NUL terminated string located in program space (FLASH) through the USART.
  107. *
  108. * \param[in] FlashStringPtr Pointer to a string located in program space.
  109. */
  110. void Serial_SendString_P(const char* FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
  111. /** Transmits a given NUL terminated string located in SRAM memory through the USART.
  112. *
  113. * \param[in] StringPtr Pointer to a string located in SRAM space.
  114. */
  115. void Serial_SendString(const char* StringPtr) ATTR_NON_NULL_PTR_ARG(1);
  116. /** Transmits a given buffer located in SRAM memory through the USART.
  117. *
  118. * \param[in] Buffer Pointer to a buffer containing the data to send.
  119. * \param[in] Length Length of the data to send, in bytes.
  120. */
  121. void Serial_SendData(const void* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
  122. /** Creates a standard character stream from the USART so that it can be used with all the regular functions
  123. * in the avr-libc \c <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf). The created
  124. * stream is bidirectional and can be used for both input and output functions.
  125. *
  126. * Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
  127. * fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
  128. * be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
  129. * line buffering.
  130. *
  131. * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed, if \c NULL, \c stdout
  132. * and \c stdin will be configured to use the USART.
  133. *
  134. * \pre The USART must first be configured via a call to \ref Serial_Init() before the stream is used.
  135. */
  136. void Serial_CreateStream(FILE* Stream);
  137. /** Identical to \ref Serial_CreateStream(), except that reads are blocking until the calling stream function terminates
  138. * the transfer.
  139. *
  140. * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed, if \c NULL, \c stdout
  141. * and \c stdin will be configured to use the USART.
  142. *
  143. * \pre The USART must first be configured via a call to \ref Serial_Init() before the stream is used.
  144. */
  145. void Serial_CreateBlockingStream(FILE* Stream);
  146. /* Inline Functions: */
  147. /** Initializes the USART, ready for serial data transmission and reception. This initializes the interface to
  148. * standard 8-bit, no parity, 1 stop bit settings suitable for most applications.
  149. *
  150. * \param[in] BaudRate Serial baud rate, in bits per second. This should be the target baud rate regardless of the
  151. * \c DoubleSpeed parameter's value.
  152. * \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate.
  153. */
  154. static inline void Serial_Init(const uint32_t BaudRate,
  155. const bool DoubleSpeed);
  156. static inline void Serial_Init(const uint32_t BaudRate,
  157. const bool DoubleSpeed)
  158. {
  159. UBRR1 = (DoubleSpeed ? SERIAL_2X_UBBRVAL(BaudRate) : SERIAL_UBBRVAL(BaudRate));
  160. UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10));
  161. UCSR1A = (DoubleSpeed ? (1 << U2X1) : 0);
  162. UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
  163. DDRD |= (1 << 3);
  164. PORTD |= (1 << 2);
  165. }
  166. /** Turns off the USART driver, disabling and returning used hardware to their default configuration. */
  167. static inline void Serial_Disable(void);
  168. static inline void Serial_Disable(void)
  169. {
  170. UCSR1B = 0;
  171. UCSR1A = 0;
  172. UCSR1C = 0;
  173. UBRR1 = 0;
  174. DDRD &= ~(1 << 3);
  175. PORTD &= ~(1 << 2);
  176. }
  177. /** Indicates whether a character has been received through the USART.
  178. *
  179. * \return Boolean \c true if a character has been received, \c false otherwise.
  180. */
  181. static inline bool Serial_IsCharReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  182. static inline bool Serial_IsCharReceived(void)
  183. {
  184. return ((UCSR1A & (1 << RXC1)) ? true : false);
  185. }
  186. /** Indicates whether there is hardware buffer space for a new transmit on the USART. This
  187. * function can be used to determine if a call to \ref Serial_SendByte() will block in advance.
  188. *
  189. * \return Boolean \c true if a character can be queued for transmission immediately, \c false otherwise.
  190. */
  191. static inline bool Serial_IsSendReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  192. static inline bool Serial_IsSendReady(void)
  193. {
  194. return ((UCSR1A & (1 << UDRE1)) ? true : false);
  195. }
  196. /** Indicates whether the hardware USART transmit buffer is completely empty, indicating all
  197. * pending transmissions have completed.
  198. *
  199. * \return Boolean \c true if no characters are buffered for transmission, \c false otherwise.
  200. */
  201. static inline bool Serial_IsSendComplete(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
  202. static inline bool Serial_IsSendComplete(void)
  203. {
  204. return ((UCSR1A & (1 << TXC1)) ? true : false);
  205. }
  206. /** Transmits a given byte through the USART.
  207. *
  208. * \note If no buffer space is available in the hardware USART, this function will block. To check if
  209. * space is available before calling this function, see \ref Serial_IsSendReady().
  210. *
  211. * \param[in] DataByte Byte to transmit through the USART.
  212. */
  213. static inline void Serial_SendByte(const char DataByte) ATTR_ALWAYS_INLINE;
  214. static inline void Serial_SendByte(const char DataByte)
  215. {
  216. while (!(Serial_IsSendReady()));
  217. UDR1 = DataByte;
  218. }
  219. /** Receives the next byte from the USART.
  220. *
  221. * \return Next byte received from the USART, or a negative value if no byte has been received.
  222. */
  223. static inline int16_t Serial_ReceiveByte(void) ATTR_ALWAYS_INLINE;
  224. static inline int16_t Serial_ReceiveByte(void)
  225. {
  226. if (!(Serial_IsCharReceived()))
  227. return -1;
  228. return UDR1;
  229. }
  230. /* Disable C linkage for C++ Compilers: */
  231. #if defined(__cplusplus)
  232. }
  233. #endif
  234. #endif
  235. /** @} */