|
@@ -0,0 +1,770 @@
|
|
1
|
+//https://github.com/niteris/ArduinoSoftSpi
|
|
2
|
+
|
|
3
|
+#include <Arduino.h>
|
|
4
|
+#ifdef __arm__
|
|
5
|
+#ifdef CORE_TEENSY
|
|
6
|
+//------------------------------------------------------------------------------
|
|
7
|
+/** read pin value
|
|
8
|
+ * @param[in] pin Arduino pin number
|
|
9
|
+ * @return value read
|
|
10
|
+ */
|
|
11
|
+static inline __attribute__((always_inline))
|
|
12
|
+bool fastDigitalRead(uint8_t pin) {
|
|
13
|
+ return *portInputRegister(pin);
|
|
14
|
+}
|
|
15
|
+//------------------------------------------------------------------------------
|
|
16
|
+/** Set pin value
|
|
17
|
+ * @param[in] pin Arduino pin number
|
|
18
|
+ * @param[in] level value to write
|
|
19
|
+ */
|
|
20
|
+static inline __attribute__((always_inline))
|
|
21
|
+void fastDigitalWrite(uint8_t pin, bool value) {
|
|
22
|
+ if (value) {
|
|
23
|
+ *portSetRegister(pin) = 1;
|
|
24
|
+ } else {
|
|
25
|
+ *portClearRegister(pin) = 1;
|
|
26
|
+ }
|
|
27
|
+}
|
|
28
|
+#else // CORE_TEENSY
|
|
29
|
+//------------------------------------------------------------------------------
|
|
30
|
+/** read pin value
|
|
31
|
+ * @param[in] pin Arduino pin number
|
|
32
|
+ * @return value read
|
|
33
|
+ */
|
|
34
|
+static inline __attribute__((always_inline))
|
|
35
|
+bool fastDigitalRead(uint8_t pin){
|
|
36
|
+ return g_APinDescription[pin].pPort->PIO_PDSR & g_APinDescription[pin].ulPin;
|
|
37
|
+}
|
|
38
|
+//------------------------------------------------------------------------------
|
|
39
|
+/** Set pin value
|
|
40
|
+ * @param[in] pin Arduino pin number
|
|
41
|
+ * @param[in] level value to write
|
|
42
|
+ */
|
|
43
|
+static inline __attribute__((always_inline))
|
|
44
|
+void fastDigitalWrite(uint8_t pin, bool value){
|
|
45
|
+ if(value) {
|
|
46
|
+ g_APinDescription[pin].pPort->PIO_SODR = g_APinDescription[pin].ulPin;
|
|
47
|
+ } else {
|
|
48
|
+ g_APinDescription[pin].pPort->PIO_CODR = g_APinDescription[pin].ulPin;
|
|
49
|
+ }
|
|
50
|
+}
|
|
51
|
+#endif // CORE_TEENSY
|
|
52
|
+//------------------------------------------------------------------------------
|
|
53
|
+inline void fastDigitalToggle(uint8_t pin) {
|
|
54
|
+ fastDigitalWrite(pin, !fastDigitalRead(pin));
|
|
55
|
+ }
|
|
56
|
+//------------------------------------------------------------------------------
|
|
57
|
+inline void fastPinMode(uint8_t pin, bool mode) {pinMode(pin, mode);}
|
|
58
|
+#else // __arm__
|
|
59
|
+#include <avr/io.h>
|
|
60
|
+#include <util/atomic.h>
|
|
61
|
+//------------------------------------------------------------------------------
|
|
62
|
+/**
|
|
63
|
+ * @class pin_map_t
|
|
64
|
+ * @brief struct for mapping digital pins
|
|
65
|
+ */
|
|
66
|
+struct pin_map_t {
|
|
67
|
+ volatile uint8_t* ddr; /**< address of DDR for this pin */
|
|
68
|
+ volatile uint8_t* pin; /**< address of PIN for this pin */
|
|
69
|
+ volatile uint8_t* port; /**< address of PORT for this pin */
|
|
70
|
+ uint8_t bit; /**< bit number for this pin */
|
|
71
|
+};
|
|
72
|
+//------------------------------------------------------------------------------
|
|
73
|
+#if defined(__AVR_ATmega168__)\
|
|
74
|
+||defined(__AVR_ATmega168P__)\
|
|
75
|
+||defined(__AVR_ATmega328P__)
|
|
76
|
+// 168 and 328 Arduinos
|
|
77
|
+const static pin_map_t pinMap[] = {
|
|
78
|
+ {&DDRD, &PIND, &PORTD, 0}, // D0 0
|
|
79
|
+ {&DDRD, &PIND, &PORTD, 1}, // D1 1
|
|
80
|
+ {&DDRD, &PIND, &PORTD, 2}, // D2 2
|
|
81
|
+ {&DDRD, &PIND, &PORTD, 3}, // D3 3
|
|
82
|
+ {&DDRD, &PIND, &PORTD, 4}, // D4 4
|
|
83
|
+ {&DDRD, &PIND, &PORTD, 5}, // D5 5
|
|
84
|
+ {&DDRD, &PIND, &PORTD, 6}, // D6 6
|
|
85
|
+ {&DDRD, &PIND, &PORTD, 7}, // D7 7
|
|
86
|
+ {&DDRB, &PINB, &PORTB, 0}, // B0 8
|
|
87
|
+ {&DDRB, &PINB, &PORTB, 1}, // B1 9
|
|
88
|
+ {&DDRB, &PINB, &PORTB, 2}, // B2 10
|
|
89
|
+ {&DDRB, &PINB, &PORTB, 3}, // B3 11
|
|
90
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 12
|
|
91
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 13
|
|
92
|
+ {&DDRC, &PINC, &PORTC, 0}, // C0 14
|
|
93
|
+ {&DDRC, &PINC, &PORTC, 1}, // C1 15
|
|
94
|
+ {&DDRC, &PINC, &PORTC, 2}, // C2 16
|
|
95
|
+ {&DDRC, &PINC, &PORTC, 3}, // C3 17
|
|
96
|
+ {&DDRC, &PINC, &PORTC, 4}, // C4 18
|
|
97
|
+ {&DDRC, &PINC, &PORTC, 5} // C5 19
|
|
98
|
+};
|
|
99
|
+//------------------------------------------------------------------------------
|
|
100
|
+#elif defined(__AVR_ATmega1280__)\
|
|
101
|
+|| defined(__AVR_ATmega2560__)
|
|
102
|
+// Mega
|
|
103
|
+static const pin_map_t pinMap[] = {
|
|
104
|
+ {&DDRE, &PINE, &PORTE, 0}, // E0 0
|
|
105
|
+ {&DDRE, &PINE, &PORTE, 1}, // E1 1
|
|
106
|
+ {&DDRE, &PINE, &PORTE, 4}, // E4 2
|
|
107
|
+ {&DDRE, &PINE, &PORTE, 5}, // E5 3
|
|
108
|
+ {&DDRG, &PING, &PORTG, 5}, // G5 4
|
|
109
|
+ {&DDRE, &PINE, &PORTE, 3}, // E3 5
|
|
110
|
+ {&DDRH, &PINH, &PORTH, 3}, // H3 6
|
|
111
|
+ {&DDRH, &PINH, &PORTH, 4}, // H4 7
|
|
112
|
+ {&DDRH, &PINH, &PORTH, 5}, // H5 8
|
|
113
|
+ {&DDRH, &PINH, &PORTH, 6}, // H6 9
|
|
114
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 10
|
|
115
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 11
|
|
116
|
+ {&DDRB, &PINB, &PORTB, 6}, // B6 12
|
|
117
|
+ {&DDRB, &PINB, &PORTB, 7}, // B7 13
|
|
118
|
+ {&DDRJ, &PINJ, &PORTJ, 1}, // J1 14
|
|
119
|
+ {&DDRJ, &PINJ, &PORTJ, 0}, // J0 15
|
|
120
|
+ {&DDRH, &PINH, &PORTH, 1}, // H1 16
|
|
121
|
+ {&DDRH, &PINH, &PORTH, 0}, // H0 17
|
|
122
|
+ {&DDRD, &PIND, &PORTD, 3}, // D3 18
|
|
123
|
+ {&DDRD, &PIND, &PORTD, 2}, // D2 19
|
|
124
|
+ {&DDRD, &PIND, &PORTD, 1}, // D1 20
|
|
125
|
+ {&DDRD, &PIND, &PORTD, 0}, // D0 21
|
|
126
|
+ {&DDRA, &PINA, &PORTA, 0}, // A0 22
|
|
127
|
+ {&DDRA, &PINA, &PORTA, 1}, // A1 23
|
|
128
|
+ {&DDRA, &PINA, &PORTA, 2}, // A2 24
|
|
129
|
+ {&DDRA, &PINA, &PORTA, 3}, // A3 25
|
|
130
|
+ {&DDRA, &PINA, &PORTA, 4}, // A4 26
|
|
131
|
+ {&DDRA, &PINA, &PORTA, 5}, // A5 27
|
|
132
|
+ {&DDRA, &PINA, &PORTA, 6}, // A6 28
|
|
133
|
+ {&DDRA, &PINA, &PORTA, 7}, // A7 29
|
|
134
|
+ {&DDRC, &PINC, &PORTC, 7}, // C7 30
|
|
135
|
+ {&DDRC, &PINC, &PORTC, 6}, // C6 31
|
|
136
|
+ {&DDRC, &PINC, &PORTC, 5}, // C5 32
|
|
137
|
+ {&DDRC, &PINC, &PORTC, 4}, // C4 33
|
|
138
|
+ {&DDRC, &PINC, &PORTC, 3}, // C3 34
|
|
139
|
+ {&DDRC, &PINC, &PORTC, 2}, // C2 35
|
|
140
|
+ {&DDRC, &PINC, &PORTC, 1}, // C1 36
|
|
141
|
+ {&DDRC, &PINC, &PORTC, 0}, // C0 37
|
|
142
|
+ {&DDRD, &PIND, &PORTD, 7}, // D7 38
|
|
143
|
+ {&DDRG, &PING, &PORTG, 2}, // G2 39
|
|
144
|
+ {&DDRG, &PING, &PORTG, 1}, // G1 40
|
|
145
|
+ {&DDRG, &PING, &PORTG, 0}, // G0 41
|
|
146
|
+ {&DDRL, &PINL, &PORTL, 7}, // L7 42
|
|
147
|
+ {&DDRL, &PINL, &PORTL, 6}, // L6 43
|
|
148
|
+ {&DDRL, &PINL, &PORTL, 5}, // L5 44
|
|
149
|
+ {&DDRL, &PINL, &PORTL, 4}, // L4 45
|
|
150
|
+ {&DDRL, &PINL, &PORTL, 3}, // L3 46
|
|
151
|
+ {&DDRL, &PINL, &PORTL, 2}, // L2 47
|
|
152
|
+ {&DDRL, &PINL, &PORTL, 1}, // L1 48
|
|
153
|
+ {&DDRL, &PINL, &PORTL, 0}, // L0 49
|
|
154
|
+ {&DDRB, &PINB, &PORTB, 3}, // B3 50
|
|
155
|
+ {&DDRB, &PINB, &PORTB, 2}, // B2 51
|
|
156
|
+ {&DDRB, &PINB, &PORTB, 1}, // B1 52
|
|
157
|
+ {&DDRB, &PINB, &PORTB, 0}, // B0 53
|
|
158
|
+ {&DDRF, &PINF, &PORTF, 0}, // F0 54
|
|
159
|
+ {&DDRF, &PINF, &PORTF, 1}, // F1 55
|
|
160
|
+ {&DDRF, &PINF, &PORTF, 2}, // F2 56
|
|
161
|
+ {&DDRF, &PINF, &PORTF, 3}, // F3 57
|
|
162
|
+ {&DDRF, &PINF, &PORTF, 4}, // F4 58
|
|
163
|
+ {&DDRF, &PINF, &PORTF, 5}, // F5 59
|
|
164
|
+ {&DDRF, &PINF, &PORTF, 6}, // F6 60
|
|
165
|
+ {&DDRF, &PINF, &PORTF, 7}, // F7 61
|
|
166
|
+ {&DDRK, &PINK, &PORTK, 0}, // K0 62
|
|
167
|
+ {&DDRK, &PINK, &PORTK, 1}, // K1 63
|
|
168
|
+ {&DDRK, &PINK, &PORTK, 2}, // K2 64
|
|
169
|
+ {&DDRK, &PINK, &PORTK, 3}, // K3 65
|
|
170
|
+ {&DDRK, &PINK, &PORTK, 4}, // K4 66
|
|
171
|
+ {&DDRK, &PINK, &PORTK, 5}, // K5 67
|
|
172
|
+ {&DDRK, &PINK, &PORTK, 6}, // K6 68
|
|
173
|
+ {&DDRK, &PINK, &PORTK, 7}, // K7 69
|
|
174
|
+
|
|
175
|
+//pins_MIGHTYBOARD_REVE.h
|
|
176
|
+ {&DDRG, &PING, &PORTG, 4}, // G4 70
|
|
177
|
+ {&DDRG, &PING, &PORTG, 3}, // G3 71
|
|
178
|
+ {&DDRJ, &PINJ, &PORTJ, 2}, // J2 72
|
|
179
|
+ {&DDRJ, &PINJ, &PORTJ, 3}, // J3 73
|
|
180
|
+ {&DDRJ, &PINJ, &PORTJ, 7}, // J7 74
|
|
181
|
+ {&DDRJ, &PINJ, &PORTJ, 4}, // J4 75
|
|
182
|
+ {&DDRJ, &PINJ, &PORTJ, 5}, // J5 76
|
|
183
|
+ {&DDRJ, &PINJ, &PORTJ, 6}, // J6 77
|
|
184
|
+ {&DDRE, &PINE, &PORTE, 2}, // E2 78
|
|
185
|
+ {&DDRE, &PINE, &PORTE, 6} // E6 79
|
|
186
|
+
|
|
187
|
+};
|
|
188
|
+//------------------------------------------------------------------------------
|
|
189
|
+#elif defined(__AVR_ATmega1284P__)\
|
|
190
|
+|| defined(__AVR_ATmega1284__)\
|
|
191
|
+|| defined(__AVR_ATmega644P__)\
|
|
192
|
+|| defined(__AVR_ATmega644__)\
|
|
193
|
+|| defined(__AVR_ATmega64__)\
|
|
194
|
+|| defined(__AVR_ATmega32__)\
|
|
195
|
+|| defined(__AVR_ATmega324__)\
|
|
196
|
+|| defined(__AVR_ATmega16__)
|
|
197
|
+
|
|
198
|
+#ifdef defined(VARIANT_MIGHTY)
|
|
199
|
+// Mighty Layout
|
|
200
|
+static const pin_map_t pinMap[] = {
|
|
201
|
+ {&DDRB, &PINB, &PORTB, 0}, // B0 0
|
|
202
|
+ {&DDRB, &PINB, &PORTB, 1}, // B1 1
|
|
203
|
+ {&DDRB, &PINB, &PORTB, 2}, // B2 2
|
|
204
|
+ {&DDRB, &PINB, &PORTB, 3}, // B3 3
|
|
205
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 4
|
|
206
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 5
|
|
207
|
+ {&DDRB, &PINB, &PORTB, 6}, // B6 6
|
|
208
|
+ {&DDRB, &PINB, &PORTB, 7}, // B7 7
|
|
209
|
+ {&DDRD, &PIND, &PORTD, 0}, // D0 8
|
|
210
|
+ {&DDRD, &PIND, &PORTD, 1}, // D1 9
|
|
211
|
+ {&DDRD, &PIND, &PORTD, 2}, // D2 10
|
|
212
|
+ {&DDRD, &PIND, &PORTD, 3}, // D3 11
|
|
213
|
+ {&DDRD, &PIND, &PORTD, 4}, // D4 12
|
|
214
|
+ {&DDRD, &PIND, &PORTD, 5}, // D5 13
|
|
215
|
+ {&DDRD, &PIND, &PORTD, 6}, // D6 14
|
|
216
|
+ {&DDRD, &PIND, &PORTD, 7}, // D7 15
|
|
217
|
+ {&DDRC, &PINC, &PORTC, 0}, // C0 16
|
|
218
|
+ {&DDRC, &PINC, &PORTC, 1}, // C1 17
|
|
219
|
+ {&DDRC, &PINC, &PORTC, 2}, // C2 18
|
|
220
|
+ {&DDRC, &PINC, &PORTC, 3}, // C3 19
|
|
221
|
+ {&DDRC, &PINC, &PORTC, 4}, // C4 20
|
|
222
|
+ {&DDRC, &PINC, &PORTC, 5}, // C5 21
|
|
223
|
+ {&DDRC, &PINC, &PORTC, 6}, // C6 22
|
|
224
|
+ {&DDRC, &PINC, &PORTC, 7}, // C7 23
|
|
225
|
+ {&DDRA, &PINA, &PORTA, 0}, // A0 24
|
|
226
|
+ {&DDRA, &PINA, &PORTA, 1}, // A1 25
|
|
227
|
+ {&DDRA, &PINA, &PORTA, 2}, // A2 26
|
|
228
|
+ {&DDRA, &PINA, &PORTA, 3}, // A3 27
|
|
229
|
+ {&DDRA, &PINA, &PORTA, 4}, // A4 28
|
|
230
|
+ {&DDRA, &PINA, &PORTA, 5}, // A5 29
|
|
231
|
+ {&DDRA, &PINA, &PORTA, 6}, // A6 30
|
|
232
|
+ {&DDRA, &PINA, &PORTA, 7} // A7 31
|
|
233
|
+};
|
|
234
|
+#elif defined(VARIANT_BOBUINO)
|
|
235
|
+// Bobuino Layout
|
|
236
|
+static const pin_map_t pinMap[] = {
|
|
237
|
+ {&DDRD, &PIND, &PORTD, 0}, // D0 0
|
|
238
|
+ {&DDRD, &PIND, &PORTD, 1}, // D1 1
|
|
239
|
+ {&DDRD, &PIND, &PORTD, 2}, // D2 2
|
|
240
|
+ {&DDRD, &PIND, &PORTD, 3}, // D3 3
|
|
241
|
+ {&DDRB, &PINB, &PORTB, 0}, // B0 4
|
|
242
|
+ {&DDRB, &PINB, &PORTB, 1}, // B1 5
|
|
243
|
+ {&DDRB, &PINB, &PORTB, 2}, // B2 6
|
|
244
|
+ {&DDRB, &PINB, &PORTB, 3}, // B3 7
|
|
245
|
+ {&DDRD, &PIND, &PORTD, 5}, // D5 8
|
|
246
|
+ {&DDRD, &PIND, &PORTD, 6}, // D6 9
|
|
247
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 10
|
|
248
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 11
|
|
249
|
+ {&DDRB, &PINB, &PORTB, 6}, // B6 12
|
|
250
|
+ {&DDRB, &PINB, &PORTB, 7}, // B7 13
|
|
251
|
+ {&DDRA, &PINA, &PORTA, 7}, // A7 14
|
|
252
|
+ {&DDRA, &PINA, &PORTA, 6}, // A6 15
|
|
253
|
+ {&DDRA, &PINA, &PORTA, 5}, // A5 16
|
|
254
|
+ {&DDRA, &PINA, &PORTA, 4}, // A4 17
|
|
255
|
+ {&DDRA, &PINA, &PORTA, 3}, // A3 18
|
|
256
|
+ {&DDRA, &PINA, &PORTA, 2}, // A2 19
|
|
257
|
+ {&DDRA, &PINA, &PORTA, 1}, // A1 20
|
|
258
|
+ {&DDRA, &PINA, &PORTA, 0}, // A0 21
|
|
259
|
+ {&DDRC, &PINC, &PORTC, 0}, // C0 22
|
|
260
|
+ {&DDRC, &PINC, &PORTC, 1}, // C1 23
|
|
261
|
+ {&DDRC, &PINC, &PORTC, 2}, // C2 24
|
|
262
|
+ {&DDRC, &PINC, &PORTC, 3}, // C3 25
|
|
263
|
+ {&DDRC, &PINC, &PORTC, 4}, // C4 26
|
|
264
|
+ {&DDRC, &PINC, &PORTC, 5}, // C5 27
|
|
265
|
+ {&DDRC, &PINC, &PORTC, 6}, // C6 28
|
|
266
|
+ {&DDRC, &PINC, &PORTC, 7}, // C7 29
|
|
267
|
+ {&DDRD, &PIND, &PORTD, 4}, // D4 30
|
|
268
|
+ {&DDRD, &PIND, &PORTD, 7} // D7 31
|
|
269
|
+};
|
|
270
|
+#elif defined(VARIANT_STANDARD)
|
|
271
|
+// Standard Layout
|
|
272
|
+static const pin_map_t pinMap[] = {
|
|
273
|
+ {&DDRB, &PINB, &PORTB, 0}, // B0 0
|
|
274
|
+ {&DDRB, &PINB, &PORTB, 1}, // B1 1
|
|
275
|
+ {&DDRB, &PINB, &PORTB, 2}, // B2 2
|
|
276
|
+ {&DDRB, &PINB, &PORTB, 3}, // B3 3
|
|
277
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 4
|
|
278
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 5
|
|
279
|
+ {&DDRB, &PINB, &PORTB, 6}, // B6 6
|
|
280
|
+ {&DDRB, &PINB, &PORTB, 7}, // B7 7
|
|
281
|
+ {&DDRD, &PIND, &PORTD, 0}, // D0 8
|
|
282
|
+ {&DDRD, &PIND, &PORTD, 1}, // D1 9
|
|
283
|
+ {&DDRD, &PIND, &PORTD, 2}, // D2 10
|
|
284
|
+ {&DDRD, &PIND, &PORTD, 3}, // D3 11
|
|
285
|
+ {&DDRD, &PIND, &PORTD, 4}, // D4 12
|
|
286
|
+ {&DDRD, &PIND, &PORTD, 5}, // D5 13
|
|
287
|
+ {&DDRD, &PIND, &PORTD, 6}, // D6 14
|
|
288
|
+ {&DDRD, &PIND, &PORTD, 7}, // D7 15
|
|
289
|
+ {&DDRC, &PINC, &PORTC, 0}, // C0 16
|
|
290
|
+ {&DDRC, &PINC, &PORTC, 1}, // C1 17
|
|
291
|
+ {&DDRC, &PINC, &PORTC, 2}, // C2 18
|
|
292
|
+ {&DDRC, &PINC, &PORTC, 3}, // C3 19
|
|
293
|
+ {&DDRC, &PINC, &PORTC, 4}, // C4 20
|
|
294
|
+ {&DDRC, &PINC, &PORTC, 5}, // C5 21
|
|
295
|
+ {&DDRC, &PINC, &PORTC, 6}, // C6 22
|
|
296
|
+ {&DDRC, &PINC, &PORTC, 7}, // C7 23
|
|
297
|
+ {&DDRA, &PINA, &PORTA, 7}, // A7 24
|
|
298
|
+ {&DDRA, &PINA, &PORTA, 6}, // A6 25
|
|
299
|
+ {&DDRA, &PINA, &PORTA, 5}, // A5 26
|
|
300
|
+ {&DDRA, &PINA, &PORTA, 4}, // A4 27
|
|
301
|
+ {&DDRA, &PINA, &PORTA, 3}, // A3 28
|
|
302
|
+ {&DDRA, &PINA, &PORTA, 2}, // A2 29
|
|
303
|
+ {&DDRA, &PINA, &PORTA, 1}, // A1 30
|
|
304
|
+ {&DDRA, &PINA, &PORTA, 0} // A0 31
|
|
305
|
+};
|
|
306
|
+#else // VARIANT_MIGHTY
|
|
307
|
+#error Undefined variant 1284, 644, 324, 64, 32
|
|
308
|
+#endif // VARIANT_MIGHTY
|
|
309
|
+//------------------------------------------------------------------------------
|
|
310
|
+#elif defined(__AVR_ATmega32U4__)
|
|
311
|
+#ifdef CORE_TEENSY
|
|
312
|
+// Teensy 2.0
|
|
313
|
+static const pin_map_t pinMap[] = {
|
|
314
|
+ {&DDRB, &PINB, &PORTB, 0}, // B0 0
|
|
315
|
+ {&DDRB, &PINB, &PORTB, 1}, // B1 1
|
|
316
|
+ {&DDRB, &PINB, &PORTB, 2}, // B2 2
|
|
317
|
+ {&DDRB, &PINB, &PORTB, 3}, // B3 3
|
|
318
|
+ {&DDRB, &PINB, &PORTB, 7}, // B7 4
|
|
319
|
+ {&DDRD, &PIND, &PORTD, 0}, // D0 5
|
|
320
|
+ {&DDRD, &PIND, &PORTD, 1}, // D1 6
|
|
321
|
+ {&DDRD, &PIND, &PORTD, 2}, // D2 7
|
|
322
|
+ {&DDRD, &PIND, &PORTD, 3}, // D3 8
|
|
323
|
+ {&DDRC, &PINC, &PORTC, 6}, // C6 9
|
|
324
|
+ {&DDRC, &PINC, &PORTC, 7}, // C7 10
|
|
325
|
+ {&DDRD, &PIND, &PORTD, 6}, // D6 11
|
|
326
|
+ {&DDRD, &PIND, &PORTD, 7}, // D7 12
|
|
327
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 13
|
|
328
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 14
|
|
329
|
+ {&DDRB, &PINB, &PORTB, 6}, // B6 15
|
|
330
|
+ {&DDRF, &PINF, &PORTF, 7}, // F7 16
|
|
331
|
+ {&DDRF, &PINF, &PORTF, 6}, // F6 17
|
|
332
|
+ {&DDRF, &PINF, &PORTF, 5}, // F5 18
|
|
333
|
+ {&DDRF, &PINF, &PORTF, 4}, // F4 19
|
|
334
|
+ {&DDRF, &PINF, &PORTF, 1}, // F1 20
|
|
335
|
+ {&DDRF, &PINF, &PORTF, 0}, // F0 21
|
|
336
|
+ {&DDRD, &PIND, &PORTD, 4}, // D4 22
|
|
337
|
+ {&DDRD, &PIND, &PORTD, 5}, // D5 23
|
|
338
|
+ {&DDRE, &PINE, &PORTE, 6} // E6 24
|
|
339
|
+};
|
|
340
|
+//------------------------------------------------------------------------------
|
|
341
|
+#else // CORE_TEENSY
|
|
342
|
+// Leonardo
|
|
343
|
+static const pin_map_t pinMap[] = {
|
|
344
|
+ {&DDRD, &PIND, &PORTD, 2}, // D2 0
|
|
345
|
+ {&DDRD, &PIND, &PORTD, 3}, // D3 1
|
|
346
|
+ {&DDRD, &PIND, &PORTD, 1}, // D1 2
|
|
347
|
+ {&DDRD, &PIND, &PORTD, 0}, // D0 3
|
|
348
|
+ {&DDRD, &PIND, &PORTD, 4}, // D4 4
|
|
349
|
+ {&DDRC, &PINC, &PORTC, 6}, // C6 5
|
|
350
|
+ {&DDRD, &PIND, &PORTD, 7}, // D7 6
|
|
351
|
+ {&DDRE, &PINE, &PORTE, 6}, // E6 7
|
|
352
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 8
|
|
353
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 9
|
|
354
|
+ {&DDRB, &PINB, &PORTB, 6}, // B6 10
|
|
355
|
+ {&DDRB, &PINB, &PORTB, 7}, // B7 11
|
|
356
|
+ {&DDRD, &PIND, &PORTD, 6}, // D6 12
|
|
357
|
+ {&DDRC, &PINC, &PORTC, 7}, // C7 13
|
|
358
|
+ {&DDRB, &PINB, &PORTB, 3}, // B3 14
|
|
359
|
+ {&DDRB, &PINB, &PORTB, 1}, // B1 15
|
|
360
|
+ {&DDRB, &PINB, &PORTB, 2}, // B2 16
|
|
361
|
+ {&DDRB, &PINB, &PORTB, 0}, // B0 17
|
|
362
|
+ {&DDRF, &PINF, &PORTF, 7}, // F7 18
|
|
363
|
+ {&DDRF, &PINF, &PORTF, 6}, // F6 19
|
|
364
|
+ {&DDRF, &PINF, &PORTF, 5}, // F5 20
|
|
365
|
+ {&DDRF, &PINF, &PORTF, 4}, // F4 21
|
|
366
|
+ {&DDRF, &PINF, &PORTF, 1}, // F1 22
|
|
367
|
+ {&DDRF, &PINF, &PORTF, 0}, // F0 23
|
|
368
|
+ {&DDRD, &PIND, &PORTD, 4}, // D4 24
|
|
369
|
+ {&DDRD, &PIND, &PORTD, 7}, // D7 25
|
|
370
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 26
|
|
371
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 27
|
|
372
|
+ {&DDRB, &PINB, &PORTB, 6}, // B6 28
|
|
373
|
+ {&DDRD, &PIND, &PORTD, 6} // D6 29
|
|
374
|
+};
|
|
375
|
+#endif // CORE_TEENSY
|
|
376
|
+//------------------------------------------------------------------------------
|
|
377
|
+#elif defined(__AVR_AT90USB646__)\
|
|
378
|
+|| defined(__AVR_AT90USB1286__)
|
|
379
|
+// Teensy++ 1.0 & 2.0
|
|
380
|
+static const pin_map_t pinMap[] = {
|
|
381
|
+ {&DDRD, &PIND, &PORTD, 0}, // D0 0
|
|
382
|
+ {&DDRD, &PIND, &PORTD, 1}, // D1 1
|
|
383
|
+ {&DDRD, &PIND, &PORTD, 2}, // D2 2
|
|
384
|
+ {&DDRD, &PIND, &PORTD, 3}, // D3 3
|
|
385
|
+ {&DDRD, &PIND, &PORTD, 4}, // D4 4
|
|
386
|
+ {&DDRD, &PIND, &PORTD, 5}, // D5 5
|
|
387
|
+ {&DDRD, &PIND, &PORTD, 6}, // D6 6
|
|
388
|
+ {&DDRD, &PIND, &PORTD, 7}, // D7 7
|
|
389
|
+ {&DDRE, &PINE, &PORTE, 0}, // E0 8
|
|
390
|
+ {&DDRE, &PINE, &PORTE, 1}, // E1 9
|
|
391
|
+ {&DDRC, &PINC, &PORTC, 0}, // C0 10
|
|
392
|
+ {&DDRC, &PINC, &PORTC, 1}, // C1 11
|
|
393
|
+ {&DDRC, &PINC, &PORTC, 2}, // C2 12
|
|
394
|
+ {&DDRC, &PINC, &PORTC, 3}, // C3 13
|
|
395
|
+ {&DDRC, &PINC, &PORTC, 4}, // C4 14
|
|
396
|
+ {&DDRC, &PINC, &PORTC, 5}, // C5 15
|
|
397
|
+ {&DDRC, &PINC, &PORTC, 6}, // C6 16
|
|
398
|
+ {&DDRC, &PINC, &PORTC, 7}, // C7 17
|
|
399
|
+ {&DDRE, &PINE, &PORTE, 6}, // E6 18
|
|
400
|
+ {&DDRE, &PINE, &PORTE, 7}, // E7 19
|
|
401
|
+ {&DDRB, &PINB, &PORTB, 0}, // B0 20
|
|
402
|
+ {&DDRB, &PINB, &PORTB, 1}, // B1 21
|
|
403
|
+ {&DDRB, &PINB, &PORTB, 2}, // B2 22
|
|
404
|
+ {&DDRB, &PINB, &PORTB, 3}, // B3 23
|
|
405
|
+ {&DDRB, &PINB, &PORTB, 4}, // B4 24
|
|
406
|
+ {&DDRB, &PINB, &PORTB, 5}, // B5 25
|
|
407
|
+ {&DDRB, &PINB, &PORTB, 6}, // B6 26
|
|
408
|
+ {&DDRB, &PINB, &PORTB, 7}, // B7 27
|
|
409
|
+ {&DDRA, &PINA, &PORTA, 0}, // A0 28
|
|
410
|
+ {&DDRA, &PINA, &PORTA, 1}, // A1 29
|
|
411
|
+ {&DDRA, &PINA, &PORTA, 2}, // A2 30
|
|
412
|
+ {&DDRA, &PINA, &PORTA, 3}, // A3 31
|
|
413
|
+ {&DDRA, &PINA, &PORTA, 4}, // A4 32
|
|
414
|
+ {&DDRA, &PINA, &PORTA, 5}, // A5 33
|
|
415
|
+ {&DDRA, &PINA, &PORTA, 6}, // A6 34
|
|
416
|
+ {&DDRA, &PINA, &PORTA, 7}, // A7 35
|
|
417
|
+ {&DDRE, &PINE, &PORTE, 4}, // E4 36
|
|
418
|
+ {&DDRE, &PINE, &PORTE, 5}, // E5 37
|
|
419
|
+ {&DDRF, &PINF, &PORTF, 0}, // F0 38
|
|
420
|
+ {&DDRF, &PINF, &PORTF, 1}, // F1 39
|
|
421
|
+ {&DDRF, &PINF, &PORTF, 2}, // F2 40
|
|
422
|
+ {&DDRF, &PINF, &PORTF, 3}, // F3 41
|
|
423
|
+ {&DDRF, &PINF, &PORTF, 4}, // F4 42
|
|
424
|
+ {&DDRF, &PINF, &PORTF, 5}, // F5 43
|
|
425
|
+ {&DDRF, &PINF, &PORTF, 6}, // F6 44
|
|
426
|
+ {&DDRF, &PINF, &PORTF, 7} // F7 45
|
|
427
|
+};
|
|
428
|
+//------------------------------------------------------------------------------
|
|
429
|
+#else // CPU type
|
|
430
|
+#error unknown CPU type
|
|
431
|
+#endif // CPU type
|
|
432
|
+//------------------------------------------------------------------------------
|
|
433
|
+/** count of pins */
|
|
434
|
+static const uint8_t digitalPinCount = sizeof(pinMap)/sizeof(pin_map_t);
|
|
435
|
+//==============================================================================
|
|
436
|
+/** generate bad pin number error */
|
|
437
|
+void badPinNumber(void)
|
|
438
|
+ __attribute__((error("Pin number is too large or not a constant")));
|
|
439
|
+//------------------------------------------------------------------------------
|
|
440
|
+/** Check for valid pin number
|
|
441
|
+ * @param[in] pin Number of pin to be checked.
|
|
442
|
+ */
|
|
443
|
+static inline __attribute__((always_inline))
|
|
444
|
+void badPinCheck(uint8_t pin) {
|
|
445
|
+ if (!__builtin_constant_p(pin) || pin >= digitalPinCount) {
|
|
446
|
+ badPinNumber();
|
|
447
|
+ }
|
|
448
|
+}
|
|
449
|
+//------------------------------------------------------------------------------
|
|
450
|
+/** fast write helper
|
|
451
|
+ * @param[in] address I/O register address
|
|
452
|
+ * @param[in] bit bit number to write
|
|
453
|
+ * @param[in] level value for bit
|
|
454
|
+ */
|
|
455
|
+static inline __attribute__((always_inline))
|
|
456
|
+void fastBitWriteSafe(volatile uint8_t* address, uint8_t bit, bool level) {
|
|
457
|
+ uint8_t oldSREG;
|
|
458
|
+ if (address > (uint8_t*)0X5F) {
|
|
459
|
+ oldSREG = SREG;
|
|
460
|
+ cli();
|
|
461
|
+ }
|
|
462
|
+ if (level) {
|
|
463
|
+ *address |= 1 << bit;
|
|
464
|
+ } else {
|
|
465
|
+ *address &= ~(1 << bit);
|
|
466
|
+ }
|
|
467
|
+ if (address > (uint8_t*)0X5F) {
|
|
468
|
+ SREG = oldSREG;
|
|
469
|
+ }
|
|
470
|
+}
|
|
471
|
+//------------------------------------------------------------------------------
|
|
472
|
+/** read pin value
|
|
473
|
+ * @param[in] pin Arduino pin number
|
|
474
|
+ * @return value read
|
|
475
|
+ */
|
|
476
|
+static inline __attribute__((always_inline))
|
|
477
|
+bool fastDigitalRead(uint8_t pin) {
|
|
478
|
+ badPinCheck(pin);
|
|
479
|
+ return (*pinMap[pin].pin >> pinMap[pin].bit) & 1;
|
|
480
|
+}
|
|
481
|
+//------------------------------------------------------------------------------
|
|
482
|
+/** toggle a pin
|
|
483
|
+ * @param[in] pin Arduino pin number
|
|
484
|
+ *
|
|
485
|
+ * If the pin is in output mode toggle the pin level.
|
|
486
|
+ * If the pin is in input mode toggle the state of the 20K pullup.
|
|
487
|
+ */
|
|
488
|
+static inline __attribute__((always_inline))
|
|
489
|
+void fastDigitalToggle(uint8_t pin) {
|
|
490
|
+ badPinCheck(pin);
|
|
491
|
+ if (pinMap[pin].pin > (uint8_t*)0X5F) {
|
|
492
|
+ // must write bit to high address port
|
|
493
|
+ *pinMap[pin].pin = 1 << pinMap[pin].bit;
|
|
494
|
+ } else {
|
|
495
|
+ // will compile to sbi and PIN register will not be read.
|
|
496
|
+ *pinMap[pin].pin |= 1 << pinMap[pin].bit;
|
|
497
|
+ }
|
|
498
|
+}
|
|
499
|
+//------------------------------------------------------------------------------
|
|
500
|
+/** Set pin value
|
|
501
|
+ * @param[in] pin Arduino pin number
|
|
502
|
+ * @param[in] level value to write
|
|
503
|
+ */
|
|
504
|
+static inline __attribute__((always_inline))
|
|
505
|
+void fastDigitalWrite(uint8_t pin, bool level) {
|
|
506
|
+ badPinCheck(pin);
|
|
507
|
+ fastBitWriteSafe(pinMap[pin].port, pinMap[pin].bit, level);
|
|
508
|
+}
|
|
509
|
+//------------------------------------------------------------------------------
|
|
510
|
+/** set pin mode
|
|
511
|
+ * @param[in] pin Arduino pin number
|
|
512
|
+ * @param[in] mode if true set output mode else input mode
|
|
513
|
+ *
|
|
514
|
+ * fastPinMode does not enable or disable the 20K pullup for input mode.
|
|
515
|
+ */
|
|
516
|
+static inline __attribute__((always_inline))
|
|
517
|
+void fastPinMode(uint8_t pin, bool mode) {
|
|
518
|
+ badPinCheck(pin);
|
|
519
|
+ fastBitWriteSafe(pinMap[pin].ddr, pinMap[pin].bit, mode);
|
|
520
|
+}
|
|
521
|
+
|
|
522
|
+#endif // __arm__
|
|
523
|
+//------------------------------------------------------------------------------
|
|
524
|
+/** set pin configuration
|
|
525
|
+ * @param[in] pin Arduino pin number
|
|
526
|
+ * @param[in] mode If true set output mode else input mode
|
|
527
|
+ * @param[in] level If mode is output, set level high/low.
|
|
528
|
+ * If mode is input, enable or disable the pin's 20K pullup.
|
|
529
|
+ */
|
|
530
|
+static inline __attribute__((always_inline))
|
|
531
|
+void fastPinConfig(uint8_t pin, bool mode, bool level) {
|
|
532
|
+ fastPinMode(pin, mode);
|
|
533
|
+ fastDigitalWrite(pin, level);
|
|
534
|
+}
|
|
535
|
+//==============================================================================
|
|
536
|
+/**
|
|
537
|
+ * @class DigitalPin
|
|
538
|
+ * @brief Fast digital port I/O
|
|
539
|
+ */
|
|
540
|
+template<uint8_t PinNumber>
|
|
541
|
+class DigitalPin {
|
|
542
|
+ public:
|
|
543
|
+ //----------------------------------------------------------------------------
|
|
544
|
+ /** Constructor */
|
|
545
|
+ DigitalPin() {}
|
|
546
|
+ //----------------------------------------------------------------------------
|
|
547
|
+ /** Constructor
|
|
548
|
+ * @param[in] pinMode if true set output mode else input mode.
|
|
549
|
+ */
|
|
550
|
+ explicit DigitalPin(bool pinMode) {
|
|
551
|
+ mode(pinMode);
|
|
552
|
+ }
|
|
553
|
+ //----------------------------------------------------------------------------
|
|
554
|
+ /** Constructor
|
|
555
|
+ * @param[in] mode If true set output mode else input mode
|
|
556
|
+ * @param[in] level If mode is output, set level high/low.
|
|
557
|
+ * If mode is input, enable or disable the pin's 20K pullup.
|
|
558
|
+ */
|
|
559
|
+ DigitalPin(bool mode, bool level) {
|
|
560
|
+ config(mode, level);
|
|
561
|
+ }
|
|
562
|
+ //----------------------------------------------------------------------------
|
|
563
|
+ /** Asignment operator
|
|
564
|
+ * @param[in] value If true set the pin's level high else set the
|
|
565
|
+ * pin's level low.
|
|
566
|
+ *
|
|
567
|
+ * @return This DigitalPin instance.
|
|
568
|
+ */
|
|
569
|
+ inline DigitalPin & operator = (bool value) __attribute__((always_inline)) {
|
|
570
|
+ write(value);
|
|
571
|
+ return *this;
|
|
572
|
+ }
|
|
573
|
+ //----------------------------------------------------------------------------
|
|
574
|
+ /** Parenthesis operator
|
|
575
|
+ * @return Pin's level
|
|
576
|
+ */
|
|
577
|
+ inline operator bool () const __attribute__((always_inline)) {
|
|
578
|
+ return read();
|
|
579
|
+ }
|
|
580
|
+ //----------------------------------------------------------------------------
|
|
581
|
+ /** set pin configuration
|
|
582
|
+ * @param[in] mode If true set output mode else input mode
|
|
583
|
+ * @param[in] level If mode is output, set level high/low.
|
|
584
|
+ * If mode is input, enable or disable the pin's 20K pullup.
|
|
585
|
+ */
|
|
586
|
+ inline __attribute__((always_inline))
|
|
587
|
+ void config(bool mode, bool level) {
|
|
588
|
+ fastPinConfig(PinNumber, mode, level);
|
|
589
|
+ }
|
|
590
|
+ //----------------------------------------------------------------------------
|
|
591
|
+ /**
|
|
592
|
+ * Set pin level high if output mode or enable 20K pullup if input mode.
|
|
593
|
+ */
|
|
594
|
+ inline __attribute__((always_inline))
|
|
595
|
+ void high() {write(true);}
|
|
596
|
+ //----------------------------------------------------------------------------
|
|
597
|
+ /**
|
|
598
|
+ * Set pin level low if output mode or disable 20K pullup if input mode.
|
|
599
|
+ */
|
|
600
|
+ inline __attribute__((always_inline))
|
|
601
|
+ void low() {write(false);}
|
|
602
|
+ //----------------------------------------------------------------------------
|
|
603
|
+ /**
|
|
604
|
+ * Set pin mode
|
|
605
|
+ * @param[in] pinMode if true set output mode else input mode.
|
|
606
|
+ *
|
|
607
|
+ * mode() does not enable or disable the 20K pullup for input mode.
|
|
608
|
+ */
|
|
609
|
+ inline __attribute__((always_inline))
|
|
610
|
+ void mode(bool pinMode) {
|
|
611
|
+ fastPinMode(PinNumber, pinMode);
|
|
612
|
+ }
|
|
613
|
+ //----------------------------------------------------------------------------
|
|
614
|
+ /** @return Pin's level */
|
|
615
|
+ inline __attribute__((always_inline))
|
|
616
|
+ bool read() const {
|
|
617
|
+ return fastDigitalRead(PinNumber);
|
|
618
|
+ }
|
|
619
|
+ //----------------------------------------------------------------------------
|
|
620
|
+ /** toggle a pin
|
|
621
|
+ *
|
|
622
|
+ * If the pin is in output mode toggle the pin's level.
|
|
623
|
+ * If the pin is in input mode toggle the state of the 20K pullup.
|
|
624
|
+ */
|
|
625
|
+ inline __attribute__((always_inline))
|
|
626
|
+ void toggle() {
|
|
627
|
+ fastDigitalToggle(PinNumber);
|
|
628
|
+ }
|
|
629
|
+ //----------------------------------------------------------------------------
|
|
630
|
+ /** Write the pin's level.
|
|
631
|
+ * @param[in] value If true set the pin's level high else set the
|
|
632
|
+ * pin's level low.
|
|
633
|
+ */
|
|
634
|
+ inline __attribute__((always_inline))
|
|
635
|
+ void write(bool value) {
|
|
636
|
+ fastDigitalWrite(PinNumber, value);
|
|
637
|
+ }
|
|
638
|
+};
|
|
639
|
+
|
|
640
|
+//------------------------------------------------------------------------------
|
|
641
|
+/** Nop for timing. */
|
|
642
|
+#define nop asm volatile ("nop\n\t")
|
|
643
|
+//------------------------------------------------------------------------------
|
|
644
|
+/** Pin Mode for MISO is input.*/
|
|
645
|
+const bool MISO_MODE = false;
|
|
646
|
+/** Pullups disabled for MISO are disabled. */
|
|
647
|
+const bool MISO_LEVEL = false;
|
|
648
|
+/** Pin Mode for MOSI is output.*/
|
|
649
|
+const bool MOSI_MODE = true;
|
|
650
|
+/** Pin Mode for SCK is output. */
|
|
651
|
+const bool SCK_MODE = true;
|
|
652
|
+//------------------------------------------------------------------------------
|
|
653
|
+/**
|
|
654
|
+ * @class SoftSPI
|
|
655
|
+ * @brief Fast software SPI.
|
|
656
|
+ */
|
|
657
|
+template<uint8_t MisoPin, uint8_t MosiPin, uint8_t SckPin, uint8_t Mode = 0>
|
|
658
|
+class SoftSPI {
|
|
659
|
+ public:
|
|
660
|
+ //----------------------------------------------------------------------------
|
|
661
|
+ /** Initialize SoftSPI pins. */
|
|
662
|
+ void begin() {
|
|
663
|
+ fastPinConfig(MisoPin, MISO_MODE, MISO_LEVEL);
|
|
664
|
+ fastPinConfig(MosiPin, MOSI_MODE, !MODE_CPHA(Mode));
|
|
665
|
+ fastPinConfig(SckPin, SCK_MODE, MODE_CPOL(Mode));
|
|
666
|
+ }
|
|
667
|
+ //----------------------------------------------------------------------------
|
|
668
|
+ /** Soft SPI receive byte.
|
|
669
|
+ * @return Data byte received.
|
|
670
|
+ */
|
|
671
|
+ inline __attribute__((always_inline))
|
|
672
|
+ uint8_t receive() {
|
|
673
|
+ uint8_t data = 0;
|
|
674
|
+ receiveBit(7, &data);
|
|
675
|
+ receiveBit(6, &data);
|
|
676
|
+ receiveBit(5, &data);
|
|
677
|
+ receiveBit(4, &data);
|
|
678
|
+ receiveBit(3, &data);
|
|
679
|
+ receiveBit(2, &data);
|
|
680
|
+ receiveBit(1, &data);
|
|
681
|
+ receiveBit(0, &data);
|
|
682
|
+ return data;
|
|
683
|
+ }
|
|
684
|
+ //----------------------------------------------------------------------------
|
|
685
|
+ /** Soft SPI send byte.
|
|
686
|
+ * @param[in] data Data byte to send.
|
|
687
|
+ */
|
|
688
|
+ inline __attribute__((always_inline))
|
|
689
|
+ void send(uint8_t data) {
|
|
690
|
+ sendBit(7, data);
|
|
691
|
+ sendBit(6, data);
|
|
692
|
+ sendBit(5, data);
|
|
693
|
+ sendBit(4, data);
|
|
694
|
+ sendBit(3, data);
|
|
695
|
+ sendBit(2, data);
|
|
696
|
+ sendBit(1, data);
|
|
697
|
+ sendBit(0, data);
|
|
698
|
+ }
|
|
699
|
+ //----------------------------------------------------------------------------
|
|
700
|
+ /** Soft SPI transfer byte.
|
|
701
|
+ * @param[in] txData Data byte to send.
|
|
702
|
+ * @return Data byte received.
|
|
703
|
+ */
|
|
704
|
+ inline __attribute__((always_inline))
|
|
705
|
+ uint8_t transfer(uint8_t txData) {
|
|
706
|
+ uint8_t rxData = 0;
|
|
707
|
+ transferBit(7, &rxData, txData);
|
|
708
|
+ transferBit(6, &rxData, txData);
|
|
709
|
+ transferBit(5, &rxData, txData);
|
|
710
|
+ transferBit(4, &rxData, txData);
|
|
711
|
+ transferBit(3, &rxData, txData);
|
|
712
|
+ transferBit(2, &rxData, txData);
|
|
713
|
+ transferBit(1, &rxData, txData);
|
|
714
|
+ transferBit(0, &rxData, txData);
|
|
715
|
+ return rxData;
|
|
716
|
+ }
|
|
717
|
+
|
|
718
|
+ private:
|
|
719
|
+ //----------------------------------------------------------------------------
|
|
720
|
+ inline __attribute__((always_inline))
|
|
721
|
+ bool MODE_CPHA(uint8_t mode) {return (mode & 1) != 0;}
|
|
722
|
+ inline __attribute__((always_inline))
|
|
723
|
+ bool MODE_CPOL(uint8_t mode) {return (mode & 2) != 0;}
|
|
724
|
+ inline __attribute__((always_inline))
|
|
725
|
+ void receiveBit(uint8_t bit, uint8_t* data) {
|
|
726
|
+ if (MODE_CPHA(Mode)) {
|
|
727
|
+ fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
|
|
728
|
+ }
|
|
729
|
+ nop;
|
|
730
|
+ nop;
|
|
731
|
+ fastDigitalWrite(SckPin,
|
|
732
|
+ MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
|
|
733
|
+ if (fastDigitalRead(MisoPin)) *data |= 1 << bit;
|
|
734
|
+ if (!MODE_CPHA(Mode)) {
|
|
735
|
+ fastDigitalWrite(SckPin, MODE_CPOL(Mode));
|
|
736
|
+ }
|
|
737
|
+ }
|
|
738
|
+ //----------------------------------------------------------------------------
|
|
739
|
+ inline __attribute__((always_inline))
|
|
740
|
+ void sendBit(uint8_t bit, uint8_t data) {
|
|
741
|
+ if (MODE_CPHA(Mode)) {
|
|
742
|
+ fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
|
|
743
|
+ }
|
|
744
|
+ fastDigitalWrite(MosiPin, data & (1 << bit));
|
|
745
|
+ fastDigitalWrite(SckPin,
|
|
746
|
+ MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
|
|
747
|
+ nop;
|
|
748
|
+ nop;
|
|
749
|
+ if (!MODE_CPHA(Mode)) {
|
|
750
|
+ fastDigitalWrite(SckPin, MODE_CPOL(Mode));
|
|
751
|
+ }
|
|
752
|
+ }
|
|
753
|
+ //----------------------------------------------------------------------------
|
|
754
|
+ inline __attribute__((always_inline))
|
|
755
|
+ void transferBit(uint8_t bit, uint8_t* rxData, uint8_t txData) {
|
|
756
|
+ if (MODE_CPHA(Mode)) {
|
|
757
|
+ fastDigitalWrite(SckPin, !MODE_CPOL(Mode));
|
|
758
|
+ }
|
|
759
|
+ fastDigitalWrite(MosiPin, txData & (1 << bit));
|
|
760
|
+ fastDigitalWrite(SckPin,
|
|
761
|
+ MODE_CPHA(Mode) ? MODE_CPOL(Mode) : !MODE_CPOL(Mode));
|
|
762
|
+ if (fastDigitalRead(MisoPin)) *rxData |= 1 << bit;
|
|
763
|
+ if (!MODE_CPHA(Mode)) {
|
|
764
|
+ fastDigitalWrite(SckPin, MODE_CPOL(Mode));
|
|
765
|
+ }
|
|
766
|
+ }
|
|
767
|
+ //----------------------------------------------------------------------------
|
|
768
|
+};
|
|
769
|
+
|
|
770
|
+
|