Fixed SPI driver init function not clearing SPI2X bit when not needed.
[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 * Functions, macros, variables, enums and types related to the setup of a the SPI port.
45 *
46 * @{
47 */
48
49 #ifndef __SPI_H__
50 #define __SPI_H__
51
52 /* Includes: */
53 #include <stdbool.h>
54
55 /* Enable C linkage for C++ Compilers: */
56 #if defined(__cplusplus)
57 extern "C" {
58 #endif
59
60 /* Private Interface - For use in library only: */
61 #if !defined(__DOXYGEN__)
62 /* Macros: */
63 #define SPI_USE_DOUBLESPEED (1 << 7)
64 #endif
65
66 /* Public Interface - May be used in end-application: */
67 /* Macros: */
68 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 2. */
69 #define SPI_SPEED_FCPU_DIV_2 SPI_USE_DOUBLESPEED
70
71 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 4. */
72 #define SPI_SPEED_FCPU_DIV_4 0
73
74 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 8. */
75 #define SPI_SPEED_FCPU_DIV_8 (SPI_USE_DOUBLESPEED | (1 << SPR0))
76
77 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 16. */
78 #define SPI_SPEED_FCPU_DIV_16 (1 << SPR0)
79
80 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 32. */
81 #define SPI_SPEED_FCPU_DIV_32 (SPI_USE_DOUBLESPEED | (1 << SPR1))
82
83 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 64. */
84 #define SPI_SPEED_FCPU_DIV_64 (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 << SPR0))
85
86 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 128. */
87 #define SPI_SPEED_FCPU_DIV_128 ((1 << SPR1) | (1 << SPR0))
88
89 /* Inline Functions: */
90 /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other
91 * SPI routines.
92 *
93 * \param PrescalerMask Prescaler mask to set the SPI clock speed
94 * \param Master If true, sets the SPI system to use master mode, slave if false
95 */
96 static inline void SPI_Init(const uint8_t PrescalerMask, const bool Master)
97 {
98 DDRB |= ((1 << 1) | (1 << 2));
99 PORTB |= ((1 << 0) | (1 << 3));
100
101 SPCR = ((1 << SPE) | (Master << MSTR) | (1 << CPOL) | (1 << CPHA) |
102 (PrescalerMask & ~SPI_USE_DOUBLESPEED));
103
104 if (PrescalerMask & SPI_USE_DOUBLESPEED)
105 SPSR |= (1 << SPI2X);
106 else
107 SPSR &= ~(1 << SPI2X);
108 }
109
110 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
111 *
112 * \param Byte Byte to send through the SPI interface
113 *
114 * \return Response byte from the attached SPI device
115 */
116 static inline uint8_t SPI_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
117 static inline uint8_t SPI_TransferByte(const uint8_t Byte)
118 {
119 SPDR = Byte;
120 while (!(SPSR & (1 << SPIF)));
121 return SPDR;
122 }
123
124 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
125 * byte sent to from the attached SPI device is ignored.
126 *
127 * \param Byte Byte to send through the SPI interface
128 */
129 static inline void SPI_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
130 static inline void SPI_SendByte(const uint8_t Byte)
131 {
132 SPDR = Byte;
133 while (!(SPSR & (1 << SPIF)));
134 }
135
136 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
137 * byte from the attached SPI device is returned.
138 *
139 * \return The response byte from the attached SPI device
140 */
141 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
142 static inline uint8_t SPI_ReceiveByte(void)
143 {
144 SPDR = 0x00;
145 while (!(SPSR & (1 << SPIF)));
146 return SPDR;
147 }
148
149 /* Disable C linkage for C++ Compilers: */
150 #if defined(__cplusplus)
151 }
152 #endif
153
154 #endif
155
156 /** @} */