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