Removed new Start of Frame event from the library; performance suffered far too much...
[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, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 * Functions, macros, variables, enums and types related to the setup of the USART for serial communications.
45 *
46 * @{
47 */
48
49 #ifndef __SERIAL_H__
50 #define __SERIAL_H__
51
52 /* Includes: */
53 #include <avr/io.h>
54 #include <avr/pgmspace.h>
55 #include <stdbool.h>
56
57 #include "../../Common/Common.h"
58 #include "../Misc/TerminalCodes.h"
59
60 /* Enable C linkage for C++ Compilers: */
61 #if defined(__cplusplus)
62 extern "C" {
63 #endif
64
65 /* Public Interface - May be used in end-application: */
66 /* Macros: */
67 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
68 * not set.
69 */
70 #define SERIAL_UBBRVAL(baud) (((F_CPU / 16) / baud) - 1)
71
72 /** Macro for calculating the baud value from a given baud rate when the U2X (double speed) bit is
73 * set.
74 */
75 #define SERIAL_2X_UBBRVAL(baud) (((F_CPU / 8) / baud) - 1)
76
77 /* Pseudo-Function Macros: */
78 #if defined(__DOXYGEN__)
79 /** Indicates whether a character has been received through the USART.
80 *
81 * \return Boolean true if a character has been received, false otherwise
82 */
83 static inline bool Serial_IsCharReceived(void);
84 #else
85 #define Serial_IsCharReceived() ((UCSR1A & (1 << RXC1)) ? true : false)
86 #endif
87
88 /* Function Prototypes: */
89 /** Transmits a given string located in program space (FLASH) through the USART.
90 *
91 * \param FlashStringPtr Pointer to a string located in program space
92 */
93 void Serial_TxString_P(const char *FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
94
95 /** Transmits a given string located in SRAM memory through the USART.
96 *
97 * \param StringPtr Pointer to a string located in SRAM space
98 */
99 void Serial_TxString(const char *StringPtr) ATTR_NON_NULL_PTR_ARG(1);
100
101 /* Inline Functions: */
102 /** Initializes the USART, ready for serial data transmission and reception. This initialises the interface to
103 * standard 8-bit, no parity, 1 stop bit settings suitable for most applications.
104 *
105 * \param BaudRate Serial baud rate, in bits per second
106 * \param DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate
107 */
108 static inline void Serial_Init(const uint32_t BaudRate, const bool DoubleSpeed)
109 {
110 UCSR1A = (DoubleSpeed ? (1 << U2X1) : 0);
111 UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
112 UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10));
113
114 DDRD |= (1 << 3);
115 PORTD |= (1 << 2);
116
117 UBRR1 = (DoubleSpeed ? SERIAL_2X_UBBRVAL(BaudRate) : SERIAL_UBBRVAL(BaudRate));
118 }
119
120 /** Transmits a given byte through the USART.
121 *
122 * \param DataByte Byte to transmit through the USART
123 */
124 static inline void Serial_TxByte(const char DataByte)
125 {
126 while (!(UCSR1A & (1 << UDRE1)));
127 UDR1 = DataByte;
128 }
129
130 /** Receives a byte from the USART.
131 *
132 * \return Byte received from the USART
133 */
134 static inline char Serial_RxByte(void)
135 {
136 while (!(UCSR1A & (1 << RXC1)));
137 return UDR1;
138 }
139
140 /* Disable C linkage for C++ Compilers: */
141 #if defined(__cplusplus)
142 }
143 #endif
144
145 #endif
146
147 /** @} */