Added basic driver example use code to the library documentation.
[pub/USBasp.git] / LUFA / Drivers / Peripheral / Serial.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 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 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 * <b>Example Usage:</b>
49 * \code
50 * // Initialise the serial USART driver before first use, with 9600 baud (and no double-speed mode)
51 * Serial_Init(9600, false);
52 *
53 * // Send a string through the USART
54 * Serial_TxString("Test String\r\n");
55 *
56 * // Receive a byte through the USART
57 * uint8_t DataByte = Serial_RxByte();
58 * \endcode
59 *
60 * @{
61 */
62
63 #ifndef __SERIAL_H__
64 #define __SERIAL_H__
65
66 /* Includes: */
67 #include <avr/io.h>
68 #include <avr/pgmspace.h>
69 #include <stdbool.h>
70
71 #include "../../Common/Common.h"
72 #include "../Misc/TerminalCodes.h"
73
74 /* Enable C linkage for C++ Compilers: */
75 #if defined(__cplusplus)
76 extern "C" {
77 #endif
78
79 /* Public Interface - May be used in end-application: */
80 /* Macros: */
81 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
82 * not set.
83 */
84 #define SERIAL_UBBRVAL(baud) ((((F_CPU / 16) + (baud / 2)) / (baud)) - 1)
85
86 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
87 * set.
88 */
89 #define SERIAL_2X_UBBRVAL(baud) ((((F_CPU / 8) + (baud / 2)) / (baud)) - 1)
90
91 /* Function Prototypes: */
92 /** Transmits a given string located in program space (FLASH) through the USART.
93 *
94 * \param[in] FlashStringPtr Pointer to a string located in program space.
95 */
96 void Serial_TxString_P(const char* FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
97
98 /** Transmits a given string located in SRAM memory through the USART.
99 *
100 * \param[in] StringPtr Pointer to a string located in SRAM space.
101 */
102 void Serial_TxString(const char* StringPtr) ATTR_NON_NULL_PTR_ARG(1);
103
104 /* Inline Functions: */
105 /** Initializes the USART, ready for serial data transmission and reception. This initializes the interface to
106 * standard 8-bit, no parity, 1 stop bit settings suitable for most applications.
107 *
108 * \param[in] BaudRate Serial baud rate, in bits per second.
109 * \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate.
110 */
111 static inline void Serial_Init(const uint32_t BaudRate,
112 const bool DoubleSpeed)
113 {
114 UBRR1 = (DoubleSpeed ? SERIAL_2X_UBBRVAL(BaudRate) : SERIAL_UBBRVAL(BaudRate));
115
116 UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10));
117 UCSR1A = (DoubleSpeed ? (1 << U2X1) : 0);
118 UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
119
120 DDRD |= (1 << 3);
121 PORTD |= (1 << 2);
122 }
123
124 /** Turns off the USART driver, disabling and returning used hardware to their default configuration. */
125 static inline void Serial_ShutDown(void)
126 {
127 UCSR1B = 0;
128 UCSR1A = 0;
129 UCSR1C = 0;
130
131 UBRR1 = 0;
132
133 DDRD &= ~(1 << 3);
134 PORTD &= ~(1 << 2);
135 }
136
137 /** Indicates whether a character has been received through the USART.
138 *
139 * \return Boolean true if a character has been received, false otherwise.
140 */
141 static inline bool Serial_IsCharReceived(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
142 static inline bool Serial_IsCharReceived(void)
143 {
144 return ((UCSR1A & (1 << RXC1)) ? true : false);
145 }
146
147 /** Transmits a given byte through the USART.
148 *
149 * \param[in] DataByte Byte to transmit through the USART.
150 */
151 static inline void Serial_TxByte(const char DataByte) ATTR_ALWAYS_INLINE;
152 static inline void Serial_TxByte(const char DataByte)
153 {
154 while (!(UCSR1A & (1 << UDRE1)));
155 UDR1 = DataByte;
156 }
157
158 /** Receives a byte from the USART. This function blocks until a byte has been
159 * received; if non-blocking behaviour is required, test for a received character
160 * beforehand with \ref Serial_IsCharReceived().
161 *
162 * \return Byte received from the USART.
163 */
164 static inline char Serial_RxByte(void) ATTR_ALWAYS_INLINE;
165 static inline char Serial_RxByte(void)
166 {
167 while (!(UCSR1A & (1 << RXC1)));
168 return UDR1;
169 }
170
171 /* Disable C linkage for C++ Compilers: */
172 #if defined(__cplusplus)
173 }
174 #endif
175
176 #endif
177
178 /** @} */
179