3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 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 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
33 * SPI driver for the 32-bit AVRs.
35 * \note This file should not be included directly. It is automatically included as needed by the SPI driver
36 * dispatch header located in LUFA/Drivers/Peripheral/SPI.h.
39 /** \ingroup Group_SPI
40 * @defgroup Group_SPI_AVR32 32-Bit AVR SPI Driver
42 * SPI driver for the 32-bit AVRs.
44 * \note This file should not be included directly. It is automatically included as needed by the ADC driver
45 * dispatch header located in LUFA/Drivers/Peripheral/SPI.h.
50 #ifndef __SPI_AVR32_H__
51 #define __SPI_AVR32_H__
57 /* Preprocessor Checks: */
58 #if !defined(__INCLUDE_FROM_SPI_H)
59 #error Do not include this file directly. Include LUFA/Drivers/Peripheral/SPI.h instead.
62 /* Enable C linkage for C++ Compilers: */
63 #if defined(__cplusplus)
67 /* Public Interface - May be used in end-application: */
69 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 1. */
70 #define SPI_SPEED_FCPU_DIV_1 0
72 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 32. */
73 #define SPI_SPEED_FCPU_DIV_32 AVR32_SPI_MR_FDIV_MASK
75 /** SPI chip selection mode for direct peripheral-to-CS pin connections. */
76 #define SPI_CS_4BITDECODER AVR32_SPI_MR_PSDEC_MASK
78 /** SPI chip selection mode for peripheral CS pin connections through a 4-bit decoder. */
79 #define SPI_CS_DIRECT 0
81 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
82 #define SPI_MODE_SLAVE 0
84 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
85 #define SPI_MODE_MASTER AVR32_SPI_MR_MSTR_MASK
87 /* Inline Functions: */
88 /** Initialises the SPI subsystem, ready for transfers. Must be called before calling any other
91 * \note The individual AVR32 chip select control registers are left at their defaults; it is up to the user
92 * to configure these seperately once the SPI module has been initialized.
94 * \note The physical GPIO pins for the AVR32's SPI are not altered; it is up to the user to
95 * configure these seperately to connect the SPI module to the desired GPIO pins via the
98 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the SPI_SPEED_*,
99 * SPI_CS_* and SPI_MODE_* masks
101 static inline void SPI_Init(const uintN_t SPIOptions
)
103 AVR32_PM
.pbamask
= (1 << 5);
105 AVR32_SPI
.CR
.swrst
= true;
106 AVR32_SPI
.CR
.spien
= true;
107 AVR32_SPI
.mr
= SPIOptions
;
110 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
111 static inline void SPI_ShutDown(void)
113 AVR32_SPI
.cr
= AVR32_SPI_CR_SPIDIS_MASK
;
115 AVR32_PM
.pbamask
&= ~(1 << 5);
118 /** Sends and receives a transfer through the SPI interface, blocking until the transfer is complete.
119 * The width of the data that is transferred is dependant on the settings of the currently selected
122 * \param[in] Data Data to send through the SPI interface
124 * \return Response data from the attached SPI device
126 static inline uint16_t SPI_Transfer(const uint16_t Data
) ATTR_ALWAYS_INLINE
;
127 static inline uint16_t SPI_Transfer(const uint16_t Data
)
129 while (!(AVR32_SPI
.SR
.tdre
));
130 AVR32_SPI
.TDR
.td
= Data
;
132 while ((AVR32_SPI
.sr
& (AVR32_SPI_SR_RDRF_MASK
| AVR32_SPI_SR_TXEMPTY_MASK
)) !=
133 (AVR32_SPI_SR_RDRF_MASK
| AVR32_SPI_SR_TXEMPTY_MASK
));
134 return AVR32_SPI
.RDR
.rd
;
137 /** Sends a transfer through the SPI interface, blocking until the transfer is complete. The response
138 * data sent to from the attached SPI device is ignored. The width of the data that is transferred is
139 * dependant on the settings of the currently selected peripheral.
141 * \param[in] Data Data to send through the SPI interface
143 static inline void SPI_Send(const uint16_t Data
) ATTR_ALWAYS_INLINE
;
144 static inline void SPI_Send(const uint16_t Data
)
146 while (!(AVR32_SPI
.SR
.tdre
));
147 AVR32_SPI
.TDR
.td
= Data
;
150 /** Sends a dummy transfer through the SPI interface, blocking until the transfer is complete. The response
151 * data from the attached SPI device is returned. The width of the data that is transferred is dependant on
152 * the settings of the currently selected peripheral.
154 * \return The response data from the attached SPI device
156 static inline uint16_t SPI_Receive(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
157 static inline uint16_t SPI_Receive(void)
159 while (!(AVR32_SPI
.SR
.tdre
));
160 AVR32_SPI
.TDR
.td
= 0x0000;
162 while ((AVR32_SPI
.sr
& (AVR32_SPI_SR_RDRF_MASK
| AVR32_SPI_SR_TXEMPTY_MASK
)) !=
163 (AVR32_SPI_SR_RDRF_MASK
| AVR32_SPI_SR_TXEMPTY_MASK
));
164 return AVR32_SPI
.RDR
.rd
;
167 /* Disable C linkage for C++ Compilers: */
168 #if defined(__cplusplus)