Added basic driver example use code to the library documentation.
[pub/USBasp.git] / LUFA / Drivers / Peripheral / SPI.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 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 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 * <b>Example Usage:</b>
49 * \code
50 * // Initialise the SPI driver before first use
51 * SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |
52 * SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);
53 *
54 * // Send several bytes, ignoring the returned data
55 * SPI_SendByte(0x01);
56 * SPI_SendByte(0x02);
57 * SPI_SendByte(0x03);
58 *
59 * // Receive several bytes, sending a dummy 0x00 byte each time
60 * uint8_t Byte1 = SPI_ReceiveByte();
61 * uint8_t Byte2 = SPI_ReceiveByte();
62 * uint8_t Byte3 = SPI_ReceiveByte();
63 *
64 * // Send a byte, and store the received byte from the same transaction
65 * uint8_t ResponseByte = SPI_TransferByte(0xDC);
66 * \endcode
67 *
68 * @{
69 */
70
71 #ifndef __SPI_H__
72 #define __SPI_H__
73
74 /* Includes: */
75 #include <stdbool.h>
76
77 /* Enable C linkage for C++ Compilers: */
78 #if defined(__cplusplus)
79 extern "C" {
80 #endif
81
82 /* Private Interface - For use in library only: */
83 #if !defined(__DOXYGEN__)
84 /* Macros: */
85 #define SPI_USE_DOUBLESPEED (1 << SPE)
86 #endif
87
88 /* Public Interface - May be used in end-application: */
89 /* Macros: */
90 /** \name SPI Prescaler Configuration Masks */
91 //@{
92 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 2. */
93 #define SPI_SPEED_FCPU_DIV_2 SPI_USE_DOUBLESPEED
94
95 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 4. */
96 #define SPI_SPEED_FCPU_DIV_4 0
97
98 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 8. */
99 #define SPI_SPEED_FCPU_DIV_8 (SPI_USE_DOUBLESPEED | (1 << SPR0))
100
101 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 16. */
102 #define SPI_SPEED_FCPU_DIV_16 (1 << SPR0)
103
104 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 32. */
105 #define SPI_SPEED_FCPU_DIV_32 (SPI_USE_DOUBLESPEED | (1 << SPR1))
106
107 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 64. */
108 #define SPI_SPEED_FCPU_DIV_64 (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 << SPR0))
109
110 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 128. */
111 #define SPI_SPEED_FCPU_DIV_128 ((1 << SPR1) | (1 << SPR0))
112 //@}
113
114 /** \name SPI SCK Polarity Configuration Masks */
115 //@{
116 /** SPI clock polarity mask for SPI_Init(). Indicates that the SCK should lead on the rising edge. */
117 #define SPI_SCK_LEAD_RISING (0 << CPOL)
118
119 /** SPI clock polarity mask for SPI_Init(). Indicates that the SCK should lead on the falling edge. */
120 #define SPI_SCK_LEAD_FALLING (1 << CPOL)
121 //@}
122
123 /** \name SPI Sample Edge Configuration Masks */
124 //@{
125 /** SPI data sample mode mask for SPI_Init(). Indicates that the data should sampled on the leading edge. */
126 #define SPI_SAMPLE_LEADING (0 << CPHA)
127
128 /** SPI data sample mode mask for SPI_Init(). Indicates that the data should be sampled on the trailing edge. */
129 #define SPI_SAMPLE_TRAILING (1 << CPHA)
130 //@}
131
132 /** \name SPI Data Ordering Configuration Masks */
133 //@{
134 /** SPI data order mask for SPI_Init(). Indicates that data should be shifted out MSB first. */
135 #define SPI_ORDER_MSB_FIRST (0 << DORD)
136
137 /** SPI data order mask for SPI_Init(). Indicates that data should be shifted out MSB first. */
138 #define SPI_ORDER_LSB_FIRST (1 << DORD)
139 //@}
140
141 /** \name SPI Mode Configuration Masks */
142 //@{
143 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
144 #define SPI_MODE_SLAVE (0 << MSTR)
145
146 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
147 #define SPI_MODE_MASTER (1 << MSTR)
148 //@}
149
150 /* Inline Functions: */
151 /** Initialises the SPI subsystem, ready for transfers. Must be called before calling any other
152 * SPI routines.
153 *
154 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the SPI_SPEED_*,
155 * SPI_SCK_*, SPI_SAMPLE_*, SPI_ORDER_* and SPI_MODE_* masks.
156 */
157 static inline void SPI_Init(const uint8_t SPIOptions)
158 {
159 DDRB |= ((1 << 1) | (1 << 2));
160 DDRB &= ~((1 << 0) | (1 << 3));
161 PORTB |= ((1 << 0) | (1 << 3));
162
163 SPCR = ((1 << SPE) | SPIOptions);
164
165 if (SPIOptions & SPI_USE_DOUBLESPEED)
166 SPSR |= (1 << SPI2X);
167 else
168 SPSR &= ~(1 << SPI2X);
169 }
170
171 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
172 static inline void SPI_ShutDown(void)
173 {
174 DDRB &= ~((1 << 1) | (1 << 2));
175 PORTB &= ~((1 << 0) | (1 << 3));
176
177 SPCR = 0;
178 SPSR = 0;
179 }
180
181 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
182 *
183 * \param[in] Byte Byte to send through the SPI interface.
184 *
185 * \return Response byte from the attached SPI device.
186 */
187 static inline uint8_t SPI_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
188 static inline uint8_t SPI_TransferByte(const uint8_t Byte)
189 {
190 SPDR = Byte;
191 while (!(SPSR & (1 << SPIF)));
192 return SPDR;
193 }
194
195 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
196 * byte sent to from the attached SPI device is ignored.
197 *
198 * \param[in] Byte Byte to send through the SPI interface.
199 */
200 static inline void SPI_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
201 static inline void SPI_SendByte(const uint8_t Byte)
202 {
203 SPDR = Byte;
204 while (!(SPSR & (1 << SPIF)));
205 }
206
207 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
208 * byte from the attached SPI device is returned.
209 *
210 * \return The response byte from the attached SPI device.
211 */
212 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
213 static inline uint8_t SPI_ReceiveByte(void)
214 {
215 SPDR = 0x00;
216 while (!(SPSR & (1 << SPIF)));
217 return SPDR;
218 }
219
220 /* Disable C linkage for C++ Compilers: */
221 #if defined(__cplusplus)
222 }
223 #endif
224
225 #endif
226
227 /** @} */
228