]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Peripheral/AVR8/SPI_AVR8.h
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Peripheral / AVR8 / SPI_AVR8.h
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 /** \file\r
32  *  \brief SPI Peripheral Driver (AVR8)\r
33  *\r
34  *  On-chip SPI driver for the 8-bit AVR microcontrollers.\r
35  *\r
36  *  \note This file should not be included directly. It is automatically included as needed by the SPI driver\r
37  *        dispatch header located in LUFA/Drivers/Peripheral/SPI.h.\r
38  */\r
39 \r
40 /** \ingroup Group_SPI\r
41  *  \defgroup Group_SPI_AVR8 SPI Peripheral Driver (AVR8)\r
42  *\r
43  *  \section Sec_ModDescription Module Description\r
44  *  Driver for the hardware SPI port available on most 8-bit AVR microcontroller models. This\r
45  *  module provides an easy to use driver for the setup and transfer of data over the\r
46  *  AVR's SPI port.\r
47  *\r
48  *  \note This file should not be included directly. It is automatically included as needed by the SPI driver\r
49  *        dispatch header located in LUFA/Drivers/Peripheral/SPI.h.\r
50  *\r
51  *  \section Sec_ExampleUsage Example Usage\r
52  *  The following snippet is an example of how this module may be used within a typical\r
53  *  application.\r
54  *\r
55  *  \code\r
56  *      // Initialize the SPI driver before first use\r
57  *      SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |\r
58  *               SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);\r
59  *      \r
60  *      // Send several bytes, ignoring the returned data\r
61  *      SPI_SendByte(0x01);\r
62  *      SPI_SendByte(0x02);\r
63  *      SPI_SendByte(0x03);\r
64  *      \r
65  *      // Receive several bytes, sending a dummy 0x00 byte each time\r
66  *      uint8_t Byte1 = SPI_ReceiveByte();\r
67  *      uint8_t Byte2 = SPI_ReceiveByte();\r
68  *      uint8_t Byte3 = SPI_ReceiveByte();\r
69  *      \r
70  *      // Send a byte, and store the received byte from the same transaction\r
71  *      uint8_t ResponseByte = SPI_TransferByte(0xDC);\r
72  *  \endcode\r
73  *\r
74  *  @{\r
75  */\r
76 \r
77 #ifndef __SPI_AVR8_H__\r
78 #define __SPI_AVR8_H__\r
79 \r
80         /* Includes: */\r
81                 #include "../../../Common/Common.h"\r
82 \r
83         /* Enable C linkage for C++ Compilers: */\r
84                 #if defined(__cplusplus)\r
85                         extern "C" {\r
86                 #endif\r
87 \r
88         /* Preprocessor Checks: */\r
89                 #if !defined(__INCLUDE_FROM_SPI_H)\r
90                         #error Do not include this file directly. Include LUFA/Drivers/Peripheral/SPI.h instead.\r
91                 #endif\r
92 \r
93         /* Private Interface - For use in library only: */\r
94         #if !defined(__DOXYGEN__)\r
95                 /* Macros: */\r
96                         #define SPI_USE_DOUBLESPEED            (1 << SPE)\r
97         #endif\r
98 \r
99         /* Public Interface - May be used in end-application: */\r
100                 /* Macros: */\r
101                         /** \name SPI Prescaler Configuration Masks */\r
102                         //@{\r
103                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 2. */\r
104                         #define SPI_SPEED_FCPU_DIV_2           SPI_USE_DOUBLESPEED\r
105 \r
106                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 4. */\r
107                         #define SPI_SPEED_FCPU_DIV_4           0\r
108 \r
109                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 8. */\r
110                         #define SPI_SPEED_FCPU_DIV_8           (SPI_USE_DOUBLESPEED | (1 << SPR0))\r
111 \r
112                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 16. */\r
113                         #define SPI_SPEED_FCPU_DIV_16          (1 << SPR0)\r
114 \r
115                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 32. */\r
116                         #define SPI_SPEED_FCPU_DIV_32          (SPI_USE_DOUBLESPEED | (1 << SPR1))\r
117 \r
118                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 64. */\r
119                         #define SPI_SPEED_FCPU_DIV_64          (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 << SPR0))\r
120 \r
121                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 128. */\r
122                         #define SPI_SPEED_FCPU_DIV_128         ((1 << SPR1) | (1 << SPR0))\r
123                         //@}\r
124 \r
125                         /** \name SPI SCK Polarity Configuration Masks */\r
126                         //@{\r
127                         /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the rising edge. */\r
128                         #define SPI_SCK_LEAD_RISING            (0 << CPOL)\r
129 \r
130                         /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the falling edge. */\r
131                         #define SPI_SCK_LEAD_FALLING           (1 << CPOL)\r
132                         //@}\r
133 \r
134                         /** \name SPI Sample Edge Configuration Masks */\r
135                         //@{\r
136                         /** SPI data sample mode mask for \ref SPI_Init(). Indicates that the data should sampled on the leading edge. */\r
137                         #define SPI_SAMPLE_LEADING             (0 << CPHA)\r
138 \r
139                         /** SPI data sample mode mask for \ref SPI_Init(). Indicates that the data should be sampled on the trailing edge. */\r
140                         #define SPI_SAMPLE_TRAILING            (1 << CPHA)\r
141                         //@}\r
142 \r
143                         /** \name SPI Data Ordering Configuration Masks */\r
144                         //@{\r
145                         /** SPI data order mask for \ref SPI_Init(). Indicates that data should be shifted out MSB first. */\r
146                         #define SPI_ORDER_MSB_FIRST            (0 << DORD)\r
147 \r
148                         /** SPI data order mask for \ref SPI_Init(). Indicates that data should be shifted out LSB first. */\r
149                         #define SPI_ORDER_LSB_FIRST            (1 << DORD)\r
150                         //@}\r
151 \r
152                         /** \name SPI Mode Configuration Masks */\r
153                         //@{\r
154                         /** SPI mode mask for \ref SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */\r
155                         #define SPI_MODE_SLAVE                 (0 << MSTR)\r
156 \r
157                         /** SPI mode mask for \ref SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */\r
158                         #define SPI_MODE_MASTER                (1 << MSTR)\r
159                         //@}\r
160 \r
161                 /* Inline Functions: */\r
162                         /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other\r
163                          *  SPI routines.\r
164                          *\r
165                          *  \param[in] SPIOptions  SPI Options, a mask consisting of one of each of the \c SPI_SPEED_*,\r
166                          *                         \c SPI_SCK_*, \c SPI_SAMPLE_*, \c SPI_ORDER_* and \c SPI_MODE_* masks.\r
167                          */\r
168                         static inline void SPI_Init(const uint8_t SPIOptions)\r
169                         {\r
170                                 /* Prevent high rise times on PB.0 (/SS) from forcing a change to SPI slave mode */\r
171                                 DDRB  |= (1 << 0);\r
172                                 PORTB |= (1 << 0);\r
173 \r
174                                 DDRB  |=  ((1 << 1) | (1 << 2));\r
175                                 DDRB  &= ~(1 << 3);\r
176                                 PORTB |=  (1 << 3);\r
177 \r
178                                 if (SPIOptions & SPI_USE_DOUBLESPEED)\r
179                                   SPSR |= (1 << SPI2X);\r
180                                 else\r
181                                   SPSR &= ~(1 << SPI2X);\r
182 \r
183                                 /* Switch /SS to input mode after configuration to allow for forced mode changes */\r
184                                 DDRB &= ~(1 << 0);\r
185 \r
186                                 SPCR  = ((1 << SPE) | SPIOptions);\r
187                         }\r
188 \r
189                         /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */\r
190                         static inline void SPI_Disable(void)\r
191                         {\r
192                                 DDRB  &= ~((1 << 1) | (1 << 2));\r
193                                 PORTB &= ~((1 << 0) | (1 << 3));\r
194 \r
195                                 SPCR   = 0;\r
196                                 SPSR   = 0;\r
197                         }\r
198 \r
199                         /** Retrieves the currently selected SPI mode, once the SPI interface has been configured.\r
200                          *\r
201                          *  \return \ref SPI_MODE_MASTER if the interface is currently in SPI Master mode, \ref SPI_MODE_SLAVE otherwise\r
202                          */\r
203                         static inline uint8_t SPI_GetCurrentMode(void) ATTR_ALWAYS_INLINE;\r
204                         static inline uint8_t SPI_GetCurrentMode(void)\r
205                         {\r
206                                 return (SPCR & SPI_MODE_MASTER);\r
207                         }\r
208 \r
209                         /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.\r
210                          *\r
211                          *  \param[in] Byte  Byte to send through the SPI interface.\r
212                          *\r
213                          *  \return Response byte from the attached SPI device.\r
214                          */\r
215                         static inline uint8_t SPI_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
216                         static inline uint8_t SPI_TransferByte(const uint8_t Byte)\r
217                         {\r
218                                 SPDR = Byte;\r
219                                 while (!(SPSR & (1 << SPIF)));\r
220                                 return SPDR;\r
221                         }\r
222 \r
223                         /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response\r
224                          *  byte sent to from the attached SPI device is ignored.\r
225                          *\r
226                          *  \param[in] Byte  Byte to send through the SPI interface.\r
227                          */\r
228                         static inline void SPI_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
229                         static inline void SPI_SendByte(const uint8_t Byte)\r
230                         {\r
231                                 SPDR = Byte;\r
232                                 while (!(SPSR & (1 << SPIF)));\r
233                         }\r
234 \r
235                         /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response\r
236                          *  byte from the attached SPI device is returned.\r
237                          *\r
238                          *  \return The response byte from the attached SPI device.\r
239                          */\r
240                         static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
241                         static inline uint8_t SPI_ReceiveByte(void)\r
242                         {\r
243                                 SPDR = 0x00;\r
244                                 while (!(SPSR & (1 << SPIF)));\r
245                                 return SPDR;\r
246                         }\r
247 \r
248         /* Disable C linkage for C++ Compilers: */\r
249                 #if defined(__cplusplus)\r
250                         }\r
251                 #endif\r
252 \r
253 #endif\r
254 \r
255 /** @} */\r
256 \r