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 software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16 */
17 
18 /* derived from Konstantin Chizhov's AVR port templates */
19 
20 #ifndef _avrpins_h_
21 #define _avrpins_h_
22 
23 #if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__)
24 /* Uncomment the following if you have Arduino Mega ADK board with MAX3421e built-in */
25 //#define BOARD_MEGA_ADK
26 #endif
27 
28 /* Uncomment the following if you are using a Teensy 2.0 */
29 //#define BOARD_TEENSY
30 
31 #include <avr/io.h>
32 
33 #ifdef PORTA
34 #define USE_PORTA
35 #endif
36 #ifdef PORTB
37 #define USE_PORTB
38 #endif
39 #ifdef PORTC
40 #define USE_PORTC
41 #endif
42 #ifdef PORTD
43 #define USE_PORTD
44 #endif
45 #ifdef PORTE
46 #define USE_PORTE
47 #endif
48 #ifdef PORTF
49 #define USE_PORTF
50 #endif
51 #ifdef PORTG
52 #define USE_PORTG
53 #endif
54 #ifdef PORTH
55 #define USE_PORTH
56 #endif
57 #ifdef PORTJ
58 #define USE_PORTJ
59 #endif
60 #ifdef PORTK
61 #define USE_PORTK
62 #endif
63 #ifdef PORTL
64 #define USE_PORTL
65 #endif
66 #ifdef PORTQ
67 #define USE_PORTQ
68 #endif
69 #ifdef PORTR
70 #define USE_PORTR
71 #endif
72 
73 #ifdef TCCR0A
74 #define USE_TCCR0A
75 #endif
76 #ifdef TCCR1A
77 #define USE_TCCR1A
78 #endif
79 #ifdef TCCR2A
80 #define USE_TCCR2A
81 #endif
82 
83 //Port definitions for AtTiny, AtMega families.
84 
85 #define MAKE_PORT(portName, ddrName, pinName, className, ID) \
86  class className{\
87  public:\
88  typedef uint8_t DataT;\
89  public:\
90  static void Write(DataT value){portName = value;}\
91  static void ClearAndSet(DataT clearMask, DataT value){portName = (portName & ~clearMask) | value;}\
92  static DataT Read(){return portName;}\
93  static void DirWrite(DataT value){ddrName = value;}\
94  static DataT DirRead(){return ddrName;}\
95  static void Set(DataT value){portName |= value;}\
96  static void Clear(DataT value){portName &= ~value;}\
97  static void Toggle(DataT value){portName ^= value;}\
98  static void DirSet(DataT value){ddrName |= value;}\
99  static void DirClear(DataT value){ddrName &= ~value;}\
100  static void DirToggle(DataT value){ddrName ^= value;}\
101  static DataT PinRead(){return pinName;}\
102  enum{Id = ID};\
103  enum{Width=sizeof(DataT)*8};\
104  };
105 
106 // TCCR registers to set/clear Arduino PWM
107 #define MAKE_TCCR(TccrName, className) \
108  class className{\
109  public:\
110  typedef uint8_t DataT;\
111  public:\
112  static void Write(DataT value){TccrName = value;}\
113  static void ClearAndSet(DataT clearMask, DataT value){TccrName = (TccrName & ~clearMask) | value;}\
114  static DataT Read(){return TccrName;}\
115  static void Set(DataT value){TccrName |= value;}\
116  static void Clear(DataT value){TccrName &= ~value;}\
117  static void Toggle(DataT value){TccrName ^= value;}\
118  enum{Width=sizeof(DataT)*8};\
119  };
120 
121 #ifdef USE_PORTA
122 MAKE_PORT(PORTA, DDRA, PINA, Porta, 'A')
123 #endif
124 #ifdef USE_PORTB
125 MAKE_PORT(PORTB, DDRB, PINB, Portb, 'B')
126 #endif
127 #ifdef USE_PORTC
128 MAKE_PORT(PORTC, DDRC, PINC, Portc, 'C')
129 #endif
130 #ifdef USE_PORTD
131 MAKE_PORT(PORTD, DDRD, PIND, Portd, 'D')
132 #endif
133 #ifdef USE_PORTE
134 MAKE_PORT(PORTE, DDRE, PINE, Porte, 'E')
135 #endif
136 #ifdef USE_PORTF
137 MAKE_PORT(PORTF, DDRF, PINF, Portf, 'F')
138 #endif
139 #ifdef USE_PORTG
140 MAKE_PORT(PORTG, DDRG, PING, Portg, 'G')
141 #endif
142 #ifdef USE_PORTH
143 MAKE_PORT(PORTH, DDRH, PINH, Porth, 'H')
144 #endif
145 #ifdef USE_PORTJ
146 MAKE_PORT(PORTJ, DDRJ, PINJ, Portj, 'J')
147 #endif
148 #ifdef USE_PORTK
149 MAKE_PORT(PORTK, DDRK, PINK, Portk, 'K')
150 #endif
151 #ifdef USE_PORTL
152 MAKE_PORT(PORTL, DDRL, PINL, Portl, 'L')
153 #endif
154 #ifdef USE_PORTQ
155 MAKE_PORT(PORTQ, DDRQ, PINQ, Portq, 'Q')
156 #endif
157 #ifdef USE_PORTR
158 MAKE_PORT(PORTR, DDRR, PINR, Portr, 'R')
159 #endif
160 
161 #ifdef USE_TCCR0A
162 MAKE_TCCR(TCCR0A, Tccr0a)
163 #endif
164 #ifdef USE_TCCR1A
165 MAKE_TCCR(TCCR1A, Tccr1a)
166 #endif
167 #ifdef USE_TCCR2A
168 MAKE_TCCR(TCCR2A, Tccr2a)
169 #endif
170 
171  // this class represents one pin in a IO port.
172  // It is fully static.
173  template<typename PORT, uint8_t PIN>
174  class TPin
175  {
176 // BOOST_STATIC_ASSERT(PIN < PORT::Width);
177  public:
178  typedef PORT Port;
179  enum{Number = PIN};
180 
181  static void Set() { PORT::Set(1 << PIN); }
182 
183  static void Set(uint8_t val){
184  if(val)
185  Set();
186  else Clear();}
187 
188  static void SetDir(uint8_t val){
189  if(val)
190  SetDirWrite();
191  else SetDirRead();}
192 
193  static void Clear(){PORT::Clear(1 << PIN);}
194 
195  static void Toggle(){PORT::Toggle(1 << PIN);}
196 
197  static void SetDirRead(){PORT::DirClear(1 << PIN);}
198 
199  static void SetDirWrite(){PORT::DirSet(1 << PIN);}
200 
201  static uint8_t IsSet(){return PORT::PinRead() & (uint8_t)(1 << PIN);}
202 
203  static void WaiteForSet(){ while(IsSet()==0){} }
204 
205  static void WaiteForClear(){ while(IsSet()){} }
206  }; //class TPin...
207 
208  // this class represents one bit in TCCR port.
209  // used to set/clear TCCRx bits
210  // It is fully static.
211  template<typename TCCR, uint8_t COM>
212  class TCom
213  {
214 // BOOST_STATIC_ASSERT(PIN < PORT::Width);
215  public:
216  typedef TCCR Tccr;
217  enum{Com = COM};
218 
219  static void Set() { TCCR::Set(1 << COM); }
220 
221  static void Clear() { TCCR::Clear(1 << COM); }
222 
223  static void Toggle() { TCCR::Toggle(1 << COM); }
224  }; //class TCom...
225 
226 //Short pin definitions
227 #ifdef USE_PORTA
228 typedef TPin<Porta, 0> Pa0;
229 typedef TPin<Porta, 1> Pa1;
230 typedef TPin<Porta, 2> Pa2;
231 typedef TPin<Porta, 3> Pa3;
232 typedef TPin<Porta, 4> Pa4;
233 typedef TPin<Porta, 5> Pa5;
234 typedef TPin<Porta, 6> Pa6;
235 typedef TPin<Porta, 7> Pa7;
236 #endif
237 
238 #ifdef USE_PORTB
239 typedef TPin<Portb, 0> Pb0;
240 typedef TPin<Portb, 1> Pb1;
241 typedef TPin<Portb, 2> Pb2;
242 typedef TPin<Portb, 3> Pb3;
243 typedef TPin<Portb, 4> Pb4;
244 typedef TPin<Portb, 5> Pb5;
245 typedef TPin<Portb, 6> Pb6;
246 typedef TPin<Portb, 7> Pb7;
247 #endif
248 
249 #ifdef USE_PORTC
250 typedef TPin<Portc, 0> Pc0;
251 typedef TPin<Portc, 1> Pc1;
252 typedef TPin<Portc, 2> Pc2;
253 typedef TPin<Portc, 3> Pc3;
254 typedef TPin<Portc, 4> Pc4;
255 typedef TPin<Portc, 5> Pc5;
256 typedef TPin<Portc, 6> Pc6;
257 typedef TPin<Portc, 7> Pc7;
258 #endif
259 
260 #ifdef USE_PORTD
261 typedef TPin<Portd, 0> Pd0;
262 typedef TPin<Portd, 1> Pd1;
263 typedef TPin<Portd, 2> Pd2;
264 typedef TPin<Portd, 3> Pd3;
265 typedef TPin<Portd, 4> Pd4;
266 typedef TPin<Portd, 5> Pd5;
267 typedef TPin<Portd, 6> Pd6;
268 typedef TPin<Portd, 7> Pd7;
269 #endif
270 
271 #ifdef USE_PORTE
272 typedef TPin<Porte, 0> Pe0;
273 typedef TPin<Porte, 1> Pe1;
274 typedef TPin<Porte, 2> Pe2;
275 typedef TPin<Porte, 3> Pe3;
276 typedef TPin<Porte, 4> Pe4;
277 typedef TPin<Porte, 5> Pe5;
278 typedef TPin<Porte, 6> Pe6;
279 typedef TPin<Porte, 7> Pe7;
280 #endif
281 
282 #ifdef USE_PORTF
283 typedef TPin<Portf, 0> Pf0;
284 typedef TPin<Portf, 1> Pf1;
285 typedef TPin<Portf, 2> Pf2;
286 typedef TPin<Portf, 3> Pf3;
287 typedef TPin<Portf, 4> Pf4;
288 typedef TPin<Portf, 5> Pf5;
289 typedef TPin<Portf, 6> Pf6;
290 typedef TPin<Portf, 7> Pf7;
291 #endif
292 
293 #ifdef USE_PORTG
294 typedef TPin<Portg, 0> Pg0;
295 typedef TPin<Portg, 1> Pg1;
296 typedef TPin<Portg, 2> Pg2;
297 typedef TPin<Portg, 3> Pg3;
298 typedef TPin<Portg, 4> Pg4;
299 typedef TPin<Portg, 5> Pg5;
300 typedef TPin<Portg, 6> Pg6;
301 typedef TPin<Portg, 7> Pg7;
302 #endif
303 
304 #ifdef USE_PORTH
305 typedef TPin<Porth, 0> Ph0;
306 typedef TPin<Porth, 1> Ph1;
307 typedef TPin<Porth, 2> Ph2;
308 typedef TPin<Porth, 3> Ph3;
309 typedef TPin<Porth, 4> Ph4;
310 typedef TPin<Porth, 5> Ph5;
311 typedef TPin<Porth, 6> Ph6;
312 typedef TPin<Porth, 7> Ph7;
313 #endif
314 
315 #ifdef USE_PORTJ
316 typedef TPin<Portj, 0> Pj0;
317 typedef TPin<Portj, 1> Pj1;
318 typedef TPin<Portj, 2> Pj2;
319 typedef TPin<Portj, 3> Pj3;
320 typedef TPin<Portj, 4> Pj4;
321 typedef TPin<Portj, 5> Pj5;
322 typedef TPin<Portj, 6> Pj6;
323 typedef TPin<Portj, 7> Pj7;
324 #endif
325 
326 #ifdef USE_PORTK
327 typedef TPin<Portk, 0> Pk0;
328 typedef TPin<Portk, 1> Pk1;
329 typedef TPin<Portk, 2> Pk2;
330 typedef TPin<Portk, 3> Pk3;
331 typedef TPin<Portk, 4> Pk4;
332 typedef TPin<Portk, 5> Pk5;
333 typedef TPin<Portk, 6> Pk6;
334 typedef TPin<Portk, 7> Pk7;
335 #endif
336 
337 #ifdef USE_PORTL
338 typedef TPin<Portl, 0> Pl0;
339 typedef TPin<Portl, 1> Pl1;
340 typedef TPin<Portl, 2> Pl2;
341 typedef TPin<Portl, 3> Pl3;
342 typedef TPin<Portl, 4> Pl4;
343 typedef TPin<Portl, 5> Pl5;
344 typedef TPin<Portl, 6> Pl6;
345 typedef TPin<Portl, 7> Pl7;
346 #endif
347 
348 #ifdef USE_PORTQ
349 typedef TPin<Portq, 0> Pq0;
350 typedef TPin<Portq, 1> Pq1;
351 typedef TPin<Portq, 2> Pq2;
352 typedef TPin<Portq, 3> Pq3;
353 typedef TPin<Portq, 4> Pq4;
354 typedef TPin<Portq, 5> Pq5;
355 typedef TPin<Portq, 6> Pq6;
356 typedef TPin<Portq, 7> Pq7;
357 #endif
358 
359 #ifdef USE_PORTR
360 typedef TPin<Portr, 0> Pr0;
361 typedef TPin<Portr, 1> Pr1;
362 typedef TPin<Portr, 2> Pr2;
363 typedef TPin<Portr, 3> Pr3;
364 typedef TPin<Portr, 4> Pr4;
365 typedef TPin<Portr, 5> Pr5;
366 typedef TPin<Portr, 6> Pr6;
367 typedef TPin<Portr, 7> Pr7;
368 #endif
369 
370 #ifdef USE_TCCR0A
371 typedef TCom<Tccr0a, COM0A1> Tc0a; //P6
372 typedef TCom<Tccr0a, COM0B1> Tc0b; //P5
373 #endif
374 
375 #ifdef USE_TCCR1A
376 typedef TCom<Tccr1a, COM1A1> Tc1a; //P9
377 typedef TCom<Tccr1a, COM1B1> Tc1b; //P10
378 #endif
379 
380 #ifdef USE_TCCR2A
381 typedef TCom<Tccr2a, COM2A1> Tc2a; //P11
382 typedef TCom<Tccr2a, COM2B1> Tc2b; //P3
383 #endif
384 
385 template<typename Tp_pin, typename Tc_bit>
386  class Tp_Tc
387  {
388  public:
389  static void SetDir(uint8_t val){
390  if(val)
391  SetDirWrite();
392  else SetDirRead();
393  }
394  static void SetDirRead(){
395  Tp_pin::SetDirRead(); //set pin direction
396  Tc_bit::Clear(); //disconnect pin from PWM
397  }
398  static void SetDirWrite(){
399  Tp_pin::SetDirWrite();
400  Tc_bit::Clear();
401  }
402  };
403 
404 /* pin definitions for cases where it's necessary to clear compare output mode bits */
405 
406 //typedef Tp_Tc<Pd3, Tc2b> P3; //Arduino pin 3
407 //typedef Tp_Tc<Pd5, Tc0b> P5; //Arduino pin 5
408 //typedef Tp_Tc<Pd6, Tc0a> P6; //Arduino pin 6
409 //typedef Tp_Tc<Pb1, Tc1a> P9; //Arduino pin 9
410 //typedef Tp_Tc<Pb2, Tc1b> P10; //Arduino pin 10
411 //typedef Tp_Tc<Pb3, Tc2a> P11; //Arduino pin 11
412 
413 /* Arduino pin definitions */
414 #if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__)
415 
416 // "Mega" Arduino pin numbers
417 
418 #define P0 Pe0
419 #define P1 Pe1
420 #define P2 Pe4
421 #define P3 Pe5
422 #define P4 Pg5
423 #define P5 Pe5
424 #define P6 Ph3
425 #define P7 Ph4
426 
427 #define P8 Ph5
428 #define P9 Ph6
429 #define P10 Pb4
430 #define P11 Pb5
431 #define P12 Pb6
432 #define P13 Pb7
433 
434 #define P14 Pj1
435 #define P15 Pj0
436 #define P16 Ph1
437 #define P17 Ph0
438 #define P18 Pd3
439 #define P19 Pd2
440 #define P20 Pd1
441 #define P21 Pd0
442 
443 #define P22 Pa0
444 #define P23 Pa1
445 #define P24 Pa2
446 #define P25 Pa3
447 #define P26 Pa4
448 #define P27 Pa5
449 #define P28 Pa6
450 #define P29 Pa7
451 #define P30 Pc7
452 #define P31 Pc6
453 #define P32 Pc5
454 #define P33 Pc4
455 #define P34 Pc3
456 #define P35 Pc2
457 #define P36 Pc1
458 #define P37 Pc0
459 
460 #define P38 Pd7
461 #define P39 Pg2
462 #define P40 Pg1
463 #define P41 Pg0
464 #define P42 Pl7
465 #define P43 Pl6
466 #define P44 Pl5
467 #define P45 Pl4
468 #define P46 Pl3
469 #define P47 Pl2
470 #define P48 Pl1
471 #define P49 Pl0
472 #define P50 Pb3
473 #define P51 Pb2
474 #define P52 Pb1
475 #define P53 Pb0
476 #define P54 Pe6 // INT on Arduino ADK
477 
478 #endif //"Mega" pin numbers
479 
480 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
481 //"Classic" Arduino pin numbers
482 
483 #define P0 Pd0
484 #define P1 Pd1
485 #define P2 Pd2
486 #define P3 Pd3
487 #define P4 Pd4
488 #define P5 Pd5
489 #define P6 Pd6
490 #define P7 Pd7
491 
492 #define P8 Pb0
493 #define P9 Pb1
494 #define P10 Pb2
495 #define P11 Pb3
496 #define P12 Pb4
497 #define P13 Pb5
498 
499 #define P14 Pc0
500 #define P15 Pc1
501 #define P16 Pc2
502 #define P17 Pc3
503 #define P18 Pc4
504 #define P19 Pc5
505 
506 #endif // "Classic" Arduino pin numbers
507 
508 #if !defined(BOARD_TEENSY) && defined(__AVR_ATmega32U4__)
509 // Arduino Leonardo pin numbers
510 
511 #define P0 Pd2 // D0 - PD2
512 #define P1 Pd3 // D1 - PD3
513 #define P2 Pd1 // D2 - PD1
514 #define P3 Pd0 // D3 - PD0
515 #define P4 Pd4 // D4 - PD4
516 #define P5 Pc6 // D5 - PC6
517 #define P6 Pd7 // D6 - PD7
518 #define P7 Pe6 // D7 - PE6
519 
520 #define P8 Pb4 // D8 - PB4
521 #define P9 Pb5 // D9 - PB5
522 #define P10 Pb6 // D10 - PB6
523 #define P11 Pb7 // D11 - PB7
524 #define P12 Pd6 // D12 - PD6
525 #define P13 Pc7 // D13 - PC7
526 
527 #define P14 Pb3 // D14 - MISO - PB3
528 #define P15 Pb1 // D15 - SCK - PB1
529 #define P16 Pb2 // D16 - MOSI - PB2
530 #define P17 Pb0 // D17 - SS - PB0
531 
532 #define P18 Pf7 // D18 - A0 - PF7
533 #define P19 Pf6 // D19 - A1 - PF6
534 #define P20 Pf5 // D20 - A2 - PF5
535 #define P21 Pf4 // D21 - A3 - PF4
536 #define P22 Pf1 // D22 - A4 - PF1
537 #define P23 Pf0 // D23 - A5 - PF0
538 
539 #define P24 Pd4 // D24 / D4 - A6 - PD4
540 #define P25 Pd7 // D25 / D6 - A7 - PD7
541 #define P26 Pb4 // D26 / D8 - A8 - PB4
542 #define P27 Pb5 // D27 / D9 - A9 - PB5
543 #define P28 Pb6 // D28 / D10 - A10 - PB6
544 #define P29 Pd6 // D29 / D12 - A11 - PD6
545 
546 #endif // Arduino Leonardo pin numbers
547 
548 #if defined(BOARD_TEENSY) && defined(__AVR_ATmega32U4__)
549 // Teensy 2.0 pin numbers
550 // http://www.pjrc.com/teensy/pinout.html
551 #define P0 Pb0
552 #define P1 Pb1
553 #define P2 Pb2
554 #define P3 Pb3
555 #define P4 Pb7
556 #define P5 Pd0
557 #define P6 Pd1
558 #define P7 Pd2
559 #define P8 Pd3
560 #define P9 Pc6
561 #define P10 Pc7
562 #define P11 Pd6
563 #define P12 Pd7
564 #define P13 Pb4
565 #define P14 Pb5
566 #define P15 Pb6
567 #define P16 Pf7
568 #define P17 Pf6
569 #define P18 Pf5
570 #define P19 Pf4
571 #define P20 Pf1
572 #define P21 Pf0
573 #define P22 Pd4
574 #define P23 Pd5
575 #define P24 Pe6
576 #endif // Teensy 2.0
577 
578 #if defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
579 // Teensy++ 2.0 pin numbers
580 // http://www.pjrc.com/teensy/pinout.html
581 #define P0 Pd0
582 #define P1 Pd1
583 #define P2 Pd2
584 #define P3 Pd3
585 #define P4 Pd4
586 #define P5 Pd5
587 #define P6 Pd6
588 #define P7 Pd7
589 #define P8 Pe0
590 #define P9 Pe1
591 #define P10 Pc0
592 #define P11 Pc1
593 #define P12 Pc2
594 #define P13 Pc3
595 #define P14 Pc4
596 #define P15 Pc5
597 #define P16 Pc6
598 #define P17 Pc7
599 #define P18 Pe6
600 #define P19 Pe7
601 #define P20 Pb0
602 #define P21 Pb1
603 #define P22 Pb2
604 #define P23 Pb3
605 #define P24 Pb4
606 #define P25 Pb5
607 #define P26 Pb6
608 #define P27 Pb7
609 #define P28 Pa0
610 #define P29 Pa1
611 #define P30 Pa2
612 #define P31 Pa3
613 #define P32 Pa4
614 #define P33 Pa5
615 #define P34 Pa6
616 #define P35 Pa7
617 #define P36 Pe4
618 #define P37 Pe5
619 #define P38 Pf0
620 #define P39 Pf1
621 #define P40 Pf2
622 #define P41 Pf3
623 #define P42 Pf4
624 #define P43 Pf5
625 #define P44 Pf6
626 #define P45 Pf7
627 #endif // Teensy++ 2.0
628 
629 #if defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__)
630 // Sanguino pin numbers
631 // http://sanguino.cc/hardware
632 #define P0 Pb0
633 #define P1 Pb1
634 #define P2 Pb2
635 #define P3 Pb3
636 #define P4 Pb4
637 #define P5 Pb5
638 #define P6 Pb6
639 #define P7 Pb7
640 #define P8 Pd0
641 #define P9 Pd1
642 #define P10 Pd2
643 #define P11 Pd3
644 #define P12 Pd4
645 #define P13 Pd5
646 #define P14 Pd6
647 #define P15 Pd7
648 #define P16 Pc0
649 #define P17 Pc1
650 #define P18 Pc2
651 #define P19 Pc3
652 #define P20 Pc4
653 #define P21 Pc5
654 #define P22 Pc6
655 #define P23 Pc7
656 #define P24 Pa0
657 #define P25 Pa1
658 #define P26 Pa2
659 #define P27 Pa3
660 #define P28 Pa4
661 #define P29 Pa5
662 #define P30 Pa6
663 #define P31 Pa7
664 #endif // Sanguino
665 
666 #endif //_avrpins_h_