forked from pichenettes/avril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.h
executable file
·495 lines (423 loc) · 14.2 KB
/
gpio.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// 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/>.
//
// -----------------------------------------------------------------------------
//
// An alternative gpio library based on templates.
//
// Examples of use:
//
// NumberedGpio<3>::set_mode(DIGITAL_INPUT)
// NumberedGpio<4>::set_mode(DIGITAL_OUTPUT)
// NumberedGpio<3>::value()
// NumberedGpio<4>::High()
// NumberedGpio<4>::Low()
// NumberedGpio<4>::set_value(1)
// NumberedGpio<4>::set_value(0)
#ifndef AVRLIB_GPIO_H_
#define AVRLIB_GPIO_H_
#include <avr/io.h>
#include "avrlib/avrlib.h"
#include "avrlib/timer.h"
namespace avrlib {
enum PinMode {
DIGITAL_INPUT = 0,
DIGITAL_OUTPUT = 1,
PWM_OUTPUT = 2
};
// All the registers used in the following definitions are wrapped here.
IORegister(DDRB);
IORegister(DDRC);
IORegister(DDRD);
IORegister(PORTB);
IORegister(PORTC);
IORegister(PORTD);
IORegister(PINB);
IORegister(PINC);
IORegister(PIND);
// Represents a i/o port, which has input, output and mode registers.
template<typename InputRegister, typename OutputRegister,
typename ModeRegister>
struct Port {
typedef InputRegister Input;
typedef OutputRegister Output;
typedef ModeRegister Mode;
};
// Definition of I/O ports.
typedef Port<PINBRegister, PORTBRegister, DDRBRegister> PortB;
typedef Port<PINCRegister, PORTCRegister, DDRCRegister> PortC;
typedef Port<PINDRegister, PORTDRegister, DDRDRegister> PortD;
#if defined(ATMEGA164P) || defined(ATMEGA324P) || defined(ATMEGA644P) || defined(ATMEGA1284P) || defined(ATMEGA2560)
IORegister(DDRA);
IORegister(PORTA);
IORegister(PINA);
typedef Port<PINARegister, PORTARegister, DDRARegister> PortA;
#endif
#if defined (ATMEGA640) || defined (ATMEGA1280) || defined(ATMEGA2560)
IORegister(DDRE);
IORegister(DDRF);
IORegister(DDRG);
IORegister(DDRH);
IORegister(DDRJ);
IORegister(DDRK);
IORegister(DDRL);
IORegister(PORTE);
IORegister(PORTF);
IORegister(PORTG);
IORegister(PORTH);
IORegister(PORTJ);
IORegister(PORTK);
IORegister(PORTL);
IORegister(PINE);
IORegister(PINF);
IORegister(PING);
IORegister(PINH);
IORegister(PINJ);
IORegister(PINK);
IORegister(PINL);
typedef Port<PINERegister, PORTERegister, DDRERegister> PortE;
typedef Port<PINFRegister, PORTFRegister, DDRFRegister> PortF;
typedef Port<PINGRegister, PORTGRegister, DDRGRegister> PortG;
typedef Port<PINHRegister, PORTHRegister, DDRHRegister> PortH;
typedef Port<PINJRegister, PORTJRegister, DDRJRegister> PortJ;
typedef Port<PINKRegister, PORTKRegister, DDRKRegister> PortK;
typedef Port<PINLRegister, PORTLRegister, DDRLRegister> PortL;
#endif
// The actual implementation of a pin, not very convenient to use because it
// requires the actual parameters of the pin to be passed as template
// arguments.
template<typename Port, typename PwmChannel, uint8_t bit>
struct GpioImpl {
typedef BitInRegister<typename Port::Mode, bit> ModeBit;
typedef BitInRegister<typename Port::Output, bit> OutputBit;
typedef BitInRegister<typename Port::Input, bit> InputBit;
typedef PwmChannel Pwm;
static inline void set_mode(uint8_t mode) {
if (mode == DIGITAL_INPUT) {
ModeBit::clear();
} else if (mode == DIGITAL_OUTPUT || mode == PWM_OUTPUT) {
ModeBit::set();
}
if (mode == PWM_OUTPUT) {
PwmChannel::Start();
} else {
PwmChannel::Stop();
}
}
static inline void High() {
OutputBit::set();
}
static inline void Low() {
OutputBit::clear();
}
static inline void Toggle() {
OutputBit::toggle();
}
static inline void set_value(uint8_t value) {
if (value == 0) {
Low();
} else {
High();
}
}
static inline void set_pwm_value(uint8_t value) {
if (PwmChannel::has_pwm) {
PwmChannel::Write(value);
} else {
set_value(value);
}
}
static inline uint8_t value() {
return InputBit::value();
}
static inline uint8_t is_high() {
return InputBit::value();
}
static inline uint8_t is_low() {
return InputBit::value() == 0;
}
};
template<typename port, uint8_t bit>
struct Gpio {
typedef GpioImpl<port, NoPwmChannel, bit> Impl;
static void High() { Impl::High(); }
static void Low() { Impl::Low(); }
static void Toggle() { Impl::Toggle(); }
static void set_mode(uint8_t mode) { Impl::set_mode(mode); }
static void set_value(uint8_t value) { Impl::set_value(value); }
static void set_pwm_value(uint8_t value) { Impl::set_pwm_value(value); }
static uint8_t value() { return Impl::value(); }
static uint8_t is_low() { return Impl::is_low(); }
static uint8_t is_high() { return Impl::is_high(); }
};
struct DummyGpio {
static void High() { }
static void Low() { }
static void set_mode(uint8_t mode) { }
static void set_value(uint8_t value) { }
static void set_pwm_value(uint8_t value) { }
static uint8_t value() { return 0; }
static uint8_t is_low() { return 0; }
static uint8_t is_high() { return 0; }
};
template<typename Gpio>
struct Inverter {
static void High() { Gpio::Low(); }
static void Low() { Gpio::High(); }
static void set_mode(uint8_t mode) { Gpio::set_mode(mode); }
static void set_value(uint8_t value) { Gpio::set_value(!value); }
static void set_pwm_value(uint8_t value) { Gpio::set_pwm_value(~value); }
static uint8_t value() { return !Gpio::value(); }
static uint8_t is_low() { return !Gpio::is_low(); }
static uint8_t is_high() { return !Gpio::is_high(); }
};
template<typename gpio>
struct DigitalInput {
enum {
buffer_size = 0,
data_size = 1,
};
static void Init() {
gpio::set_mode(DIGITAL_INPUT);
}
static void EnablePullUpResistor() {
gpio::High();
}
static void DisablePullUpResistor() {
gpio::Low();
}
static uint8_t Read() {
return gpio::value();
}
};
// A template that will be specialized for each pin, allowing the pin number to
// be specified as a template parameter.
template<int n>
struct NumberedGpioInternal { };
// Macro to make the pin definitions (template specializations) easier to read.
#define SetupGpio(n, port, timer, bit) \
template<> struct NumberedGpioInternal<n> { \
typedef GpioImpl<port, timer, bit> Impl; };
// Pin definitions for ATmega lineup
#if defined(ATMEGA48P) || defined(ATMEGA88P) || defined(ATMEGA168P) || defined(ATMEGA328P)
SetupGpio(0, PortD, NoPwmChannel, 0);
SetupGpio(1, PortD, NoPwmChannel, 1);
SetupGpio(2, PortD, NoPwmChannel, 2);
SetupGpio(3, PortD, PwmChannel2B, 3);
SetupGpio(4, PortD, NoPwmChannel, 4);
SetupGpio(5, PortD, PwmChannel0B, 5);
SetupGpio(6, PortD, PwmChannel0A, 6);
SetupGpio(7, PortD, NoPwmChannel, 7);
SetupGpio(8, PortB, NoPwmChannel, 0);
SetupGpio(9, PortB, PwmChannel1A, 1);
SetupGpio(10, PortB, PwmChannel1B, 2);
SetupGpio(11, PortB, PwmChannel2A, 3);
SetupGpio(12, PortB, NoPwmChannel, 4);
SetupGpio(13, PortB, NoPwmChannel, 5);
SetupGpio(14, PortC, NoPwmChannel, 0);
SetupGpio(15, PortC, NoPwmChannel, 1);
SetupGpio(16, PortC, NoPwmChannel, 2);
SetupGpio(17, PortC, NoPwmChannel, 3);
SetupGpio(18, PortC, NoPwmChannel, 4);
SetupGpio(19, PortC, NoPwmChannel, 5);
SetupGpio(255, PortB, NoPwmChannel, 0);
typedef Gpio<PortB, 5> SpiSCK;
typedef Gpio<PortB, 4> SpiMISO;
typedef Gpio<PortB, 3> SpiMOSI;
typedef Gpio<PortB, 2> SpiSS;
typedef Gpio<PortD, 4> UartSpi0XCK;
typedef Gpio<PortD, 1> UartSpi0TX;
typedef Gpio<PortD, 0> UartSpi0RX;
#define HAS_USART0
#elif defined(ATMEGA164P) || defined(ATMEGA324P) || defined(ATMEGA644P) || defined(ATMEGA1284P)
SetupGpio(0, PortB, NoPwmChannel, 0);
SetupGpio(1, PortB, NoPwmChannel, 1);
SetupGpio(2, PortB, NoPwmChannel, 2);
SetupGpio(3, PortB, PwmChannel0A, 3);
SetupGpio(4, PortB, PwmChannel0B, 4);
SetupGpio(5, PortB, NoPwmChannel, 5);
SetupGpio(6, PortB, NoPwmChannel, 6);
SetupGpio(7, PortB, NoPwmChannel, 7);
SetupGpio(8, PortD, NoPwmChannel, 0);
SetupGpio(9, PortD, NoPwmChannel, 1);
SetupGpio(10, PortD, NoPwmChannel, 2);
SetupGpio(11, PortD, NoPwmChannel, 3);
SetupGpio(12, PortD, PwmChannel1B, 4);
SetupGpio(13, PortD, PwmChannel1A, 5);
SetupGpio(14, PortD, PwmChannel2B, 6);
SetupGpio(15, PortD, PwmChannel2A, 7);
SetupGpio(16, PortC, NoPwmChannel, 0);
SetupGpio(17, PortC, NoPwmChannel, 1);
SetupGpio(18, PortC, NoPwmChannel, 2);
SetupGpio(19, PortC, NoPwmChannel, 3);
SetupGpio(20, PortC, NoPwmChannel, 4);
SetupGpio(21, PortC, NoPwmChannel, 5);
SetupGpio(22, PortC, NoPwmChannel, 6);
SetupGpio(23, PortC, NoPwmChannel, 7);
SetupGpio(255, PortB, NoPwmChannel, 0);
typedef Gpio<PortB, 7> SpiSCK;
typedef Gpio<PortB, 6> SpiMISO;
typedef Gpio<PortB, 5> SpiMOSI;
typedef Gpio<PortB, 4> SpiSS;
typedef Gpio<PortB, 0> UartSpi0XCK;
typedef Gpio<PortD, 1> UartSpi0TX;
typedef Gpio<PortD, 0> UartSpi0RX;
typedef Gpio<PortD, 4> UartSpi1XCK;
typedef Gpio<PortD, 3> UartSpi1TX;
typedef Gpio<PortD, 2> UartSpi1RX;
#define HAS_USART0
#define HAS_USART1
#if defined(ATMEGA1284P) || defined(ATMEGA640) || defined(ATMEGA1280) \
|| defined(ATMEGA2560)
#define HAS_TIMER3
#endif
#elif defined (ATMEGA640) || defined(ATMEGA1280) || defined(ATMEGA2560)
SetupGpio(0, PortB, NoPwmChannel, 0);
SetupGpio(1, PortB, NoPwmChannel, 1);
SetupGpio(2, PortB, NoPwmChannel, 2);
SetupGpio(3, PortB, NoPwmChannel, 3);
SetupGpio(4, PortB, PwmChannel2A, 4);
SetupGpio(5, PortB, PwmChannel1A, 5);
SetupGpio(6, PortB, PwmChannel1B, 6);
SetupGpio(7, PortB, PwmChannel0A, 7);
SetupGpio(8, PortD, NoPwmChannel, 0);
SetupGpio(9, PortD, NoPwmChannel, 1);
SetupGpio(10, PortD, NoPwmChannel, 2);
SetupGpio(11, PortD, NoPwmChannel, 3);
SetupGpio(12, PortD, NoPwmChannel, 4);
SetupGpio(13, PortD, NoPwmChannel, 5);
SetupGpio(14, PortD, NoPwmChannel, 6);
SetupGpio(15, PortD, NoPwmChannel, 7);
SetupGpio(16, PortC, NoPwmChannel, 0);
SetupGpio(17, PortC, NoPwmChannel, 1);
SetupGpio(18, PortC, NoPwmChannel, 2);
SetupGpio(19, PortC, NoPwmChannel, 3);
SetupGpio(20, PortC, NoPwmChannel, 4);
SetupGpio(21, PortC, NoPwmChannel, 5);
SetupGpio(22, PortC, NoPwmChannel, 6);
SetupGpio(23, PortC, NoPwmChannel, 7);
SetupGpio(24, PortE, NoPwmChannel, 0);
SetupGpio(25, PortE, NoPwmChannel, 1);
SetupGpio(26, PortE, NoPwmChannel, 2);
SetupGpio(27, PortE, NoPwmChannel, 3);
SetupGpio(28, PortE, NoPwmChannel, 4);
SetupGpio(29, PortE, NoPwmChannel, 5);
SetupGpio(30, PortE, NoPwmChannel, 6);
SetupGpio(31, PortE, NoPwmChannel, 7);
SetupGpio(32, PortF, NoPwmChannel, 0);
SetupGpio(33, PortF, NoPwmChannel, 1);
SetupGpio(34, PortF, NoPwmChannel, 2);
SetupGpio(35, PortF, NoPwmChannel, 3);
SetupGpio(36, PortF, NoPwmChannel, 4);
SetupGpio(37, PortF, NoPwmChannel, 5);
SetupGpio(38, PortF, NoPwmChannel, 6);
SetupGpio(39, PortF, NoPwmChannel, 7);
SetupGpio(40, PortG, NoPwmChannel, 0);
SetupGpio(41, PortG, NoPwmChannel, 1);
SetupGpio(42, PortG, NoPwmChannel, 2);
SetupGpio(43, PortG, NoPwmChannel, 3);
SetupGpio(44, PortG, NoPwmChannel, 4);
SetupGpio(45, PortG, NoPwmChannel, 5);
SetupGpio(46, PortG, NoPwmChannel, 6);
SetupGpio(47, PortG, NoPwmChannel, 7);
SetupGpio(48, PortH, NoPwmChannel, 0);
SetupGpio(49, PortH, NoPwmChannel, 1);
SetupGpio(50, PortH, NoPwmChannel, 2);
SetupGpio(51, PortH, NoPwmChannel, 3);
SetupGpio(52, PortH, NoPwmChannel, 4);
SetupGpio(53, PortH, NoPwmChannel, 5);
SetupGpio(54, PortH, NoPwmChannel, 6);
SetupGpio(55, PortH, NoPwmChannel, 7);
SetupGpio(56, PortJ, NoPwmChannel, 0);
SetupGpio(57, PortJ, NoPwmChannel, 1);
SetupGpio(58, PortJ, NoPwmChannel, 2);
SetupGpio(59, PortJ, NoPwmChannel, 3);
SetupGpio(60, PortJ, NoPwmChannel, 4);
SetupGpio(61, PortJ, NoPwmChannel, 5);
SetupGpio(62, PortJ, NoPwmChannel, 6);
SetupGpio(63, PortJ, NoPwmChannel, 7);
SetupGpio(64, PortK, NoPwmChannel, 0);
SetupGpio(65, PortK, NoPwmChannel, 1);
SetupGpio(66, PortK, NoPwmChannel, 2);
SetupGpio(67, PortK, NoPwmChannel, 3);
SetupGpio(68, PortK, NoPwmChannel, 4);
SetupGpio(69, PortK, NoPwmChannel, 5);
SetupGpio(70, PortK, NoPwmChannel, 6);
SetupGpio(71, PortK, NoPwmChannel, 7);
SetupGpio(72, PortL, NoPwmChannel, 0);
SetupGpio(73, PortL, NoPwmChannel, 1);
SetupGpio(74, PortL, NoPwmChannel, 2);
SetupGpio(75, PortL, NoPwmChannel, 3);
SetupGpio(76, PortL, NoPwmChannel, 4);
SetupGpio(77, PortL, NoPwmChannel, 5);
SetupGpio(78, PortL, NoPwmChannel, 6);
SetupGpio(79, PortL, NoPwmChannel, 7);
typedef Gpio<PortB, 0> SpiSS;
typedef Gpio<PortB, 1> SpiSCK;
typedef Gpio<PortB, 2> SpiMOSI;
typedef Gpio<PortB, 3> SpiMISO;
typedef Gpio<PortE, 2> UartSpi0XCK;
typedef Gpio<PortE, 1> UartSpi0TX;
typedef Gpio<PortE, 0> UartSpi0RX;
typedef Gpio<PortD, 5> UartSpi1XCK;
typedef Gpio<PortD, 3> UartSpi1TX;
typedef Gpio<PortD, 2> UartSpi1RX;
typedef Gpio<PortH, 2> UartSpi2XCK;
typedef Gpio<PortH, 1> UartSpi2TX;
typedef Gpio<PortH, 0> UartSpi2RX;
typedef Gpio<PortJ, 2> UartSpi3XCK;
typedef Gpio<PortJ, 1> UartSpi3TX;
typedef Gpio<PortJ, 0> UartSpi3RX;
#define HAS_USART0
#define HAS_USART1
#define HAS_USART2
#define HAS_USART3
#else
#error Unsupported MCU type
#endif
// Two specializations of the numbered pin template, one which clears the timer
// for each access to the PWM pins, as does the original Arduino wire lib,
// the other that does not (use with care!).
template<int n>
struct NumberedGpio {
typedef typename NumberedGpioInternal<n>::Impl Impl;
static void High() { Impl::High(); }
static void Low() { Impl::Low(); }
static void set_mode(uint8_t mode) { Impl::set_mode(mode); }
static void set_value(uint8_t value) { Impl::set_value(value); }
static void set_pwm_value(uint8_t value) { Impl::set_pwm_value(value); }
static uint8_t value() { return Impl::value(); }
};
template<int n>
struct PwmOutput {
enum {
buffer_size = 0,
data_size = 8,
};
static void Init() {
NumberedGpio<n>::set_mode(PWM_OUTPUT);
}
static void Write(uint8_t value) {
return NumberedGpio<n>::set_pwm_value(value);
}
static void Stop() {
NumberedGpio<n>::Impl::Pwm::Stop();
}
static void Start() {
NumberedGpio<n>::Impl::Pwm::Start();
}
};
} // namespace avrlib
#endif // AVRLIB_GPIO_H_