3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 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
32 * \brief Master include file for the SPI peripheral driver.
34 * Hardware SPI subsystem driver for the supported USB AVRs models.
37 /** \ingroup Group_PeripheralDrivers
38 * @defgroup Group_SPI SPI Driver - LUFA/Drivers/Peripheral/SPI.h
40 * \section Sec_Dependencies Module Source Dependencies
41 * The following files must be built with any user project that uses this module:
44 * \section Sec_ModDescription Module Description
45 * Driver for the hardware SPI port available on most AVR models. This module provides
46 * an easy to use driver for the setup of and transfer of data over the AVR's SPI port.
48 * \section Sec_ExampleUsage Example Usage
49 * The following snippet is an example of how this module may be used within a typical
53 * // Initialise the SPI driver before first use
54 * SPI_Init(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
62 * // Receive several bytes, sending a dummy 0x00 byte each time
63 * uint8_t Byte1 = SPI_ReceiveByte();
64 * uint8_t Byte2 = SPI_ReceiveByte();
65 * uint8_t Byte3 = SPI_ReceiveByte();
67 * // Send a byte, and store the received byte from the same transaction
68 * uint8_t ResponseByte = SPI_TransferByte(0xDC);
80 /* Enable C linkage for C++ Compilers: */
81 #if defined(__cplusplus)
85 /* Private Interface - For use in library only: */
86 #if !defined(__DOXYGEN__)
88 #define SPI_USE_DOUBLESPEED (1 << SPE)
91 /* Public Interface - May be used in end-application: */
93 /** \name SPI Prescaler Configuration Masks */
95 /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 2. */
96 #define SPI_SPEED_FCPU_DIV_2 SPI_USE_DOUBLESPEED
98 /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 4. */
99 #define SPI_SPEED_FCPU_DIV_4 0
101 /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 8. */
102 #define SPI_SPEED_FCPU_DIV_8 (SPI_USE_DOUBLESPEED | (1 << SPR0))
104 /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 16. */
105 #define SPI_SPEED_FCPU_DIV_16 (1 << SPR0)
107 /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 32. */
108 #define SPI_SPEED_FCPU_DIV_32 (SPI_USE_DOUBLESPEED | (1 << SPR1))
110 /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 64. */
111 #define SPI_SPEED_FCPU_DIV_64 (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 << SPR0))
113 /** SPI prescaler mask for \c SPI_Init(). Divides the system clock by a factor of 128. */
114 #define SPI_SPEED_FCPU_DIV_128 ((1 << SPR1) | (1 << SPR0))
117 /** \name SPI SCK Polarity Configuration Masks */
119 /** SPI clock polarity mask for \c SPI_Init(). Indicates that the SCK should lead on the rising edge. */
120 #define SPI_SCK_LEAD_RISING (0 << CPOL)
122 /** SPI clock polarity mask for \c SPI_Init(). Indicates that the SCK should lead on the falling edge. */
123 #define SPI_SCK_LEAD_FALLING (1 << CPOL)
126 /** \name SPI Sample Edge Configuration Masks */
128 /** SPI data sample mode mask for \c SPI_Init(). Indicates that the data should sampled on the leading edge. */
129 #define SPI_SAMPLE_LEADING (0 << CPHA)
131 /** SPI data sample mode mask for \c SPI_Init(). Indicates that the data should be sampled on the trailing edge. */
132 #define SPI_SAMPLE_TRAILING (1 << CPHA)
135 /** \name SPI Data Ordering Configuration Masks */
137 /** SPI data order mask for \c SPI_Init(). Indicates that data should be shifted out MSB first. */
138 #define SPI_ORDER_MSB_FIRST (0 << DORD)
140 /** SPI data order mask for \c SPI_Init(). Indicates that data should be shifted out MSB first. */
141 #define SPI_ORDER_LSB_FIRST (1 << DORD)
144 /** \name SPI Mode Configuration Masks */
146 /** SPI mode mask for \c SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
147 #define SPI_MODE_SLAVE (0 << MSTR)
149 /** SPI mode mask for \c SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
150 #define SPI_MODE_MASTER (1 << MSTR)
153 /* Inline Functions: */
154 /** Initialises the SPI subsystem, ready for transfers. Must be called before calling any other
157 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the \c SPI_SPEED_*,
158 * \c SPI_SCK_*, \c SPI_SAMPLE_*, \c SPI_ORDER_* and \c SPI_MODE_* masks.
160 static inline void SPI_Init(const uint8_t SPIOptions
)
162 DDRB
|= ((1 << 1) | (1 << 2));
163 DDRB
&= ~((1 << 0) | (1 << 3));
164 PORTB
|= ((1 << 0) | (1 << 3));
166 SPCR
= ((1 << SPE
) | SPIOptions
);
168 if (SPIOptions
& SPI_USE_DOUBLESPEED
)
169 SPSR
|= (1 << SPI2X
);
171 SPSR
&= ~(1 << SPI2X
);
174 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
175 static inline void SPI_ShutDown(void)
177 DDRB
&= ~((1 << 1) | (1 << 2));
178 PORTB
&= ~((1 << 0) | (1 << 3));
184 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
186 * \param[in] Byte Byte to send through the SPI interface.
188 * \return Response byte from the attached SPI device.
190 static inline uint8_t SPI_TransferByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
191 static inline uint8_t SPI_TransferByte(const uint8_t Byte
)
194 while (!(SPSR
& (1 << SPIF
)));
198 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
199 * byte sent to from the attached SPI device is ignored.
201 * \param[in] Byte Byte to send through the SPI interface.
203 static inline void SPI_SendByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
204 static inline void SPI_SendByte(const uint8_t Byte
)
207 while (!(SPSR
& (1 << SPIF
)));
210 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
211 * byte from the attached SPI device is returned.
213 * \return The response byte from the attached SPI device.
215 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
216 static inline uint8_t SPI_ReceiveByte(void)
219 while (!(SPSR
& (1 << SPIF
)));
223 /* Disable C linkage for C++ Compilers: */
224 #if defined(__cplusplus)