]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/LUFA/Drivers/Peripheral/AVR8/SerialSPI_AVR8.h
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / LUFA / Drivers / Peripheral / AVR8 / SerialSPI_AVR8.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 disclaims 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 SPI Mode Serial USART Peripheral Driver (XMEGA)
33 *
34 * On-chip Master SPI mode USART driver for the XMEGA AVR microcontrollers.
35 *
36 * \note This file should not be included directly. It is automatically included as needed by the SPI Master
37 * Mode USART driver dispatch header located in LUFA/Drivers/Peripheral/Serial.h.
38 */
39
40 /** \ingroup Group_SerialSPI
41 * \defgroup Group_SerialSPI_AVR8 Master SPI Mode Serial USART Peripheral Driver (AVR8)
42 *
43 * \section Sec_SerialSPI_AVR8_ModDescription Module Description
44 * On-chip serial USART driver for the 8-bit AVR8 microcontrollers.
45 *
46 * \note This file should not be included directly. It is automatically included as needed by the SPI Master
47 * driver dispatch header located in LUFA/Drivers/Peripheral/SerialSPI.h.
48 *
49 * \section Sec_SerialSPI_AVR8_ExampleUsage Example Usage
50 * The following snippet is an example of how this module may be used within a typical
51 * application.
52 *
53 * \code
54 * // Initialize the Master SPI mode USART driver before first use, with 1Mbit baud
55 * SerialSPI_Init((USART_SPI_SCK_LEAD_RISING | USART_SPI_SAMPLE_LEADING | USART_SPI_ORDER_MSB_FIRST), 1000000);
56 *
57 * // Send several bytes, ignoring the returned data
58 * SerialSPI_SendByte(0x01);
59 * SerialSPI_SendByte(0x02);
60 * SerialSPI_SendByte(0x03);
61 *
62 * // Receive several bytes, sending a dummy 0x00 byte each time
63 * uint8_t Byte1 = SerialSPI_ReceiveByte();
64 * uint8_t Byte2 = SerialSPI_ReceiveByte();
65 * uint8_t Byte3 = SerialSPI_ReceiveByte();
66 *
67 * // Send a byte, and store the received byte from the same transaction
68 * uint8_t ResponseByte = SerialSPI_TransferByte(0xDC);
69 * \endcode
70 *
71 * @{
72 */
73
74 #ifndef __SERIAL_SPI_AVR8_H__
75 #define __SERIAL_SPI_AVR8_H__
76
77 /* Includes: */
78 #include "../../../Common/Common.h"
79
80 #include <stdio.h>
81
82 /* Enable C linkage for C++ Compilers: */
83 #if defined(__cplusplus)
84 extern "C" {
85 #endif
86
87 /* Preprocessor Checks: */
88 #if !defined(__INCLUDE_FROM_SERIAL_SPI_H)
89 #error Do not include this file directly. Include LUFA/Drivers/Peripheral/Serial.h instead.
90 #endif
91
92 /* Private Interface - For use in library only: */
93 #if !defined(__DOXYGEN__)
94 #define SERIAL_SPI_UBBRVAL(Baud) ((Baud < (F_CPU / 2)) ? ((F_CPU / (2 * Baud)) - 1) : 0)
95
96 /* Master USART SPI mode flag definitions missing in the AVR8 toolchain */
97 #if !defined(UCPHA1)
98 #define UCPHA1 1
99 #endif
100 #if !defined(UDORD1)
101 #define UDORD1 2
102 #endif
103 #endif
104
105 /* Public Interface - May be used in end-application: */
106 /* Macros: */
107 /** \name SPI SCK Polarity Configuration Masks */
108 //@{
109 /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the rising edge. */
110 #define USART_SPI_SCK_LEAD_RISING (0 << UCPOL1)
111
112 /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the falling edge. */
113 #define USART_SPI_SCK_LEAD_FALLING (1 << UCPOL1)
114 //@}
115
116 /** \name SPI Sample Edge Configuration Masks */
117 //@{
118 /** SPI data sample mode mask for \ref SerialSPI_Init(). Indicates that the data should sampled on the leading edge. */
119 #define USART_SPI_SAMPLE_LEADING (0 << UCPHA1)
120
121 /** SPI data sample mode mask for \ref SerialSPI_Init(). Indicates that the data should be sampled on the trailing edge. */
122 #define USART_SPI_SAMPLE_TRAILING (1 << UCPHA1)
123 //@}
124
125 /** \name SPI Data Ordering Configuration Masks */
126 //@{
127 /** SPI data order mask for \ref SerialSPI_Init(). Indicates that data should be shifted out MSB first. */
128 #define USART_SPI_ORDER_MSB_FIRST (0 << UDORD1)
129
130 /** SPI data order mask for \ref SerialSPI_Init(). Indicates that data should be shifted out LSB first. */
131 #define USART_SPI_ORDER_LSB_FIRST (1 << UDORD1)
132 //@}
133
134 /* Inline Functions: */
135 /** Initialize the USART module in Master SPI mode.
136 *
137 * \param[in] SPIOptions USART SPI Options, a mask consisting of one of each of the \c USART_SPI_SCK_*,
138 * \c USART_SPI_SAMPLE_* and \c USART_SPI_ORDER_* masks.
139 * \param[in] BaudRate SPI baud rate, in bits per second.
140 */
141 static inline void SerialSPI_Init(const uint8_t SPIOptions,
142 const uint32_t BaudRate)
143 {
144 DDRD |= ((1 << 3) | (1 << 5));
145 PORTD |= (1 << 2);
146
147 UCSR1C = ((1 << UMSEL11) | (1 << UMSEL10) | SPIOptions);
148 UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
149
150 UBRR1 = SERIAL_SPI_UBBRVAL(BaudRate);
151 }
152
153 /** Turns off the USART driver, disabling and returning used hardware to their default configuration. */
154 static inline void SerialSPI_Disable(void)
155 {
156 UCSR1B = 0;
157 UCSR1A = 0;
158 UCSR1C = 0;
159
160 UBRR1 = 0;
161
162 DDRD &= ~((1 << 3) | (1 << 5));
163 PORTD &= ~(1 << 2);
164 }
165
166 /** Sends and receives a byte through the USART SPI interface, blocking until the transfer is complete.
167 *
168 * \param[in] DataByte Byte to send through the USART SPI interface.
169 *
170 * \return Response byte from the attached SPI device.
171 */
172 static inline uint8_t SerialSPI_TransferByte(const uint8_t DataByte)
173 {
174 UDR1 = DataByte;
175 while (!(UCSR1A & (1 << TXC1)));
176 UCSR1A = (1 << TXC1);
177 return UDR1;
178 }
179
180 /** Sends a byte through the USART SPI interface, blocking until the transfer is complete. The response
181 * byte sent to from the attached SPI device is ignored.
182 *
183 * \param[in] DataByte Byte to send through the USART SPI interface.
184 */
185 static inline void SerialSPI_SendByte(const uint8_t DataByte)
186 {
187 SerialSPI_TransferByte(DataByte);
188 }
189
190 /** Sends a dummy byte through the USART SPI interface, blocking until the transfer is complete. The response
191 * byte from the attached SPI device is returned.
192 *
193 * \return The response byte from the attached SPI device.
194 */
195 static inline uint8_t SerialSPI_ReceiveByte(void)
196 {
197 return SerialSPI_TransferByte(0);
198 }
199
200 /* Disable C linkage for C++ Compilers: */
201 #if defined(__cplusplus)
202 }
203 #endif
204
205 #endif
206
207 /** @} */
208
Imprint / Impressum