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