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