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