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