USB Host Shield 2.0
avrpins.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 
17 Contact information
18 -------------------
19 
20 Circuits At Home, LTD
21 Web : http://www.circuitsathome.com
22 e-mail : support@circuitsathome.com
23  */
24 
25 /* derived from Konstantin Chizhov's AVR port templates */
26 
27 #if !defined(_usb_h_) || defined(_avrpins_h_)
28 #error "Never include avrpins.h directly; include Usb.h instead"
29 #else
30 #define _avrpins_h_
31 
32 #if defined(__AVR__)
33 
34 // pointers are 16 bits on AVR
35 #define pgm_read_pointer(p) pgm_read_word(p)
36 
37 // Support for these boards needs to be manually activated in settings.h or in a makefile
38 #if !defined(BOARD_MEGA_ADK) && defined(__AVR_ATmega2560__) && (USE_UHS_MEGA_ADK || defined(ARDUINO_AVR_ADK))
39 #define BOARD_MEGA_ADK
40 #elif !defined(BOARD_BLACK_WIDDOW) && USE_UHS_BLACK_WIDDOW
41 #define BOARD_BLACK_WIDDOW
42 #endif
43 
44 #ifdef PORTA
45 #define USE_PORTA
46 #endif
47 #ifdef PORTB
48 #define USE_PORTB
49 #endif
50 #ifdef PORTC
51 #define USE_PORTC
52 #endif
53 #ifdef PORTD
54 #define USE_PORTD
55 #endif
56 #ifdef PORTE
57 #define USE_PORTE
58 #endif
59 #ifdef PORTF
60 #define USE_PORTF
61 #endif
62 #ifdef PORTG
63 #define USE_PORTG
64 #endif
65 #ifdef PORTH
66 #define USE_PORTH
67 #endif
68 #ifdef PORTJ
69 #define USE_PORTJ
70 #endif
71 #ifdef PORTK
72 #define USE_PORTK
73 #endif
74 #ifdef PORTL
75 #define USE_PORTL
76 #endif
77 #ifdef PORTQ
78 #define USE_PORTQ
79 #endif
80 #ifdef PORTR
81 #define USE_PORTR
82 #endif
83 
84 #ifdef TCCR0A
85 #define USE_TCCR0A
86 #endif
87 #ifdef TCCR1A
88 #define USE_TCCR1A
89 #endif
90 #ifdef TCCR2A
91 #define USE_TCCR2A
92 #endif
93 
94 //Port definitions for AtTiny, AtMega families.
95 
96 #define MAKE_PORT(portName, ddrName, pinName, className, ID) \
97  class className{\
98  public:\
99  typedef uint8_t DataT;\
100  public:\
101  static void Write(DataT value){portName = value;}\
102  static void ClearAndSet(DataT clearMask, DataT value){portName = (portName & ~clearMask) | value;}\
103  static DataT Read(){return portName;}\
104  static void DirWrite(DataT value){ddrName = value;}\
105  static DataT DirRead(){return ddrName;}\
106  static void Set(DataT value){portName |= value;}\
107  static void Clear(DataT value){portName &= ~value;}\
108  static void Toggle(DataT value){portName ^= value;}\
109  static void DirSet(DataT value){ddrName |= value;}\
110  static void DirClear(DataT value){ddrName &= ~value;}\
111  static void DirToggle(DataT value){ddrName ^= value;}\
112  static DataT PinRead(){return pinName;}\
113  enum{Id = ID};\
114  enum{Width=sizeof(DataT)*8};\
115  };
116 
117 // TCCR registers to set/clear Arduino PWM
118 #define MAKE_TCCR(TccrName, className) \
119  class className{\
120  public:\
121  typedef uint8_t DataT;\
122  public:\
123  static void Write(DataT value){TccrName = value;}\
124  static void ClearAndSet(DataT clearMask, DataT value){TccrName = (TccrName & ~clearMask) | value;}\
125  static DataT Read(){return TccrName;}\
126  static void Set(DataT value){TccrName |= value;}\
127  static void Clear(DataT value){TccrName &= ~value;}\
128  static void Toggle(DataT value){TccrName ^= value;}\
129  enum{Width=sizeof(DataT)*8};\
130  };
131 
132 #ifdef USE_PORTA
133 
134 MAKE_PORT(PORTA, DDRA, PINA, Porta, 'A')
135 #endif
136 #ifdef USE_PORTB
137 MAKE_PORT(PORTB, DDRB, PINB, Portb, 'B')
138 #endif
139 #ifdef USE_PORTC
140 MAKE_PORT(PORTC, DDRC, PINC, Portc, 'C')
141 #endif
142 #ifdef USE_PORTD
143 MAKE_PORT(PORTD, DDRD, PIND, Portd, 'D')
144 #endif
145 #ifdef USE_PORTE
146 MAKE_PORT(PORTE, DDRE, PINE, Porte, 'E')
147 #endif
148 #ifdef USE_PORTF
149 MAKE_PORT(PORTF, DDRF, PINF, Portf, 'F')
150 #endif
151 #ifdef USE_PORTG
152 MAKE_PORT(PORTG, DDRG, PING, Portg, 'G')
153 #endif
154 #ifdef USE_PORTH
155 MAKE_PORT(PORTH, DDRH, PINH, Porth, 'H')
156 #endif
157 #ifdef USE_PORTJ
158 MAKE_PORT(PORTJ, DDRJ, PINJ, Portj, 'J')
159 #endif
160 #ifdef USE_PORTK
161 MAKE_PORT(PORTK, DDRK, PINK, Portk, 'K')
162 #endif
163 #ifdef USE_PORTL
164 MAKE_PORT(PORTL, DDRL, PINL, Portl, 'L')
165 #endif
166 #ifdef USE_PORTQ
167 MAKE_PORT(PORTQ, DDRQ, PINQ, Portq, 'Q')
168 #endif
169 #ifdef USE_PORTR
170 MAKE_PORT(PORTR, DDRR, PINR, Portr, 'R')
171 #endif
172 
173 #ifdef USE_TCCR0A
174 MAKE_TCCR(TCCR0A, Tccr0a)
175 #endif
176 #ifdef USE_TCCR1A
177 MAKE_TCCR(TCCR1A, Tccr1a)
178 #endif
179 #ifdef USE_TCCR2A
180 MAKE_TCCR(TCCR2A, Tccr2a)
181 #endif
182 
183 // this class represents one pin in a IO port.
184 // It is fully static.
185 template<typename PORT, uint8_t PIN>
186 class TPin {
187  // BOOST_STATIC_ASSERT(PIN < PORT::Width);
188 public:
189  typedef PORT Port;
190 
191  enum {
192  Number = PIN
193  };
194 
195  static void Set() {
196  PORT::Set(1 << PIN);
197  }
198 
199  static void Set(uint8_t val) {
200  if(val)
201  Set();
202  else Clear();
203  }
204 
205  static void SetDir(uint8_t val) {
206  if(val)
207  SetDirWrite();
208  else SetDirRead();
209  }
210 
211  static void Clear() {
212  PORT::Clear(1 << PIN);
213  }
214 
215  static void Toggle() {
216  PORT::Toggle(1 << PIN);
217  }
218 
219  static void SetDirRead() {
220  PORT::DirClear(1 << PIN);
221  }
222 
223  static void SetDirWrite() {
224  PORT::DirSet(1 << PIN);
225  }
226 
227  static uint8_t IsSet() {
228  return PORT::PinRead() & (uint8_t)(1 << PIN);
229  }
230 
231  static void WaiteForSet() {
232  while(IsSet() == 0) {
233  }
234  }
235 
236  static void WaiteForClear() {
237  while(IsSet()) {
238  }
239  }
240 }; //class TPin...
241 
242 // this class represents one bit in TCCR port.
243 // used to set/clear TCCRx bits
244 // It is fully static.
245 
246 template<typename TCCR, uint8_t COM>
247 class TCom {
248  // BOOST_STATIC_ASSERT(PIN < PORT::Width);
249 public:
250  typedef TCCR Tccr;
251 
252  enum {
253  Com = COM
254  };
255 
256  static void Set() {
257  TCCR::Set(1 << COM);
258  }
259 
260  static void Clear() {
261  TCCR::Clear(1 << COM);
262  }
263 
264  static void Toggle() {
265  TCCR::Toggle(1 << COM);
266  }
267 }; //class TCom...
268 
269 //Short pin definitions
270 #ifdef USE_PORTA
271 typedef TPin<Porta, 0 > Pa0;
272 typedef TPin<Porta, 1 > Pa1;
273 typedef TPin<Porta, 2 > Pa2;
274 typedef TPin<Porta, 3 > Pa3;
275 typedef TPin<Porta, 4 > Pa4;
276 typedef TPin<Porta, 5 > Pa5;
277 typedef TPin<Porta, 6 > Pa6;
278 typedef TPin<Porta, 7 > Pa7;
279 #endif
280 
281 #ifdef USE_PORTB
282 typedef TPin<Portb, 0 > Pb0;
283 typedef TPin<Portb, 1 > Pb1;
284 typedef TPin<Portb, 2 > Pb2;
285 typedef TPin<Portb, 3 > Pb3;
286 typedef TPin<Portb, 4 > Pb4;
287 typedef TPin<Portb, 5 > Pb5;
288 typedef TPin<Portb, 6 > Pb6;
289 typedef TPin<Portb, 7 > Pb7;
290 #endif
291 
292 #ifdef USE_PORTC
293 typedef TPin<Portc, 0 > Pc0;
294 typedef TPin<Portc, 1 > Pc1;
295 typedef TPin<Portc, 2 > Pc2;
296 typedef TPin<Portc, 3 > Pc3;
297 typedef TPin<Portc, 4 > Pc4;
298 typedef TPin<Portc, 5 > Pc5;
299 typedef TPin<Portc, 6 > Pc6;
300 typedef TPin<Portc, 7 > Pc7;
301 #endif
302 
303 #ifdef USE_PORTD
304 typedef TPin<Portd, 0 > Pd0;
305 typedef TPin<Portd, 1 > Pd1;
306 typedef TPin<Portd, 2 > Pd2;
307 typedef TPin<Portd, 3 > Pd3;
308 typedef TPin<Portd, 4 > Pd4;
309 typedef TPin<Portd, 5 > Pd5;
310 typedef TPin<Portd, 6 > Pd6;
311 typedef TPin<Portd, 7 > Pd7;
312 #endif
313 
314 #ifdef USE_PORTE
315 typedef TPin<Porte, 0 > Pe0;
316 typedef TPin<Porte, 1 > Pe1;
317 typedef TPin<Porte, 2 > Pe2;
318 typedef TPin<Porte, 3 > Pe3;
319 typedef TPin<Porte, 4 > Pe4;
320 typedef TPin<Porte, 5 > Pe5;
321 typedef TPin<Porte, 6 > Pe6;
322 typedef TPin<Porte, 7 > Pe7;
323 #endif
324 
325 #ifdef USE_PORTF
326 typedef TPin<Portf, 0 > Pf0;
327 typedef TPin<Portf, 1 > Pf1;
328 typedef TPin<Portf, 2 > Pf2;
329 typedef TPin<Portf, 3 > Pf3;
330 typedef TPin<Portf, 4 > Pf4;
331 typedef TPin<Portf, 5 > Pf5;
332 typedef TPin<Portf, 6 > Pf6;
333 typedef TPin<Portf, 7 > Pf7;
334 #endif
335 
336 #ifdef USE_PORTG
337 typedef TPin<Portg, 0 > Pg0;
338 typedef TPin<Portg, 1 > Pg1;
339 typedef TPin<Portg, 2 > Pg2;
340 typedef TPin<Portg, 3 > Pg3;
341 typedef TPin<Portg, 4 > Pg4;
342 typedef TPin<Portg, 5 > Pg5;
343 typedef TPin<Portg, 6 > Pg6;
344 typedef TPin<Portg, 7 > Pg7;
345 #endif
346 
347 #ifdef USE_PORTH
348 typedef TPin<Porth, 0 > Ph0;
349 typedef TPin<Porth, 1 > Ph1;
350 typedef TPin<Porth, 2 > Ph2;
351 typedef TPin<Porth, 3 > Ph3;
352 typedef TPin<Porth, 4 > Ph4;
353 typedef TPin<Porth, 5 > Ph5;
354 typedef TPin<Porth, 6 > Ph6;
355 typedef TPin<Porth, 7 > Ph7;
356 #endif
357 
358 #ifdef USE_PORTJ
359 typedef TPin<Portj, 0 > Pj0;
360 typedef TPin<Portj, 1 > Pj1;
361 typedef TPin<Portj, 2 > Pj2;
362 typedef TPin<Portj, 3 > Pj3;
363 typedef TPin<Portj, 4 > Pj4;
364 typedef TPin<Portj, 5 > Pj5;
365 typedef TPin<Portj, 6 > Pj6;
366 typedef TPin<Portj, 7 > Pj7;
367 #endif
368 
369 #ifdef USE_PORTK
370 typedef TPin<Portk, 0 > Pk0;
371 typedef TPin<Portk, 1 > Pk1;
372 typedef TPin<Portk, 2 > Pk2;
373 typedef TPin<Portk, 3 > Pk3;
374 typedef TPin<Portk, 4 > Pk4;
375 typedef TPin<Portk, 5 > Pk5;
376 typedef TPin<Portk, 6 > Pk6;
377 typedef TPin<Portk, 7 > Pk7;
378 #endif
379 
380 #ifdef USE_PORTL
381 typedef TPin<Portl, 0 > Pl0;
382 typedef TPin<Portl, 1 > Pl1;
383 typedef TPin<Portl, 2 > Pl2;
384 typedef TPin<Portl, 3 > Pl3;
385 typedef TPin<Portl, 4 > Pl4;
386 typedef TPin<Portl, 5 > Pl5;
387 typedef TPin<Portl, 6 > Pl6;
388 typedef TPin<Portl, 7 > Pl7;
389 #endif
390 
391 #ifdef USE_PORTQ
392 typedef TPin<Portq, 0 > Pq0;
393 typedef TPin<Portq, 1 > Pq1;
394 typedef TPin<Portq, 2 > Pq2;
395 typedef TPin<Portq, 3 > Pq3;
396 typedef TPin<Portq, 4 > Pq4;
397 typedef TPin<Portq, 5 > Pq5;
398 typedef TPin<Portq, 6 > Pq6;
399 typedef TPin<Portq, 7 > Pq7;
400 #endif
401 
402 #ifdef USE_PORTR
403 typedef TPin<Portr, 0 > Pr0;
404 typedef TPin<Portr, 1 > Pr1;
405 typedef TPin<Portr, 2 > Pr2;
406 typedef TPin<Portr, 3 > Pr3;
407 typedef TPin<Portr, 4 > Pr4;
408 typedef TPin<Portr, 5 > Pr5;
409 typedef TPin<Portr, 6 > Pr6;
410 typedef TPin<Portr, 7 > Pr7;
411 #endif
412 
413 #ifdef USE_TCCR0A
414 typedef TCom<Tccr0a, COM0A1> Tc0a; //P6
415 typedef TCom<Tccr0a, COM0B1> Tc0b; //P5
416 #endif
417 
418 #ifdef USE_TCCR1A
419 typedef TCom<Tccr1a, COM1A1> Tc1a; //P9
420 typedef TCom<Tccr1a, COM1B1> Tc1b; //P10
421 #endif
422 
423 #ifdef USE_TCCR2A
424 typedef TCom<Tccr2a, COM2A1> Tc2a; //P11
425 typedef TCom<Tccr2a, COM2B1> Tc2b; //P3
426 #endif
427 
428 template<typename Tp_pin, typename Tc_bit>
429 class Tp_Tc {
430 public:
431 
432  static void SetDir(uint8_t val) {
433  if(val)
434  SetDirWrite();
435  else SetDirRead();
436  }
437 
438  static void SetDirRead() {
439  Tp_pin::SetDirRead(); //set pin direction
440  Tc_bit::Clear(); //disconnect pin from PWM
441  }
442 
443  static void SetDirWrite() {
444  Tp_pin::SetDirWrite();
445  Tc_bit::Clear();
446  }
447 };
448 
449 /* pin definitions for cases where it's necessary to clear compare output mode bits */
450 
451 //typedef Tp_Tc<Pd3, Tc2b> P3; //Arduino pin 3
452 //typedef Tp_Tc<Pd5, Tc0b> P5; //Arduino pin 5
453 //typedef Tp_Tc<Pd6, Tc0a> P6; //Arduino pin 6
454 //typedef Tp_Tc<Pb1, Tc1a> P9; //Arduino pin 9
455 //typedef Tp_Tc<Pb2, Tc1b> P10; //Arduino pin 10
456 //typedef Tp_Tc<Pb3, Tc2a> P11; //Arduino pin 11
457 
458 /* Arduino pin definitions */
459 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
460 // "Mega" Arduino pin numbers
461 
462 #define P0 Pe0
463 #define P1 Pe1
464 #define P2 Pe4
465 #define P3 Pe5
466 #define P4 Pg5
467 #define P5 Pe3
468 #define P6 Ph3
469 #define P7 Ph4
470 
471 #define P8 Ph5
472 #define P9 Ph6
473 #define P10 Pb4
474 #define P11 Pb5
475 #define P12 Pb6
476 #define P13 Pb7
477 
478 #define P14 Pj1
479 #define P15 Pj0
480 #define P16 Ph1
481 #define P17 Ph0
482 #define P18 Pd3
483 #define P19 Pd2
484 #define P20 Pd1
485 #define P21 Pd0
486 
487 #define P22 Pa0
488 #define P23 Pa1
489 #define P24 Pa2
490 #define P25 Pa3
491 #define P26 Pa4
492 #define P27 Pa5
493 #define P28 Pa6
494 #define P29 Pa7
495 #define P30 Pc7
496 #define P31 Pc6
497 #define P32 Pc5
498 #define P33 Pc4
499 #define P34 Pc3
500 #define P35 Pc2
501 #define P36 Pc1
502 #define P37 Pc0
503 
504 #define P38 Pd7
505 #define P39 Pg2
506 #define P40 Pg1
507 #define P41 Pg0
508 #define P42 Pl7
509 #define P43 Pl6
510 #define P44 Pl5
511 #define P45 Pl4
512 #define P46 Pl3
513 #define P47 Pl2
514 #define P48 Pl1
515 #define P49 Pl0
516 #define P50 Pb3
517 #define P51 Pb2
518 #define P52 Pb1
519 #define P53 Pb0
520 
521 #ifdef BOARD_MEGA_ADK // These pins are not broken out on the Arduino ADK
522 #define P54 Pe6 // INT on Arduino ADK
523 #define P55 Pj2 // MAX_RESET on Arduino ADK
524 #endif
525 
526 // "Mega" pin numbers
527 
528 #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
529 // "Classic" Arduino pin numbers
530 
531 #define P0 Pd0
532 #define P1 Pd1
533 #define P2 Pd2
534 #define P3 Pd3
535 #define P4 Pd4
536 #define P5 Pd5
537 #define P6 Pd6
538 #define P7 Pd7
539 
540 #define P8 Pb0
541 #define P9 Pb1
542 #define P10 Pb2
543 #define P11 Pb3
544 #define P12 Pb4
545 #define P13 Pb5
546 
547 #define P14 Pc0
548 #define P15 Pc1
549 #define P16 Pc2
550 #define P17 Pc3
551 #define P18 Pc4
552 #define P19 Pc5
553 
554 // "Classic" Arduino pin numbers
555 
556 #elif defined(CORE_TEENSY) && defined(__AVR_ATmega32U4__)
557 // Teensy 2.0 pin numbers
558 // http://www.pjrc.com/teensy/pinout.html
559 #define P0 Pb0
560 #define P1 Pb1
561 #define P2 Pb2
562 #define P3 Pb3
563 #define P4 Pb7
564 #define P5 Pd0
565 #define P6 Pd1
566 #define P7 Pd2
567 #define P8 Pd3
568 #define P9 Pc6
569 #define P10 Pc7
570 #define P11 Pd6
571 #define P12 Pd7
572 #define P13 Pb4
573 #define P14 Pb5
574 #define P15 Pb6
575 #define P16 Pf7
576 #define P17 Pf6
577 #define P18 Pf5
578 #define P19 Pf4
579 #define P20 Pf1
580 #define P21 Pf0
581 #define P22 Pd4
582 #define P23 Pd5
583 #define P24 Pe6
584 // Teensy 2.0
585 
586 #elif defined(__AVR_ATmega32U4__)
587 // Arduino Leonardo pin numbers
588 
589 #define P0 Pd2 // D0 - PD2
590 #define P1 Pd3 // D1 - PD3
591 #define P2 Pd1 // D2 - PD1
592 #define P3 Pd0 // D3 - PD0
593 #define P4 Pd4 // D4 - PD4
594 #define P5 Pc6 // D5 - PC6
595 #define P6 Pd7 // D6 - PD7
596 #define P7 Pe6 // D7 - PE6
597 
598 #define P8 Pb4 // D8 - PB4
599 #define P9 Pb5 // D9 - PB5
600 #define P10 Pb6 // D10 - PB6
601 #define P11 Pb7 // D11 - PB7
602 #define P12 Pd6 // D12 - PD6
603 #define P13 Pc7 // D13 - PC7
604 
605 #define P14 Pb3 // D14 - MISO - PB3
606 #define P15 Pb1 // D15 - SCK - PB1
607 #define P16 Pb2 // D16 - MOSI - PB2
608 #define P17 Pb0 // D17 - SS - PB0
609 
610 #define P18 Pf7 // D18 - A0 - PF7
611 #define P19 Pf6 // D19 - A1 - PF6
612 #define P20 Pf5 // D20 - A2 - PF5
613 #define P21 Pf4 // D21 - A3 - PF4
614 #define P22 Pf1 // D22 - A4 - PF1
615 #define P23 Pf0 // D23 - A5 - PF0
616 
617 #define P24 Pd4 // D24 / D4 - A6 - PD4
618 #define P25 Pd7 // D25 / D6 - A7 - PD7
619 #define P26 Pb4 // D26 / D8 - A8 - PB4
620 #define P27 Pb5 // D27 / D9 - A9 - PB5
621 #define P28 Pb6 // D28 / D10 - A10 - PB6
622 #define P29 Pd6 // D29 / D12 - A11 - PD6
623 
624 // Arduino Leonardo pin numbers
625 
626 #elif defined(CORE_TEENSY) && (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__))
627 // Teensy++ 1.0 and 2.0 pin numbers
628 // http://www.pjrc.com/teensy/pinout.html
629 #define P0 Pd0
630 #define P1 Pd1
631 #define P2 Pd2
632 #define P3 Pd3
633 #define P4 Pd4
634 #define P5 Pd5
635 #define P6 Pd6
636 #define P7 Pd7
637 #define P8 Pe0
638 #define P9 Pe1
639 #define P10 Pc0
640 #define P11 Pc1
641 #define P12 Pc2
642 #define P13 Pc3
643 #define P14 Pc4
644 #define P15 Pc5
645 #define P16 Pc6
646 #define P17 Pc7
647 #define P18 Pe6
648 #define P19 Pe7
649 #define P20 Pb0
650 #define P21 Pb1
651 #define P22 Pb2
652 #define P23 Pb3
653 #define P24 Pb4
654 #define P25 Pb5
655 #define P26 Pb6
656 #define P27 Pb7
657 #define P28 Pa0
658 #define P29 Pa1
659 #define P30 Pa2
660 #define P31 Pa3
661 #define P32 Pa4
662 #define P33 Pa5
663 #define P34 Pa6
664 #define P35 Pa7
665 #define P36 Pe4
666 #define P37 Pe5
667 #define P38 Pf0
668 #define P39 Pf1
669 #define P40 Pf2
670 #define P41 Pf3
671 #define P42 Pf4
672 #define P43 Pf5
673 #define P44 Pf6
674 #define P45 Pf7
675 // Teensy++ 1.0 and 2.0
676 
677 #elif defined(ARDUINO_AVR_BALANDUINO) && (defined(__AVR_ATmega644__) || defined(__AVR_ATmega1284P__))
678 // Balanduino pin numbers
679 // http://balanduino.net/
680 #define P0 Pd0 /* 0 - PD0 */
681 #define P1 Pd1 /* 1 - PD1 */
682 
683 #if BALANDUINO_REVISION < 13
684  #define P2 Pb2 /* 2 - PB2 */
685  #define P3 Pd6 /* 3 - PD6 */
686  #define P4 Pd7 /* 4 - PD7 */
687  #define P5 Pb3 /* 5 - PB3 */
688 #else
689  #define P2 Pd2 /* 2 - PD2 */
690  #define P3 Pd3 /* 3 - PD3 */
691  #define P4 Pd6 /* 4 - PD6 */
692  #define P5 Pd7 /* 5 - PD7 */
693 #endif
694 
695 #define P6 Pb4 /* 6 - PB4 */
696 #define P7 Pa0 /* 7 - PA0 */
697 #define P8 Pa1 /* 8 - PA1 */
698 #define P9 Pa2 /* 9 - PA2 */
699 #define P10 Pa3 /* 10 - PA3 */
700 #define P11 Pa4 /* 11 - PA4 */
701 #define P12 Pa5 /* 12 - PA5 */
702 #define P13 Pc1 /* 13 - PC1 */
703 #define P14 Pc0 /* 14 - PC0 */
704 
705 #if BALANDUINO_REVISION < 13
706  #define P15 Pd2 /* 15 - PD2 */
707  #define P16 Pd3 /* 16 - PD3 */
708 #else
709  #define P15 Pb2 /* 15 - PB2 */
710  #define P16 Pb3 /* 16 - PB2 */
711 #endif
712 
713 #define P17 Pd4 /* 17 - PD4 */
714 #define P18 Pd5 /* 18 - PD5 */
715 #define P19 Pc2 /* 19 - PC2 */
716 #define P20 Pc3 /* 20 - PC3 */
717 #define P21 Pc4 /* 21 - PC4 */
718 #define P22 Pc5 /* 22 - PC5 */
719 #define P23 Pc6 /* 23 - PC6 */
720 #define P24 Pc7 /* 24 - PC7 */
721 #define P25 Pb0 /* 25 - PB0 */
722 #define P26 Pb1 /* 26 - PB1 */
723 #define P27 Pb5 /* 27 - PB5 */
724 #define P28 Pb6 /* 28 - PB6 */
725 #define P29 Pb7 /* 29 - PB7 */
726 #define P30 Pa6 /* 30 - PA6 */
727 #define P31 Pa7 /* 31 - PA7 */
728 // Balanduino
729 
730 #elif defined(ARDUINO_AVR_UNO_PRO) && defined(__AVR_ATmega1284P__)
731 // UNO*Pro pin numbers
732 // Homepage: http://www.hobbytronics.co.uk/arduino-uno-pro
733 // Pin Reference: http://www.hobbytronics.co.uk/download/uno_pro/pins_arduino.h
734 #define P0 Pd0
735 #define P1 Pd1
736 #define P2 Pb2
737 #define P3 Pb3
738 #define P4 Pb0
739 #define P5 Pb1
740 #define P6 Pd2
741 #define P7 Pd3
742 #define P8 Pd5
743 #define P9 Pd6
744 #define P10 Pb4
745 #define P11 Pb5
746 #define P12 Pb6
747 #define P13 Pb7
748 #define P14 Pa7
749 #define P15 Pa6
750 #define P16 Pa5
751 #define P17 Pa4
752 #define P18 Pa3
753 #define P19 Pa2
754 #define P20 Pa1
755 #define P21 Pa0
756 #define P22 Pc0
757 #define P23 Pc1
758 #define P24 Pc2
759 #define P25 Pc3
760 #define P26 Pc4
761 #define P27 Pc5
762 #define P28 Pc6
763 #define P29 Pc7
764 #define P30 Pd4
765 #define P31 Pd7
766 // UNO*Pro
767 
768 #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
769 // Sanguino pin numbers
770 // Homepage: http://sanguino.cc/hardware
771 // Hardware add-on: https://github.com/Lauszus/Sanguino
772 #define P0 Pb0
773 #define P1 Pb1
774 #define P2 Pb2
775 #define P3 Pb3
776 #define P4 Pb4
777 #define P5 Pb5
778 #define P6 Pb6
779 #define P7 Pb7
780 #define P8 Pd0
781 #define P9 Pd1
782 #define P10 Pd2
783 #define P11 Pd3
784 #define P12 Pd4
785 #define P13 Pd5
786 #define P14 Pd6
787 #define P15 Pd7
788 #define P16 Pc0
789 #define P17 Pc1
790 #define P18 Pc2
791 #define P19 Pc3
792 #define P20 Pc4
793 #define P21 Pc5
794 #define P22 Pc6
795 #define P23 Pc7
796 #define P24 Pa0
797 #define P25 Pa1
798 #define P26 Pa2
799 #define P27 Pa3
800 #define P28 Pa4
801 #define P29 Pa5
802 #define P30 Pa6
803 #define P31 Pa7
804 // Sanguino
805 
806 #else
807 #error "Please define board in avrpins.h"
808 
809 #endif // Arduino pin definitions
810 
811 #elif defined(__arm__)
812 
813 // pointers are 32 bits on ARM
814 #define pgm_read_pointer(p) pgm_read_dword(p)
815 
816 #if defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__))
817 // Teensy 3.x
818 
819 #include "core_pins.h"
820 #include "avr_emulation.h"
821 
822 #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
823 #define GPIO_BITBAND_PTR(reg, bit) ((uint8_t *)GPIO_BITBAND_ADDR((reg), (bit)))
824 
825 #define MAKE_PIN(className, baseReg, pinNum, configReg) \
826 class className { \
827 public: \
828  static void Set() { \
829  *GPIO_BITBAND_PTR(baseReg, pinNum) = 1; \
830  } \
831  static void Clear() { \
832  *GPIO_BITBAND_PTR(baseReg, pinNum) = 0; \
833  } \
834  static void SetDirRead() { \
835  configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \
836  *(GPIO_BITBAND_PTR(baseReg, pinNum) + 640) = 0; \
837  } \
838  static void SetDirWrite() { \
839  configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \
840  *(GPIO_BITBAND_PTR(baseReg, pinNum) + 640) = 1; \
841  } \
842  static uint8_t IsSet() { \
843  return *(GPIO_BITBAND_PTR(baseReg, pinNum) + 512); \
844  } \
845 };
846 
847 MAKE_PIN(P0, CORE_PIN0_PORTREG, CORE_PIN0_BIT, CORE_PIN0_CONFIG);
848 MAKE_PIN(P1, CORE_PIN1_PORTREG, CORE_PIN1_BIT, CORE_PIN1_CONFIG);
849 MAKE_PIN(P2, CORE_PIN2_PORTREG, CORE_PIN2_BIT, CORE_PIN2_CONFIG);
850 MAKE_PIN(P3, CORE_PIN3_PORTREG, CORE_PIN3_BIT, CORE_PIN3_CONFIG);
851 MAKE_PIN(P4, CORE_PIN4_PORTREG, CORE_PIN4_BIT, CORE_PIN4_CONFIG);
852 MAKE_PIN(P5, CORE_PIN5_PORTREG, CORE_PIN5_BIT, CORE_PIN5_CONFIG);
853 MAKE_PIN(P6, CORE_PIN6_PORTREG, CORE_PIN6_BIT, CORE_PIN6_CONFIG);
854 MAKE_PIN(P7, CORE_PIN7_PORTREG, CORE_PIN7_BIT, CORE_PIN7_CONFIG);
855 MAKE_PIN(P8, CORE_PIN8_PORTREG, CORE_PIN8_BIT, CORE_PIN8_CONFIG);
856 MAKE_PIN(P9, CORE_PIN9_PORTREG, CORE_PIN9_BIT, CORE_PIN9_CONFIG);
857 MAKE_PIN(P10, CORE_PIN10_PORTREG, CORE_PIN10_BIT, CORE_PIN10_CONFIG);
858 MAKE_PIN(P11, CORE_PIN11_PORTREG, CORE_PIN11_BIT, CORE_PIN11_CONFIG);
859 MAKE_PIN(P12, CORE_PIN12_PORTREG, CORE_PIN12_BIT, CORE_PIN12_CONFIG);
860 MAKE_PIN(P13, CORE_PIN13_PORTREG, CORE_PIN13_BIT, CORE_PIN13_CONFIG);
861 MAKE_PIN(P14, CORE_PIN14_PORTREG, CORE_PIN14_BIT, CORE_PIN14_CONFIG);
862 MAKE_PIN(P15, CORE_PIN15_PORTREG, CORE_PIN15_BIT, CORE_PIN15_CONFIG);
863 MAKE_PIN(P16, CORE_PIN16_PORTREG, CORE_PIN16_BIT, CORE_PIN16_CONFIG);
864 MAKE_PIN(P17, CORE_PIN17_PORTREG, CORE_PIN17_BIT, CORE_PIN17_CONFIG);
865 MAKE_PIN(P18, CORE_PIN18_PORTREG, CORE_PIN18_BIT, CORE_PIN18_CONFIG);
866 MAKE_PIN(P19, CORE_PIN19_PORTREG, CORE_PIN19_BIT, CORE_PIN19_CONFIG);
867 MAKE_PIN(P20, CORE_PIN20_PORTREG, CORE_PIN20_BIT, CORE_PIN20_CONFIG);
868 MAKE_PIN(P21, CORE_PIN21_PORTREG, CORE_PIN21_BIT, CORE_PIN21_CONFIG);
869 MAKE_PIN(P22, CORE_PIN22_PORTREG, CORE_PIN22_BIT, CORE_PIN22_CONFIG);
870 MAKE_PIN(P23, CORE_PIN23_PORTREG, CORE_PIN23_BIT, CORE_PIN23_CONFIG);
871 MAKE_PIN(P24, CORE_PIN24_PORTREG, CORE_PIN24_BIT, CORE_PIN24_CONFIG);
872 MAKE_PIN(P25, CORE_PIN25_PORTREG, CORE_PIN25_BIT, CORE_PIN25_CONFIG);
873 MAKE_PIN(P26, CORE_PIN26_PORTREG, CORE_PIN26_BIT, CORE_PIN26_CONFIG);
874 MAKE_PIN(P27, CORE_PIN27_PORTREG, CORE_PIN27_BIT, CORE_PIN27_CONFIG);
875 MAKE_PIN(P28, CORE_PIN28_PORTREG, CORE_PIN28_BIT, CORE_PIN28_CONFIG);
876 MAKE_PIN(P29, CORE_PIN29_PORTREG, CORE_PIN29_BIT, CORE_PIN29_CONFIG);
877 MAKE_PIN(P30, CORE_PIN30_PORTREG, CORE_PIN30_BIT, CORE_PIN30_CONFIG);
878 MAKE_PIN(P31, CORE_PIN31_PORTREG, CORE_PIN31_BIT, CORE_PIN31_CONFIG);
879 MAKE_PIN(P32, CORE_PIN32_PORTREG, CORE_PIN32_BIT, CORE_PIN32_CONFIG);
880 MAKE_PIN(P33, CORE_PIN33_PORTREG, CORE_PIN33_BIT, CORE_PIN33_CONFIG);
881 #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
882 MAKE_PIN(P34, CORE_PIN34_PORTREG, CORE_PIN34_BIT, CORE_PIN34_CONFIG);
883 MAKE_PIN(P35, CORE_PIN35_PORTREG, CORE_PIN35_BIT, CORE_PIN35_CONFIG);
884 MAKE_PIN(P36, CORE_PIN36_PORTREG, CORE_PIN36_BIT, CORE_PIN36_CONFIG);
885 MAKE_PIN(P37, CORE_PIN37_PORTREG, CORE_PIN37_BIT, CORE_PIN37_CONFIG);
886 MAKE_PIN(P38, CORE_PIN38_PORTREG, CORE_PIN38_BIT, CORE_PIN38_CONFIG);
887 MAKE_PIN(P39, CORE_PIN39_PORTREG, CORE_PIN39_BIT, CORE_PIN39_CONFIG);
888 MAKE_PIN(P40, CORE_PIN40_PORTREG, CORE_PIN40_BIT, CORE_PIN40_CONFIG);
889 MAKE_PIN(P41, CORE_PIN41_PORTREG, CORE_PIN41_BIT, CORE_PIN41_CONFIG);
890 MAKE_PIN(P42, CORE_PIN42_PORTREG, CORE_PIN42_BIT, CORE_PIN42_CONFIG);
891 MAKE_PIN(P43, CORE_PIN43_PORTREG, CORE_PIN43_BIT, CORE_PIN43_CONFIG);
892 MAKE_PIN(P44, CORE_PIN44_PORTREG, CORE_PIN44_BIT, CORE_PIN44_CONFIG);
893 MAKE_PIN(P45, CORE_PIN45_PORTREG, CORE_PIN45_BIT, CORE_PIN45_CONFIG);
894 MAKE_PIN(P46, CORE_PIN46_PORTREG, CORE_PIN46_BIT, CORE_PIN46_CONFIG);
895 MAKE_PIN(P47, CORE_PIN47_PORTREG, CORE_PIN47_BIT, CORE_PIN47_CONFIG);
896 MAKE_PIN(P48, CORE_PIN48_PORTREG, CORE_PIN48_BIT, CORE_PIN48_CONFIG);
897 MAKE_PIN(P49, CORE_PIN49_PORTREG, CORE_PIN49_BIT, CORE_PIN49_CONFIG);
898 MAKE_PIN(P50, CORE_PIN50_PORTREG, CORE_PIN50_BIT, CORE_PIN50_CONFIG);
899 MAKE_PIN(P51, CORE_PIN51_PORTREG, CORE_PIN51_BIT, CORE_PIN51_CONFIG);
900 MAKE_PIN(P52, CORE_PIN52_PORTREG, CORE_PIN52_BIT, CORE_PIN52_CONFIG);
901 MAKE_PIN(P53, CORE_PIN53_PORTREG, CORE_PIN53_BIT, CORE_PIN53_CONFIG);
902 MAKE_PIN(P54, CORE_PIN54_PORTREG, CORE_PIN54_BIT, CORE_PIN54_CONFIG);
903 MAKE_PIN(P55, CORE_PIN55_PORTREG, CORE_PIN55_BIT, CORE_PIN55_CONFIG);
904 MAKE_PIN(P56, CORE_PIN56_PORTREG, CORE_PIN56_BIT, CORE_PIN56_CONFIG);
905 MAKE_PIN(P57, CORE_PIN57_PORTREG, CORE_PIN57_BIT, CORE_PIN57_CONFIG);
906 MAKE_PIN(P58, CORE_PIN58_PORTREG, CORE_PIN58_BIT, CORE_PIN58_CONFIG);
907 MAKE_PIN(P59, CORE_PIN59_PORTREG, CORE_PIN59_BIT, CORE_PIN59_CONFIG);
908 MAKE_PIN(P60, CORE_PIN60_PORTREG, CORE_PIN60_BIT, CORE_PIN60_CONFIG);
909 MAKE_PIN(P61, CORE_PIN61_PORTREG, CORE_PIN61_BIT, CORE_PIN61_CONFIG);
910 MAKE_PIN(P62, CORE_PIN62_PORTREG, CORE_PIN62_BIT, CORE_PIN62_CONFIG);
911 MAKE_PIN(P63, CORE_PIN63_PORTREG, CORE_PIN63_BIT, CORE_PIN63_CONFIG);
912 #endif
913 
914 #undef MAKE_PIN
915 
916 #elif defined(CORE_TEENSY) && (defined(__MKL26Z64__))
917 // Teensy-LC
918 
919 // we could get lower level by making these macros work properly.
920 // for now just use the semi optimised version, it costs a lookup in the pin pgm table per op
921 // but for now it will do.
922 //#define GPIO_BITBAND_ADDR(reg, bit) (((volatile uint8_t *)&(reg) + ((bit) >> 3)))
923 //#define GPIO_BITBAND_MASK(reg, bit) (1<<((bit) & 7))
924 //#define GPIO_BITBAND_PTR(reg, bit) ((volatile uint8_t *)GPIO_BITBAND_ADDR((reg), (bit)))
925 
926 #include "core_pins.h"
927 #include "avr_emulation.h"
928 
929 #define MAKE_PIN(className, baseReg, pinNum, configReg) \
930 class className { \
931 public: \
932  static void Set() { \
933  *portSetRegister(pinNum) = digitalPinToBitMask(pinNum); \
934  } \
935  static void Clear() { \
936  *portClearRegister(pinNum) = digitalPinToBitMask(pinNum); \
937  } \
938  static void SetDirRead() { \
939  configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \
940  *portModeRegister(pinNum) &= ~digitalPinToBitMask(pinNum); \
941  } \
942  static void SetDirWrite() { \
943  configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \
944  *portModeRegister(pinNum) |= digitalPinToBitMask(pinNum); \
945  } \
946  static uint8_t IsSet() { \
947  return (*portInputRegister(pinNum) & digitalPinToBitMask(pinNum)) ? 1 : 0; \
948  } \
949 };
950 
951 MAKE_PIN(P0, CORE_PIN0_PORTREG, 0, CORE_PIN0_CONFIG);
952 MAKE_PIN(P1, CORE_PIN1_PORTREG, 1, CORE_PIN1_CONFIG);
953 MAKE_PIN(P2, CORE_PIN2_PORTREG, 2, CORE_PIN2_CONFIG);
954 MAKE_PIN(P3, CORE_PIN3_PORTREG, 3, CORE_PIN3_CONFIG);
955 MAKE_PIN(P4, CORE_PIN4_PORTREG, 4, CORE_PIN4_CONFIG);
956 MAKE_PIN(P5, CORE_PIN5_PORTREG, 5, CORE_PIN5_CONFIG);
957 MAKE_PIN(P6, CORE_PIN6_PORTREG, 6, CORE_PIN6_CONFIG);
958 MAKE_PIN(P7, CORE_PIN7_PORTREG, 7, CORE_PIN7_CONFIG);
959 MAKE_PIN(P8, CORE_PIN8_PORTREG, 8, CORE_PIN8_CONFIG);
960 MAKE_PIN(P9, CORE_PIN9_PORTREG, 9, CORE_PIN9_CONFIG);
961 MAKE_PIN(P10, CORE_PIN10_PORTREG, 10, CORE_PIN10_CONFIG);
962 MAKE_PIN(P11, CORE_PIN11_PORTREG, 11, CORE_PIN11_CONFIG);
963 MAKE_PIN(P12, CORE_PIN12_PORTREG, 12, CORE_PIN12_CONFIG);
964 MAKE_PIN(P13, CORE_PIN13_PORTREG, 13, CORE_PIN13_CONFIG);
965 MAKE_PIN(P14, CORE_PIN14_PORTREG, 14, CORE_PIN14_CONFIG);
966 MAKE_PIN(P15, CORE_PIN15_PORTREG, 15, CORE_PIN15_CONFIG);
967 MAKE_PIN(P16, CORE_PIN16_PORTREG, 16, CORE_PIN16_CONFIG);
968 MAKE_PIN(P17, CORE_PIN17_PORTREG, 17, CORE_PIN17_CONFIG);
969 MAKE_PIN(P18, CORE_PIN18_PORTREG, 18, CORE_PIN18_CONFIG);
970 MAKE_PIN(P19, CORE_PIN19_PORTREG, 19, CORE_PIN19_CONFIG);
971 MAKE_PIN(P20, CORE_PIN20_PORTREG, 20, CORE_PIN20_CONFIG);
972 MAKE_PIN(P21, CORE_PIN21_PORTREG, 21, CORE_PIN21_CONFIG);
973 MAKE_PIN(P22, CORE_PIN22_PORTREG, 22, CORE_PIN22_CONFIG);
974 MAKE_PIN(P23, CORE_PIN23_PORTREG, 23, CORE_PIN23_CONFIG);
975 MAKE_PIN(P24, CORE_PIN24_PORTREG, 24, CORE_PIN24_CONFIG);
976 MAKE_PIN(P25, CORE_PIN25_PORTREG, 25, CORE_PIN25_CONFIG);
977 MAKE_PIN(P26, CORE_PIN26_PORTREG, 26, CORE_PIN26_CONFIG);
978 
979 #undef MAKE_PIN
980 
981 #elif defined(__IMXRT1062__) && (defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41))
982 // Teensy 4.x
983 
984 #include "core_pins.h"
985 
986 #define MAKE_PIN(className, pin) \
987 class className { \
988 public: \
989  static void Set() { \
990  digitalWriteFast(pin, HIGH);\
991  } \
992  static void Clear() { \
993  digitalWriteFast(pin, LOW); \
994  } \
995  static void SetDirRead() { \
996  pinMode(pin, INPUT); \
997  } \
998  static void SetDirWrite() { \
999  pinMode(pin, OUTPUT); \
1000  } \
1001  static uint8_t IsSet() { \
1002  return digitalReadFast(pin); \
1003  } \
1004 };
1005 
1006 MAKE_PIN(P0, 0);
1007 MAKE_PIN(P1, 1);
1008 MAKE_PIN(P2, 2);
1009 MAKE_PIN(P3, 3);
1010 MAKE_PIN(P4, 4);
1011 MAKE_PIN(P5, 5);
1012 MAKE_PIN(P6, 6);
1013 MAKE_PIN(P7, 7);
1014 MAKE_PIN(P8, 8);
1015 MAKE_PIN(P9, 9);
1016 MAKE_PIN(P10, 10);
1017 MAKE_PIN(P11, 11);
1018 MAKE_PIN(P12, 12);
1019 MAKE_PIN(P13, 13);
1020 MAKE_PIN(P14, 14);
1021 MAKE_PIN(P15, 15);
1022 MAKE_PIN(P16, 16);
1023 MAKE_PIN(P17, 17);
1024 MAKE_PIN(P18, 18);
1025 MAKE_PIN(P19, 19);
1026 MAKE_PIN(P20, 20);
1027 MAKE_PIN(P21, 21);
1028 MAKE_PIN(P22, 22);
1029 MAKE_PIN(P23, 23);
1030 MAKE_PIN(P24, 24);
1031 MAKE_PIN(P25, 25);
1032 MAKE_PIN(P26, 26);
1033 MAKE_PIN(P27, 27);
1034 MAKE_PIN(P28, 28);
1035 MAKE_PIN(P29, 29);
1036 MAKE_PIN(P30, 30);
1037 MAKE_PIN(P31, 31);
1038 MAKE_PIN(P32, 35);
1039 MAKE_PIN(P33, 33);
1040 MAKE_PIN(P34, 34);
1041 MAKE_PIN(P35, 35);
1042 MAKE_PIN(P36, 36);
1043 MAKE_PIN(P37, 37);
1044 MAKE_PIN(P38, 38);
1045 MAKE_PIN(P39, 39);
1046 #ifdef ARDUINO_TEENSY41
1047 MAKE_PIN(P40, 40);
1048 MAKE_PIN(P41, 41);
1049 MAKE_PIN(P42, 42);
1050 MAKE_PIN(P43, 43);
1051 MAKE_PIN(P44, 44);
1052 MAKE_PIN(P45, 45);
1053 MAKE_PIN(P46, 46);
1054 MAKE_PIN(P47, 47);
1055 MAKE_PIN(P48, 48);
1056 MAKE_PIN(P49, 49);
1057 MAKE_PIN(P50, 50);
1058 MAKE_PIN(P51, 51);
1059 MAKE_PIN(P52, 52);
1060 MAKE_PIN(P53, 53);
1061 MAKE_PIN(P54, 54);
1062 #endif
1063 
1064 #undef MAKE_PIN
1065 
1066 #elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
1067 
1068 // SetDirRead:
1069 // Disable interrupts
1070 // Disable the pull up resistor
1071 // Set to INPUT
1072 // Enable PIO
1073 
1074 // SetDirWrite:
1075 // Disable interrupts
1076 // Disable the pull up resistor
1077 // Set to OUTPUT
1078 // Enable PIO
1079 
1080 #define MAKE_PIN(className, pio, pinMask) \
1081 class className { \
1082 public: \
1083  static void Set() { \
1084  pio->PIO_SODR = pinMask; \
1085  } \
1086  static void Clear() { \
1087  pio->PIO_CODR = pinMask; \
1088  } \
1089  static void SetDirRead() { \
1090  pio->PIO_IDR = pinMask ; \
1091  pio->PIO_PUDR = pinMask; \
1092  pio->PIO_ODR = pinMask; \
1093  pio->PIO_PER = pinMask; \
1094  } \
1095  static void SetDirWrite() { \
1096  pio->PIO_IDR = pinMask ; \
1097  pio->PIO_PUDR = pinMask; \
1098  pio->PIO_OER = pinMask; \
1099  pio->PIO_PER = pinMask; \
1100  } \
1101  static uint8_t IsSet() { \
1102  return pio->PIO_PDSR & pinMask; \
1103  } \
1104 };
1105 
1106 // See: http://arduino.cc/en/Hacking/PinMappingSAM3X and variant.cpp
1107 
1108 MAKE_PIN(P0, PIOA, PIO_PA8);
1109 MAKE_PIN(P1, PIOA, PIO_PA9);
1110 MAKE_PIN(P2, PIOB, PIO_PB25);
1111 MAKE_PIN(P3, PIOC, PIO_PC28);
1112 MAKE_PIN(P4, PIOC, PIO_PC26);
1113 MAKE_PIN(P5, PIOC, PIO_PC25);
1114 MAKE_PIN(P6, PIOC, PIO_PC24);
1115 MAKE_PIN(P7, PIOC, PIO_PC23);
1116 MAKE_PIN(P8, PIOC, PIO_PC22);
1117 MAKE_PIN(P9, PIOC, PIO_PC21);
1118 MAKE_PIN(P10, PIOC, PIO_PC29);
1119 MAKE_PIN(P11, PIOD, PIO_PD7);
1120 MAKE_PIN(P12, PIOD, PIO_PD8);
1121 MAKE_PIN(P13, PIOB, PIO_PB27);
1122 MAKE_PIN(P14, PIOD, PIO_PD4);
1123 MAKE_PIN(P15, PIOD, PIO_PD5);
1124 MAKE_PIN(P16, PIOA, PIO_PA13);
1125 MAKE_PIN(P17, PIOA, PIO_PA12);
1126 MAKE_PIN(P18, PIOA, PIO_PA11);
1127 MAKE_PIN(P19, PIOA, PIO_PA10);
1128 MAKE_PIN(P20, PIOB, PIO_PB12);
1129 MAKE_PIN(P21, PIOB, PIO_PB13);
1130 MAKE_PIN(P22, PIOB, PIO_PB26);
1131 MAKE_PIN(P23, PIOA, PIO_PA14);
1132 MAKE_PIN(P24, PIOA, PIO_PA15);
1133 MAKE_PIN(P25, PIOD, PIO_PD0);
1134 MAKE_PIN(P26, PIOD, PIO_PD1);
1135 MAKE_PIN(P27, PIOD, PIO_PD2);
1136 MAKE_PIN(P28, PIOD, PIO_PD3);
1137 MAKE_PIN(P29, PIOD, PIO_PD6);
1138 MAKE_PIN(P30, PIOD, PIO_PD9);
1139 MAKE_PIN(P31, PIOA, PIO_PA7);
1140 MAKE_PIN(P32, PIOD, PIO_PD10);
1141 MAKE_PIN(P33, PIOC, PIO_PC1);
1142 MAKE_PIN(P34, PIOC, PIO_PC2);
1143 MAKE_PIN(P35, PIOC, PIO_PC3);
1144 MAKE_PIN(P36, PIOC, PIO_PC4);
1145 MAKE_PIN(P37, PIOC, PIO_PC5);
1146 MAKE_PIN(P38, PIOC, PIO_PC6);
1147 MAKE_PIN(P39, PIOC, PIO_PC7);
1148 MAKE_PIN(P40, PIOC, PIO_PC8);
1149 MAKE_PIN(P41, PIOC, PIO_PC9);
1150 MAKE_PIN(P42, PIOA, PIO_PA19);
1151 MAKE_PIN(P43, PIOA, PIO_PA20);
1152 MAKE_PIN(P44, PIOC, PIO_PC19);
1153 MAKE_PIN(P45, PIOC, PIO_PC18);
1154 MAKE_PIN(P46, PIOC, PIO_PC17);
1155 MAKE_PIN(P47, PIOC, PIO_PC16);
1156 MAKE_PIN(P48, PIOC, PIO_PC15);
1157 MAKE_PIN(P49, PIOC, PIO_PC14);
1158 MAKE_PIN(P50, PIOC, PIO_PC13);
1159 MAKE_PIN(P51, PIOC, PIO_PC12);
1160 MAKE_PIN(P52, PIOB, PIO_PB21);
1161 MAKE_PIN(P53, PIOB, PIO_PB14);
1162 MAKE_PIN(P54, PIOA, PIO_PA16);
1163 MAKE_PIN(P55, PIOA, PIO_PA24);
1164 MAKE_PIN(P56, PIOA, PIO_PA23);
1165 MAKE_PIN(P57, PIOA, PIO_PA22);
1166 MAKE_PIN(P58, PIOA, PIO_PA6);
1167 MAKE_PIN(P59, PIOA, PIO_PA4);
1168 MAKE_PIN(P60, PIOA, PIO_PA3);
1169 MAKE_PIN(P61, PIOA, PIO_PA2);
1170 MAKE_PIN(P62, PIOB, PIO_PB17);
1171 MAKE_PIN(P63, PIOB, PIO_PB18);
1172 MAKE_PIN(P64, PIOB, PIO_PB19);
1173 MAKE_PIN(P65, PIOB, PIO_PB20);
1174 MAKE_PIN(P66, PIOB, PIO_PB15);
1175 MAKE_PIN(P67, PIOB, PIO_PB16);
1176 MAKE_PIN(P68, PIOA, PIO_PA1);
1177 MAKE_PIN(P69, PIOA, PIO_PA0);
1178 MAKE_PIN(P70, PIOA, PIO_PA17);
1179 MAKE_PIN(P71, PIOA, PIO_PA18);
1180 MAKE_PIN(P72, PIOC, PIO_PC30);
1181 MAKE_PIN(P73, PIOA, PIO_PA21);
1182 MAKE_PIN(P74, PIOA, PIO_PA25); // MISO
1183 MAKE_PIN(P75, PIOA, PIO_PA26); // MOSI
1184 MAKE_PIN(P76, PIOA, PIO_PA27); // CLK
1185 MAKE_PIN(P77, PIOA, PIO_PA28);
1186 MAKE_PIN(P78, PIOB, PIO_PB23); // Unconnected
1187 
1188 #undef MAKE_PIN
1189 
1190 #elif defined(RBL_NRF51822)
1191 
1192 #define MAKE_PIN(className, pin) \
1193 class className { \
1194 public: \
1195  static void Set() { \
1196  nrf_gpio_pin_set(pin); \
1197  } \
1198  static void Clear() { \
1199  nrf_gpio_pin_clear(pin); \
1200  } \
1201  static void SetDirRead() { \
1202  nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL); \
1203  } \
1204  static void SetDirWrite() { \
1205  nrf_gpio_cfg_output(pin); \
1206  } \
1207  static uint8_t IsSet() { \
1208  return (uint8_t)nrf_gpio_pin_read(pin); \
1209  } \
1210 };
1211 
1212 // See: pin_transform.c in RBL nRF51822 SDK
1213 MAKE_PIN(P0, Pin_nRF51822_to_Arduino(D0));
1214 MAKE_PIN(P1, Pin_nRF51822_to_Arduino(D1));
1215 MAKE_PIN(P2, Pin_nRF51822_to_Arduino(D2));
1216 MAKE_PIN(P3, Pin_nRF51822_to_Arduino(D3));
1217 MAKE_PIN(P4, Pin_nRF51822_to_Arduino(D4));
1218 MAKE_PIN(P5, Pin_nRF51822_to_Arduino(D5));
1219 MAKE_PIN(P6, Pin_nRF51822_to_Arduino(D6));
1220 MAKE_PIN(P7, Pin_nRF51822_to_Arduino(D7));
1221 MAKE_PIN(P8, Pin_nRF51822_to_Arduino(D8));
1222 MAKE_PIN(P9, Pin_nRF51822_to_Arduino(D9)); // INT
1223 MAKE_PIN(P10, Pin_nRF51822_to_Arduino(D10)); // SS
1224 MAKE_PIN(P11, Pin_nRF51822_to_Arduino(D11));
1225 MAKE_PIN(P12, Pin_nRF51822_to_Arduino(D12));
1226 MAKE_PIN(P13, Pin_nRF51822_to_Arduino(D13));
1227 MAKE_PIN(P14, Pin_nRF51822_to_Arduino(D14));
1228 MAKE_PIN(P15, Pin_nRF51822_to_Arduino(D15));
1229 MAKE_PIN(P17, Pin_nRF51822_to_Arduino(D17)); // MISO
1230 MAKE_PIN(P18, Pin_nRF51822_to_Arduino(D18)); // MOSI
1231 MAKE_PIN(P16, Pin_nRF51822_to_Arduino(D16)); // CLK
1232 MAKE_PIN(P19, Pin_nRF51822_to_Arduino(D19));
1233 MAKE_PIN(P20, Pin_nRF51822_to_Arduino(D20));
1234 MAKE_PIN(P21, Pin_nRF51822_to_Arduino(D21));
1235 MAKE_PIN(P22, Pin_nRF51822_to_Arduino(D22));
1236 MAKE_PIN(P23, Pin_nRF51822_to_Arduino(D23));
1237 MAKE_PIN(P24, Pin_nRF51822_to_Arduino(D24));
1238 
1239 #undef MAKE_PIN
1240 
1241 #elif defined(STM32F446xx)
1242 // NUCLEO-F446RE
1243 
1244 #define MAKE_PIN(className, port, pin) \
1245 class className { \
1246 public: \
1247  static void Set() { \
1248  HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET); \
1249  } \
1250  static void Clear() { \
1251  HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET); \
1252  } \
1253  static void SetDirRead() { \
1254  static GPIO_InitTypeDef GPIO_InitStruct; \
1255  GPIO_InitStruct.Pin = pin; \
1256  GPIO_InitStruct.Mode = GPIO_MODE_INPUT; \
1257  GPIO_InitStruct.Pull = GPIO_NOPULL; \
1258  HAL_GPIO_Init(port, &GPIO_InitStruct); \
1259  } \
1260  static void SetDirWrite() { \
1261  static GPIO_InitTypeDef GPIO_InitStruct; \
1262  GPIO_InitStruct.Pin = pin; \
1263  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; \
1264  GPIO_InitStruct.Pull = GPIO_NOPULL; \
1265  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; \
1266  HAL_GPIO_Init(port, &GPIO_InitStruct); \
1267  } \
1268  static GPIO_PinState IsSet() { \
1269  return HAL_GPIO_ReadPin(port, pin); \
1270  } \
1271 };
1272 
1273 MAKE_PIN(P0, GPIOA, GPIO_PIN_3); // D0
1274 MAKE_PIN(P1, GPIOA, GPIO_PIN_2); // D1
1275 MAKE_PIN(P2, GPIOA, GPIO_PIN_10); // D2
1276 MAKE_PIN(P3, GPIOB, GPIO_PIN_3); // D3
1277 MAKE_PIN(P4, GPIOB, GPIO_PIN_5); // D4
1278 MAKE_PIN(P5, GPIOB, GPIO_PIN_4); // D5
1279 MAKE_PIN(P6, GPIOB, GPIO_PIN_10); // D6
1280 MAKE_PIN(P7, GPIOA, GPIO_PIN_8); // D7
1281 MAKE_PIN(P8, GPIOA, GPIO_PIN_9); // D8
1282 MAKE_PIN(P9, GPIOC, GPIO_PIN_7); // D9
1283 MAKE_PIN(P10, GPIOB, GPIO_PIN_6); // D10
1284 MAKE_PIN(P11, GPIOA, GPIO_PIN_7); // D11
1285 MAKE_PIN(P12, GPIOA, GPIO_PIN_6); // D12
1286 MAKE_PIN(P13, GPIOA, GPIO_PIN_5); // D13
1287 
1288 MAKE_PIN(P14, GPIOA, GPIO_PIN_0); // A0
1289 MAKE_PIN(P15, GPIOA, GPIO_PIN_1); // A1
1290 MAKE_PIN(P16, GPIOA, GPIO_PIN_4); // A2
1291 MAKE_PIN(P17, GPIOB, GPIO_PIN_0); // A3
1292 MAKE_PIN(P18, GPIOC, GPIO_PIN_1); // A4
1293 MAKE_PIN(P19, GPIOC, GPIO_PIN_0); // A5
1294 
1295 #undef MAKE_PIN
1296 
1297 #else
1298 #error "Please define board in avrpins.h"
1299 
1300 #endif
1301 
1302 #elif defined(__ARDUINO_ARC__)
1303 
1304 #include <avr/pgmspace.h>
1305 // Pointers are 32 bits on arc
1306 #define pgm_read_pointer(p) pgm_read_dword(p)
1307 
1308 #define MAKE_PIN(className, pin) \
1309 class className { \
1310 public: \
1311  static void Set() { \
1312  digitalWrite(pin, HIGH);\
1313  } \
1314  static void Clear() { \
1315  digitalWrite(pin, LOW); \
1316  } \
1317  static void SetDirRead() { \
1318  pinMode(pin, INPUT); \
1319  } \
1320  static void SetDirWrite() { \
1321  pinMode(pin, OUTPUT); \
1322  } \
1323  static uint8_t IsSet() { \
1324  return digitalRead(pin); \
1325  } \
1326 };
1327 
1328 MAKE_PIN(P0, 0);
1329 MAKE_PIN(P1, 1);
1330 MAKE_PIN(P2, 2);
1331 MAKE_PIN(P3, 3); //PWM
1332 MAKE_PIN(P4, 4);
1333 MAKE_PIN(P5, 5); //PWM
1334 MAKE_PIN(P6, 6); //PWM
1335 MAKE_PIN(P7, 7);
1336 MAKE_PIN(P8, 8);
1337 MAKE_PIN(P9, 9); //PWM
1338 
1339 MAKE_PIN(P10, 10); //SPI SS
1340 MAKE_PIN(P11, 11); //SPI MOSI
1341 MAKE_PIN(P12, 12); //SPI MISO
1342 MAKE_PIN(P13, 13); //SPI SCK / BUILTIN LED
1343 
1344 MAKE_PIN(P14, 14); // A0
1345 MAKE_PIN(P15, 15); // A1
1346 MAKE_PIN(P16, 16); // A2
1347 MAKE_PIN(P17, 17); // A3
1348 MAKE_PIN(P18, 18); // A4 SDA
1349 MAKE_PIN(P19, 19); // A5 SCL
1350 MAKE_PIN(P20, 20); // ATN
1351 
1352 #undef MAKE_PIN
1353 
1354 #elif defined(__ARDUINO_X86__) // Intel Galileo, Intel Galileo 2 and Intel Edison
1355 
1356 #include <avr/pgmspace.h>
1357 
1358 // Pointers are 32 bits on x86
1359 #define pgm_read_pointer(p) pgm_read_dword(p)
1360 
1361 #if PLATFORM_ID == 0xE1 // Edison platform id
1362 #define pinToFastPin(pin) 1 // As far as I can tell all pins can be used as fast pins
1363 #endif
1364 
1365 // Pin 2 and 3 on the Intel Galileo supports a higher rate,
1366 // so it is recommended to use one of these as the SS pin.
1367 
1368 #define MAKE_PIN(className, pin) \
1369 class className { \
1370 public: \
1371  static void Set() { \
1372  fastDigitalWrite(pin, HIGH); \
1373  } \
1374  static void Clear() { \
1375  fastDigitalWrite(pin, LOW); \
1376  } \
1377  static void SetDirRead() { \
1378  if (pinToFastPin(pin)) \
1379  pinMode(pin, INPUT_FAST); \
1380  else \
1381  pinMode(pin, INPUT); \
1382  } \
1383  static void SetDirWrite() { \
1384  if (pinToFastPin(pin)) \
1385  pinMode(pin, OUTPUT_FAST); \
1386  else \
1387  pinMode(pin, OUTPUT); \
1388  } \
1389  static uint8_t IsSet() { \
1390  return fastDigitalRead(pin); \
1391  } \
1392 };
1393 
1394 MAKE_PIN(P0, 0);
1395 MAKE_PIN(P1, 1);
1396 MAKE_PIN(P2, 2);
1397 MAKE_PIN(P3, 3);
1398 MAKE_PIN(P4, 4);
1399 MAKE_PIN(P5, 5);
1400 MAKE_PIN(P6, 6);
1401 MAKE_PIN(P7, 7);
1402 MAKE_PIN(P8, 8);
1403 MAKE_PIN(P9, 9);
1404 MAKE_PIN(P10, 10);
1405 MAKE_PIN(P11, 11);
1406 MAKE_PIN(P12, 12);
1407 MAKE_PIN(P13, 13);
1408 MAKE_PIN(P14, 14); // A0
1409 MAKE_PIN(P15, 15); // A1
1410 MAKE_PIN(P16, 16); // A2
1411 MAKE_PIN(P17, 17); // A3
1412 MAKE_PIN(P18, 18); // A4
1413 MAKE_PIN(P19, 19); // A5
1414 
1415 #undef MAKE_PIN
1416 
1417 #elif defined(__MIPSEL__)
1418 // MIPSEL (MIPS architecture using a little endian byte order)
1419 
1420 // MIPS size_t = 4
1421 #define pgm_read_pointer(p) pgm_read_dword(p)
1422 
1423 #define MAKE_PIN(className, pin) \
1424 class className { \
1425 public: \
1426  static void Set() { \
1427  digitalWrite(pin, HIGH);\
1428  } \
1429  static void Clear() { \
1430  digitalWrite(pin, LOW); \
1431  } \
1432  static void SetDirRead() { \
1433  pinMode(pin, INPUT); \
1434  } \
1435  static void SetDirWrite() { \
1436  pinMode(pin, OUTPUT); \
1437  } \
1438  static uint8_t IsSet() { \
1439  return digitalRead(pin); \
1440  } \
1441 };
1442 
1443 // 0 .. 13 - Digital pins
1444 MAKE_PIN(P0, 0); // RX
1445 MAKE_PIN(P1, 1); // TX
1446 MAKE_PIN(P2, 2); //
1447 MAKE_PIN(P3, 3); //
1448 MAKE_PIN(P4, 4); //
1449 MAKE_PIN(P5, 5); //
1450 MAKE_PIN(P6, 6); //
1451 MAKE_PIN(P7, 7); //
1452 MAKE_PIN(P8, 8); //
1453 MAKE_PIN(P9, 9); //
1454 MAKE_PIN(P10, 10); //
1455 MAKE_PIN(P11, 11); //
1456 MAKE_PIN(P12, 12); //
1457 MAKE_PIN(P13, 13); //
1458 
1459 #undef MAKE_PIN
1460 
1461 #elif defined(ESP8266) || defined(ESP32)
1462 
1463 #define MAKE_PIN(className, pin) \
1464 class className { \
1465 public: \
1466  static void Set() { \
1467  digitalWrite(pin, HIGH);\
1468  } \
1469  static void Clear() { \
1470  digitalWrite(pin, LOW); \
1471  } \
1472  static void SetDirRead() { \
1473  pinMode(pin, INPUT); \
1474  } \
1475  static void SetDirWrite() { \
1476  pinMode(pin, OUTPUT); \
1477  } \
1478  static uint8_t IsSet() { \
1479  return digitalRead(pin); \
1480  } \
1481 };
1482 
1483 #if defined(ESP8266)
1484 
1485 // Workaround the following issue: https://github.com/esp8266/Arduino/pull/5735
1486 #undef pgm_read_ptr_aligned
1487 #ifdef __cplusplus
1488 #define pgm_read_ptr_aligned(addr) (*reinterpret_cast<const void* const*>(addr))
1489 #else
1490 #define pgm_read_ptr_aligned(addr) (*(const void* const*)(addr))
1491 #endif
1492 
1493 #undef pgm_read_ptr
1494 #if PGM_READ_UNALIGNED
1495 #define pgm_read_ptr(p) pgm_read_ptr_unaligned(p)
1496 #else
1497 #define pgm_read_ptr(p) pgm_read_ptr_aligned(p)
1498 #endif
1499 
1500 #ifdef pgm_read_pointer
1501 #undef pgm_read_pointer
1502 #endif
1503 #define pgm_read_pointer(p) pgm_read_ptr(p)
1504 
1505 // Pinout for ESP-12 module
1506 // 0 .. 16 - Digital pins
1507 // GPIO 6 to 11 and 16 are not usable in this library.
1508 
1509 MAKE_PIN(P0, 0);
1510 MAKE_PIN(P1, 1); // TX0
1511 MAKE_PIN(P2, 2); // TX1
1512 MAKE_PIN(P3, 3); // RX0
1513 MAKE_PIN(P4, 4); // SDA
1514 MAKE_PIN(P5, 5); // SCL
1515 MAKE_PIN(P12, 12); // MISO
1516 MAKE_PIN(P13, 13); // MOSI
1517 MAKE_PIN(P14, 14); // SCK
1518 MAKE_PIN(P15, 15); // SS
1519 
1520 #elif defined(ESP32)
1521 
1522 // Workaround strict-aliasing warnings
1523 #ifdef pgm_read_word
1524 #undef pgm_read_word
1525 #endif
1526 #ifdef pgm_read_dword
1527 #undef pgm_read_dword
1528 #endif
1529 #ifdef pgm_read_float
1530 #undef pgm_read_float
1531 #endif
1532 #ifdef pgm_read_ptr
1533 #undef pgm_read_ptr
1534 #endif
1535 
1536 #define pgm_read_word(addr) ({ \
1537  typeof(addr) _addr = (addr); \
1538  *(const unsigned short *)(_addr); \
1539 })
1540 #define pgm_read_dword(addr) ({ \
1541  typeof(addr) _addr = (addr); \
1542  *(const unsigned long *)(_addr); \
1543 })
1544 #define pgm_read_float(addr) ({ \
1545  typeof(addr) _addr = (addr); \
1546  *(const float *)(_addr); \
1547 })
1548 #define pgm_read_ptr(addr) ({ \
1549  typeof(addr) _addr = (addr); \
1550  *(void * const *)(_addr); \
1551 })
1552 
1553 // Pinout for ESP32 dev module
1554 
1555 MAKE_PIN(P0, 0);
1556 MAKE_PIN(P1, 1); // TX0
1557 MAKE_PIN(P10, 10); // TX1
1558 MAKE_PIN(P3, 3); // RX0
1559 MAKE_PIN(P21, 21); // SDA
1560 MAKE_PIN(P22, 22); // SCL
1561 MAKE_PIN(P19, 19); // MISO
1562 MAKE_PIN(P23, 23); // MOSI
1563 MAKE_PIN(P18, 18); // SCK
1564 MAKE_PIN(P5, 5); // SS
1565 MAKE_PIN(P17, 17); // INT
1566 
1567 #endif
1568 
1569 #undef MAKE_PIN
1570 
1571 // pgm_read_ptr is not defined in the ESP32, so we have to undef the diffinition from version_helper.h
1572 #ifdef pgm_read_pointer
1573 #undef pgm_read_pointer
1574 #endif
1575 #define pgm_read_pointer(p) pgm_read_ptr(p)
1576 
1577 #else
1578 #error "Please define board in avrpins.h"
1579 
1580 #endif
1581 
1582 #endif //_avrpins_h_