USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
818 #include "core_pins.h"
819 #include "avr_emulation.h"
820 
821 #define GPIO_BITBAND_ADDR(reg, bit) (((uint32_t)&(reg) - 0x40000000) * 32 + (bit) * 4 + 0x42000000)
822 #define GPIO_BITBAND_PTR(reg, bit) ((uint8_t *)GPIO_BITBAND_ADDR((reg), (bit)))
823 
824 #define MAKE_PIN(className, baseReg, pinNum, configReg) \
825 class className { \
826 public: \
827  static void Set() { \
828  *GPIO_BITBAND_PTR(baseReg, pinNum) = 1; \
829  } \
830  static void Clear() { \
831  *GPIO_BITBAND_PTR(baseReg, pinNum) = 0; \
832  } \
833  static void SetDirRead() { \
834  configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \
835  *(GPIO_BITBAND_PTR(baseReg, pinNum) + 640) = 0; \
836  } \
837  static void SetDirWrite() { \
838  configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \
839  *(GPIO_BITBAND_PTR(baseReg, pinNum) + 640) = 1; \
840  } \
841  static uint8_t IsSet() { \
842  return *(GPIO_BITBAND_PTR(baseReg, pinNum) + 512); \
843  } \
844 };
845 
846 MAKE_PIN(P0, CORE_PIN0_PORTREG, CORE_PIN0_BIT, CORE_PIN0_CONFIG);
847 MAKE_PIN(P1, CORE_PIN1_PORTREG, CORE_PIN1_BIT, CORE_PIN1_CONFIG);
848 MAKE_PIN(P2, CORE_PIN2_PORTREG, CORE_PIN2_BIT, CORE_PIN2_CONFIG);
849 MAKE_PIN(P3, CORE_PIN3_PORTREG, CORE_PIN3_BIT, CORE_PIN3_CONFIG);
850 MAKE_PIN(P4, CORE_PIN4_PORTREG, CORE_PIN4_BIT, CORE_PIN4_CONFIG);
851 MAKE_PIN(P5, CORE_PIN5_PORTREG, CORE_PIN5_BIT, CORE_PIN5_CONFIG);
852 MAKE_PIN(P6, CORE_PIN6_PORTREG, CORE_PIN6_BIT, CORE_PIN6_CONFIG);
853 MAKE_PIN(P7, CORE_PIN7_PORTREG, CORE_PIN7_BIT, CORE_PIN7_CONFIG);
854 MAKE_PIN(P8, CORE_PIN8_PORTREG, CORE_PIN8_BIT, CORE_PIN8_CONFIG);
855 MAKE_PIN(P9, CORE_PIN9_PORTREG, CORE_PIN9_BIT, CORE_PIN9_CONFIG);
856 MAKE_PIN(P10, CORE_PIN10_PORTREG, CORE_PIN10_BIT, CORE_PIN10_CONFIG);
857 MAKE_PIN(P11, CORE_PIN11_PORTREG, CORE_PIN11_BIT, CORE_PIN11_CONFIG);
858 MAKE_PIN(P12, CORE_PIN12_PORTREG, CORE_PIN12_BIT, CORE_PIN12_CONFIG);
859 MAKE_PIN(P13, CORE_PIN13_PORTREG, CORE_PIN13_BIT, CORE_PIN13_CONFIG);
860 MAKE_PIN(P14, CORE_PIN14_PORTREG, CORE_PIN14_BIT, CORE_PIN14_CONFIG);
861 MAKE_PIN(P15, CORE_PIN15_PORTREG, CORE_PIN15_BIT, CORE_PIN15_CONFIG);
862 MAKE_PIN(P16, CORE_PIN16_PORTREG, CORE_PIN16_BIT, CORE_PIN16_CONFIG);
863 MAKE_PIN(P17, CORE_PIN17_PORTREG, CORE_PIN17_BIT, CORE_PIN17_CONFIG);
864 MAKE_PIN(P18, CORE_PIN18_PORTREG, CORE_PIN18_BIT, CORE_PIN18_CONFIG);
865 MAKE_PIN(P19, CORE_PIN19_PORTREG, CORE_PIN19_BIT, CORE_PIN19_CONFIG);
866 MAKE_PIN(P20, CORE_PIN20_PORTREG, CORE_PIN20_BIT, CORE_PIN20_CONFIG);
867 MAKE_PIN(P21, CORE_PIN21_PORTREG, CORE_PIN21_BIT, CORE_PIN21_CONFIG);
868 MAKE_PIN(P22, CORE_PIN22_PORTREG, CORE_PIN22_BIT, CORE_PIN22_CONFIG);
869 MAKE_PIN(P23, CORE_PIN23_PORTREG, CORE_PIN23_BIT, CORE_PIN23_CONFIG);
870 MAKE_PIN(P24, CORE_PIN24_PORTREG, CORE_PIN24_BIT, CORE_PIN24_CONFIG);
871 MAKE_PIN(P25, CORE_PIN25_PORTREG, CORE_PIN25_BIT, CORE_PIN25_CONFIG);
872 MAKE_PIN(P26, CORE_PIN26_PORTREG, CORE_PIN26_BIT, CORE_PIN26_CONFIG);
873 MAKE_PIN(P27, CORE_PIN27_PORTREG, CORE_PIN27_BIT, CORE_PIN27_CONFIG);
874 MAKE_PIN(P28, CORE_PIN28_PORTREG, CORE_PIN28_BIT, CORE_PIN28_CONFIG);
875 MAKE_PIN(P29, CORE_PIN29_PORTREG, CORE_PIN29_BIT, CORE_PIN29_CONFIG);
876 MAKE_PIN(P30, CORE_PIN30_PORTREG, CORE_PIN30_BIT, CORE_PIN30_CONFIG);
877 MAKE_PIN(P31, CORE_PIN31_PORTREG, CORE_PIN31_BIT, CORE_PIN31_CONFIG);
878 MAKE_PIN(P32, CORE_PIN32_PORTREG, CORE_PIN32_BIT, CORE_PIN32_CONFIG);
879 MAKE_PIN(P33, CORE_PIN33_PORTREG, CORE_PIN33_BIT, CORE_PIN33_CONFIG);
880 #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
881 MAKE_PIN(P34, CORE_PIN34_PORTREG, CORE_PIN34_BIT, CORE_PIN34_CONFIG);
882 MAKE_PIN(P35, CORE_PIN35_PORTREG, CORE_PIN35_BIT, CORE_PIN35_CONFIG);
883 MAKE_PIN(P36, CORE_PIN36_PORTREG, CORE_PIN36_BIT, CORE_PIN36_CONFIG);
884 MAKE_PIN(P37, CORE_PIN37_PORTREG, CORE_PIN37_BIT, CORE_PIN37_CONFIG);
885 MAKE_PIN(P38, CORE_PIN38_PORTREG, CORE_PIN38_BIT, CORE_PIN38_CONFIG);
886 MAKE_PIN(P39, CORE_PIN39_PORTREG, CORE_PIN39_BIT, CORE_PIN39_CONFIG);
887 MAKE_PIN(P40, CORE_PIN40_PORTREG, CORE_PIN40_BIT, CORE_PIN40_CONFIG);
888 MAKE_PIN(P41, CORE_PIN41_PORTREG, CORE_PIN41_BIT, CORE_PIN41_CONFIG);
889 MAKE_PIN(P42, CORE_PIN42_PORTREG, CORE_PIN42_BIT, CORE_PIN42_CONFIG);
890 MAKE_PIN(P43, CORE_PIN43_PORTREG, CORE_PIN43_BIT, CORE_PIN43_CONFIG);
891 MAKE_PIN(P44, CORE_PIN44_PORTREG, CORE_PIN44_BIT, CORE_PIN44_CONFIG);
892 MAKE_PIN(P45, CORE_PIN45_PORTREG, CORE_PIN45_BIT, CORE_PIN45_CONFIG);
893 MAKE_PIN(P46, CORE_PIN46_PORTREG, CORE_PIN46_BIT, CORE_PIN46_CONFIG);
894 MAKE_PIN(P47, CORE_PIN47_PORTREG, CORE_PIN47_BIT, CORE_PIN47_CONFIG);
895 MAKE_PIN(P48, CORE_PIN48_PORTREG, CORE_PIN48_BIT, CORE_PIN48_CONFIG);
896 MAKE_PIN(P49, CORE_PIN49_PORTREG, CORE_PIN49_BIT, CORE_PIN49_CONFIG);
897 MAKE_PIN(P50, CORE_PIN50_PORTREG, CORE_PIN50_BIT, CORE_PIN50_CONFIG);
898 MAKE_PIN(P51, CORE_PIN51_PORTREG, CORE_PIN51_BIT, CORE_PIN51_CONFIG);
899 MAKE_PIN(P52, CORE_PIN52_PORTREG, CORE_PIN52_BIT, CORE_PIN52_CONFIG);
900 MAKE_PIN(P53, CORE_PIN53_PORTREG, CORE_PIN53_BIT, CORE_PIN53_CONFIG);
901 MAKE_PIN(P54, CORE_PIN54_PORTREG, CORE_PIN54_BIT, CORE_PIN54_CONFIG);
902 MAKE_PIN(P55, CORE_PIN55_PORTREG, CORE_PIN55_BIT, CORE_PIN55_CONFIG);
903 MAKE_PIN(P56, CORE_PIN56_PORTREG, CORE_PIN56_BIT, CORE_PIN56_CONFIG);
904 MAKE_PIN(P57, CORE_PIN57_PORTREG, CORE_PIN57_BIT, CORE_PIN57_CONFIG);
905 MAKE_PIN(P58, CORE_PIN58_PORTREG, CORE_PIN58_BIT, CORE_PIN58_CONFIG);
906 MAKE_PIN(P59, CORE_PIN59_PORTREG, CORE_PIN59_BIT, CORE_PIN59_CONFIG);
907 MAKE_PIN(P60, CORE_PIN60_PORTREG, CORE_PIN60_BIT, CORE_PIN60_CONFIG);
908 MAKE_PIN(P61, CORE_PIN61_PORTREG, CORE_PIN61_BIT, CORE_PIN61_CONFIG);
909 MAKE_PIN(P62, CORE_PIN62_PORTREG, CORE_PIN62_BIT, CORE_PIN62_CONFIG);
910 MAKE_PIN(P63, CORE_PIN63_PORTREG, CORE_PIN63_BIT, CORE_PIN63_CONFIG);
911 #endif
912 
913 #undef MAKE_PIN
914 
915 #elif defined(CORE_TEENSY) && (defined(__MKL26Z64__))
916 
917 // we could get lower level by making these macros work properly.
918 // for now just use the semi optimised version, it costs a lookup in the pin pgm table per op
919 // but for now it will do.
920 //#define GPIO_BITBAND_ADDR(reg, bit) (((volatile uint8_t *)&(reg) + ((bit) >> 3)))
921 //#define GPIO_BITBAND_MASK(reg, bit) (1<<((bit) & 7))
922 //#define GPIO_BITBAND_PTR(reg, bit) ((volatile uint8_t *)GPIO_BITBAND_ADDR((reg), (bit)))
923 
924 #include "core_pins.h"
925 #include "avr_emulation.h"
926 
927 #define MAKE_PIN(className, baseReg, pinNum, configReg) \
928 class className { \
929 public: \
930  static void Set() { \
931  *portSetRegister(pinNum) = digitalPinToBitMask(pinNum); \
932  } \
933  static void Clear() { \
934  *portClearRegister(pinNum) = digitalPinToBitMask(pinNum); \
935  } \
936  static void SetDirRead() { \
937  configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \
938  *portModeRegister(pinNum) &= ~digitalPinToBitMask(pinNum); \
939  } \
940  static void SetDirWrite() { \
941  configReg = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); \
942  *portModeRegister(pinNum) |= digitalPinToBitMask(pinNum); \
943  } \
944  static uint8_t IsSet() { \
945  return (*portInputRegister(pinNum) & digitalPinToBitMask(pinNum)) ? 1 : 0; \
946  } \
947 };
948 
949 MAKE_PIN(P0, CORE_PIN0_PORTREG, 0, CORE_PIN0_CONFIG);
950 MAKE_PIN(P1, CORE_PIN1_PORTREG, 1, CORE_PIN1_CONFIG);
951 MAKE_PIN(P2, CORE_PIN2_PORTREG, 2, CORE_PIN2_CONFIG);
952 MAKE_PIN(P3, CORE_PIN3_PORTREG, 3, CORE_PIN3_CONFIG);
953 MAKE_PIN(P4, CORE_PIN4_PORTREG, 4, CORE_PIN4_CONFIG);
954 MAKE_PIN(P5, CORE_PIN5_PORTREG, 5, CORE_PIN5_CONFIG);
955 MAKE_PIN(P6, CORE_PIN6_PORTREG, 6, CORE_PIN6_CONFIG);
956 MAKE_PIN(P7, CORE_PIN7_PORTREG, 7, CORE_PIN7_CONFIG);
957 MAKE_PIN(P8, CORE_PIN8_PORTREG, 8, CORE_PIN8_CONFIG);
958 MAKE_PIN(P9, CORE_PIN9_PORTREG, 9, CORE_PIN9_CONFIG);
959 MAKE_PIN(P10, CORE_PIN10_PORTREG, 10, CORE_PIN10_CONFIG);
960 MAKE_PIN(P11, CORE_PIN11_PORTREG, 11, CORE_PIN11_CONFIG);
961 MAKE_PIN(P12, CORE_PIN12_PORTREG, 12, CORE_PIN12_CONFIG);
962 MAKE_PIN(P13, CORE_PIN13_PORTREG, 13, CORE_PIN13_CONFIG);
963 MAKE_PIN(P14, CORE_PIN14_PORTREG, 14, CORE_PIN14_CONFIG);
964 MAKE_PIN(P15, CORE_PIN15_PORTREG, 15, CORE_PIN15_CONFIG);
965 MAKE_PIN(P16, CORE_PIN16_PORTREG, 16, CORE_PIN16_CONFIG);
966 MAKE_PIN(P17, CORE_PIN17_PORTREG, 17, CORE_PIN17_CONFIG);
967 MAKE_PIN(P18, CORE_PIN18_PORTREG, 18, CORE_PIN18_CONFIG);
968 MAKE_PIN(P19, CORE_PIN19_PORTREG, 19, CORE_PIN19_CONFIG);
969 MAKE_PIN(P20, CORE_PIN20_PORTREG, 20, CORE_PIN20_CONFIG);
970 MAKE_PIN(P21, CORE_PIN21_PORTREG, 21, CORE_PIN21_CONFIG);
971 MAKE_PIN(P22, CORE_PIN22_PORTREG, 22, CORE_PIN22_CONFIG);
972 MAKE_PIN(P23, CORE_PIN23_PORTREG, 23, CORE_PIN23_CONFIG);
973 MAKE_PIN(P24, CORE_PIN24_PORTREG, 24, CORE_PIN24_CONFIG);
974 MAKE_PIN(P25, CORE_PIN25_PORTREG, 25, CORE_PIN25_CONFIG);
975 MAKE_PIN(P26, CORE_PIN26_PORTREG, 26, CORE_PIN26_CONFIG);
976 
977 #undef MAKE_PIN
978 
979 #elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
980 
981 // SetDirRead:
982 // Disable interrupts
983 // Disable the pull up resistor
984 // Set to INPUT
985 // Enable PIO
986 
987 // SetDirWrite:
988 // Disable interrupts
989 // Disable the pull up resistor
990 // Set to OUTPUT
991 // Enable PIO
992 
993 #define MAKE_PIN(className, pio, pinMask) \
994 class className { \
995 public: \
996  static void Set() { \
997  pio->PIO_SODR = pinMask; \
998  } \
999  static void Clear() { \
1000  pio->PIO_CODR = pinMask; \
1001  } \
1002  static void SetDirRead() { \
1003  pio->PIO_IDR = pinMask ; \
1004  pio->PIO_PUDR = pinMask; \
1005  pio->PIO_ODR = pinMask; \
1006  pio->PIO_PER = pinMask; \
1007  } \
1008  static void SetDirWrite() { \
1009  pio->PIO_IDR = pinMask ; \
1010  pio->PIO_PUDR = pinMask; \
1011  pio->PIO_OER = pinMask; \
1012  pio->PIO_PER = pinMask; \
1013  } \
1014  static uint8_t IsSet() { \
1015  return pio->PIO_PDSR & pinMask; \
1016  } \
1017 };
1018 
1019 // See: http://arduino.cc/en/Hacking/PinMappingSAM3X and variant.cpp
1020 
1021 MAKE_PIN(P0, PIOA, PIO_PA8);
1022 MAKE_PIN(P1, PIOA, PIO_PA9);
1023 MAKE_PIN(P2, PIOB, PIO_PB25);
1024 MAKE_PIN(P3, PIOC, PIO_PC28);
1025 MAKE_PIN(P4, PIOC, PIO_PC26);
1026 MAKE_PIN(P5, PIOC, PIO_PC25);
1027 MAKE_PIN(P6, PIOC, PIO_PC24);
1028 MAKE_PIN(P7, PIOC, PIO_PC23);
1029 MAKE_PIN(P8, PIOC, PIO_PC22);
1030 MAKE_PIN(P9, PIOC, PIO_PC21);
1031 MAKE_PIN(P10, PIOC, PIO_PC29);
1032 MAKE_PIN(P11, PIOD, PIO_PD7);
1033 MAKE_PIN(P12, PIOD, PIO_PD8);
1034 MAKE_PIN(P13, PIOB, PIO_PB27);
1035 MAKE_PIN(P14, PIOD, PIO_PD4);
1036 MAKE_PIN(P15, PIOD, PIO_PD5);
1037 MAKE_PIN(P16, PIOA, PIO_PA13);
1038 MAKE_PIN(P17, PIOA, PIO_PA12);
1039 MAKE_PIN(P18, PIOA, PIO_PA11);
1040 MAKE_PIN(P19, PIOA, PIO_PA10);
1041 MAKE_PIN(P20, PIOB, PIO_PB12);
1042 MAKE_PIN(P21, PIOB, PIO_PB13);
1043 MAKE_PIN(P22, PIOB, PIO_PB26);
1044 MAKE_PIN(P23, PIOA, PIO_PA14);
1045 MAKE_PIN(P24, PIOA, PIO_PA15);
1046 MAKE_PIN(P25, PIOD, PIO_PD0);
1047 MAKE_PIN(P26, PIOD, PIO_PD1);
1048 MAKE_PIN(P27, PIOD, PIO_PD2);
1049 MAKE_PIN(P28, PIOD, PIO_PD3);
1050 MAKE_PIN(P29, PIOD, PIO_PD6);
1051 MAKE_PIN(P30, PIOD, PIO_PD9);
1052 MAKE_PIN(P31, PIOA, PIO_PA7);
1053 MAKE_PIN(P32, PIOD, PIO_PD10);
1054 MAKE_PIN(P33, PIOC, PIO_PC1);
1055 MAKE_PIN(P34, PIOC, PIO_PC2);
1056 MAKE_PIN(P35, PIOC, PIO_PC3);
1057 MAKE_PIN(P36, PIOC, PIO_PC4);
1058 MAKE_PIN(P37, PIOC, PIO_PC5);
1059 MAKE_PIN(P38, PIOC, PIO_PC6);
1060 MAKE_PIN(P39, PIOC, PIO_PC7);
1061 MAKE_PIN(P40, PIOC, PIO_PC8);
1062 MAKE_PIN(P41, PIOC, PIO_PC9);
1063 MAKE_PIN(P42, PIOA, PIO_PA19);
1064 MAKE_PIN(P43, PIOA, PIO_PA20);
1065 MAKE_PIN(P44, PIOC, PIO_PC19);
1066 MAKE_PIN(P45, PIOC, PIO_PC18);
1067 MAKE_PIN(P46, PIOC, PIO_PC17);
1068 MAKE_PIN(P47, PIOC, PIO_PC16);
1069 MAKE_PIN(P48, PIOC, PIO_PC15);
1070 MAKE_PIN(P49, PIOC, PIO_PC14);
1071 MAKE_PIN(P50, PIOC, PIO_PC13);
1072 MAKE_PIN(P51, PIOC, PIO_PC12);
1073 MAKE_PIN(P52, PIOB, PIO_PB21);
1074 MAKE_PIN(P53, PIOB, PIO_PB14);
1075 MAKE_PIN(P54, PIOA, PIO_PA16);
1076 MAKE_PIN(P55, PIOA, PIO_PA24);
1077 MAKE_PIN(P56, PIOA, PIO_PA23);
1078 MAKE_PIN(P57, PIOA, PIO_PA22);
1079 MAKE_PIN(P58, PIOA, PIO_PA6);
1080 MAKE_PIN(P59, PIOA, PIO_PA4);
1081 MAKE_PIN(P60, PIOA, PIO_PA3);
1082 MAKE_PIN(P61, PIOA, PIO_PA2);
1083 MAKE_PIN(P62, PIOB, PIO_PB17);
1084 MAKE_PIN(P63, PIOB, PIO_PB18);
1085 MAKE_PIN(P64, PIOB, PIO_PB19);
1086 MAKE_PIN(P65, PIOB, PIO_PB20);
1087 MAKE_PIN(P66, PIOB, PIO_PB15);
1088 MAKE_PIN(P67, PIOB, PIO_PB16);
1089 MAKE_PIN(P68, PIOA, PIO_PA1);
1090 MAKE_PIN(P69, PIOA, PIO_PA0);
1091 MAKE_PIN(P70, PIOA, PIO_PA17);
1092 MAKE_PIN(P71, PIOA, PIO_PA18);
1093 MAKE_PIN(P72, PIOC, PIO_PC30);
1094 MAKE_PIN(P73, PIOA, PIO_PA21);
1095 MAKE_PIN(P74, PIOA, PIO_PA25); // MISO
1096 MAKE_PIN(P75, PIOA, PIO_PA26); // MOSI
1097 MAKE_PIN(P76, PIOA, PIO_PA27); // CLK
1098 MAKE_PIN(P77, PIOA, PIO_PA28);
1099 MAKE_PIN(P78, PIOB, PIO_PB23); // Unconnected
1100 
1101 #undef MAKE_PIN
1102 
1103 #elif defined(RBL_NRF51822)
1104 
1105 #define MAKE_PIN(className, pin) \
1106 class className { \
1107 public: \
1108  static void Set() { \
1109  nrf_gpio_pin_set(pin); \
1110  } \
1111  static void Clear() { \
1112  nrf_gpio_pin_clear(pin); \
1113  } \
1114  static void SetDirRead() { \
1115  nrf_gpio_cfg_input(pin, NRF_GPIO_PIN_NOPULL); \
1116  } \
1117  static void SetDirWrite() { \
1118  nrf_gpio_cfg_output(pin); \
1119  } \
1120  static uint8_t IsSet() { \
1121  return (uint8_t)nrf_gpio_pin_read(pin); \
1122  } \
1123 };
1124 
1125 // See: pin_transform.c in RBL nRF51822 SDK
1126 MAKE_PIN(P0, Pin_nRF51822_to_Arduino(D0));
1127 MAKE_PIN(P1, Pin_nRF51822_to_Arduino(D1));
1128 MAKE_PIN(P2, Pin_nRF51822_to_Arduino(D2));
1129 MAKE_PIN(P3, Pin_nRF51822_to_Arduino(D3));
1130 MAKE_PIN(P4, Pin_nRF51822_to_Arduino(D4));
1131 MAKE_PIN(P5, Pin_nRF51822_to_Arduino(D5));
1132 MAKE_PIN(P6, Pin_nRF51822_to_Arduino(D6));
1133 MAKE_PIN(P7, Pin_nRF51822_to_Arduino(D7));
1134 MAKE_PIN(P8, Pin_nRF51822_to_Arduino(D8));
1135 MAKE_PIN(P9, Pin_nRF51822_to_Arduino(D9)); // INT
1136 MAKE_PIN(P10, Pin_nRF51822_to_Arduino(D10)); // SS
1137 MAKE_PIN(P11, Pin_nRF51822_to_Arduino(D11));
1138 MAKE_PIN(P12, Pin_nRF51822_to_Arduino(D12));
1139 MAKE_PIN(P13, Pin_nRF51822_to_Arduino(D13));
1140 MAKE_PIN(P14, Pin_nRF51822_to_Arduino(D14));
1141 MAKE_PIN(P15, Pin_nRF51822_to_Arduino(D15));
1142 MAKE_PIN(P17, Pin_nRF51822_to_Arduino(D17)); // MISO
1143 MAKE_PIN(P18, Pin_nRF51822_to_Arduino(D18)); // MOSI
1144 MAKE_PIN(P16, Pin_nRF51822_to_Arduino(D16)); // CLK
1145 MAKE_PIN(P19, Pin_nRF51822_to_Arduino(D19));
1146 MAKE_PIN(P20, Pin_nRF51822_to_Arduino(D20));
1147 MAKE_PIN(P21, Pin_nRF51822_to_Arduino(D21));
1148 MAKE_PIN(P22, Pin_nRF51822_to_Arduino(D22));
1149 MAKE_PIN(P23, Pin_nRF51822_to_Arduino(D23));
1150 MAKE_PIN(P24, Pin_nRF51822_to_Arduino(D24));
1151 
1152 #undef MAKE_PIN
1153 
1154 #elif defined(STM32F446xx)
1155 // NUCLEO-F446RE
1156 
1157 #define MAKE_PIN(className, port, pin) \
1158 class className { \
1159 public: \
1160  static void Set() { \
1161  HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET); \
1162  } \
1163  static void Clear() { \
1164  HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET); \
1165  } \
1166  static void SetDirRead() { \
1167  static GPIO_InitTypeDef GPIO_InitStruct; \
1168  GPIO_InitStruct.Pin = pin; \
1169  GPIO_InitStruct.Mode = GPIO_MODE_INPUT; \
1170  GPIO_InitStruct.Pull = GPIO_NOPULL; \
1171  HAL_GPIO_Init(port, &GPIO_InitStruct); \
1172  } \
1173  static void SetDirWrite() { \
1174  static GPIO_InitTypeDef GPIO_InitStruct; \
1175  GPIO_InitStruct.Pin = pin; \
1176  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; \
1177  GPIO_InitStruct.Pull = GPIO_NOPULL; \
1178  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; \
1179  HAL_GPIO_Init(port, &GPIO_InitStruct); \
1180  } \
1181  static GPIO_PinState IsSet() { \
1182  return HAL_GPIO_ReadPin(port, pin); \
1183  } \
1184 };
1185 
1186 MAKE_PIN(P0, GPIOA, GPIO_PIN_3); // D0
1187 MAKE_PIN(P1, GPIOA, GPIO_PIN_2); // D1
1188 MAKE_PIN(P2, GPIOA, GPIO_PIN_10); // D2
1189 MAKE_PIN(P3, GPIOB, GPIO_PIN_3); // D3
1190 MAKE_PIN(P4, GPIOB, GPIO_PIN_5); // D4
1191 MAKE_PIN(P5, GPIOB, GPIO_PIN_4); // D5
1192 MAKE_PIN(P6, GPIOB, GPIO_PIN_10); // D6
1193 MAKE_PIN(P7, GPIOA, GPIO_PIN_8); // D7
1194 MAKE_PIN(P8, GPIOA, GPIO_PIN_9); // D8
1195 MAKE_PIN(P9, GPIOC, GPIO_PIN_7); // D9
1196 MAKE_PIN(P10, GPIOB, GPIO_PIN_6); // D10
1197 MAKE_PIN(P11, GPIOA, GPIO_PIN_7); // D11
1198 MAKE_PIN(P12, GPIOA, GPIO_PIN_6); // D12
1199 MAKE_PIN(P13, GPIOA, GPIO_PIN_5); // D13
1200 
1201 MAKE_PIN(P14, GPIOA, GPIO_PIN_0); // A0
1202 MAKE_PIN(P15, GPIOA, GPIO_PIN_1); // A1
1203 MAKE_PIN(P16, GPIOA, GPIO_PIN_4); // A2
1204 MAKE_PIN(P17, GPIOB, GPIO_PIN_0); // A3
1205 MAKE_PIN(P18, GPIOC, GPIO_PIN_1); // A4
1206 MAKE_PIN(P19, GPIOC, GPIO_PIN_0); // A5
1207 
1208 #undef MAKE_PIN
1209 
1210 #else
1211 #error "Please define board in avrpins.h"
1212 
1213 #endif
1214 
1215 #elif defined(__ARDUINO_ARC__)
1216 
1217 #include <avr/pgmspace.h>
1218 // Pointers are 32 bits on arc
1219 #define pgm_read_pointer(p) pgm_read_dword(p)
1220 
1221 #define MAKE_PIN(className, pin) \
1222 class className { \
1223 public: \
1224  static void Set() { \
1225  digitalWrite(pin, HIGH);\
1226  } \
1227  static void Clear() { \
1228  digitalWrite(pin, LOW); \
1229  } \
1230  static void SetDirRead() { \
1231  pinMode(pin, INPUT); \
1232  } \
1233  static void SetDirWrite() { \
1234  pinMode(pin, OUTPUT); \
1235  } \
1236  static uint8_t IsSet() { \
1237  return digitalRead(pin); \
1238  } \
1239 };
1240 
1241 MAKE_PIN(P0, 0);
1242 MAKE_PIN(P1, 1);
1243 MAKE_PIN(P2, 2);
1244 MAKE_PIN(P3, 3); //PWM
1245 MAKE_PIN(P4, 4);
1246 MAKE_PIN(P5, 5); //PWM
1247 MAKE_PIN(P6, 6); //PWM
1248 MAKE_PIN(P7, 7);
1249 MAKE_PIN(P8, 8);
1250 MAKE_PIN(P9, 9); //PWM
1251 
1252 MAKE_PIN(P10, 10); //SPI SS
1253 MAKE_PIN(P11, 11); //SPI MOSI
1254 MAKE_PIN(P12, 12); //SPI MISO
1255 MAKE_PIN(P13, 13); //SPI SCK / BUILTIN LED
1256 
1257 MAKE_PIN(P14, 14); // A0
1258 MAKE_PIN(P15, 15); // A1
1259 MAKE_PIN(P16, 16); // A2
1260 MAKE_PIN(P17, 17); // A3
1261 MAKE_PIN(P18, 18); // A4 SDA
1262 MAKE_PIN(P19, 19); // A5 SCL
1263 MAKE_PIN(P20, 20); // ATN
1264 
1265 #undef MAKE_PIN
1266 
1267 #elif defined(__ARDUINO_X86__) // Intel Galileo, Intel Galileo 2 and Intel Edison
1268 
1269 #include <avr/pgmspace.h>
1270 
1271 // Pointers are 32 bits on x86
1272 #define pgm_read_pointer(p) pgm_read_dword(p)
1273 
1274 #if PLATFORM_ID == 0xE1 // Edison platform id
1275 #define pinToFastPin(pin) 1 // As far as I can tell all pins can be used as fast pins
1276 #endif
1277 
1278 // Pin 2 and 3 on the Intel Galileo supports a higher rate,
1279 // so it is recommended to use one of these as the SS pin.
1280 
1281 #define MAKE_PIN(className, pin) \
1282 class className { \
1283 public: \
1284  static void Set() { \
1285  fastDigitalWrite(pin, HIGH); \
1286  } \
1287  static void Clear() { \
1288  fastDigitalWrite(pin, LOW); \
1289  } \
1290  static void SetDirRead() { \
1291  if (pinToFastPin(pin)) \
1292  pinMode(pin, INPUT_FAST); \
1293  else \
1294  pinMode(pin, INPUT); \
1295  } \
1296  static void SetDirWrite() { \
1297  if (pinToFastPin(pin)) \
1298  pinMode(pin, OUTPUT_FAST); \
1299  else \
1300  pinMode(pin, OUTPUT); \
1301  } \
1302  static uint8_t IsSet() { \
1303  return fastDigitalRead(pin); \
1304  } \
1305 };
1306 
1307 MAKE_PIN(P0, 0);
1308 MAKE_PIN(P1, 1);
1309 MAKE_PIN(P2, 2);
1310 MAKE_PIN(P3, 3);
1311 MAKE_PIN(P4, 4);
1312 MAKE_PIN(P5, 5);
1313 MAKE_PIN(P6, 6);
1314 MAKE_PIN(P7, 7);
1315 MAKE_PIN(P8, 8);
1316 MAKE_PIN(P9, 9);
1317 MAKE_PIN(P10, 10);
1318 MAKE_PIN(P11, 11);
1319 MAKE_PIN(P12, 12);
1320 MAKE_PIN(P13, 13);
1321 MAKE_PIN(P14, 14); // A0
1322 MAKE_PIN(P15, 15); // A1
1323 MAKE_PIN(P16, 16); // A2
1324 MAKE_PIN(P17, 17); // A3
1325 MAKE_PIN(P18, 18); // A4
1326 MAKE_PIN(P19, 19); // A5
1327 
1328 #undef MAKE_PIN
1329 
1330 #elif defined(__MIPSEL__)
1331 // MIPSEL (MIPS architecture using a little endian byte order)
1332 
1333 // MIPS size_t = 4
1334 #define pgm_read_pointer(p) pgm_read_dword(p)
1335 
1336 #define MAKE_PIN(className, pin) \
1337 class className { \
1338 public: \
1339  static void Set() { \
1340  digitalWrite(pin, HIGH);\
1341  } \
1342  static void Clear() { \
1343  digitalWrite(pin, LOW); \
1344  } \
1345  static void SetDirRead() { \
1346  pinMode(pin, INPUT); \
1347  } \
1348  static void SetDirWrite() { \
1349  pinMode(pin, OUTPUT); \
1350  } \
1351  static uint8_t IsSet() { \
1352  return digitalRead(pin); \
1353  } \
1354 };
1355 
1356 // 0 .. 13 - Digital pins
1357 MAKE_PIN(P0, 0); // RX
1358 MAKE_PIN(P1, 1); // TX
1359 MAKE_PIN(P2, 2); //
1360 MAKE_PIN(P3, 3); //
1361 MAKE_PIN(P4, 4); //
1362 MAKE_PIN(P5, 5); //
1363 MAKE_PIN(P6, 6); //
1364 MAKE_PIN(P7, 7); //
1365 MAKE_PIN(P8, 8); //
1366 MAKE_PIN(P9, 9); //
1367 MAKE_PIN(P10, 10); //
1368 MAKE_PIN(P11, 11); //
1369 MAKE_PIN(P12, 12); //
1370 MAKE_PIN(P13, 13); //
1371 
1372 #undef MAKE_PIN
1373 
1374 #elif defined(ESP8266) || defined(ESP32)
1375 
1376 #define MAKE_PIN(className, pin) \
1377 class className { \
1378 public: \
1379  static void Set() { \
1380  digitalWrite(pin, HIGH);\
1381  } \
1382  static void Clear() { \
1383  digitalWrite(pin, LOW); \
1384  } \
1385  static void SetDirRead() { \
1386  pinMode(pin, INPUT); \
1387  } \
1388  static void SetDirWrite() { \
1389  pinMode(pin, OUTPUT); \
1390  } \
1391  static uint8_t IsSet() { \
1392  return digitalRead(pin); \
1393  } \
1394 };
1395 
1396 #if defined(ESP8266)
1397 
1398 // Workaround the following issue: https://github.com/esp8266/Arduino/pull/5735
1399 #undef pgm_read_ptr_aligned
1400 #ifdef __cplusplus
1401 #define pgm_read_ptr_aligned(addr) (*reinterpret_cast<const void* const*>(addr))
1402 #else
1403 #define pgm_read_ptr_aligned(addr) (*(const void* const*)(addr))
1404 #endif
1405 
1406 #undef pgm_read_ptr
1407 #if PGM_READ_UNALIGNED
1408 #define pgm_read_ptr(p) pgm_read_ptr_unaligned(p)
1409 #else
1410 #define pgm_read_ptr(p) pgm_read_ptr_aligned(p)
1411 #endif
1412 
1413 #ifdef pgm_read_pointer
1414 #undef pgm_read_pointer
1415 #endif
1416 #define pgm_read_pointer(p) pgm_read_ptr(p)
1417 
1418 // Pinout for ESP-12 module
1419 // 0 .. 16 - Digital pins
1420 // GPIO 6 to 11 and 16 are not usable in this library.
1421 
1422 MAKE_PIN(P0, 0);
1423 MAKE_PIN(P1, 1); // TX0
1424 MAKE_PIN(P2, 2); // TX1
1425 MAKE_PIN(P3, 3); // RX0
1426 MAKE_PIN(P4, 4); // SDA
1427 MAKE_PIN(P5, 5); // SCL
1428 MAKE_PIN(P12, 12); // MISO
1429 MAKE_PIN(P13, 13); // MOSI
1430 MAKE_PIN(P14, 14); // SCK
1431 MAKE_PIN(P15, 15); // SS
1432 
1433 #elif defined(ESP32)
1434 
1435 // Workaround strict-aliasing warnings
1436 #ifdef pgm_read_word
1437 #undef pgm_read_word
1438 #endif
1439 #ifdef pgm_read_dword
1440 #undef pgm_read_dword
1441 #endif
1442 #ifdef pgm_read_float
1443 #undef pgm_read_float
1444 #endif
1445 #ifdef pgm_read_ptr
1446 #undef pgm_read_ptr
1447 #endif
1448 
1449 #define pgm_read_word(addr) ({ \
1450  typeof(addr) _addr = (addr); \
1451  *(const unsigned short *)(_addr); \
1452 })
1453 #define pgm_read_dword(addr) ({ \
1454  typeof(addr) _addr = (addr); \
1455  *(const unsigned long *)(_addr); \
1456 })
1457 #define pgm_read_float(addr) ({ \
1458  typeof(addr) _addr = (addr); \
1459  *(const float *)(_addr); \
1460 })
1461 #define pgm_read_ptr(addr) ({ \
1462  typeof(addr) _addr = (addr); \
1463  *(void * const *)(_addr); \
1464 })
1465 
1466 // Pinout for ESP32 dev module
1467 
1468 MAKE_PIN(P0, 0);
1469 MAKE_PIN(P1, 1); // TX0
1470 MAKE_PIN(P10, 10); // TX1
1471 MAKE_PIN(P3, 3); // RX0
1472 MAKE_PIN(P21, 21); // SDA
1473 MAKE_PIN(P22, 22); // SCL
1474 MAKE_PIN(P19, 19); // MISO
1475 MAKE_PIN(P23, 23); // MOSI
1476 MAKE_PIN(P18, 18); // SCK
1477 MAKE_PIN(P5, 5); // SS
1478 MAKE_PIN(P17, 17); // INT
1479 
1480 #endif
1481 
1482 #undef MAKE_PIN
1483 
1484 // pgm_read_ptr is not defined in the ESP32, so we have to undef the diffinition from version_helper.h
1485 #ifdef pgm_read_pointer
1486 #undef pgm_read_pointer
1487 #endif
1488 #define pgm_read_pointer(p) pgm_read_ptr(p)
1489 
1490 #else
1491 #error "Please define board in avrpins.h"
1492 
1493 #endif
1494 
1495 #endif //_avrpins_h_