/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved. This software may be distributed and modified under the terms of the GNU General Public License version 2 (GPL2) as published by the Free Software Foundation and appearing in the file GPL2.TXT included in the packaging of this file. Please note that GPL2 Section 2[b] requires that all works based on this software must also be made publicly available under the terms of the GPL2 ("Copyleft"). Contact information ------------------- Circuits At Home, LTD Web : http://www.circuitsathome.com e-mail : support@circuitsathome.com */ /* derived from Konstantin Chizhov's AVR port templates */ #ifndef _avrpins_h_ #define _avrpins_h_ #include #ifdef PORTA #define USE_PORTA #endif #ifdef PORTB #define USE_PORTB #endif #ifdef PORTC #define USE_PORTC #endif #ifdef PORTD #define USE_PORTD #endif #ifdef PORTE #define USE_PORTE #endif #ifdef PORTF #define USE_PORTF #endif #ifdef PORTG #define USE_PORTG #endif #ifdef PORTH #define USE_PORTH #endif #ifdef PORTJ #define USE_PORTJ #endif #ifdef PORTK #define USE_PORTK #endif #ifdef PORTQ #define USE_PORTQ #endif #ifdef PORTR #define USE_PORTR #endif #ifdef TCCR0A #define USE_TCCR0A #endif #ifdef TCCR1A #define USE_TCCR1A #endif #ifdef TCCR2A #define USE_TCCR2A #endif //Port definitions for AtTiny, AtMega families. #define MAKE_PORT(portName, ddrName, pinName, className, ID) \ class className{\ public:\ typedef uint8_t DataT;\ public:\ static void Write(DataT value){portName = value;}\ static void ClearAndSet(DataT clearMask, DataT value){portName = (portName & ~clearMask) | value;}\ static DataT Read(){return portName;}\ static void DirWrite(DataT value){ddrName = value;}\ static DataT DirRead(){return ddrName;}\ static void Set(DataT value){portName |= value;}\ static void Clear(DataT value){portName &= ~value;}\ static void Toggle(DataT value){portName ^= value;}\ static void DirSet(DataT value){ddrName |= value;}\ static void DirClear(DataT value){ddrName &= ~value;}\ static void DirToggle(DataT value){ddrName ^= value;}\ static DataT PinRead(){return pinName;}\ enum{Id = ID};\ enum{Width=sizeof(DataT)*8};\ }; // TCCR registers to set/clear Arduino PWM #define MAKE_TCCR(TccrName, className) \ class className{\ public:\ typedef uint8_t DataT;\ public:\ static void Write(DataT value){TccrName = value;}\ static void ClearAndSet(DataT clearMask, DataT value){TccrName = (TccrName & ~clearMask) | value;}\ static DataT Read(){return TccrName;}\ static void Set(DataT value){TccrName |= value;}\ static void Clear(DataT value){TccrName &= ~value;}\ static void Toggle(DataT value){TccrName ^= value;}\ enum{Width=sizeof(DataT)*8};\ }; #ifdef USE_PORTA MAKE_PORT(PORTA, DDRA, PINA, Porta, 'A') #endif #ifdef USE_PORTB MAKE_PORT(PORTB, DDRB, PINB, Portb, 'B') #endif #ifdef USE_PORTC MAKE_PORT(PORTC, DDRC, PINC, Portc, 'C') #endif #ifdef USE_PORTD MAKE_PORT(PORTD, DDRD, PIND, Portd, 'D') #endif #ifdef USE_PORTE MAKE_PORT(PORTE, DDRE, PINE, Porte, 'E') #endif #ifdef USE_PORTF MAKE_PORT(PORTF, DDRF, PINF, Portf, 'F') #endif #ifdef USE_PORTG MAKE_PORT(PORTG, DDRG, PING, Portg, 'G') #endif #ifdef USE_PORTH MAKE_PORT(PORTH, DDRH, PINH, Porth, 'H') #endif #ifdef USE_PORTJ MAKE_PORT(PORTJ, DDRJ, PINJ, Portj, 'J') #endif #ifdef USE_PORTK MAKE_PORT(PORTK, DDRK, PINK, Portk, 'K') #endif #ifdef USE_PORTQ MAKE_PORT(PORTQ, DDRQ, PINQ, Portq, 'Q') #endif #ifdef USE_PORTR MAKE_PORT(PORTR, DDRR, PINR, Portr, 'R') #endif #ifdef USE_TCCR0A MAKE_TCCR(TCCR0A, Tccr0a) #endif #ifdef USE_TCCR1A MAKE_TCCR(TCCR1A, Tccr1a) #endif #ifdef USE_TCCR2A MAKE_TCCR(TCCR2A, Tccr2a) #endif // this class represents one pin in a IO port. // It is fully static. template class TPin { // BOOST_STATIC_ASSERT(PIN < PORT::Width); public: typedef PORT Port; enum{Number = PIN}; static void Set() { PORT::Set(1 << PIN); } static void Set(uint8_t val){ if(val) Set(); else Clear();} static void SetDir(uint8_t val){ if(val) SetDirWrite(); else SetDirRead();} static void Clear(){PORT::Clear(1 << PIN);} static void Toggle(){PORT::Toggle(1 << PIN);} static void SetDirRead(){PORT::DirClear(1 << PIN);} static void SetDirWrite(){PORT::DirSet(1 << PIN);} static uint8_t IsSet(){return PORT::PinRead() & (uint8_t)(1 << PIN);} static void WaiteForSet(){ while(IsSet()==0){} } static void WaiteForClear(){ while(IsSet()){} } }; //class TPin... // this class represents one bit in TCCR port. // used to set/clear TCCRx bits // It is fully static. template class TCom { // BOOST_STATIC_ASSERT(PIN < PORT::Width); public: typedef TCCR Tccr; enum{Com = COM}; static void Set() { TCCR::Set(1 << COM); } static void Clear() { TCCR::Clear(1 << COM); } static void Toggle() { TCCR::Toggle(1 << COM); } }; //class TCom... //Short pin definitions #ifdef USE_PORTA typedef TPin Pa0; typedef TPin Pa1; typedef TPin Pa2; typedef TPin Pa3; typedef TPin Pa4; typedef TPin Pa5; typedef TPin Pa6; typedef TPin Pa7; #endif #ifdef USE_PORTB typedef TPin Pb0; typedef TPin Pb1; typedef TPin Pb2; typedef TPin Pb3; typedef TPin Pb4; typedef TPin Pb5; typedef TPin Pb6; typedef TPin Pb7; #endif #ifdef USE_PORTC typedef TPin Pc0; typedef TPin Pc1; typedef TPin Pc2; typedef TPin Pc3; typedef TPin Pc4; typedef TPin Pc5; typedef TPin Pc6; typedef TPin Pc7; #endif #ifdef USE_PORTD typedef TPin Pd0; typedef TPin Pd1; typedef TPin Pd2; typedef TPin Pd3; typedef TPin Pd4; typedef TPin Pd5; typedef TPin Pd6; typedef TPin Pd7; #endif #ifdef USE_PORTE typedef TPin Pe0; typedef TPin Pe1; typedef TPin Pe2; typedef TPin Pe3; typedef TPin Pe4; typedef TPin Pe5; typedef TPin Pe6; typedef TPin Pe7; #endif #ifdef USE_PORTF typedef TPin Pf0; typedef TPin Pf1; typedef TPin Pf2; typedef TPin Pf3; typedef TPin Pf4; typedef TPin Pf5; typedef TPin Pf6; typedef TPin Pf7; #endif #ifdef USE_PORTG typedef TPin Pg0; typedef TPin Pg1; typedef TPin Pg2; typedef TPin Pg3; typedef TPin Pg4; typedef TPin Pg5; typedef TPin Pg6; typedef TPin Pg7; #endif #ifdef USE_PORTH typedef TPin Ph0; typedef TPin Ph1; typedef TPin Ph2; typedef TPin Ph3; typedef TPin Ph4; typedef TPin Ph5; typedef TPin Ph6; typedef TPin Ph7; #endif #ifdef USE_PORTJ typedef TPin Pj0; typedef TPin Pj1; typedef TPin Pj2; typedef TPin Pj3; typedef TPin Pj4; typedef TPin Pj5; typedef TPin Pj6; typedef TPin Pj7; #endif #ifdef USE_PORTK typedef TPin Pk0; typedef TPin Pk1; typedef TPin Pk2; typedef TPin Pk3; typedef TPin Pk4; typedef TPin Pk5; typedef TPin Pk6; typedef TPin Pk7; #endif #ifdef USE_PORTQ typedef TPin Pq0; typedef TPin Pq1; typedef TPin Pq2; typedef TPin Pq3; typedef TPin Pq4; typedef TPin Pq5; typedef TPin Pq6; typedef TPin Pq7; #endif #ifdef USE_PORTR typedef TPin Pr0; typedef TPin Pr1; typedef TPin Pr2; typedef TPin Pr3; typedef TPin Pr4; typedef TPin Pr5; typedef TPin Pr6; typedef TPin Pr7; #endif #ifdef USE_TCCR0A typedef TCom Tc0a; //P6 typedef TCom Tc0b; //P5 #endif #ifdef USE_TCCR1A typedef TCom Tc1a; //P9 typedef TCom Tc1b; //P10 #endif #ifdef USE_TCCR2A typedef TCom Tc2a; //P11 typedef TCom Tc2b; //P3 #endif template class Tp_Tc { public: static void SetDir(uint8_t val){ if(val) SetDirWrite(); else SetDirRead(); } static void SetDirRead(){ Tp_pin::SetDirRead(); //set pin direction Tc_bit::Clear(); //disconnect pin from PWM } static void SetDirWrite(){ Tp_pin::SetDirWrite(); Tc_bit::Clear(); } }; /* pin definitions for cases where it's necessary to clear compare output mode bits */ //typedef Tp_Tc P3; //Arduino pin 3 //typedef Tp_Tc P5; //Arduino pin 5 //typedef Tp_Tc P6; //Arduino pin 6 //typedef Tp_Tc P9; //Arduino pin 9 //typedef Tp_Tc P10; //Arduino pin 10 //typedef Tp_Tc P11; //Arduino pin 11 /* Arduino pin definitions */ #if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__) // "Mega" Arduino pin numbers #define P0 Pe0 #define P1 Pe1 #define P2 Pe4 #define P3 Pe5 #define P4 Pg5 #define P5 Pe5 #define P6 Ph3 #define P7 Ph4 #define P8 Ph5 #define P9 Ph6 #define P10 Pb4 #define P11 Pb5 #define P12 Pb6 #define P13 Pb7 #define P14 Pj1 #define P15 Pj0 #define P16 Ph1 #define P17 Ph0 #define P18 Pd3 #define P19 Pd2 #define P20 Pd1 #define P21 Pd0 #define P22 Pa0 #define P23 Pa1 #define P24 Pa2 #define P25 Pa3 #define P26 Pa4 #define P27 Pa5 #define P28 Pa6 #define P29 Pa7 #define P30 Pc7 #define P31 Pc6 #define P32 Pc5 #define P33 Pc4 #define P34 Pc3 #define P35 Pc2 #define P36 Pc1 #define P37 Pc0 #define P38 Pd7 #define P39 Pg2 #define P40 Pg1 #define P41 Pg0 #define P42 Pl7 #define P43 Pl6 #define P44 Pl5 #define P45 Pl4 #define P46 Pl3 #define P47 Pl2 #define P48 Pl1 #define P49 Pl0 #define P50 Pb3 #define P51 Pb2 #define P52 Pb1 #define P53 Pb0 #endif //"Mega" pin definitions #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) //"Classical" Arduino pin numbers #define P0 Pd0 #define P1 Pd1 #define P2 Pd2 #define P3 Pd3 #define P4 Pd4 #define P5 Pd5 #define P6 Pd6 #define P7 Pd7 #define P8 Pb0 #define P9 Pb1 #define P10 Pb2 #define P11 Pb3 #define P12 Pb4 #define P13 Pb5 #define P14 Pc0 #define P15 Pc1 #define P16 Pc2 #define P17 Pc3 #define P18 Pc4 #define P19 Pc5 #endif #endif //_avrpins_h_