More AVR32 achitecture ports.
[pub/USBasp.git] / LUFA / Drivers / Peripheral / AVR32 / SPI.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 disclaim 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 *
33 * SPI driver for the 32-bit AVRs.
34 *
35 * \note This file should not be included directly. It is automatically included as needed by the SPI driver
36 * dispatch header located in LUFA/Drivers/Peripheral/SPI.h.
37 */
38
39 /** \ingroup Group_SPI
40 * @defgroup Group_SPI_AVR32 32-Bit AVR SPI Driver
41 *
42 * SPI driver for the 32-bit AVRs.
43 *
44 * \note This file should not be included directly. It is automatically included as needed by the ADC driver
45 * dispatch header located in LUFA/Drivers/Peripheral/SPI.h.
46 *
47 * @{
48 */
49
50 #ifndef __SPI_AVR32_H__
51 #define __SPI_AVR32_H__
52
53 /* Includes: */
54 #include <avr32/io.h>
55 #include <stdbool.h>
56
57 /* Preprocessor Checks: */
58 #if !defined(__INCLUDE_FROM_SPI_H)
59 #error Do not include this file directly. Include LUFA/Drivers/Peripheral/SPI.h instead.
60 #endif
61
62 /* Enable C linkage for C++ Compilers: */
63 #if defined(__cplusplus)
64 extern "C" {
65 #endif
66
67 /* Public Interface - May be used in end-application: */
68 /* Macros: */
69 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 1. */
70 #define SPI_SPEED_FCPU_DIV_1 0
71
72 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 32. */
73 #define SPI_SPEED_FCPU_DIV_32 AVR32_SPI_MR_FDIV_MASK
74
75 /** SPI chip selection mode for direct peripheral-to-CS pin connections. */
76 #define SPI_CS_4BITDECODER AVR32_SPI_MR_PSDEC_MASK
77
78 /** SPI chip selection mode for peripheral CS pin connections through a 4-bit decoder. */
79 #define SPI_CS_DIRECT 0
80
81 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
82 #define SPI_MODE_SLAVE 0
83
84 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
85 #define SPI_MODE_MASTER AVR32_SPI_MR_MSTR_MASK
86
87 /* Inline Functions: */
88 /** Initialises the SPI subsystem, ready for transfers. Must be called before calling any other
89 * SPI routines.
90 *
91 * \note The individual AVR32 chip select control registers are left at their defaults; it is up to the user
92 * to configure these seperately once the SPI module has been initialized.
93 *
94 * \note The physical GPIO pins for the AVR32's SPI are not altered; it is up to the user to
95 * configure these seperately to connect the SPI module to the desired GPIO pins via the
96 * GPIO MUX registers.
97 *
98 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the SPI_SPEED_*,
99 * SPI_CS_* and SPI_MODE_* masks
100 */
101 static inline void SPI_Init(const uintN_t SPIOptions)
102 {
103 AVR32_PM.pbamask = (1 << 5);
104
105 AVR32_SPI.CR.swrst = true;
106 AVR32_SPI.CR.spien = true;
107 AVR32_SPI.mr = SPIOptions;
108 }
109
110 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
111 static inline void SPI_ShutDown(void)
112 {
113 AVR32_SPI.cr = AVR32_SPI_CR_SPIDIS_MASK;
114
115 AVR32_PM.pbamask &= ~(1 << 5);
116 }
117
118 /** Sends and receives a transfer through the SPI interface, blocking until the transfer is complete.
119 * The width of the data that is transferred is dependant on the settings of the currently selected
120 * peripheral.
121 *
122 * \param[in] Data Data to send through the SPI interface
123 *
124 * \return Response data from the attached SPI device
125 */
126 static inline uint16_t SPI_Transfer(const uint16_t Data) ATTR_ALWAYS_INLINE;
127 static inline uint16_t SPI_Transfer(const uint16_t Data)
128 {
129 while (!(AVR32_SPI.SR.tdre));
130 AVR32_SPI.TDR.td = Data;
131
132 while ((AVR32_SPI.sr & (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) !=
133 (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK));
134 return AVR32_SPI.RDR.rd;
135 }
136
137 /** Sends a transfer through the SPI interface, blocking until the transfer is complete. The response
138 * data sent to from the attached SPI device is ignored. The width of the data that is transferred is
139 * dependant on the settings of the currently selected peripheral.
140 *
141 * \param[in] Data Data to send through the SPI interface
142 */
143 static inline void SPI_Send(const uint16_t Data) ATTR_ALWAYS_INLINE;
144 static inline void SPI_Send(const uint16_t Data)
145 {
146 while (!(AVR32_SPI.SR.tdre));
147 AVR32_SPI.TDR.td = Data;
148 }
149
150 /** Sends a dummy transfer through the SPI interface, blocking until the transfer is complete. The response
151 * data from the attached SPI device is returned. The width of the data that is transferred is dependant on
152 * the settings of the currently selected peripheral.
153 *
154 * \return The response data from the attached SPI device
155 */
156 static inline uint16_t SPI_Receive(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
157 static inline uint16_t SPI_Receive(void)
158 {
159 while (!(AVR32_SPI.SR.tdre));
160 AVR32_SPI.TDR.td = 0x0000;
161
162 while ((AVR32_SPI.sr & (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK)) !=
163 (AVR32_SPI_SR_RDRF_MASK | AVR32_SPI_SR_TXEMPTY_MASK));
164 return AVR32_SPI.RDR.rd;
165 }
166
167 /* Disable C linkage for C++ Compilers: */
168 #if defined(__cplusplus)
169 }
170 #endif
171
172 #endif
173
174 /** @} */