Minor documentation improvements.
[pub/USBasp.git] / LUFA / Drivers / Peripheral / SPI.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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 Master include file for the SPI peripheral driver.
33 *
34 * Hardware SPI subsystem driver for the supported USB AVRs models.
35 */
36
37 /** \ingroup Group_PeripheralDrivers
38 * @defgroup Group_SPI SPI Driver - LUFA/Drivers/Peripheral/SPI.h
39 *
40 * \section Sec_Dependencies Module Source Dependencies
41 * The following files must be built with any user project that uses this module:
42 * - None
43 *
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.
47 *
48 * \section Sec_ExampleUsage Example Usage
49 * The following snippet is an example of how this module may be used within a typical
50 * application.
51 *
52 * \code
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);
56 *
57 * // Send several bytes, ignoring the returned data
58 * SPI_SendByte(0x01);
59 * SPI_SendByte(0x02);
60 * SPI_SendByte(0x03);
61 *
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();
66 *
67 * // Send a byte, and store the received byte from the same transaction
68 * uint8_t ResponseByte = SPI_TransferByte(0xDC);
69 * \endcode
70 *
71 * @{
72 */
73
74 #ifndef __SPI_H__
75 #define __SPI_H__
76
77 /* Includes: */
78 #include <stdbool.h>
79
80 /* Enable C linkage for C++ Compilers: */
81 #if defined(__cplusplus)
82 extern "C" {
83 #endif
84
85 /* Private Interface - For use in library only: */
86 #if !defined(__DOXYGEN__)
87 /* Macros: */
88 #define SPI_USE_DOUBLESPEED (1 << SPE)
89 #endif
90
91 /* Public Interface - May be used in end-application: */
92 /* Macros: */
93 /** \name SPI Prescaler Configuration Masks */
94 //@{
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
97
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
100
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))
103
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)
106
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))
109
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))
112
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))
115 //@}
116
117 /** \name SPI SCK Polarity Configuration Masks */
118 //@{
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)
121
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)
124 //@}
125
126 /** \name SPI Sample Edge Configuration Masks */
127 //@{
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)
130
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)
133 //@}
134
135 /** \name SPI Data Ordering Configuration Masks */
136 //@{
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)
139
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)
142 //@}
143
144 /** \name SPI Mode Configuration Masks */
145 //@{
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)
148
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)
151 //@}
152
153 /* Inline Functions: */
154 /** Initialises the SPI subsystem, ready for transfers. Must be called before calling any other
155 * SPI routines.
156 *
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.
159 */
160 static inline void SPI_Init(const uint8_t SPIOptions)
161 {
162 DDRB |= ((1 << 1) | (1 << 2));
163 DDRB &= ~((1 << 0) | (1 << 3));
164 PORTB |= ((1 << 0) | (1 << 3));
165
166 SPCR = ((1 << SPE) | SPIOptions);
167
168 if (SPIOptions & SPI_USE_DOUBLESPEED)
169 SPSR |= (1 << SPI2X);
170 else
171 SPSR &= ~(1 << SPI2X);
172 }
173
174 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
175 static inline void SPI_ShutDown(void)
176 {
177 DDRB &= ~((1 << 1) | (1 << 2));
178 PORTB &= ~((1 << 0) | (1 << 3));
179
180 SPCR = 0;
181 SPSR = 0;
182 }
183
184 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
185 *
186 * \param[in] Byte Byte to send through the SPI interface.
187 *
188 * \return Response byte from the attached SPI device.
189 */
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)
192 {
193 SPDR = Byte;
194 while (!(SPSR & (1 << SPIF)));
195 return SPDR;
196 }
197
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.
200 *
201 * \param[in] Byte Byte to send through the SPI interface.
202 */
203 static inline void SPI_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
204 static inline void SPI_SendByte(const uint8_t Byte)
205 {
206 SPDR = Byte;
207 while (!(SPSR & (1 << SPIF)));
208 }
209
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.
212 *
213 * \return The response byte from the attached SPI device.
214 */
215 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
216 static inline uint8_t SPI_ReceiveByte(void)
217 {
218 SPDR = 0x00;
219 while (!(SPSR & (1 << SPIF)));
220 return SPDR;
221 }
222
223 /* Disable C linkage for C++ Compilers: */
224 #if defined(__cplusplus)
225 }
226 #endif
227
228 #endif
229
230 /** @} */
231