3 Copyright (C) Dean Camera, 2018.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2018 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 (AVR8)
34 * On-chip SPI driver for the 8-bit AVR 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_AVR8 SPI Peripheral Driver (AVR8)
43 * \section Sec_SPI_AVR8_ModDescription Module Description
44 * Driver for the hardware SPI port available on most 8-bit AVR microcontroller models. This
45 * module provides an easy to use driver for the setup and transfer of data over the
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.
51 * \section Sec_SPI_AVR8_ExampleUsage Example Usage
52 * The following snippet is an example of how this module may be used within a typical
56 * // Initialize the SPI driver before first use
57 * SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
58 * SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
60 * // Send several bytes, ignoring the returned data
65 * // Receive several bytes, sending a dummy 0x00 byte each time
66 * uint8_t Byte1 = SPI_ReceiveByte();
67 * uint8_t Byte2 = SPI_ReceiveByte();
68 * uint8_t Byte3 = SPI_ReceiveByte();
70 * // Send a byte, and store the received byte from the same transaction
71 * uint8_t ResponseByte = SPI_TransferByte(0xDC);
77 #ifndef __SPI_AVR8_H__
78 #define __SPI_AVR8_H__
81 #include "../../../Common/Common.h"
83 /* Enable C linkage for C++ Compilers: */
84 #if defined(__cplusplus)
88 /* Preprocessor Checks: */
89 #if !defined(__INCLUDE_FROM_SPI_H)
90 #error Do not include this file directly. Include LUFA/Drivers/Peripheral/SPI.h instead.
93 /* Private Interface - For use in library only: */
94 #if !defined(__DOXYGEN__)
96 #define SPI_USE_DOUBLESPEED (1 << SPE)
99 /* Public Interface - May be used in end-application: */
101 /** \name SPI Prescaler Configuration Masks */
103 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 2. */
104 #define SPI_SPEED_FCPU_DIV_2 SPI_USE_DOUBLESPEED
106 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 4. */
107 #define SPI_SPEED_FCPU_DIV_4 0
109 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 8. */
110 #define SPI_SPEED_FCPU_DIV_8 (SPI_USE_DOUBLESPEED | (1 << SPR0))
112 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 16. */
113 #define SPI_SPEED_FCPU_DIV_16 (1 << SPR0)
115 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 32. */
116 #define SPI_SPEED_FCPU_DIV_32 (SPI_USE_DOUBLESPEED | (1 << SPR1))
118 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 64. */
119 #define SPI_SPEED_FCPU_DIV_64 (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 << SPR0))
121 /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 128. */
122 #define SPI_SPEED_FCPU_DIV_128 ((1 << SPR1) | (1 << SPR0))
125 /** \name SPI SCK Polarity Configuration Masks */
127 /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the rising edge. */
128 #define SPI_SCK_LEAD_RISING (0 << CPOL)
130 /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the falling edge. */
131 #define SPI_SCK_LEAD_FALLING (1 << CPOL)
134 /** \name SPI Sample Edge Configuration Masks */
136 /** SPI data sample mode mask for \ref SPI_Init(). Indicates that the data should sampled on the leading edge. */
137 #define SPI_SAMPLE_LEADING (0 << CPHA)
139 /** SPI data sample mode mask for \ref SPI_Init(). Indicates that the data should be sampled on the trailing edge. */
140 #define SPI_SAMPLE_TRAILING (1 << CPHA)
143 /** \name SPI Data Ordering Configuration Masks */
145 /** SPI data order mask for \ref SPI_Init(). Indicates that data should be shifted out MSB first. */
146 #define SPI_ORDER_MSB_FIRST (0 << DORD)
148 /** SPI data order mask for \ref SPI_Init(). Indicates that data should be shifted out LSB first. */
149 #define SPI_ORDER_LSB_FIRST (1 << DORD)
152 /** \name SPI Mode Configuration Masks */
154 /** SPI mode mask for \ref SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
155 #define SPI_MODE_SLAVE (0 << MSTR)
157 /** SPI mode mask for \ref SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
158 #define SPI_MODE_MASTER (1 << MSTR)
161 /* Inline Functions: */
162 /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other
165 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the \c SPI_SPEED_*,
166 * \c SPI_SCK_*, \c SPI_SAMPLE_*, \c SPI_ORDER_* and \c SPI_MODE_* masks.
168 static inline void SPI_Init(const uint8_t SPIOptions
);
169 static inline void SPI_Init(const uint8_t SPIOptions
)
171 /* Prevent high rise times on PB.0 (/SS) from forcing a change to SPI slave mode */
175 DDRB
|= ((1 << 1) | (1 << 2));
179 if (SPIOptions
& SPI_USE_DOUBLESPEED
)
180 SPSR
|= (1 << SPI2X
);
182 SPSR
&= ~(1 << SPI2X
);
184 /* Switch /SS to input mode after configuration to allow for forced mode changes */
187 SPCR
= ((1 << SPE
) | SPIOptions
);
190 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
191 static inline void SPI_Disable(void);
192 static inline void SPI_Disable(void)
194 DDRB
&= ~((1 << 1) | (1 << 2));
195 PORTB
&= ~((1 << 0) | (1 << 3));
201 /** Retrieves the currently selected SPI mode, once the SPI interface has been configured.
203 * \return \ref SPI_MODE_MASTER if the interface is currently in SPI Master mode, \ref SPI_MODE_SLAVE otherwise
205 static inline uint8_t SPI_GetCurrentMode(void) ATTR_ALWAYS_INLINE
;
206 static inline uint8_t SPI_GetCurrentMode(void)
208 return (SPCR
& SPI_MODE_MASTER
);
211 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
213 * \param[in] Byte Byte to send through the SPI interface.
215 * \return Response byte from the attached SPI device.
217 static inline uint8_t SPI_TransferByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
218 static inline uint8_t SPI_TransferByte(const uint8_t Byte
)
221 while (!(SPSR
& (1 << SPIF
)));
225 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
226 * byte sent to from the attached SPI device is ignored.
228 * \param[in] Byte Byte to send through the SPI interface.
230 static inline void SPI_SendByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
;
231 static inline void SPI_SendByte(const uint8_t Byte
)
234 while (!(SPSR
& (1 << SPIF
)));
237 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
238 * byte from the attached SPI device is returned.
240 * \return The response byte from the attached SPI device.
242 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
;
243 static inline uint8_t SPI_ReceiveByte(void)
246 while (!(SPSR
& (1 << SPIF
)));
250 /* Disable C linkage for C++ Compilers: */
251 #if defined(__cplusplus)