Fixed software PDI/TPI programming mode in the AVRISP project not correctly toggling...
[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 * \param[in] SPIOptions SPI Options, a mask consisting of one of each of the SPI_SPEED_*
86 * and SPI_MODE_* masks
87 */
88 static inline void SPI_Init(const uint8_t SPIOptions)
89 {
90 AVR32_SPI.cr = AVR32_SPI_CR_SPIEN_MASK | AVR32_SPI_CR_SWRST_MASK;
91 AVR32_SPI.mr = SPIOptions;
92 }
93
94 /** Turns off the SPI driver, disabling and returning used hardware to their default configuration. */
95 static inline void SPI_ShutDown(void)
96 {
97 AVR32_SPI.cr = AVR32_SPI_CR_SPIDIS_MASK;
98 }
99
100 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
101 *
102 * \param[in] Byte Byte to send through the SPI interface
103 *
104 * \return Response byte from the attached SPI device
105 */
106 static inline uint8_t SPI_TransferByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
107 static inline uint8_t SPI_TransferByte(const uint8_t Byte)
108 {
109 AVR32_SPI.tdr = Byte;
110 // TODO: Wait for receive
111 return AVR32_SPI.rdr;
112 }
113
114 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
115 * byte sent to from the attached SPI device is ignored.
116 *
117 * \param[in] Byte Byte to send through the SPI interface
118 */
119 static inline void SPI_SendByte(const uint8_t Byte) ATTR_ALWAYS_INLINE;
120 static inline void SPI_SendByte(const uint8_t Byte)
121 {
122 AVR32_SPI.tdr = Byte;
123 // TODO: Wait for receive
124 }
125
126 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
127 * byte from the attached SPI device is returned.
128 *
129 * \return The response byte from the attached SPI device
130 */
131 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
132 static inline uint8_t SPI_ReceiveByte(void)
133 {
134 AVR32_SPI.tdr = 0x00;
135 // TODO: Wait for receive
136 return AVR32_SPI.rdr;
137 }
138
139 /* Disable C linkage for C++ Compilers: */
140 #if defined(__cplusplus)
141 }
142 #endif
143
144 #endif
145
146 /** @} */