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