|
@@ -27,8 +27,6 @@
|
27
|
27
|
|
28
|
28
|
/**
|
29
|
29
|
* Description: HAL for AVR - SPI functions
|
30
|
|
- *
|
31
|
|
- * For __AVR__
|
32
|
30
|
*/
|
33
|
31
|
|
34
|
32
|
#ifdef __AVR__
|
|
@@ -68,10 +66,12 @@ void spiBegin (void) {
|
68
|
66
|
}
|
69
|
67
|
|
70
|
68
|
|
71
|
|
-//------------------------------------------------------------------------------
|
72
|
69
|
#if DISABLED(SOFTWARE_SPI)
|
73
|
|
- // functions for hardware SPI
|
|
70
|
+
|
74
|
71
|
//------------------------------------------------------------------------------
|
|
72
|
+ // Hardware SPI
|
|
73
|
+ //------------------------------------------------------------------------------
|
|
74
|
+
|
75
|
75
|
// make sure SPCR rate is in expected bits
|
76
|
76
|
#if (SPR0 != 0 || SPR1 != 1)
|
77
|
77
|
#error "unexpected SPCR bits"
|
|
@@ -95,14 +95,13 @@ void spiBegin (void) {
|
95
|
95
|
SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X);
|
96
|
96
|
}
|
97
|
97
|
|
98
|
|
- //------------------------------------------------------------------------------
|
99
|
98
|
/** SPI receive a byte */
|
100
|
99
|
uint8_t spiRec(void) {
|
101
|
100
|
SPDR = 0xFF;
|
102
|
101
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
103
|
102
|
return SPDR;
|
104
|
103
|
}
|
105
|
|
- //------------------------------------------------------------------------------
|
|
104
|
+
|
106
|
105
|
/** SPI read data */
|
107
|
106
|
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
108
|
107
|
if (nbyte-- == 0) return;
|
|
@@ -115,13 +114,13 @@ void spiBegin (void) {
|
115
|
114
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
116
|
115
|
buf[nbyte] = SPDR;
|
117
|
116
|
}
|
118
|
|
- //------------------------------------------------------------------------------
|
|
117
|
+
|
119
|
118
|
/** SPI send a byte */
|
120
|
119
|
void spiSend(uint8_t b) {
|
121
|
120
|
SPDR = b;
|
122
|
121
|
while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
|
123
|
122
|
}
|
124
|
|
- //------------------------------------------------------------------------------
|
|
123
|
+
|
125
|
124
|
/** SPI send block */
|
126
|
125
|
void spiSendBlock(uint8_t token, const uint8_t* buf) {
|
127
|
126
|
SPDR = token;
|
|
@@ -191,25 +190,21 @@ void spiBegin (void) {
|
191
|
190
|
}
|
192
|
191
|
|
193
|
192
|
|
194
|
|
- //------------------------------------------------------------------------------
|
195
|
|
-#else // SOFTWARE_SPI
|
196
|
|
- //------------------------------------------------------------------------------
|
|
193
|
+#else
|
|
194
|
+
|
197
|
195
|
/** nop to tune soft SPI timing */
|
198
|
196
|
#define nop asm volatile ("\tnop\n")
|
199
|
197
|
|
200
|
198
|
/** Set SPI rate */
|
201
|
199
|
void spiInit(uint8_t spiRate) {
|
202
|
|
- // nothing to do
|
203
|
|
- UNUSED(spiRate);
|
|
200
|
+ UNUSED(spiRate); // nothing to do
|
204
|
201
|
}
|
205
|
202
|
|
206
|
203
|
/** Begin SPI transaction, set clock, bit order, data mode */
|
207
|
204
|
void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
|
208
|
|
- // nothing to do
|
209
|
|
- UNUSED(spiBeginTransaction);
|
|
205
|
+ UNUSED(spiBeginTransaction); // nothing to do
|
210
|
206
|
}
|
211
|
207
|
|
212
|
|
- //------------------------------------------------------------------------------
|
213
|
208
|
/** Soft SPI receive byte */
|
214
|
209
|
uint8_t spiRec() {
|
215
|
210
|
uint8_t data = 0;
|
|
@@ -221,8 +216,7 @@ void spiBegin (void) {
|
221
|
216
|
for (uint8_t i = 0; i < 8; i++) {
|
222
|
217
|
WRITE(SCK_PIN, HIGH);
|
223
|
218
|
|
224
|
|
- // adjust so SCK is nice
|
225
|
|
- nop;
|
|
219
|
+ nop; // adjust so SCK is nice
|
226
|
220
|
nop;
|
227
|
221
|
|
228
|
222
|
data <<= 1;
|
|
@@ -231,48 +225,45 @@ void spiBegin (void) {
|
231
|
225
|
|
232
|
226
|
WRITE(SCK_PIN, LOW);
|
233
|
227
|
}
|
234
|
|
- // enable interrupts
|
|
228
|
+
|
235
|
229
|
sei();
|
236
|
230
|
return data;
|
237
|
231
|
}
|
238
|
|
- //------------------------------------------------------------------------------
|
|
232
|
+
|
239
|
233
|
/** Soft SPI read data */
|
240
|
234
|
void spiRead(uint8_t* buf, uint16_t nbyte) {
|
241
|
235
|
for (uint16_t i = 0; i < nbyte; i++)
|
242
|
236
|
buf[i] = spiRec();
|
243
|
237
|
}
|
244
|
|
- //------------------------------------------------------------------------------
|
|
238
|
+
|
245
|
239
|
/** Soft SPI send byte */
|
246
|
240
|
void spiSend(uint8_t data) {
|
247
|
241
|
// no interrupts during byte send - about 8µs
|
248
|
242
|
cli();
|
249
|
243
|
for (uint8_t i = 0; i < 8; i++) {
|
250
|
244
|
WRITE(SCK_PIN, LOW);
|
251
|
|
-
|
252
|
245
|
WRITE(MOSI_PIN, data & 0x80);
|
253
|
|
-
|
254
|
246
|
data <<= 1;
|
255
|
|
-
|
256
|
247
|
WRITE(SCK_PIN, HIGH);
|
257
|
248
|
}
|
258
|
|
- // hold SCK high for a few ns
|
259
|
|
- nop;
|
|
249
|
+
|
|
250
|
+ nop; // hold SCK high for a few ns
|
260
|
251
|
nop;
|
261
|
252
|
nop;
|
262
|
253
|
nop;
|
263
|
254
|
|
264
|
255
|
WRITE(SCK_PIN, LOW);
|
265
|
|
- // enable interrupts
|
|
256
|
+
|
266
|
257
|
sei();
|
267
|
258
|
}
|
268
|
|
- //------------------------------------------------------------------------------
|
|
259
|
+
|
269
|
260
|
/** Soft SPI send block */
|
270
|
261
|
void spiSendBlock(uint8_t token, const uint8_t* buf) {
|
271
|
262
|
spiSend(token);
|
272
|
263
|
for (uint16_t i = 0; i < 512; i++)
|
273
|
264
|
spiSend(buf[i]);
|
274
|
265
|
}
|
275
|
|
-#endif // SOFTWARE_SPI
|
276
|
266
|
|
|
267
|
+#endif // SOFTWARE_SPI
|
277
|
268
|
|
278
|
269
|
#endif // __AVR__
|