Rename Drivers/AT90USBXXX to Drivers/Peripheral.
[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_SubsystemDrivers
37 * @defgroup Group_Serial Serial USART Driver - LUFA/Drivers/Peripheral/Serial.h
38 *
39 * Functions, macros, variables, enums and types related to the setup of the USART for serial communications.
40 *
41 * @{
42 */
43
44 #ifndef __SERIAL_H__
45 #define __SERIAL_H__
46
47 /* Includes: */
48 #include <avr/io.h>
49 #include <avr/pgmspace.h>
50 #include <stdbool.h>
51
52 #include "../../Common/Common.h"
53 #include "../Misc/TerminalCodes.h"
54
55 /* Enable C linkage for C++ Compilers: */
56 #if defined(__cplusplus)
57 extern "C" {
58 #endif
59
60 /* Public Interface - May be used in end-application: */
61 /* Macros: */
62 /** Indicates whether a character has been received through the USART - boolean false if no character
63 * has been received, or non-zero if a character is waiting to be read from the reception buffer.
64 */
65 #define Serial_IsCharReceived() ((UCSR1A & (1 << RXC1)) ? true : false)
66
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 /* Function Prototypes: */
78 /** Initializes the USART, ready for serial data transmission and reception.
79 *
80 * \param BaudRate Baud rate to configure the USART to
81 * \param DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate
82 */
83 void Serial_Init(const uint32_t BaudRate, const bool DoubleSpeed);
84
85 /** Transmits a given string located in program space (FLASH) through the USART.
86 *
87 * \param FlashStringPtr Pointer to a string located in program space
88 */
89 void Serial_TxString_P(const char *FlashStringPtr) ATTR_NON_NULL_PTR_ARG(1);
90
91 /** Transmits a given string located in SRAM memory through the USART.
92 *
93 * \param StringPtr Pointer to a string located in SRAM space
94 */
95 void Serial_TxString(const char *StringPtr) ATTR_NON_NULL_PTR_ARG(1);
96
97 /* Inline Functions: */
98 /** Transmits a given byte through the USART.
99 *
100 * \param DataByte Byte to transmit through the USART
101 */
102 static inline void Serial_TxByte(const char DataByte)
103 {
104 while (!(UCSR1A & (1 << UDRE1)));
105 UDR1 = DataByte;
106 }
107
108 /** Receives a byte from the USART.
109 *
110 * \return Byte received from the USART
111 */
112 static inline char Serial_RxByte(void)
113 {
114 while (!(UCSR1A & (1 << RXC1)));
115 return UDR1;
116 }
117
118 /* Disable C linkage for C++ Compilers: */
119 #if defined(__cplusplus)
120 }
121 #endif
122
123 #endif
124
125 /** @} */