Moved all source to the trunk directory.
[pub/USBasp.git] / LUFA / Drivers / AT90USBXXX / SPI.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 * Hardware SPI subsystem driver for the supported USB AVRs models.
34 */
35
36 #ifndef __SPI_H__
37 #define __SPI_H__
38
39 /* Includes: */
40 #include <stdbool.h>
41
42 /* Enable C linkage for C++ Compilers: */
43 #if defined(__cplusplus)
44 extern "C" {
45 #endif
46
47 /* Private Interface - For use in library only: */
48 #if !defined(__DOXYGEN__)
49 /* Macros: */
50 #define SPI_USE_DOUBLESPEED (1 << 7)
51 #endif
52
53 /* Public Interface - May be used in end-application: */
54 /* Macros: */
55 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 2. */
56 #define SPI_SPEED_FCPU_DIV_2 SPI_USE_DOUBLESPEED
57
58 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 4. */
59 #define SPI_SPEED_FCPU_DIV_4 0
60
61 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 8. */
62 #define SPI_SPEED_FCPU_DIV_8 (SPI_USE_DOUBLESPEED | (1 << SPR0))
63
64 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 16. */
65 #define SPI_SPEED_FCPU_DIV_16 (1 << SPR0)
66
67 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 32. */
68 #define SPI_SPEED_FCPU_DIV_32 (SPI_USE_DOUBLESPEED | (1 << SPR1))
69
70 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 64. */
71 #define SPI_SPEED_FCPU_DIV_64 (SPI_USE_DOUBLESPEED | (1 << SPR1) | (1 < SPR0))
72
73 /** SPI prescaler mask for SPI_Init(). Divides the system clock by a factor of 128. */
74 #define SPI_SPEED_FCPU_DIV_128 ((1 << SPR1) | (1 < SPR0))
75
76 /* Inline Functions: */
77 /** Initializes the SPI subsystem, ready for transfers. Must be called before calling any other
78 * SPI routines.
79 *
80 * \param PrescalerMask Prescaler mask to set the SPI clock speed
81 * \param Master If true, sets the SPI system to use master mode, slave if false
82 */
83 static inline void SPI_Init(const uint8_t PrescalerMask, const bool Master)
84 {
85 DDRB |= ((1 << 1) | (1 << 2));
86 PORTB |= ((1 << 0) | (1 << 3));
87
88 SPCR = ((1 << SPE) | (Master << MSTR) | (1 << CPOL) | (1 << CPHA) |
89 (PrescalerMask & ~SPI_USE_DOUBLESPEED));
90
91 if (PrescalerMask & SPI_USE_DOUBLESPEED)
92 SPSR = (1 << SPI2X);
93 }
94
95 /** Sends and receives a byte through the SPI interface, blocking until the transfer is complete.
96 *
97 * \param Byte Byte to send through the SPI interface
98 *
99 * \return Response byte from the attached SPI device
100 */
101 static inline uint8_t SPI_TransferByte(const uint8_t Byte) ATTR_ALWAYSINLINE;
102 static inline uint8_t SPI_TransferByte(const uint8_t Byte)
103 {
104 SPDR = Byte;
105 while (!(SPSR & (1 << SPIF)));
106 return SPDR;
107 }
108
109 /** Sends a byte through the SPI interface, blocking until the transfer is complete. The response
110 * byte sent to from the attached SPI device is ignored.
111 *
112 * \param Byte Byte to send through the SPI interface
113 */
114 static inline void SPI_SendByte(const uint8_t Byte) ATTR_ALWAYSINLINE;
115 static inline void SPI_SendByte(const uint8_t Byte)
116 {
117 SPDR = Byte;
118 while (!(SPSR & (1 << SPIF)));
119 }
120
121 /** Sends a dummy byte through the SPI interface, blocking until the transfer is complete. The response
122 * byte from the attached SPI device is returned.
123 *
124 * \return The response byte from the attached SPI device
125 */
126 static inline uint8_t SPI_ReceiveByte(void) ATTR_ALWAYSINLINE ATTR_WARN_UNUSED_RESULT;
127 static inline uint8_t SPI_ReceiveByte(void)
128 {
129 SPDR = 0x00;
130 while (!(SPSR & (1 << SPIF)));
131 return SPDR;
132 }
133
134 /* Disable C linkage for C++ Compilers: */
135 #if defined(__cplusplus)
136 }
137 #endif
138
139 #endif