]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/Peripheral/XMEGA/SPI_XMEGA.h
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / Peripheral / XMEGA / SPI_XMEGA.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 (XMEGA)\r
33  *\r
34  *  On-chip SPI driver for the XMEGA 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_XMEGA SPI Peripheral Driver (XMEGA)\r
42  *\r
43  *  \section Sec_ModDescription Module Description\r
44  *  Driver for the hardware SPI port(s) available on XMEGA AVR microcontroller models. This\r
45  *  module provides an easy to use driver for the setup and transfer of data over the AVR's\r
46  *  SPI ports.\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  *  \code\r
52  *      // Initialize the SPI driver before first use\r
53  *      SPI_Init(&SPIC,\r
54  *               SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING |\r
55  *               SPI_SAMPLE_TRAILING | SPI_MODE_MASTER);\r
56  *      \r
57  *      // Send several bytes, ignoring the returned data\r
58  *      SPI_SendByte(&SPIC, 0x01);\r
59  *      SPI_SendByte(&SPIC, 0x02);\r
60  *      SPI_SendByte(&SPIC, 0x03);\r
61  *      \r
62  *      // Receive several bytes, sending a dummy 0x00 byte each time\r
63  *      uint8_t Byte1 = SPI_ReceiveByte(&SPIC);\r
64  *      uint8_t Byte2 = SPI_ReceiveByte(&SPIC);\r
65  *      uint8_t Byte3 = SPI_ReceiveByte(&SPIC);\r
66  *      \r
67  *      // Send a byte, and store the received byte from the same transaction\r
68  *      uint8_t ResponseByte = SPI_TransferByte(&SPIC, 0xDC);\r
69  *  \endcode\r
70  *\r
71  *  @{\r
72  */\r
73 \r
74 #ifndef __SPI_XMEGA_H__\r
75 #define __SPI_XMEGA_H__\r
76 \r
77         /* Includes: */\r
78                 #include "../../../Common/Common.h"\r
79 \r
80         /* Enable C linkage for C++ Compilers: */\r
81                 #if defined(__cplusplus)\r
82                         extern "C" {\r
83                 #endif\r
84 \r
85         /* Preprocessor Checks: */\r
86                 #if !defined(__INCLUDE_FROM_SPI_H)\r
87                         #error Do not include this file directly. Include LUFA/Drivers/Peripheral/SPI.h instead.\r
88                 #endif\r
89 \r
90         /* Private Interface - For use in library only: */\r
91         #if !defined(__DOXYGEN__)\r
92                 /* Macros: */\r
93                         #define SPI_USE_DOUBLESPEED            SPI_CLK2X_bm\r
94         #endif\r
95 \r
96         /* Public Interface - May be used in end-application: */\r
97                 /* Macros: */\r
98                         /** \name SPI Prescaler Configuration Masks */\r
99                         //@{\r
100                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 2. */\r
101                         #define SPI_SPEED_FCPU_DIV_2           SPI_USE_DOUBLESPEED\r
102 \r
103                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 4. */\r
104                         #define SPI_SPEED_FCPU_DIV_4           0\r
105 \r
106                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 8. */\r
107                         #define SPI_SPEED_FCPU_DIV_8           (SPI_USE_DOUBLESPEED | (1 << SPI_PRESCALER_gp))\r
108 \r
109                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 16. */\r
110                         #define SPI_SPEED_FCPU_DIV_16          (1 << SPI_PRESCALER_gp)\r
111 \r
112                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 32. */\r
113                         #define SPI_SPEED_FCPU_DIV_32          (SPI_USE_DOUBLESPEED | (2 << SPI_PRESCALER_gp))\r
114 \r
115                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 64. */\r
116                         #define SPI_SPEED_FCPU_DIV_64          (2 << SPI_PRESCALER_gp)\r
117 \r
118                         /** SPI prescaler mask for \ref SPI_Init(). Divides the system clock by a factor of 128. */\r
119                         #define SPI_SPEED_FCPU_DIV_128         (3 << SPI_PRESCALER_gp)\r
120                         //@}\r
121 \r
122                         /** \name SPI SCK Polarity Configuration Masks */\r
123                         //@{\r
124                         /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the rising edge. */\r
125                         #define SPI_SCK_LEAD_RISING            0\r
126 \r
127                         /** SPI clock polarity mask for \ref SPI_Init(). Indicates that the SCK should lead on the falling edge. */\r
128                         #define SPI_SCK_LEAD_FALLING           SPI_MODE1_bm\r
129                         //@}\r
130 \r
131                         /** \name SPI Sample Edge Configuration Masks */\r
132                         //@{\r
133                         /** SPI data sample mode mask for \ref SPI_Init(). Indicates that the data should sampled on the leading edge. */\r
134                         #define SPI_SAMPLE_LEADING             0\r
135 \r
136                         /** SPI data sample mode mask for \ref SPI_Init(). Indicates that the data should be sampled on the trailing edge. */\r
137                         #define SPI_SAMPLE_TRAILING            SPI_MODE0_bm\r
138                         //@}\r
139 \r
140                         /** \name SPI Data Ordering Configuration Masks */\r
141                         //@{\r
142                         /** SPI data order mask for \ref SPI_Init(). Indicates that data should be shifted out MSB first. */\r
143                         #define SPI_ORDER_MSB_FIRST            0\r
144 \r
145                         /** SPI data order mask for \ref SPI_Init(). Indicates that data should be shifted out LSB first. */\r
146                         #define SPI_ORDER_LSB_FIRST            SPI_DORD_bm\r
147                         //@}\r
148 \r
149                         /** \name SPI Mode Configuration Masks */\r
150                         //@{\r
151                         /** SPI mode mask for \ref SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */\r
152                         #define SPI_MODE_SLAVE                 0\r
153 \r
154                         /** SPI mode mask for \ref SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */\r
155                         #define SPI_MODE_MASTER                SPI_MASTER_bm\r
156                         //@}\r
157 \r
158                 /* Inline Functions: */\r
159                         /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other\r
160                          *  SPI routines.\r
161                          *\r
162                          *  \param[in,out] SPI         Pointer to the base of the SPI peripheral within the device.\r
163                          *  \param[in]     SPIOptions  SPI Options, a mask consisting of one of each of the \c SPI_SPEED_*,\r
164                          *                             \c SPI_SCK_*, \c SPI_SAMPLE_*, \c SPI_ORDER_* and \c SPI_MODE_* masks.\r
165                          */\r
166                         static inline void SPI_Init(SPI_t* const SPI,\r
167                                                     const uint8_t SPIOptions)\r
168                         {\r
169                                 SPI->CTRL = (SPIOptions | SPI_ENABLE_bm);\r
170                         }\r
171 \r
172                         /** Turns off the SPI driver, disabling and returning used hardware to their default configuration.\r
173                          *\r
174                          *  \param[in,out] SPI   Pointer to the base of the SPI peripheral within the device.\r
175                          */\r
176                         static inline void SPI_Disable(SPI_t* const SPI)\r
177                         {\r
178                                 SPI->CTRL &= ~SPI_ENABLE_bm;\r
179                         }\r
180 \r
181                         /** Retrieves the currently selected SPI mode, once the SPI interface has been configured.\r
182                          *\r
183                          *  \param[in,out] SPI  Pointer to the base of the SPI peripheral within the device.\r
184                          *\r
185                          *  \return \ref SPI_MODE_MASTER if the interface is currently in SPI Master mode, \ref SPI_MODE_SLAVE otherwise\r
186                          */\r
187                         static inline uint8_t SPI_GetCurrentMode(SPI_t* const SPI) ATTR_ALWAYS_INLINE;\r
188                         static inline uint8_t SPI_GetCurrentMode(SPI_t* const SPI)\r
189                         {\r
190                                 return (SPI->CTRL & SPI_MASTER_bm);\r
191                         }\r
192 \r
193                         /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.\r
194                          *\r
195                          *  \param[in,out] SPI   Pointer to the base of the SPI peripheral within the device.\r
196                          *  \param[in]     Byte  Byte to send through the SPI interface.\r
197                          *\r
198                          *  \return Response byte from the attached SPI device.\r
199                          */\r
200                         static inline uint8_t SPI_TransferByte(SPI_t* const SPI,\r
201                                                                const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
202                         static inline uint8_t SPI_TransferByte(SPI_t* const SPI,\r
203                                                                const uint8_t Byte)\r
204                         {\r
205                                 SPI->DATA = Byte;\r
206                                 while (!(SPI->STATUS & SPI_IF_bm));\r
207                                 return SPI->DATA;\r
208                         }\r
209 \r
210                         /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response\r
211                          *  byte sent to from the attached SPI device is ignored.\r
212                          *\r
213                          *  \param[in,out] SPI   Pointer to the base of the SPI peripheral within the device.\r
214                          *  \param[in]     Byte  Byte to send through the SPI interface.\r
215                          */\r
216                         static inline void SPI_SendByte(SPI_t* const SPI,\r
217                                                         const uint8_t Byte) ATTR_ALWAYS_INLINE;\r
218                         static inline void SPI_SendByte(SPI_t* const SPI,\r
219                                                         const uint8_t Byte)\r
220                         {\r
221                                 SPI->DATA = Byte;\r
222                                 while (!(SPI->STATUS & SPI_IF_bm));\r
223                         }\r
224 \r
225                         /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response\r
226                          *  byte from the attached SPI device is returned.\r
227                          *\r
228                          *  \param[in,out] SPI  Pointer to the base of the SPI peripheral within the device.\r
229                          *\r
230                          *  \return The response byte from the attached SPI device.\r
231                          */\r
232                         static inline uint8_t SPI_ReceiveByte(SPI_t* const SPI) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;\r
233                         static inline uint8_t SPI_ReceiveByte(SPI_t* const SPI)\r
234                         {\r
235                                 SPI->DATA = 0;\r
236                                 while (!(SPI->STATUS & SPI_IF_bm));\r
237                                 return SPI->DATA;\r
238                         }\r
239 \r
240         /* Disable C linkage for C++ Compilers: */\r
241                 #if defined(__cplusplus)\r
242                         }\r
243                 #endif\r
244 \r
245 #endif\r
246 \r
247 /** @} */\r
248 \r