]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/Peripheral/XMEGA/SerialSPI_XMEGA.h
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / Peripheral / XMEGA / SerialSPI_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 Master SPI Mode Serial USART Peripheral Driver (XMEGA)
33  *
34  *  On-chip Master SPI mode USART driver for the XMEGA AVR microcontrollers.
35  *
36  *  \note This file should not be included directly. It is automatically included as needed by the SPI Master
37  *        Mode USART driver dispatch header located in LUFA/Drivers/Peripheral/Serial.h.
38  */
39
40 /** \ingroup Group_SerialSPI
41  *  \defgroup Group_SerialSPI_XMEGA Master SPI Mode Serial USART Peripheral Driver (XMEGA)
42  *
43  *  \section Sec_SerialSPI_XMEGA_ModDescription Module Description
44  *  On-chip serial USART driver for the XMEGA AVR microcontrollers.
45  *
46  *  \note This file should not be included directly. It is automatically included as needed by the ADC driver
47  *        dispatch header located in LUFA/Drivers/Peripheral/SerialSPI.h.
48  *
49  *  \section Sec_SerialSPI_XMEGA_ExampleUsage Example Usage
50  *  The following snippet is an example of how this module may be used within a typical
51  *  application.
52  *
53  *  \code
54  *      // Initialize the Master SPI mode USART driver before first use, with 1Mbit baud
55  *      SerialSPI_Init(&USARTD0, (USART_SPI_SCK_LEAD_RISING | USART_SPI_SAMPLE_LEADING | USART_SPI_ORDER_MSB_FIRST), 1000000);
56  *
57  *      // Send several bytes, ignoring the returned data
58  *      SerialSPI_SendByte(&USARTD0, 0x01);
59  *      SerialSPI_SendByte(&USARTD0, 0x02);
60  *      SerialSPI_SendByte(&USARTD0, 0x03);
61  *
62  *      // Receive several bytes, sending a dummy 0x00 byte each time
63  *      uint8_t Byte1 = SerialSPI_ReceiveByte(&USARTD);
64  *      uint8_t Byte2 = SerialSPI_ReceiveByte(&USARTD);
65  *      uint8_t Byte3 = SerialSPI_ReceiveByte(&USARTD);
66  *
67  *      // Send a byte, and store the received byte from the same transaction
68  *      uint8_t ResponseByte = SerialSPI_TransferByte(&USARTD0, 0xDC);
69  *  \endcode
70  *
71  *  @{
72  */
73
74 #ifndef __SERIAL_SPI_XMEGA_H__
75 #define __SERIAL_SPI_XMEGA_H__
76
77         /* Includes: */
78                 #include "../../../Common/Common.h"
79
80                 #include <stdio.h>
81
82         /* Enable C linkage for C++ Compilers: */
83                 #if defined(__cplusplus)
84                         extern "C" {
85                 #endif
86
87         /* Preprocessor Checks: */
88                 #if !defined(__INCLUDE_FROM_SERIAL_SPI_H)
89                         #error Do not include this file directly. Include LUFA/Drivers/Peripheral/Serial.h instead.
90                 #endif
91
92         /* Private Interface - For use in library only: */
93         #if !defined(__DOXYGEN__)
94                         #define SERIAL_SPI_UBBRVAL(Baud)       ((Baud < (F_CPU / 2)) ? ((F_CPU / (2 * Baud)) - 1) : 0)
95         #endif
96
97         /* Public Interface - May be used in end-application: */
98                 /* Macros: */
99                         /** \name SPI SCK Polarity Configuration Masks */
100                         //@{
101                         /** SPI clock polarity mask for \ref SerialSPI_Init(). Indicates that the SCK should lead on the rising edge. */
102                         #define USART_SPI_SCK_LEAD_RISING      0
103                         //@}
104
105                         /** \name SPI Sample Edge Configuration Masks */
106                         //@{
107                         /** SPI data sample mode mask for \ref SerialSPI_Init(). Indicates that the data should sampled on the leading edge. */
108                         #define USART_SPI_SAMPLE_LEADING       0
109
110                         /** SPI data sample mode mask for \ref SerialSPI_Init(). Indicates that the data should be sampled on the trailing edge. */
111                         #define USART_SPI_SAMPLE_TRAILING      (1 << 1)
112                         //@}
113
114                         /** \name SPI Data Ordering Configuration Masks */
115                         //@{
116                         /** SPI data order mask for \ref SerialSPI_Init(). Indicates that data should be shifted out MSB first. */
117                         #define USART_SPI_ORDER_MSB_FIRST      0
118
119                         /** SPI data order mask for \ref SerialSPI_Init(). Indicates that data should be shifted out LSB first. */
120                         #define USART_SPI_ORDER_LSB_FIRST      (1 << 2)
121                         //@}
122
123                 /* Inline Functions: */
124                         /** Initialize the USART module in Master SPI mode.
125                          *
126                          *  \param[in,out] USART        Pointer to the base of the USART peripheral within the device.
127                          *  \param[in]     SPIOptions   USART SPI Options, a mask consisting of one of each of the \c USART_SPI_SCK_*,
128                          *                              \c USART_SPI_SAMPLE_* and \c USART_SPI_ORDER_* masks.
129                          *  \param[in]     BaudRate     SPI baud rate, in bits per second.
130                          */
131                         static inline void SerialSPI_Init(USART_t* const USART,
132                                                           const uint8_t SPIOptions,
133                                                           const uint32_t BaudRate) ATTR_NON_NULL_PTR_ARG(1);
134                         static inline void SerialSPI_Init(USART_t* const USART,
135                                                           const uint8_t SPIOptions,
136                                                           const uint32_t BaudRate)
137                         {
138                                 uint16_t BaudValue = SERIAL_SPI_UBBRVAL(BaudRate);
139
140                                 USART->BAUDCTRLB = (BaudValue >> 8);
141                                 USART->BAUDCTRLA = (BaudValue & 0xFF);
142
143                                 USART->CTRLC = (USART_CMODE_MSPI_gc | SPIOptions);
144                                 USART->CTRLB = (USART_RXEN_bm | USART_TXEN_bm);
145                         }
146
147                         /** Turns off the USART driver, disabling and returning used hardware to their default configuration.
148                          *
149                          *  \param[in,out] USART  Pointer to the base of the USART peripheral within the device.
150                          */
151                         static inline void SerialSPI_Disable(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
152                         static inline void SerialSPI_Disable(USART_t* const USART)
153                         {
154                                 USART->CTRLA = 0;
155                                 USART->CTRLB = 0;
156                                 USART->CTRLC = 0;
157                         }
158
159                         /** Sends and receives a byte through the USART SPI interface, blocking until the transfer is complete.
160                          *
161                          *  \param[in,out] USART     Pointer to the base of the USART peripheral within the device.
162                          *  \param[in]     DataByte  Byte to send through the USART SPI interface.
163                          *
164                          *  \return Response byte from the attached SPI device.
165                          */
166                         static inline uint8_t SerialSPI_TransferByte(USART_t* const USART,
167                                                                      const uint8_t DataByte) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
168                         static inline uint8_t SerialSPI_TransferByte(USART_t* const USART,
169                                                                      const uint8_t DataByte)
170                         {
171                                 USART->DATA   = DataByte;
172                                 while (!(USART->STATUS & USART_TXCIF_bm));
173                                 USART->STATUS = USART_TXCIF_bm;
174                                 return USART->DATA;
175                         }
176
177                         /** Sends a byte through the USART SPI interface, blocking until the transfer is complete. The response
178                          *  byte sent to from the attached SPI device is ignored.
179                          *
180                          *  \param[in,out] USART     Pointer to the base of the USART peripheral within the device.
181                          *  \param[in]     DataByte  Byte to send through the USART SPI interface.
182                          */
183                         static inline void SerialSPI_SendByte(USART_t* const USART,
184                                                               const uint8_t DataByte) ATTR_ALWAYS_INLINE ATTR_NON_NULL_PTR_ARG(1);
185                         static inline void SerialSPI_SendByte(USART_t* const USART,
186                                                               const uint8_t DataByte)
187                         {
188                                 SerialSPI_TransferByte(USART, DataByte);
189                         }
190
191                         /** Sends a dummy byte through the USART SPI interface, blocking until the transfer is complete. The response
192                          *  byte from the attached SPI device is returned.
193                          *
194                          *  \param[in,out] USART  Pointer to the base of the USART peripheral within the device.
195                          *
196                          *  \return The response byte from the attached SPI device.
197                          */
198                         static inline uint8_t SerialSPI_ReceiveByte(USART_t* const USART) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(1);
199                         static inline uint8_t SerialSPI_ReceiveByte(USART_t* const USART)
200                         {
201                                 return SerialSPI_TransferByte(USART, 0);
202                         }
203
204         /* Disable C linkage for C++ Compilers: */
205                 #if defined(__cplusplus)
206                         }
207                 #endif
208
209 #endif
210
211 /** @} */
212