forked from pichenettes/avril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi.h
309 lines (261 loc) · 6.77 KB
/
spi.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2009 Olivier Gillet.
//
// Author: Olivier Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Fast SPI communication (using the hardware implementation). This will take
// ownership of the pins 11 (data output), 12 (data input) and 13 (clock), +
// a user-definable pin for slave selection. Pin 10 should be kept as an output
// pin, since the SPI master/slave mode is based upon the value of this pin.
#ifndef AVRLIB_SPI_H_
#define AVRLIB_SPI_H_
#include "avrlib/avrlib.h"
#include "avrlib/gpio.h"
#include "avrlib/serial.h"
namespace avrlib {
IORegister(SPSR);
typedef BitInRegister<SPSRRegister, SPI2X> DoubleSpeed;
typedef BitInRegister<SPSRRegister, SPIF> TransferComplete;
template<typename SlaveSelect,
DataOrder order = MSB_FIRST,
uint8_t speed = 4>
class SpiMaster {
public:
enum {
buffer_size = 0,
data_size = 8
};
static void Init() {
SpiSCK::set_mode(DIGITAL_OUTPUT);
SpiMOSI::set_mode(DIGITAL_OUTPUT);
SpiMISO::set_mode(DIGITAL_INPUT);
SpiSS::set_mode(DIGITAL_OUTPUT); // I'm a master!
SpiSS::High();
SlaveSelect::set_mode(DIGITAL_OUTPUT);
SlaveSelect::High();
// SPI enabled, configured as master.
uint8_t configuration = _BV(SPE) | _BV(MSTR);
if (order == LSB_FIRST) {
configuration |= _BV(DORD);
}
DoubleSpeed::clear();
switch (speed) {
case 2:
DoubleSpeed::set();
case 4:
break;
case 8:
DoubleSpeed::set();
case 16:
configuration |= _BV(SPR0);
break;
case 32:
DoubleSpeed::set();
case 64:
configuration |= _BV(SPR1);
break;
case 128:
configuration |= _BV(SPR0);
configuration |= _BV(SPR1);
break;
}
SPCR = configuration;
}
static inline void PullUpMISO() {
SpiMISO::High();
}
static inline void Begin() {
SlaveSelect::Low();
}
static inline void End() {
SlaveSelect::High();
}
static inline void Strobe() {
SlaveSelect::High();
SlaveSelect::Low();
}
static inline void Write(uint8_t v) {
Begin();
Send(v);
End();
}
static inline uint8_t Read() {
Begin();
uint8_t result = Receive();
End();
return result;
}
static inline void Send(uint8_t v) {
Overwrite(v);
Wait();
}
static inline uint8_t Receive() {
Send(0xff);
return ImmediateRead();
}
static inline uint8_t ImmediateRead() {
return SPDR;
}
static inline void Wait() {
while (!TransferComplete::value());
}
static inline void OptimisticWait() {
Wait();
}
static inline void Overwrite(uint8_t v) {
SPDR = v;
}
static inline void WriteWord(uint8_t a, uint8_t b) {
Begin();
Send(a);
Send(b);
End();
}
};
template<DataOrder order = MSB_FIRST,
bool enable_interrupt = false>
class SpiSlave {
public:
enum {
buffer_size = 128,
data_size = 8
};
static void Init() {
SpiSCK::set_mode(DIGITAL_INPUT);
SpiMOSI::set_mode(DIGITAL_INPUT);
SpiMISO::set_mode(DIGITAL_OUTPUT);
SpiSS::set_mode(DIGITAL_INPUT); // Ohhh mistress, ohhhh!
// SPI enabled, configured as master.
uint8_t configuration = _BV(SPE);
if (order == LSB_FIRST) {
configuration |= _BV(DORD);
}
if (enable_interrupt) {
configuration |= _BV(SPIE);
}
SPCR = configuration;
}
static inline void Reply(uint8_t value) {
SPDR = value;
}
static inline uint8_t readable() {
return TransferComplete::value();
}
static inline uint8_t ImmediateRead() {
return SPDR;
}
static inline uint8_t Read() {
while (!readable());
return ImmediateRead();
}
};
template<typename XckPort,
typename TxPort,
typename RxPort,
typename PrescalerRegister,
typename ControlRegisterB,
uint8_t BFlags,
typename ControlRegisterC,
uint8_t CFlags,
typename TxReadyBit,
typename DataRegister>
struct UartSpiPort {
static inline uint8_t tx_ready() { return TxReadyBit::value(); }
static inline uint8_t data() { return *DataRegister::ptr(); }
static inline void set_data(uint8_t value) { *DataRegister::ptr() = value; }
static inline void Setup(uint16_t rate) {
*PrescalerRegister::ptr() = 0;
XckPort::set_mode(DIGITAL_OUTPUT);
TxPort::set_mode(DIGITAL_OUTPUT);
RxPort::set_mode(DIGITAL_INPUT);
*ControlRegisterC::ptr() = CFlags;
*ControlRegisterB::ptr() = BFlags;
*PrescalerRegister::ptr() = rate;
}
};
#ifdef HAS_USART0
typedef UartSpiPort<
UartSpi0XCK,
UartSpi0TX,
UartSpi0RX,
UBRR0Register,
UCSR0BRegister,
_BV(RXEN0) | _BV(TXEN0),
UCSR0CRegister,
_BV(UMSEL01) | _BV(UMSEL00),
BitInRegister<UCSR0ARegister, UDRE0>,
UDR0Register> UartSpiPort0;
#endif // HAS_USART0
#ifdef HAS_USART1
typedef UartSpiPort<
UartSpi1XCK,
UartSpi1TX,
UartSpi1RX,
UBRR1Register,
UCSR1BRegister,
_BV(RXEN1) | _BV(TXEN1),
UCSR1CRegister,
_BV(UMSEL11) | _BV(UMSEL10),
BitInRegister<UCSR1ARegister, UDRE1>,
UDR1Register> UartSpiPort1;
#endif // HAS_USART1
template<typename Port, typename SlaveSelect, uint8_t speed = 2>
class UartSpiMaster {
public:
enum {
buffer_size = 0,
data_size = 8
};
static void Init() {
SlaveSelect::set_mode(DIGITAL_OUTPUT);
SlaveSelect::High();
Port::Setup((speed / 2) - 1);
}
static inline void Begin() {
SlaveSelect::Low();
}
static inline void End() {
SlaveSelect::High();
}
static inline void Strobe() {
SlaveSelect::High();
SlaveSelect::Low();
}
static inline void Write(uint8_t v) {
Begin();
Send(v);
End();
}
static inline void Send(uint8_t v) {
Overwrite(v);
Wait();
}
static inline void Wait() {
while (!Port::tx_ready());
}
static inline void OptimisticWait() { }
static inline void Overwrite(uint8_t v) {
Port::set_data(v);
}
static inline void WriteWord(uint8_t a, uint8_t b) {
Begin();
Send(a);
Send(b);
End();
}
};
#define SPI_RECEIVE ISR(SPI_STC_vect)
} // namespace avrlib
#endif AVRLIB_SPI_H_