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