Change bootloader makefiles to automatically compute the bootloader starting address...
[pub/USBasp.git] / LUFA / Drivers / Peripheral / Serial.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 disclaim 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 Master include file for the USART peripheral driver.
33 *
34 * Driver for the USART subsystem on supported USB AVRs.
35 */
36
37 /** \ingroup Group_PeripheralDrivers
38 * @defgroup Group_Serial Serial USART Driver - LUFA/Drivers/Peripheral/Serial.h
39 *
40 * \section Sec_Dependencies Module Source Dependencies
41 * The following files must be built with any user project that uses this module:
42 * - LUFA/Drivers/Peripheral/Serial.c <i>(Makefile source module name: LUFA_SRC_SERIAL)</i>
43 *
44 * \section Sec_ModDescription Module Description
45 * Hardware serial USART driver. This module provides an easy to use driver for
46 * the setup of and transfer of data over the AVR's USART port.
47 *
48 * \section Sec_ExampleUsage Example Usage
49 * The following snippet is an example of how this module may be used within a typical
50 * application.
51 *
52 * \code
53 * // Initialise the serial USART driver before first use, with 9600 baud (and no double-speed mode)
54 * Serial_Init(9600, false);
55 *
56 * // Send a string through the USART
57 * Serial_TxString("Test String\r\n");
58 *
59 * // Receive a byte through the USART
60 * uint8_t DataByte = Serial_RxByte();
61 * \endcode
62 *
63 * @{
64 */
65
66 #ifndef __SERIAL_H__
67 #define __SERIAL_H__
68
69 /* Includes: */
70 #include <avr/io.h>
71 #include <avr/pgmspace.h>
72 #include <stdbool.h>
73 #include <stdio.h>
74
75 #include "../../Common/Common.h"
76 #include "../Misc/TerminalCodes.h"
77
78 /* Enable C linkage for C++ Compilers: */
79 #if defined(__cplusplus)
80 extern "C" {
81 #endif
82
83 /* Private Interface - For use in library only: */
84 #if !defined(__DOXYGEN__)
85 /* External Variables: */
86 extern FILE USARTSerialStream;
87
88 /* Function Prototypes: */
89 int Serial_putchar(char DataByte,
90 FILE *Stream);
91 int Serial_getchar(FILE *Stream);
92 int Serial_getchar_Blocking(FILE *Stream);
93 #endif
94
95 /* Public Interface - May be used in end-application: */
96 /* Macros: */
97 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
98 * not set.
99 */
100 #define SERIAL_UBBRVAL(baud) ((((F_CPU / 16) + (baud / 2)) / (baud)) - 1)
101
102 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
103 * set.
104 */
105 #define SERIAL_2X_UBBRVAL(baud) ((((F_CPU / 8) + (baud / 2)) / (baud)) - 1)
106
107 /* Function Prototypes: */
108 /** Transmits a given string located in program space (FLASH) through the USART.
109 *
110 * \param[in] FlashStringPtr Pointer to a string located in program space.
111 */
112 void Serial_SendString_P(const char* FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
113
114 /** Transmits a given string located in SRAM memory through the USART.
115 *
116 * \param[in] StringPtr Pointer to a string located in SRAM space.
117 */
118 void Serial_SendString(const char* StringPtr) ATTR_NON_NULL_PTR_ARG(1);
119
120 /** Transmits a given buffer located in SRAM memory through the USART.
121 *
122 * \param[in] Buffer Pointer to a buffer containing the data to send.
123 * \param[in] Length Length of the data to send, in bytes.
124 */
125 void Serial_SendData(const uint8_t* Buffer, uint16_t Length) ATTR_NON_NULL_PTR_ARG(1);
126
127 /* Inline Functions: */
128 /** Initializes the USART, ready for serial data transmission and reception. This initializes the interface to
129 * standard 8-bit, no parity, 1 stop bit settings suitable for most applications.
130 *
131 * \param[in] BaudRate Serial baud rate, in bits per second.
132 * \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate.
133 */
134 static inline void Serial_Init(const uint32_t BaudRate,
135 const bool DoubleSpeed)
136 {
137 UBRR1 = (DoubleSpeed ? SERIAL_2X_UBBRVAL(BaudRate) : SERIAL_UBBRVAL(BaudRate));
138
139 UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10));
140 UCSR1A = (DoubleSpeed ? (1 << U2X1) : 0);
141 UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
142
143 DDRD |= (1 << 3);
144 PORTD |= (1 << 2);
145 }
146
147 /** Turns off the USART driver, disabling and returning used hardware to their default configuration. */
148 static inline void Serial_Disable(void)
149 {
150 UCSR1B = 0;
151 UCSR1A = 0;
152 UCSR1C = 0;
153
154 UBRR1 = 0;
155
156 DDRD &= ~(1 << 3);
157 PORTD &= ~(1 << 2);
158 }
159
160 /** Creates a standard character stream from the USART so that it can be used with all the regular functions
161 * in the avr-libc \c <stdio.h> library that accept a \c FILE stream as a destination (e.g. \c fprintf). The created
162 * stream is bidirectional and can be used for both input and output functions.
163 *
164 * Reading data from this stream is non-blocking, i.e. in most instances, complete strings cannot be read in by a single
165 * fetch, as the endpoint will not be ready at some point in the transmission, aborting the transfer. However, this may
166 * be used when the read data is processed byte-per-bye (via \c getc()) or when the user application will implement its own
167 * line buffering.
168 *
169 * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed, if \c NULL stdio
170 * and stdin will be configured to use the USART.
171 *
172 * \pre The USART must first be configured via a call to \ref Serial_Init() before the stream is used.
173 */
174 static inline void Serial_CreateStream(FILE* Stream)
175 {
176 if (!(Stream))
177 {
178 Stream = &USARTSerialStream;
179 stdin = Stream;
180 stdout = Stream;
181 }
182
183 *Stream = (FILE)FDEV_SETUP_STREAM(Serial_putchar, Serial_getchar, _FDEV_SETUP_RW);
184 }
185
186 /** Identical to \ref Serial_CreateStream(), except that reads are blocking until the calling stream function terminates
187 * the transfer.
188 *
189 * \param[in,out] Stream Pointer to a FILE structure where the created stream should be placed, if \c NULL stdio
190 * and stdin will be configured to use the USART.
191 *
192 * \pre The USART must first be configured via a call to \ref Serial_Init() before the stream is used.
193 */
194 static inline void Serial_CreateBlockingStream(FILE* Stream)
195 {
196 if (!(Stream))
197 {
198 Stream = &USARTSerialStream;
199 stdin = Stream;
200 stdout = Stream;
201 }
202
203 *Stream = (FILE)FDEV_SETUP_STREAM(Serial_putchar, Serial_getchar_Blocking, _FDEV_SETUP_RW);
204 }
205
206 /** Indicates whether a character has been received through the USART.
207 *
208 * \return Boolean \c true if a character has been received, \c false otherwise.
209 */
210 static inline bool Serial_IsCharReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
211 static inline bool Serial_IsCharReceived(void)
212 {
213 return ((UCSR1A & (1 << RXC1)) ? true : false);
214 }
215
216 /** Transmits a given byte through the USART.
217 *
218 * \param[in] DataByte Byte to transmit through the USART.
219 */
220 static inline void Serial_SendByte(const char DataByte) ATTR_ALWAYS_INLINE;
221 static inline void Serial_SendByte(const char DataByte)
222 {
223 while (!(UCSR1A & (1 << UDRE1)));
224 UDR1 = DataByte;
225 }
226
227 /** Receives the next byte from the USART.
228 *
229 * \return Next byte received from the USART, or a negative value if no byte has been received.
230 */
231 static inline int16_t Serial_ReceiveByte(void) ATTR_ALWAYS_INLINE;
232 static inline int16_t Serial_ReceiveByte(void)
233 {
234 if (!(Serial_IsCharReceived()))
235 return -1;
236
237 return UDR1;
238 }
239
240 /* Disable C linkage for C++ Compilers: */
241 #if defined(__cplusplus)
242 }
243 #endif
244
245 #endif
246
247 /** @} */
248