Fix MIT license language to make its intent clearer.
[pub/USBasp.git] / LUFA / Drivers / Peripheral / Serial.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 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 *
33 * Driver for the USART subsystem on supported USB AVRs.
34 */
35
36 /** \ingroup Group_PeripheralDrivers
37 * @defgroup Group_Serial Serial USART Driver - LUFA/Drivers/Peripheral/Serial.h
38 *
39 * \section Sec_Dependencies Module Source Dependencies
40 * The following files must be built with any user project that uses this module:
41 * - LUFA/Drivers/Peripheral/Serial.c
42 *
43 * \section Module Description
44 * Hardware serial USART driver. This module provides an easy to use driver for
45 * the setup of and transfer of data over the AVR's USART port.
46 *
47 * @{
48 */
49
50 #ifndef __SERIAL_H__
51 #define __SERIAL_H__
52
53 /* Includes: */
54 #include <avr/io.h>
55 #include <avr/pgmspace.h>
56 #include <stdbool.h>
57
58 #include "../../Common/Common.h"
59 #include "../Misc/TerminalCodes.h"
60
61 /* Enable C linkage for C++ Compilers: */
62 #if defined(__cplusplus)
63 extern "C" {
64 #endif
65
66 /* Public Interface - May be used in end-application: */
67 /* Macros: */
68 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
69 * not set.
70 */
71 #define SERIAL_UBBRVAL(baud) (((F_CPU / 16) / (baud)) - 1)
72
73 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
74 * set.
75 */
76 #define SERIAL_2X_UBBRVAL(baud) (((F_CPU / 8) / (baud)) - 1)
77
78 /* Pseudo-Function Macros: */
79 #if defined(__DOXYGEN__)
80 /** Indicates whether a character has been received through the USART.
81 *
82 * \return Boolean true if a character has been received, false otherwise
83 */
84 static inline bool Serial_IsCharReceived(void);
85 #else
86 #define Serial_IsCharReceived() ((UCSR1A & (1 << RXC1)) ? true : false)
87 #endif
88
89 /* Function Prototypes: */
90 /** Transmits a given string located in program space (FLASH) through the USART.
91 *
92 * \param[in] FlashStringPtr Pointer to a string located in program space
93 */
94 void Serial_TxString_P(const char *FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
95
96 /** Transmits a given string located in SRAM memory through the USART.
97 *
98 * \param[in] StringPtr Pointer to a string located in SRAM space
99 */
100 void Serial_TxString(const char *StringPtr) ATTR_NON_NULL_PTR_ARG(1);
101
102 /* Inline Functions: */
103 /** Initializes the USART, ready for serial data transmission and reception. This initializes the interface to
104 * standard 8-bit, no parity, 1 stop bit settings suitable for most applications.
105 *
106 * \param[in] BaudRate Serial baud rate, in bits per second
107 * \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate
108 */
109 static inline void Serial_Init(const uint32_t BaudRate, const bool DoubleSpeed)
110 {
111 UCSR1A = (DoubleSpeed ? (1 << U2X1) : 0);
112 UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
113 UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10));
114
115 DDRD |= (1 << 3);
116 PORTD |= (1 << 2);
117
118 UBRR1 = (DoubleSpeed ? SERIAL_2X_UBBRVAL(BaudRate) : SERIAL_UBBRVAL(BaudRate));
119 }
120
121 /** Turns off the USART driver, disabling and returning used hardware to their default configuration. */
122 static inline void Serial_ShutDown(void)
123 {
124 UCSR1A = 0;
125 UCSR1B = 0;
126 UCSR1C = 0;
127
128 DDRD &= ~(1 << 3);
129 PORTD &= ~(1 << 2);
130
131 UBRR1 = 0;
132 }
133
134 /** Transmits a given byte through the USART.
135 *
136 * \param[in] DataByte Byte to transmit through the USART
137 */
138 static inline void Serial_TxByte(const char DataByte)
139 {
140 while (!(UCSR1A & (1 << UDRE1)));
141 UDR1 = DataByte;
142 }
143
144 /** Receives a byte from the USART.
145 *
146 * \return Byte received from the USART
147 */
148 static inline char Serial_RxByte(void)
149 {
150 while (!(UCSR1A & (1 << RXC1)));
151 return UDR1;
152 }
153
154 /* Disable C linkage for C++ Compilers: */
155 #if defined(__cplusplus)
156 }
157 #endif
158
159 #endif
160
161 /** @} */