3 Copyright (C) Dean Camera, 2013.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
32 * \brief SPI Peripheral Driver (XMEGA)
34 * On-chip SPI driver for the XMEGA microcontrollers.
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.
40 /** \ingroup Group_SPI
41 * \defgroup Group_SPI_XMEGA SPI Peripheral Driver (XMEGA)
43 * \section Sec_SPI_XMEGA_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
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.
52 * // Initialize the SPI driver before first use
54 * SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
55 * SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
57 * // Send several bytes, ignoring the returned data
58 * SPI_SendByte(&SPIC, 0x01);
59 * SPI_SendByte(&SPIC, 0x02);
60 * SPI_SendByte(&SPIC, 0x03);
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);
67 * // Send a byte, and store the received byte from the same transaction
68 * uint8_t ResponseByte = SPI_TransferByte(&SPIC, 0xDC);
74 #ifndef __SPI_XMEGA_H__
75 #define __SPI_XMEGA_H__
78 #include "../../../Common/Common.h"
80 /* Enable C linkage for C++ Compilers: */
81 #if defined(__cplusplus)
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.
90 /* Private Interface - For use in library only: */
91 #if !defined(__DOXYGEN__)
93 #define SPI_USE_DOUBLESPEED SPI_CLK2X_bm
96 /* Public Interface - May be used in end-application: */
98 /** \name SPI Prescaler Configuration Masks */
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
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
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))
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)
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))
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)
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)
122 /** \name SPI SCK Polarity Configuration Masks */
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
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
131 /** \name SPI Sample Edge Configuration Masks */
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
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
140 /** \name SPI Data Ordering Configuration Masks */
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
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
149 /** \name SPI Mode Configuration Masks */
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
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
158 /* Inline Functions: */
159 /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other
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.
166 static inline void SPI_Init(SPI_t
* const SPI
,
167 const uint8_t SPIOptions
) ATTR_NON_NULL_PTR_ARG(1);
168 static inline void SPI_Init(SPI_t
* const SPI
,
169 const uint8_t SPIOptions
)
171 SPI
->CTRL
= (SPIOptions
| SPI_ENABLE_bm
);
174 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration.
176 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
178 static inline void SPI_Disable(SPI_t
* const SPI
) ATTR_NON_NULL_PTR_ARG(1);
179 static inline void SPI_Disable(SPI_t
* const SPI
)
181 SPI
->CTRL
&= ~SPI_ENABLE_bm
;
184 /** Retrieves the currently selected SPI mode, once the SPI interface has been configured.
186 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
188 * \return \ref SPI_MODE_MASTER if the interface is currently in SPI Master mode, \ref SPI_MODE_SLAVE otherwise
190 static inline uint8_t SPI_GetCurrentMode(SPI_t
* const SPI
) ATTR_ALWAYS_INLINE
ATTR_NON_NULL_PTR_ARG(1);
191 static inline uint8_t SPI_GetCurrentMode(SPI_t
* const SPI
)
193 return (SPI
->CTRL
& SPI_MASTER_bm
);
196 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
198 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
199 * \param[in] Byte Byte to send through the SPI interface.
201 * \return Response byte from the attached SPI device.
203 static inline uint8_t SPI_TransferByte(SPI_t
* const SPI
,
204 const uint8_t Byte
) ATTR_ALWAYS_INLINE
ATTR_NON_NULL_PTR_ARG(1);
205 static inline uint8_t SPI_TransferByte(SPI_t
* const SPI
,
209 while (!(SPI
->STATUS
& SPI_IF_bm
));
213 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
214 * byte sent to from the attached SPI device is ignored.
216 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
217 * \param[in] Byte Byte to send through the SPI interface.
219 static inline void SPI_SendByte(SPI_t
* const SPI
,
220 const uint8_t Byte
) ATTR_ALWAYS_INLINE
ATTR_NON_NULL_PTR_ARG(1);
221 static inline void SPI_SendByte(SPI_t
* const SPI
,
225 while (!(SPI
->STATUS
& SPI_IF_bm
));
228 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
229 * byte from the attached SPI device is returned.
231 * \param[in,out] SPI Pointer to the base of the SPI peripheral within the device.
233 * \return The response byte from the attached SPI device.
235 static inline uint8_t SPI_ReceiveByte(SPI_t
* const SPI
) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
ATTR_NON_NULL_PTR_ARG(1);
236 static inline uint8_t SPI_ReceiveByte(SPI_t
* const SPI
)
239 while (!(SPI
->STATUS
& SPI_IF_bm
));
243 /* Disable C linkage for C++ Compilers: */
244 #if defined(__cplusplus)