Digital InOut

Committer:
kiran_mbed
Date:
Sat Sep 27 18:42:42 2014 +0000
Revision:
1:b4bc3430a055
Parent:
0:b2ebb454185a
Recent;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kiran_mbed 0:b2ebb454185a 1 /*
kiran_mbed 0:b2ebb454185a 2 Test Code
kiran_mbed 0:b2ebb454185a 3 #include "Digital_InOut.h"
kiran_mbed 0:b2ebb454185a 4
kiran_mbed 0:b2ebb454185a 5 DigitalOut myled(LED2);
kiran_mbed 0:b2ebb454185a 6
kiran_mbed 0:b2ebb454185a 7 int main() {
kiran_mbed 0:b2ebb454185a 8 Digital_InOut toggle(p2_5,output);
kiran_mbed 0:b2ebb454185a 9 Digital_InOut in(p0_9,input);
kiran_mbed 0:b2ebb454185a 10 in.mode(pull_down);
kiran_mbed 0:b2ebb454185a 11 Digital_InOut led2(p1_20,1,output);
kiran_mbed 0:b2ebb454185a 12 toggle=1;
kiran_mbed 0:b2ebb454185a 13
kiran_mbed 0:b2ebb454185a 14 while(1) {
kiran_mbed 0:b2ebb454185a 15 if(in.read()){
kiran_mbed 0:b2ebb454185a 16 toggle.input();
kiran_mbed 0:b2ebb454185a 17 toggle.mode(pull_down);
kiran_mbed 0:b2ebb454185a 18 while(1){
kiran_mbed 0:b2ebb454185a 19 if(toggle.read())
kiran_mbed 0:b2ebb454185a 20 led2=1;
kiran_mbed 0:b2ebb454185a 21 else
kiran_mbed 0:b2ebb454185a 22 led2=0;
kiran_mbed 0:b2ebb454185a 23 }
kiran_mbed 0:b2ebb454185a 24 }
kiran_mbed 0:b2ebb454185a 25 else
kiran_mbed 0:b2ebb454185a 26 led2=0;
kiran_mbed 0:b2ebb454185a 27 }
kiran_mbed 0:b2ebb454185a 28 }
kiran_mbed 0:b2ebb454185a 29
kiran_mbed 0:b2ebb454185a 30
kiran_mbed 0:b2ebb454185a 31 */
kiran_mbed 0:b2ebb454185a 32
kiran_mbed 1:b4bc3430a055 33 #ifndef DIGITAL_INOUT_H
kiran_mbed 1:b4bc3430a055 34 #define DIGITAL_INOUT_H
kiran_mbed 0:b2ebb454185a 35
kiran_mbed 0:b2ebb454185a 36 #include "mbed.h"
kiran_mbed 0:b2ebb454185a 37
kiran_mbed 0:b2ebb454185a 38 #define _PINSEL_BASE 0x4002C000
kiran_mbed 0:b2ebb454185a 39 #define _FIODIR_BASE 0x2009C000
kiran_mbed 0:b2ebb454185a 40 #define _PINMODE_BASE 0x4002C040
kiran_mbed 0:b2ebb454185a 41 #define _FIOPIN_BASE 0x2009C014
kiran_mbed 0:b2ebb454185a 42 #define _FIOSET_BASE 0x2009C018
kiran_mbed 0:b2ebb454185a 43 #define _FIOCLR_BASE 0x2009C01C
kiran_mbed 0:b2ebb454185a 44
kiran_mbed 0:b2ebb454185a 45 #ifndef _PIN_
kiran_mbed 0:b2ebb454185a 46 #define _PIN_
kiran_mbed 0:b2ebb454185a 47 typedef struct
kiran_mbed 0:b2ebb454185a 48 {
kiran_mbed 0:b2ebb454185a 49 int port;
kiran_mbed 0:b2ebb454185a 50 int pin;
kiran_mbed 0:b2ebb454185a 51 }Pin;
kiran_mbed 0:b2ebb454185a 52 #endif /* Pin */
kiran_mbed 0:b2ebb454185a 53
kiran_mbed 0:b2ebb454185a 54 #ifndef _PIN_NAME_
kiran_mbed 0:b2ebb454185a 55 #define _PIN_NAME_
kiran_mbed 0:b2ebb454185a 56 // Note many pins are not available as fully functional GPIOs, they are ommitted below
kiran_mbed 0:b2ebb454185a 57 enum Pin_Name
kiran_mbed 0:b2ebb454185a 58 {
kiran_mbed 0:b2ebb454185a 59 p0_0 = 0,
kiran_mbed 0:b2ebb454185a 60 p0_1 = 1,
kiran_mbed 0:b2ebb454185a 61 p0_2 = 2,
kiran_mbed 0:b2ebb454185a 62 p0_3 = 3,
kiran_mbed 0:b2ebb454185a 63 p0_4 = 4,
kiran_mbed 0:b2ebb454185a 64 p0_5 = 5,
kiran_mbed 0:b2ebb454185a 65 p0_6 = 6,
kiran_mbed 0:b2ebb454185a 66 p0_7 = 7,
kiran_mbed 0:b2ebb454185a 67 p0_8 = 8,
kiran_mbed 0:b2ebb454185a 68 p0_9 = 9,
kiran_mbed 0:b2ebb454185a 69 p0_10 = 10,
kiran_mbed 0:b2ebb454185a 70 p0_11 = 11,
kiran_mbed 0:b2ebb454185a 71 /*p0_12 = 12,
kiran_mbed 0:b2ebb454185a 72 p0_13 = 13,
kiran_mbed 0:b2ebb454185a 73 p0_14 = 14,*/
kiran_mbed 0:b2ebb454185a 74 p0_15 = 15,
kiran_mbed 0:b2ebb454185a 75 p0_16 = 16,
kiran_mbed 0:b2ebb454185a 76
kiran_mbed 0:b2ebb454185a 77 p0_17 = 17,
kiran_mbed 0:b2ebb454185a 78 p0_18 = 18,
kiran_mbed 0:b2ebb454185a 79 p0_19 = 19,
kiran_mbed 0:b2ebb454185a 80 p0_20 = 20,
kiran_mbed 0:b2ebb454185a 81 p0_21 = 21,
kiran_mbed 0:b2ebb454185a 82 p0_22 = 22,
kiran_mbed 0:b2ebb454185a 83 p0_23 = 23,
kiran_mbed 0:b2ebb454185a 84 p0_24 = 24,
kiran_mbed 0:b2ebb454185a 85 p0_25 = 25,
kiran_mbed 0:b2ebb454185a 86 p0_26 = 26,
kiran_mbed 0:b2ebb454185a 87 /* p0_27 = 27,
kiran_mbed 0:b2ebb454185a 88 p0_28 = 28,
kiran_mbed 0:b2ebb454185a 89 p0_29 = 29,
kiran_mbed 0:b2ebb454185a 90 p0_30 = 30,*/
kiran_mbed 0:b2ebb454185a 91
kiran_mbed 0:b2ebb454185a 92
kiran_mbed 0:b2ebb454185a 93 p1_0 = 100,
kiran_mbed 0:b2ebb454185a 94 p1_1 = 101,
kiran_mbed 0:b2ebb454185a 95 /* p1_2 = 102,
kiran_mbed 0:b2ebb454185a 96 p1_3 = 103,*/
kiran_mbed 0:b2ebb454185a 97 p1_4 = 104,
kiran_mbed 0:b2ebb454185a 98 /* p1_5 = 105,
kiran_mbed 0:b2ebb454185a 99 p1_6 = 106,
kiran_mbed 0:b2ebb454185a 100 p1_7 = 107,*/
kiran_mbed 0:b2ebb454185a 101 p1_8 = 108,
kiran_mbed 0:b2ebb454185a 102 p1_9 = 109,
kiran_mbed 0:b2ebb454185a 103 p1_10 = 110,
kiran_mbed 0:b2ebb454185a 104 /* p1_11 = 111,
kiran_mbed 0:b2ebb454185a 105 p1_12 = 112,
kiran_mbed 0:b2ebb454185a 106 p1_13 = 113,*/
kiran_mbed 0:b2ebb454185a 107 p1_14 = 114,
kiran_mbed 0:b2ebb454185a 108 p1_15 = 115,
kiran_mbed 0:b2ebb454185a 109
kiran_mbed 0:b2ebb454185a 110 p1_16 = 116,
kiran_mbed 0:b2ebb454185a 111 p1_17 = 117,
kiran_mbed 0:b2ebb454185a 112 p1_18 = 118,
kiran_mbed 0:b2ebb454185a 113 p1_19 = 119,
kiran_mbed 0:b2ebb454185a 114 p1_20 = 120,
kiran_mbed 0:b2ebb454185a 115 p1_21 = 121,
kiran_mbed 0:b2ebb454185a 116 p1_22 = 122,
kiran_mbed 0:b2ebb454185a 117 p1_23 = 123,
kiran_mbed 0:b2ebb454185a 118 p1_24 = 124,
kiran_mbed 0:b2ebb454185a 119 p1_25 = 125,
kiran_mbed 0:b2ebb454185a 120 p1_26 = 126,
kiran_mbed 0:b2ebb454185a 121 p1_27 = 127,
kiran_mbed 0:b2ebb454185a 122 p1_28 = 128,
kiran_mbed 0:b2ebb454185a 123 p1_29 = 129,
kiran_mbed 0:b2ebb454185a 124 p1_30 = 130,
kiran_mbed 0:b2ebb454185a 125
kiran_mbed 0:b2ebb454185a 126
kiran_mbed 0:b2ebb454185a 127 p2_0 = 200,
kiran_mbed 0:b2ebb454185a 128 p2_1 = 201,
kiran_mbed 0:b2ebb454185a 129 p2_2 = 202,
kiran_mbed 0:b2ebb454185a 130 p2_3 = 203,
kiran_mbed 0:b2ebb454185a 131 p2_4 = 204,
kiran_mbed 0:b2ebb454185a 132 p2_5 = 205,
kiran_mbed 0:b2ebb454185a 133 p2_6 = 206,
kiran_mbed 0:b2ebb454185a 134 p2_7 = 207,
kiran_mbed 0:b2ebb454185a 135 p2_8 = 208,
kiran_mbed 0:b2ebb454185a 136 p2_9 = 209,
kiran_mbed 0:b2ebb454185a 137 p2_10 = 210,
kiran_mbed 0:b2ebb454185a 138 p2_11 = 211,
kiran_mbed 0:b2ebb454185a 139 p2_12 = 212,
kiran_mbed 0:b2ebb454185a 140 p2_13 = 213,
kiran_mbed 0:b2ebb454185a 141 /* p2_14 = 214,
kiran_mbed 0:b2ebb454185a 142 p2_15 = 215,
kiran_mbed 0:b2ebb454185a 143
kiran_mbed 0:b2ebb454185a 144 p2_16 = 216,
kiran_mbed 0:b2ebb454185a 145 p2_17 = 217,
kiran_mbed 0:b2ebb454185a 146 p2_18 = 218,
kiran_mbed 0:b2ebb454185a 147 p2_19 = 219,
kiran_mbed 0:b2ebb454185a 148 p2_20 = 220,
kiran_mbed 0:b2ebb454185a 149 p2_21 = 221,
kiran_mbed 0:b2ebb454185a 150 p2_22 = 222,
kiran_mbed 0:b2ebb454185a 151 p2_23 = 223,
kiran_mbed 0:b2ebb454185a 152 p2_24 = 224,
kiran_mbed 0:b2ebb454185a 153 p2_25 = 225,
kiran_mbed 0:b2ebb454185a 154 p2_26 = 226,
kiran_mbed 0:b2ebb454185a 155 p2_27 = 227,
kiran_mbed 0:b2ebb454185a 156 p2_28 = 228,
kiran_mbed 0:b2ebb454185a 157 p2_29 = 229,
kiran_mbed 0:b2ebb454185a 158 p2_30 = 230,*/
kiran_mbed 0:b2ebb454185a 159
kiran_mbed 0:b2ebb454185a 160
kiran_mbed 0:b2ebb454185a 161 /* p3_0 = 300,
kiran_mbed 0:b2ebb454185a 162 p3_1 = 301,
kiran_mbed 0:b2ebb454185a 163 p3_2 = 302,
kiran_mbed 0:b2ebb454185a 164 p3_3 = 303,
kiran_mbed 0:b2ebb454185a 165 p3_4 = 304,
kiran_mbed 0:b2ebb454185a 166 p3_5 = 305,
kiran_mbed 0:b2ebb454185a 167 p3_6 = 306,
kiran_mbed 0:b2ebb454185a 168 p3_7 = 307,
kiran_mbed 0:b2ebb454185a 169 p3_8 = 308,
kiran_mbed 0:b2ebb454185a 170 p3_9 = 309,
kiran_mbed 0:b2ebb454185a 171 p3_10 = 310,
kiran_mbed 0:b2ebb454185a 172 p3_11 = 311,
kiran_mbed 0:b2ebb454185a 173 p3_12 = 312,
kiran_mbed 0:b2ebb454185a 174 p3_13 = 313,
kiran_mbed 0:b2ebb454185a 175 p3_14 = 314,
kiran_mbed 0:b2ebb454185a 176 p3_15 = 315,
kiran_mbed 0:b2ebb454185a 177
kiran_mbed 0:b2ebb454185a 178 p3_16 = 316,
kiran_mbed 0:b2ebb454185a 179 p3_17 = 317,
kiran_mbed 0:b2ebb454185a 180 p3_18 = 318,
kiran_mbed 0:b2ebb454185a 181 p3_19 = 319,
kiran_mbed 0:b2ebb454185a 182 p3_20 = 320,
kiran_mbed 0:b2ebb454185a 183 p3_21 = 321,
kiran_mbed 0:b2ebb454185a 184 p3_22 = 322,
kiran_mbed 0:b2ebb454185a 185 p3_23 = 323,
kiran_mbed 0:b2ebb454185a 186 p3_24 = 324,*/
kiran_mbed 0:b2ebb454185a 187 p3_25 = 325,
kiran_mbed 0:b2ebb454185a 188 p3_26 = 326,
kiran_mbed 0:b2ebb454185a 189 /*p3_27 = 327,
kiran_mbed 0:b2ebb454185a 190 p3_28 = 328,
kiran_mbed 0:b2ebb454185a 191 p3_29 = 329,
kiran_mbed 0:b2ebb454185a 192 p3_30 = 330,*/
kiran_mbed 0:b2ebb454185a 193
kiran_mbed 0:b2ebb454185a 194 /*p4_0 = 400,
kiran_mbed 0:b2ebb454185a 195 p4_1 = 401,
kiran_mbed 0:b2ebb454185a 196 p4_2 = 402,
kiran_mbed 0:b2ebb454185a 197 p4_3 = 403,
kiran_mbed 0:b2ebb454185a 198 p4_4 = 404,
kiran_mbed 0:b2ebb454185a 199 p4_5 = 405,
kiran_mbed 0:b2ebb454185a 200 p4_6 = 406,
kiran_mbed 0:b2ebb454185a 201 p4_7 = 407,
kiran_mbed 0:b2ebb454185a 202 p4_8 = 408,
kiran_mbed 0:b2ebb454185a 203 p4_9 = 409,
kiran_mbed 0:b2ebb454185a 204 p4_10 = 410,
kiran_mbed 0:b2ebb454185a 205 p4_11 = 411,
kiran_mbed 0:b2ebb454185a 206 p4_12 = 412,
kiran_mbed 0:b2ebb454185a 207 p4_13 = 413,
kiran_mbed 0:b2ebb454185a 208 p4_14 = 414,
kiran_mbed 0:b2ebb454185a 209 p4_15 = 415,
kiran_mbed 0:b2ebb454185a 210
kiran_mbed 0:b2ebb454185a 211 p4_16 = 416,
kiran_mbed 0:b2ebb454185a 212 p4_17 = 417,
kiran_mbed 0:b2ebb454185a 213 p4_18 = 418,
kiran_mbed 0:b2ebb454185a 214 p4_19 = 419,
kiran_mbed 0:b2ebb454185a 215 p4_20 = 420,
kiran_mbed 0:b2ebb454185a 216 p4_21 = 421,
kiran_mbed 0:b2ebb454185a 217 p4_22 = 422,
kiran_mbed 0:b2ebb454185a 218 p4_23 = 423,
kiran_mbed 0:b2ebb454185a 219 p4_24 = 424,
kiran_mbed 0:b2ebb454185a 220 p4_25 = 425,
kiran_mbed 0:b2ebb454185a 221 p4_26 = 426,
kiran_mbed 0:b2ebb454185a 222 p4_27 = 427,*/
kiran_mbed 0:b2ebb454185a 223 p4_28 = 428,
kiran_mbed 0:b2ebb454185a 224 p4_29 = 429,
kiran_mbed 0:b2ebb454185a 225 /*p4_30 = 430*/
kiran_mbed 0:b2ebb454185a 226
kiran_mbed 0:b2ebb454185a 227 };
kiran_mbed 0:b2ebb454185a 228 #endif /* Pin_Name */
kiran_mbed 0:b2ebb454185a 229
kiran_mbed 1:b4bc3430a055 230 #ifndef _PIN_MODE_
kiran_mbed 1:b4bc3430a055 231 #define _PIN_MODE_
kiran_mbed 0:b2ebb454185a 232 enum Pin_Mode
kiran_mbed 0:b2ebb454185a 233 {
kiran_mbed 0:b2ebb454185a 234 pull_up = 0,
kiran_mbed 0:b2ebb454185a 235 pull_down = 1,
kiran_mbed 0:b2ebb454185a 236 neither = 2,
kiran_mbed 0:b2ebb454185a 237 repeater = 3
kiran_mbed 0:b2ebb454185a 238 };
kiran_mbed 1:b4bc3430a055 239 #endif /* _PIN_MODE_ */
kiran_mbed 0:b2ebb454185a 240
kiran_mbed 1:b4bc3430a055 241 #ifndef _PIN_DIRECTION_
kiran_mbed 1:b4bc3430a055 242 #define _PIN_DIRECTION_
kiran_mbed 0:b2ebb454185a 243 enum Pin_Direction
kiran_mbed 0:b2ebb454185a 244 {
kiran_mbed 0:b2ebb454185a 245 input = 0,
kiran_mbed 0:b2ebb454185a 246 output = 1
kiran_mbed 0:b2ebb454185a 247 };
kiran_mbed 1:b4bc3430a055 248 #endif/* _PIN_DIRECTION_ */
kiran_mbed 0:b2ebb454185a 249
kiran_mbed 0:b2ebb454185a 250 class Digital_InOut
kiran_mbed 0:b2ebb454185a 251 {
kiran_mbed 0:b2ebb454185a 252 private: Pin _pin;
kiran_mbed 0:b2ebb454185a 253 bool _Dir;
kiran_mbed 0:b2ebb454185a 254 public: Digital_InOut(Pin_Name IPin, Pin_Direction IDir)
kiran_mbed 0:b2ebb454185a 255 {
kiran_mbed 0:b2ebb454185a 256 _pin.port=(IPin/100);
kiran_mbed 0:b2ebb454185a 257 _pin.pin=(IPin%100);
kiran_mbed 0:b2ebb454185a 258 const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 259 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 260
kiran_mbed 0:b2ebb454185a 261 *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO
kiran_mbed 0:b2ebb454185a 262
kiran_mbed 0:b2ebb454185a 263 if(!IDir)
kiran_mbed 0:b2ebb454185a 264 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input
kiran_mbed 0:b2ebb454185a 265 else
kiran_mbed 0:b2ebb454185a 266 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output
kiran_mbed 0:b2ebb454185a 267 _Dir=IDir;
kiran_mbed 0:b2ebb454185a 268 }
kiran_mbed 0:b2ebb454185a 269
kiran_mbed 0:b2ebb454185a 270 Digital_InOut(Pin_Name IPin, Pin_Mode value, Pin_Direction IDir)
kiran_mbed 0:b2ebb454185a 271 {
kiran_mbed 0:b2ebb454185a 272 _pin.port=(IPin/100);
kiran_mbed 0:b2ebb454185a 273 _pin.pin=(IPin%100);
kiran_mbed 0:b2ebb454185a 274 const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 275 const int PINMODEx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 276 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 277
kiran_mbed 0:b2ebb454185a 278 *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO
kiran_mbed 0:b2ebb454185a 279 _Dir=IDir;
kiran_mbed 0:b2ebb454185a 280 if(!_Dir)
kiran_mbed 0:b2ebb454185a 281 {
kiran_mbed 0:b2ebb454185a 282 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input
kiran_mbed 0:b2ebb454185a 283
kiran_mbed 0:b2ebb454185a 284 if(value==0)
kiran_mbed 0:b2ebb454185a 285 {
kiran_mbed 0:b2ebb454185a 286 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_UP
kiran_mbed 0:b2ebb454185a 287 }
kiran_mbed 0:b2ebb454185a 288 else if(value==1)
kiran_mbed 0:b2ebb454185a 289 {
kiran_mbed 0:b2ebb454185a 290 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_DOWN
kiran_mbed 0:b2ebb454185a 291 }
kiran_mbed 0:b2ebb454185a 292 else if(value==2)
kiran_mbed 0:b2ebb454185a 293 {
kiran_mbed 0:b2ebb454185a 294 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << (((_pin.pin%16)*2)+1) ); //NEITHER
kiran_mbed 0:b2ebb454185a 295 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2)); //NEITHER
kiran_mbed 0:b2ebb454185a 296 }
kiran_mbed 0:b2ebb454185a 297 else if(value==3)
kiran_mbed 0:b2ebb454185a 298 {
kiran_mbed 0:b2ebb454185a 299 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << (((_pin.pin%16)*2)+1) ); //REPEATER
kiran_mbed 0:b2ebb454185a 300 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2)); //REPEATER
kiran_mbed 0:b2ebb454185a 301 }
kiran_mbed 0:b2ebb454185a 302 }
kiran_mbed 0:b2ebb454185a 303 }
kiran_mbed 0:b2ebb454185a 304
kiran_mbed 0:b2ebb454185a 305 Digital_InOut(Pin_Name IPin, int value, Pin_Direction IDir)
kiran_mbed 0:b2ebb454185a 306 {
kiran_mbed 0:b2ebb454185a 307 _pin.port=(IPin/100);
kiran_mbed 0:b2ebb454185a 308 _pin.pin=(IPin%100);
kiran_mbed 0:b2ebb454185a 309 const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 310 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 311
kiran_mbed 0:b2ebb454185a 312 _Dir=IDir;
kiran_mbed 0:b2ebb454185a 313 *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO
kiran_mbed 0:b2ebb454185a 314 if(_Dir)
kiran_mbed 0:b2ebb454185a 315 {
kiran_mbed 0:b2ebb454185a 316 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output
kiran_mbed 0:b2ebb454185a 317 *(uint32_t*)((_FIOSET_BASE*value) + (_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Write
kiran_mbed 0:b2ebb454185a 318 }
kiran_mbed 0:b2ebb454185a 319 }
kiran_mbed 0:b2ebb454185a 320
kiran_mbed 0:b2ebb454185a 321 void input()
kiran_mbed 0:b2ebb454185a 322 {
kiran_mbed 0:b2ebb454185a 323 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 324 int value=0;
kiran_mbed 0:b2ebb454185a 325 *(uint32_t*)((_FIOSET_BASE*value)+(_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Reset
kiran_mbed 0:b2ebb454185a 326 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input //Output
kiran_mbed 0:b2ebb454185a 327 _Dir=0;
kiran_mbed 0:b2ebb454185a 328 }
kiran_mbed 0:b2ebb454185a 329
kiran_mbed 0:b2ebb454185a 330 void output()
kiran_mbed 0:b2ebb454185a 331 {
kiran_mbed 0:b2ebb454185a 332 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 333 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output
kiran_mbed 0:b2ebb454185a 334 _Dir=1;
kiran_mbed 0:b2ebb454185a 335 }
kiran_mbed 0:b2ebb454185a 336
kiran_mbed 0:b2ebb454185a 337 int read()
kiran_mbed 0:b2ebb454185a 338 {
kiran_mbed 1:b4bc3430a055 339 int FIOPINvalue = *(uint32_t*)(_FIOPIN_BASE +_pin.port*0x20);
kiran_mbed 1:b4bc3430a055 340 return ((FIOPINvalue & (1 << _pin.pin)) >> _pin.pin);
kiran_mbed 0:b2ebb454185a 341 }
kiran_mbed 0:b2ebb454185a 342
kiran_mbed 0:b2ebb454185a 343 void write(int value)
kiran_mbed 0:b2ebb454185a 344 {
kiran_mbed 0:b2ebb454185a 345 if(_Dir)
kiran_mbed 0:b2ebb454185a 346 {
kiran_mbed 0:b2ebb454185a 347 if (value != 1 && value != 0) return;
kiran_mbed 0:b2ebb454185a 348 *(uint32_t*)((_FIOSET_BASE*value)+(_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Write
kiran_mbed 0:b2ebb454185a 349 }
kiran_mbed 0:b2ebb454185a 350 }
kiran_mbed 0:b2ebb454185a 351
kiran_mbed 0:b2ebb454185a 352 Digital_InOut& operator= (int value)
kiran_mbed 0:b2ebb454185a 353 {
kiran_mbed 0:b2ebb454185a 354 write(value);
kiran_mbed 0:b2ebb454185a 355 return *this;
kiran_mbed 0:b2ebb454185a 356 }
kiran_mbed 0:b2ebb454185a 357
kiran_mbed 0:b2ebb454185a 358 Digital_InOut& operator= (Digital_InOut& rhs)
kiran_mbed 0:b2ebb454185a 359 {
kiran_mbed 0:b2ebb454185a 360 write(rhs.read());
kiran_mbed 0:b2ebb454185a 361 return *this;
kiran_mbed 0:b2ebb454185a 362 }
kiran_mbed 0:b2ebb454185a 363
kiran_mbed 0:b2ebb454185a 364 void mode(Pin_Mode value)
kiran_mbed 0:b2ebb454185a 365 {
kiran_mbed 0:b2ebb454185a 366 const int PINMODEx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 367
kiran_mbed 0:b2ebb454185a 368 if(!_Dir)
kiran_mbed 0:b2ebb454185a 369 {
kiran_mbed 0:b2ebb454185a 370 if(value==0)
kiran_mbed 0:b2ebb454185a 371 {
kiran_mbed 0:b2ebb454185a 372 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_UP
kiran_mbed 0:b2ebb454185a 373 }
kiran_mbed 0:b2ebb454185a 374 else if(value==1)
kiran_mbed 0:b2ebb454185a 375 {
kiran_mbed 0:b2ebb454185a 376 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_DOWN
kiran_mbed 0:b2ebb454185a 377 }
kiran_mbed 0:b2ebb454185a 378 else if(value==2)
kiran_mbed 0:b2ebb454185a 379 {
kiran_mbed 0:b2ebb454185a 380 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << (((_pin.pin%16)*2)+1) ); //NEITHER
kiran_mbed 0:b2ebb454185a 381 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2)); //NEITHER
kiran_mbed 0:b2ebb454185a 382 }
kiran_mbed 0:b2ebb454185a 383 else if(value==3)
kiran_mbed 0:b2ebb454185a 384 {
kiran_mbed 0:b2ebb454185a 385 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << (((_pin.pin%16)*2)+1) ); //REPEATER
kiran_mbed 0:b2ebb454185a 386 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2)); //REPEATER
kiran_mbed 0:b2ebb454185a 387 }
kiran_mbed 0:b2ebb454185a 388 }
kiran_mbed 0:b2ebb454185a 389 }
kiran_mbed 0:b2ebb454185a 390
kiran_mbed 0:b2ebb454185a 391 operator int()
kiran_mbed 0:b2ebb454185a 392 {
kiran_mbed 0:b2ebb454185a 393 return read();
kiran_mbed 0:b2ebb454185a 394 }
kiran_mbed 0:b2ebb454185a 395 };
kiran_mbed 0:b2ebb454185a 396
kiran_mbed 1:b4bc3430a055 397 #endif /* DIGITAL_INOUT_H */