Fixed SPI driver not explicitly setting /SS and MISO pins as inputs when SPI_Init...
[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.fourwalledcubicle.com
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 * @{
49 */
50
51 #ifndef __SPI_H__
52 #define __SPI_H__
53
54 /* Includes: */
55 #include <stdbool.h>
56
57 /* Enable C linkage for C++ Compilers: */
58 #if defined(__cplusplus)
59 extern "C" {
60 #endif
61
62 /* Private Interface - For use in library only: */
63 #if !defined(__DOXYGEN__)
64 /* Macros: */
65 #define SPI_USE_DOUBLESPEED (1 << SPE)
66 #endif
67
68 /* Public Interface - May be used in end-application: */
69 /* Macros: */
70 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 2. */
71 #define SPI_SPEED_FCPU_DIV_2 SPI_USE_DOUBLESPEED
72
73 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 4. */
74 #define SPI_SPEED_FCPU_DIV_4 0
75
76 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 8. */
77 #define SPI_SPEED_FCPU_DIV_8 (SPI_USE_DOUBLESPEED | (1 << SPR0))
78
79 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 16. */
80 #define SPI_SPEED_FCPU_DIV_16 (1 << SPR0)
81
82 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 32. */
83 #define SPI_SPEED_FCPU_DIV_32 (SPI_USE_DOUBLESPEED | (1 << SPR1))
84
85 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 64. */
86 #define SPI_SPEED_FCPU_DIV_64 (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 << SPR0))
87
88 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 128. */
89 #define SPI_SPEED_FCPU_DIV_128 ((1 << SPR1) | (1 << SPR0))
90
91 /** SPI clock polarity mask for SPI_Init(). Indicates that the SCK should lead on the rising edge. */
92 #define SPI_SCK_LEAD_RISING (0 << CPOL)
93
94 /** SPI clock polarity mask for SPI_Init(). Indicates that the SCK should lead on the falling edge. */
95 #define SPI_SCK_LEAD_FALLING (1 << CPOL)
96
97 /** SPI data sample mode mask for SPI_Init(). Indicates that the data should sampled on the leading edge. */
98 #define SPI_SAMPLE_LEADING (0 << CPHA)
99
100 /** SPI data sample mode mask for SPI_Init(). Indicates that the data should be sampled on the trailing edge. */
101 #define SPI_SAMPLE_TRAILING (1 << CPHA)
102
103 /** SPI data order mask for SPI_Init(). Indicates that data should be shifted out MSB first. */
104 #define SPI_ORDER_MSB_FIRST (0 << DORD)
105
106 /** SPI data order mask for SPI_Init(). Indicates that data should be shifted out MSB first. */
107 #define SPI_ORDER_LSB_FIRST (1 << DORD)
108
109 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
110 #define SPI_MODE_SLAVE (0 << MSTR)
111
112 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
113 #define SPI_MODE_MASTER (1 << MSTR)
114
115 /* Inline Functions: */
116 /** Initialises the SPI subsystem, ready for transfers. Must be called before calling any other
117 * SPI routines.
118 *
119 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the SPI_SPEED_*,
120 * SPI_SCK_*, SPI_SAMPLE_*, SPI_ORDER_* and SPI_MODE_* masks.
121 */
122 static inline void SPI_Init(const uint8_t SPIOptions)
123 {
124 DDRB |= ((1 << 1) | (1 << 2));
125 DDRB &= ((1 << 0) | (1 << 3));
126 PORTB |= ((1 << 0) | (1 << 3));
127
128 SPCR = ((1 << SPE) | SPIOptions);
129
130 if (SPIOptions & SPI_USE_DOUBLESPEED)
131 SPSR |= (1 << SPI2X);
132 else
133 SPSR &= ~(1 << SPI2X);
134 }
135
136 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
137 static inline void SPI_ShutDown(void)
138 {
139 DDRB &= ~((1 << 1) | (1 << 2));
140 PORTB &= ~((1 << 0) | (1 << 3));
141
142 SPCR = 0;
143 SPSR = 0;
144 }
145
146 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
147 *
148 * \param[in] Byte Byte to send through the SPI interface.
149 *
150 * \return Response byte from the attached SPI device.
151 */
152 static inline uint8_t SPI_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
153 static inline uint8_t SPI_TransferByte(const uint8_t Byte)
154 {
155 SPDR = Byte;
156 while (!(SPSR & (1 << SPIF)));
157 return SPDR;
158 }
159
160 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
161 * byte sent to from the attached SPI device is ignored.
162 *
163 * \param[in] Byte Byte to send through the SPI interface.
164 */
165 static inline void SPI_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
166 static inline void SPI_SendByte(const uint8_t Byte)
167 {
168 SPDR = Byte;
169 while (!(SPSR & (1 << SPIF)));
170 }
171
172 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
173 * byte from the attached SPI device is returned.
174 *
175 * \return The response byte from the attached SPI device.
176 */
177 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
178 static inline uint8_t SPI_ReceiveByte(void)
179 {
180 SPDR = 0x00;
181 while (!(SPSR & (1 << SPIF)));
182 return SPDR;
183 }
184
185 /* Disable C linkage for C++ Compilers: */
186 #if defined(__cplusplus)
187 }
188 #endif
189
190 #endif
191
192 /** @} */