Update Temperature board driver to be AVR32 compatible when the ADC peripheral driver...
[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 mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into slave mode. */
76 #define SPI_MODE_SLAVE 0
77
78 /** SPI mode mask for SPI_Init(). Indicates that the SPI interface should be initialized into master mode. */
79 #define SPI_MODE_MASTER AVR32_SPI_MR_MSTR_MASK
80
81 /* Inline Functions: */
82 /** Initialises the SPI subsystem, ready for transfers. Must be called before calling any other
83 * SPI routines.
84 *
85 * \note The individual AVR32 chip select control registers are left at their defaults; it is up to the user
86 * to configure these seperately once the SPI module has been initialized.
87 *
88 * \note The physical GPIO pins for the AVR32's SPI are not altered; it is up to the user to
89 * configure these seperately to connect the SPI module to the desired GPIO pins via the
90 * GPIO MUX registers.
91 *
92 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the SPI_SPEED_*
93 * and SPI_MODE_* masks
94 */
95 static inline void SPI_Init(const uintN_t SPIOptions)
96 {
97 AVR32_SPI.cr = (AVR32_SPI_CR_SPIEN_MASK | AVR32_SPI_CR_SWRST_MASK);
98 AVR32_SPI.mr = SPIOptions;
99 }
100
101 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
102 static inline void SPI_ShutDown(void)
103 {
104 AVR32_SPI.cr = AVR32_SPI_CR_SPIDIS_MASK;
105 }
106
107 /** Sends and receives a transfer through the SPI interface, blocking until the transfer is complete.
108 * The width of the data that is transferred is dependant on the settings of the currently selected
109 * peripheral.
110 *
111 * \param[in] Data Data to send through the SPI interface
112 *
113 * \return Response data from the attached SPI device
114 */
115 static inline uint16_t SPI_Transfer(const uint16_t Data) ATTR_ALWAYS_INLINE;
116 static inline uint16_t SPI_Transfer(const uint16_t Data)
117 {
118 AVR32_SPI.TDR.td = Data;
119 while (!(AVR32_SPI.SR.tdre));
120 return AVR32_SPI.rdr;
121 }
122
123 /** Sends a transfer through the SPI interface, blocking until the transfer is complete. The response
124 * data sent to from the attached SPI device is ignored. The width of the data that is transferred is
125 * dependant on the settings of the currently selected peripheral.
126 *
127 * \param[in] Data Data to send through the SPI interface
128 */
129 static inline void SPI_Send(const uint16_t Data) ATTR_ALWAYS_INLINE;
130 static inline void SPI_Send(const uint16_t Data)
131 {
132 AVR32_SPI.TDR.td = Data;
133 while (!(AVR32_SPI.SR.tdre));
134 }
135
136 /** Sends a dummy transfer through the SPI interface, blocking until the transfer is complete. The response
137 * data from the attached SPI device is returned. The width of the data that is transferred is dependant on
138 * the settings of the currently selected peripheral.
139 *
140 * \return The response data from the attached SPI device
141 */
142 static inline uint16_t SPI_Receive(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
143 static inline uint16_t SPI_Receive(void)
144 {
145 AVR32_SPI.TDR.td = 0x0000;
146 while (!(AVR32_SPI.SR.tdre));
147 return AVR32_SPI.RDR.rd;
148 }
149
150 /* Disable C linkage for C++ Compilers: */
151 #if defined(__cplusplus)
152 }
153 #endif
154
155 #endif
156
157 /** @} */