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