More AVR32 achitecture ports.
[pub/USBasp.git] / LUFA / Drivers / Peripheral / AVR8 / Serial.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
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 *
33 * ADC driver for the 8-bit AVRs.
34 *
35 * \note This file should not be included directly. It is automatically included as needed by the USART driver
36 * dispatch header located in LUFA/Drivers/Peripheral/Serial.h.
37 */
38
39 /** \ingroup Group_Serial
40 * @defgroup Group_Serial_AVR8 8-Bit AVR Serial Driver
41 *
42 * Serial driver for the 8-bit AVRs.
43 *
44 * \note This file should not be included directly. It is automatically included as needed by the ADC driver
45 * dispatch header located in LUFA/Drivers/Peripheral/USART.h.
46 *
47 * @{
48 */
49
50 #ifndef __SERIAL_AVR8_H__
51 #define __SERIAL_AVR8_H__
52
53 /* Includes: */
54 #include <avr/io.h>
55 #include <avr/pgmspace.h>
56 #include <stdbool.h>
57
58 /* Preprocessor Checks: */
59 #if !defined(__INCLUDE_FROM_SERIAL_H)
60 #error Do not include this file directly. Include LUFA/Drivers/Peripheral/Serial.h instead.
61 #endif
62
63 /* Public Interface - May be used in end-application: */
64 /* Macros: */
65 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
66 * not set.
67 */
68 #define SERIAL_UBBRVAL(baud) ((((F_CPU / 16) + (baud / 2)) / (baud)) - 1)
69
70 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
71 * set.
72 */
73 #define SERIAL_2X_UBBRVAL(baud) ((((F_CPU / 8) + (baud / 2)) / (baud)) - 1)
74
75 /* Pseudo-Function Macros: */
76 #if defined(__DOXYGEN__)
77 /** Indicates whether a character has been received through the USART.
78 *
79 * \return Boolean true if a character has been received, false otherwise
80 */
81 static inline bool Serial_IsCharReceived(void);
82 #else
83 #define Serial_IsCharReceived() ((UCSR1A & (1 << RXC1)) ? true : false)
84 #endif
85
86 /* Inline Functions: */
87 /** Initializes the USART, ready for serial data transmission and reception. This initializes the interface to
88 * standard 8-bit, no parity, 1 stop bit settings suitable for most applications.
89 *
90 * \param[in] BaudRate Serial baud rate, in bits per second
91 * \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate
92 */
93 static inline void Serial_Init(const uint32_t BaudRate, const bool DoubleSpeed)
94 {
95 UCSR1A = (DoubleSpeed ? (1 << U2X1) : 0);
96 UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
97 UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10));
98
99 DDRD |= (1 << 3);
100 PORTD |= (1 << 2);
101
102 UBRR1 = (DoubleSpeed ? SERIAL_2X_UBBRVAL(BaudRate) : SERIAL_UBBRVAL(BaudRate));
103 }
104
105 /** Turns off the USART driver, disabling and returning used hardware to their default configuration. */
106 static inline void Serial_ShutDown(void)
107 {
108 UCSR1A = 0;
109 UCSR1B = 0;
110 UCSR1C = 0;
111
112 DDRD &= ~(1 << 3);
113 PORTD &= ~(1 << 2);
114
115 UBRR1 = 0;
116 }
117
118 /** Transmits a given byte through the USART.
119 *
120 * \param[in] DataByte Byte to transmit through the USART
121 */
122 static inline void Serial_TxByte(const char DataByte)
123 {
124 while (!(UCSR1A & (1 << UDRE1)));
125 UDR1 = DataByte;
126 }
127
128 /** Receives a byte from the USART.
129 *
130 * \return Byte received from the USART
131 */
132 static inline uint8_t Serial_RxByte(void)
133 {
134 while (!(UCSR1A & (1 << RXC1)));
135 return UDR1;
136 }
137
138 /* Disable C linkage for C++ Compilers: */
139 #if defined(__cplusplus)
140 }
141 #endif
142
143 #endif
144
145 /** @} */