Removed unused INCLUDE_FROM_BOARD_DRIVER internal define from the board driver dispat...
[pub/USBasp.git] / LUFA / Drivers / Peripheral / SPI.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 * Hardware SPI subsystem driver for the supported USB AVRs models.
34 */
35
36 /** \ingroup Group_PeripheralDrivers
37 * @defgroup Group_SPI SPI Driver - LUFA/Drivers/Peripheral/SPI.h
38 *
39 * \section Sec_Dependencies Module Source Dependencies
40 * The following files must be built with any user project that uses this module:
41 * - None
42 *
43 * \section Module Description
44 * Driver for the hardware SPI port avaliable on most AVR models. This module provides
45 * an easy to use driver for the setup of and transfer of data over the AVR's SPI port.
46 *
47 * @{
48 */
49
50 #ifndef __SPI_H__
51 #define __SPI_H__
52
53 /* Includes: */
54 #include <stdbool.h>
55
56 /* Enable C linkage for C++ Compilers: */
57 #if defined(__cplusplus)
58 extern "C" {
59 #endif
60
61 /* Private Interface - For use in library only: */
62 #if !defined(__DOXYGEN__)
63 /* Macros: */
64 #define SPI_USE_DOUBLESPEED (1 << SPE)
65 #endif
66
67 /* Public Interface - May be used in end-application: */
68 /* Macros: */
69 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 2. */
70 #define SPI_SPEED_FCPU_DIV_2 SPI_USE_DOUBLESPEED
71
72 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 4. */
73 #define SPI_SPEED_FCPU_DIV_4 0
74
75 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 8. */
76 #define SPI_SPEED_FCPU_DIV_8 (SPI_USE_DOUBLESPEED | (1 << SPR0))
77
78 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 16. */
79 #define SPI_SPEED_FCPU_DIV_16 (1 << SPR0)
80
81 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 32. */
82 #define SPI_SPEED_FCPU_DIV_32 (SPI_USE_DOUBLESPEED | (1 << SPR1))
83
84 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 64. */
85 #define SPI_SPEED_FCPU_DIV_64 (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 << SPR0))
86
87 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 128. */
88 #define SPI_SPEED_FCPU_DIV_128 ((1 << SPR1) | (1 << SPR0))
89
90 /** SPI clock polarity mask for SPI_Init(). Indicates that the SCK should lead on the rising edge. */
91 #define SPI_SCK_LEAD_RISING (0 << CPOL)
92
93 /** SPI clock polarity mask for SPI_Init(). Indicates that the SCK should lead on the falling edge. */
94 #define SPI_SCK_LEAD_FALLING (1 << CPOL)
95
96 /** SPI data sample mode mask for SPI_Init(). Indicates that the data should sampled on the leading edge. */
97 #define SPI_SAMPLE_LEADING (0 << CPHA)
98
99 /** SPI data sample mode mask for SPI_Init(). Indicates that the data should be sampled on the trailing edge. */
100 #define SPI_SAMPLE_TRAILING (1 << CPHA)
101
102 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
103 #define SPI_MODE_SLAVE (0 << MSTR)
104
105 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
106 #define SPI_MODE_MASTER (1 << MSTR)
107
108 /* Inline Functions: */
109 /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other
110 * SPI routines.
111 *
112 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the SPI_SPEED_*,
113 * SPI_SCK_*, SPI_SAMPLE_* and SPI_MODE_* masks
114 */
115 static inline void SPI_Init(const uint8_t SPIOptions)
116 {
117 DDRB |= ((1 << 1) | (1 << 2));
118 PORTB |= ((1 << 0) | (1 << 3));
119
120 SPCR = ((1 << SPE) | SPIOptions);
121
122 if (SPIOptions & SPI_USE_DOUBLESPEED)
123 SPSR |= (1 << SPI2X);
124 else
125 SPSR &= ~(1 << SPI2X);
126 }
127
128 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
129 static inline void SPI_ShutDown(void)
130 {
131 DDRB &= ~((1 << 1) | (1 << 2));
132 PORTB &= ~((1 << 0) | (1 << 3));
133
134 SPCR = 0;
135 SPSR = 0;
136 }
137
138 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
139 *
140 * \param[in] Byte Byte to send through the SPI interface
141 *
142 * \return Response byte from the attached SPI device
143 */
144 static inline uint8_t SPI_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
145 static inline uint8_t SPI_TransferByte(const uint8_t Byte)
146 {
147 SPDR = Byte;
148 while (!(SPSR & (1 << SPIF)));
149 return SPDR;
150 }
151
152 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
153 * byte sent to from the attached SPI device is ignored.
154 *
155 * \param[in] Byte Byte to send through the SPI interface
156 */
157 static inline void SPI_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
158 static inline void SPI_SendByte(const uint8_t Byte)
159 {
160 SPDR = Byte;
161 while (!(SPSR & (1 << SPIF)));
162 }
163
164 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
165 * byte from the attached SPI device is returned.
166 *
167 * \return The response byte from the attached SPI device
168 */
169 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
170 static inline uint8_t SPI_ReceiveByte(void)
171 {
172 SPDR = 0x00;
173 while (!(SPSR & (1 << SPIF)));
174 return SPDR;
175 }
176
177 /* Disable C linkage for C++ Compilers: */
178 #if defined(__cplusplus)
179 }
180 #endif
181
182 #endif
183
184 /** @} */