Digital InOut
Diff: Digital_InOut.h
- Revision:
- 0:b2ebb454185a
- Child:
- 1:b4bc3430a055
diff -r 000000000000 -r b2ebb454185a Digital_InOut.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Digital_InOut.h Thu Jun 12 03:00:34 2014 +0000 @@ -0,0 +1,390 @@ +/* +Test Code +#include "Digital_InOut.h" + +DigitalOut myled(LED2); + +int main() { + Digital_InOut toggle(p2_5,output); + Digital_InOut in(p0_9,input); + in.mode(pull_down); + Digital_InOut led2(p1_20,1,output); + toggle=1; + + while(1) { + if(in.read()){ + toggle.input(); + toggle.mode(pull_down); + while(1){ + if(toggle.read()) + led2=1; + else + led2=0; + } + } + else + led2=0; + } +} + + +*/ + +#ifndef DIGITAL_IN_H + #define DIGITAL_IN_H + + #include "mbed.h" + + #define _PINSEL_BASE 0x4002C000 + #define _FIODIR_BASE 0x2009C000 + #define _PINMODE_BASE 0x4002C040 + #define _FIOPIN_BASE 0x2009C014 + #define _FIOSET_BASE 0x2009C018 + #define _FIOCLR_BASE 0x2009C01C + + #ifndef _PIN_ + #define _PIN_ + typedef struct + { + int port; + int pin; + }Pin; + #endif /* Pin */ + + #ifndef _PIN_NAME_ + #define _PIN_NAME_ + // Note many pins are not available as fully functional GPIOs, they are ommitted below + enum Pin_Name + { + p0_0 = 0, + p0_1 = 1, + p0_2 = 2, + p0_3 = 3, + p0_4 = 4, + p0_5 = 5, + p0_6 = 6, + p0_7 = 7, + p0_8 = 8, + p0_9 = 9, + p0_10 = 10, + p0_11 = 11, + /*p0_12 = 12, + p0_13 = 13, + p0_14 = 14,*/ + p0_15 = 15, + p0_16 = 16, + + p0_17 = 17, + p0_18 = 18, + p0_19 = 19, + p0_20 = 20, + p0_21 = 21, + p0_22 = 22, + p0_23 = 23, + p0_24 = 24, + p0_25 = 25, + p0_26 = 26, + /* p0_27 = 27, + p0_28 = 28, + p0_29 = 29, + p0_30 = 30,*/ + + + p1_0 = 100, + p1_1 = 101, + /* p1_2 = 102, + p1_3 = 103,*/ + p1_4 = 104, + /* p1_5 = 105, + p1_6 = 106, + p1_7 = 107,*/ + p1_8 = 108, + p1_9 = 109, + p1_10 = 110, + /* p1_11 = 111, + p1_12 = 112, + p1_13 = 113,*/ + p1_14 = 114, + p1_15 = 115, + + p1_16 = 116, + p1_17 = 117, + p1_18 = 118, + p1_19 = 119, + p1_20 = 120, + p1_21 = 121, + p1_22 = 122, + p1_23 = 123, + p1_24 = 124, + p1_25 = 125, + p1_26 = 126, + p1_27 = 127, + p1_28 = 128, + p1_29 = 129, + p1_30 = 130, + + + p2_0 = 200, + p2_1 = 201, + p2_2 = 202, + p2_3 = 203, + p2_4 = 204, + p2_5 = 205, + p2_6 = 206, + p2_7 = 207, + p2_8 = 208, + p2_9 = 209, + p2_10 = 210, + p2_11 = 211, + p2_12 = 212, + p2_13 = 213, + /* p2_14 = 214, + p2_15 = 215, + + p2_16 = 216, + p2_17 = 217, + p2_18 = 218, + p2_19 = 219, + p2_20 = 220, + p2_21 = 221, + p2_22 = 222, + p2_23 = 223, + p2_24 = 224, + p2_25 = 225, + p2_26 = 226, + p2_27 = 227, + p2_28 = 228, + p2_29 = 229, + p2_30 = 230,*/ + + + /* p3_0 = 300, + p3_1 = 301, + p3_2 = 302, + p3_3 = 303, + p3_4 = 304, + p3_5 = 305, + p3_6 = 306, + p3_7 = 307, + p3_8 = 308, + p3_9 = 309, + p3_10 = 310, + p3_11 = 311, + p3_12 = 312, + p3_13 = 313, + p3_14 = 314, + p3_15 = 315, + + p3_16 = 316, + p3_17 = 317, + p3_18 = 318, + p3_19 = 319, + p3_20 = 320, + p3_21 = 321, + p3_22 = 322, + p3_23 = 323, + p3_24 = 324,*/ + p3_25 = 325, + p3_26 = 326, + /*p3_27 = 327, + p3_28 = 328, + p3_29 = 329, + p3_30 = 330,*/ + + /*p4_0 = 400, + p4_1 = 401, + p4_2 = 402, + p4_3 = 403, + p4_4 = 404, + p4_5 = 405, + p4_6 = 406, + p4_7 = 407, + p4_8 = 408, + p4_9 = 409, + p4_10 = 410, + p4_11 = 411, + p4_12 = 412, + p4_13 = 413, + p4_14 = 414, + p4_15 = 415, + + p4_16 = 416, + p4_17 = 417, + p4_18 = 418, + p4_19 = 419, + p4_20 = 420, + p4_21 = 421, + p4_22 = 422, + p4_23 = 423, + p4_24 = 424, + p4_25 = 425, + p4_26 = 426, + p4_27 = 427,*/ + p4_28 = 428, + p4_29 = 429, + /*p4_30 = 430*/ + + }; + #endif /* Pin_Name */ + + enum Pin_Mode + { + pull_up = 0, + pull_down = 1, + neither = 2, + repeater = 3 + }; + + enum Pin_Direction + { + input = 0, + output = 1 + }; + + class Digital_InOut + { + private: Pin _pin; + bool _Dir; + public: Digital_InOut(Pin_Name IPin, Pin_Direction IDir) + { + _pin.port=(IPin/100); + _pin.pin=(IPin%100); + const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0)); + const int FIODIRx = _pin.port; + + *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO + + if(!IDir) + *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input + else + *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output + _Dir=IDir; + } + + Digital_InOut(Pin_Name IPin, Pin_Mode value, Pin_Direction IDir) + { + _pin.port=(IPin/100); + _pin.pin=(IPin%100); + const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0)); + const int PINMODEx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0)); + const int FIODIRx = _pin.port; + + *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO + _Dir=IDir; + if(!_Dir) + { + *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input + + if(value==0) + { + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_UP + } + else if(value==1) + { + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_DOWN + } + else if(value==2) + { + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << (((_pin.pin%16)*2)+1) ); //NEITHER + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2)); //NEITHER + } + else if(value==3) + { + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << (((_pin.pin%16)*2)+1) ); //REPEATER + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2)); //REPEATER + } + } + } + + Digital_InOut(Pin_Name IPin, int value, Pin_Direction IDir) + { + _pin.port=(IPin/100); + _pin.pin=(IPin%100); + const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0)); + const int FIODIRx = _pin.port; + + _Dir=IDir; + *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO + if(_Dir) + { + *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output + *(uint32_t*)((_FIOSET_BASE*value) + (_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Write + } + } + + void input() + { + const int FIODIRx = _pin.port; + int value=0; + *(uint32_t*)((_FIOSET_BASE*value)+(_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Reset + *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input //Output + _Dir=0; + } + + void output() + { + const int FIODIRx = _pin.port; + *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output + _Dir=1; + } + + int read() + { + return ((*(uint32_t*)(_FIOPIN_BASE +_pin.port*0x20) & (1 << _pin.pin)) >> _pin.pin); //Read + } + + void write(int value) + { + if(_Dir) + { + if (value != 1 && value != 0) return; + *(uint32_t*)((_FIOSET_BASE*value)+(_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Write + } + } + + Digital_InOut& operator= (int value) + { + write(value); + return *this; + } + + Digital_InOut& operator= (Digital_InOut& rhs) + { + write(rhs.read()); + return *this; + } + + void mode(Pin_Mode value) + { + const int PINMODEx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0)); + + if(!_Dir) + { + if(value==0) + { + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_UP + } + else if(value==1) + { + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_DOWN + } + else if(value==2) + { + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << (((_pin.pin%16)*2)+1) ); //NEITHER + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2)); //NEITHER + } + else if(value==3) + { + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << (((_pin.pin%16)*2)+1) ); //REPEATER + *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2)); //REPEATER + } + } + } + + operator int() + { + return read(); + } + }; + +#endif /* DIGITAL_IN_H */ \ No newline at end of file