]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Peripheral/XMEGA/SPI_XMEGA.h
Change TOP_DIR to TMK_DIR in makefiles
[tmk_keyboard.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Peripheral / XMEGA / SPI_XMEGA.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 * \brief SPI Peripheral Driver (XMEGA)
33 *
34 * On-chip SPI driver for the XMEGA microcontrollers.
35 *
36 * \note This file should not be included directly. It is automatically included as needed by the SPI driver
37 * dispatch header located in LUFA/Drivers/Peripheral/SPI.h.
38 */
39
40 /** \ingroup Group_SPI
41 * \defgroup Group_SPI_XMEGA SPI Peripheral Driver (XMEGA)
42 *
43 * \section Sec_ModDescription Module Description
44 * Driver for the hardware SPI port(s) available on XMEGA AVR microcontroller models. This
45 * module provides an easy to use driver for the setup and transfer of data over the AVR's
46 * SPI ports.
47 *
48 * \note This file should not be included directly. It is automatically included as needed by the SPI driver
49 * dispatch header located in LUFA/Drivers/Peripheral/SPI.h.
50 *
51 * \code
52 * // Initialize the SPI driver before first use
53 * SPI_Init(&SPIC,
54 * SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
55 * SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
56 *
57 * // Send several bytes, ignoring the returned data
58 * SPI_SendByte(&SPIC, 0x01);
59 * SPI_SendByte(&SPIC, 0x02);
60 * SPI_SendByte(&SPIC, 0x03);
61 *
62 * // Receive several bytes, sending a dummy 0x00 byte each time
63 * uint8_t Byte1 = SPI_ReceiveByte(&SPIC);
64 * uint8_t Byte2 = SPI_ReceiveByte(&SPIC);
65 * uint8_t Byte3 = SPI_ReceiveByte(&SPIC);
66 *
67 * // Send a byte, and store the received byte from the same transaction
68 * uint8_t ResponseByte = SPI_TransferByte(&SPIC, 0xDC);
69 * \endcode
70 *
71 * @{
72 */
73
74 #ifndef __SPI_XMEGA_H__
75 #define __SPI_XMEGA_H__
76
77 /* Includes: */
78 #include "../../../Common/Common.h"
79
80 /* Enable C linkage for C++ Compilers: */
81 #if defined(__cplusplus)
82 extern "C" {
83 #endif
84
85 /* Preprocessor Checks: */
86 #if !defined(__INCLUDE_FROM_SPI_H)
87 #error Do not include this file directly. Include LUFA/Drivers/Peripheral/SPI.h instead.
88 #endif
89
90 /* Private Interface - For use in library only: */
91 #if !defined(__DOXYGEN__)
92 /* Macros: */
93 #define SPI_USE_DOUBLESPEED SPI_CLK2X_bm
94 #endif
95
96 /* Public Interface - May be used in end-application: */
97 /* Macros: */
98 /** \name SPI Prescaler Configuration Masks */
99 //@{
100 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 2. */
101 #define SPI_SPEED_FCPU_DIV_2 SPI_USE_DOUBLESPEED
102
103 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 4. */
104 #define SPI_SPEED_FCPU_DIV_4 0
105
106 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 8. */
107 #define SPI_SPEED_FCPU_DIV_8 (SPI_USE_DOUBLESPEED | (1 << SPI_PRESCALER_gp))
108
109 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 16. */
110 #define SPI_SPEED_FCPU_DIV_16 (1 << SPI_PRESCALER_gp)
111
112 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 32. */
113 #define SPI_SPEED_FCPU_DIV_32 (SPI_USE_DOUBLESPEED | (2 << SPI_PRESCALER_gp))
114
115 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 64. */
116 #define SPI_SPEED_FCPU_DIV_64 (2 << SPI_PRESCALER_gp)
117
118 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 128. */
119 #define SPI_SPEED_FCPU_DIV_128 (3 << SPI_PRESCALER_gp)
120 //@}
121
122 /** \name SPI SCK Polarity Configuration Masks */
123 //@{
124 /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the rising edge. */
125 #define SPI_SCK_LEAD_RISING 0
126
127 /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the falling edge. */
128 #define SPI_SCK_LEAD_FALLING SPI_MODE1_bm
129 //@}
130
131 /** \name SPI Sample Edge Configuration Masks */
132 //@{
133 /** SPI data sample mode mask for \ref SPI_Init(). Indicates that the data should sampled on the leading edge. */
134 #define SPI_SAMPLE_LEADING 0
135
136 /** SPI data sample mode mask for \ref SPI_Init(). Indicates that the data should be sampled on the trailing edge. */
137 #define SPI_SAMPLE_TRAILING SPI_MODE0_bm
138 //@}
139
140 /** \name SPI Data Ordering Configuration Masks */
141 //@{
142 /** SPI data order mask for \ref SPI_Init(). Indicates that data should be shifted out MSB first. */
143 #define SPI_ORDER_MSB_FIRST 0
144
145 /** SPI data order mask for \ref SPI_Init(). Indicates that data should be shifted out LSB first. */
146 #define SPI_ORDER_LSB_FIRST SPI_DORD_bm
147 //@}
148
149 /** \name SPI Mode Configuration Masks */
150 //@{
151 /** SPI mode mask for \ref SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
152 #define SPI_MODE_SLAVE 0
153
154 /** SPI mode mask for \ref SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
155 #define SPI_MODE_MASTER SPI_MASTER_bm
156 //@}
157
158 /* Inline Functions: */
159 /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other
160 * SPI routines.
161 *
162 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
163 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the \c SPI_SPEED_*,
164 * \c SPI_SCK_*, \c SPI_SAMPLE_*, \c SPI_ORDER_* and \c SPI_MODE_* masks.
165 */
166 static inline void SPI_Init(SPI_t* const SPI,
167 const uint8_t SPIOptions)
168 {
169 SPI->CTRL = (SPIOptions | SPI_ENABLE_bm);
170 }
171
172 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration.
173 *
174 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
175 */
176 static inline void SPI_Disable(SPI_t* const SPI)
177 {
178 SPI->CTRL &= ~SPI_ENABLE_bm;
179 }
180
181 /** Retrieves the currently selected SPI mode, once the SPI interface has been configured.
182 *
183 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
184 *
185 * \return \ref SPI_MODE_MASTER if the interface is currently in SPI Master mode, \ref SPI_MODE_SLAVE otherwise
186 */
187 static inline uint8_t SPI_GetCurrentMode(SPI_t* const SPI) ATTR_ALWAYS_INLINE;
188 static inline uint8_t SPI_GetCurrentMode(SPI_t* const SPI)
189 {
190 return (SPI->CTRL & SPI_MASTER_bm);
191 }
192
193 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
194 *
195 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
196 * \param[in] Byte Byte to send through the SPI interface.
197 *
198 * \return Response byte from the attached SPI device.
199 */
200 static inline uint8_t SPI_TransferByte(SPI_t* const SPI,
201 const uint8_t Byte) ATTR_ALWAYS_INLINE;
202 static inline uint8_t SPI_TransferByte(SPI_t* const SPI,
203 const uint8_t Byte)
204 {
205 SPI->DATA = Byte;
206 while (!(SPI->STATUS & SPI_IF_bm));
207 return SPI->DATA;
208 }
209
210 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
211 * byte sent to from the attached SPI device is ignored.
212 *
213 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
214 * \param[in] Byte Byte to send through the SPI interface.
215 */
216 static inline void SPI_SendByte(SPI_t* const SPI,
217 const uint8_t Byte) ATTR_ALWAYS_INLINE;
218 static inline void SPI_SendByte(SPI_t* const SPI,
219 const uint8_t Byte)
220 {
221 SPI->DATA = Byte;
222 while (!(SPI->STATUS & SPI_IF_bm));
223 }
224
225 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
226 * byte from the attached SPI device is returned.
227 *
228 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
229 *
230 * \return The response byte from the attached SPI device.
231 */
232 static inline uint8_t SPI_ReceiveByte(SPI_t* const SPI) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
233 static inline uint8_t SPI_ReceiveByte(SPI_t* const SPI)
234 {
235 SPI->DATA = 0;
236 while (!(SPI->STATUS & SPI_IF_bm));
237 return SPI->DATA;
238 }
239
240 /* Disable C linkage for C++ Compilers: */
241 #if defined(__cplusplus)
242 }
243 #endif
244
245 #endif
246
247 /** @} */
248
Imprint / Impressum