Digital InOut

Committer:
kiran_mbed
Date:
Thu Jun 12 03:00:34 2014 +0000
Revision:
0:b2ebb454185a
Child:
1:b4bc3430a055
Digital InOut for all pins

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 0:b2ebb454185a 33 #ifndef DIGITAL_IN_H
kiran_mbed 0:b2ebb454185a 34 #define DIGITAL_IN_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 0:b2ebb454185a 230 enum Pin_Mode
kiran_mbed 0:b2ebb454185a 231 {
kiran_mbed 0:b2ebb454185a 232 pull_up = 0,
kiran_mbed 0:b2ebb454185a 233 pull_down = 1,
kiran_mbed 0:b2ebb454185a 234 neither = 2,
kiran_mbed 0:b2ebb454185a 235 repeater = 3
kiran_mbed 0:b2ebb454185a 236 };
kiran_mbed 0:b2ebb454185a 237
kiran_mbed 0:b2ebb454185a 238 enum Pin_Direction
kiran_mbed 0:b2ebb454185a 239 {
kiran_mbed 0:b2ebb454185a 240 input = 0,
kiran_mbed 0:b2ebb454185a 241 output = 1
kiran_mbed 0:b2ebb454185a 242 };
kiran_mbed 0:b2ebb454185a 243
kiran_mbed 0:b2ebb454185a 244 class Digital_InOut
kiran_mbed 0:b2ebb454185a 245 {
kiran_mbed 0:b2ebb454185a 246 private: Pin _pin;
kiran_mbed 0:b2ebb454185a 247 bool _Dir;
kiran_mbed 0:b2ebb454185a 248 public: Digital_InOut(Pin_Name IPin, Pin_Direction IDir)
kiran_mbed 0:b2ebb454185a 249 {
kiran_mbed 0:b2ebb454185a 250 _pin.port=(IPin/100);
kiran_mbed 0:b2ebb454185a 251 _pin.pin=(IPin%100);
kiran_mbed 0:b2ebb454185a 252 const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 253 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 254
kiran_mbed 0:b2ebb454185a 255 *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO
kiran_mbed 0:b2ebb454185a 256
kiran_mbed 0:b2ebb454185a 257 if(!IDir)
kiran_mbed 0:b2ebb454185a 258 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input
kiran_mbed 0:b2ebb454185a 259 else
kiran_mbed 0:b2ebb454185a 260 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output
kiran_mbed 0:b2ebb454185a 261 _Dir=IDir;
kiran_mbed 0:b2ebb454185a 262 }
kiran_mbed 0:b2ebb454185a 263
kiran_mbed 0:b2ebb454185a 264 Digital_InOut(Pin_Name IPin, Pin_Mode value, Pin_Direction IDir)
kiran_mbed 0:b2ebb454185a 265 {
kiran_mbed 0:b2ebb454185a 266 _pin.port=(IPin/100);
kiran_mbed 0:b2ebb454185a 267 _pin.pin=(IPin%100);
kiran_mbed 0:b2ebb454185a 268 const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 269 const int PINMODEx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 270 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 271
kiran_mbed 0:b2ebb454185a 272 *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO
kiran_mbed 0:b2ebb454185a 273 _Dir=IDir;
kiran_mbed 0:b2ebb454185a 274 if(!_Dir)
kiran_mbed 0:b2ebb454185a 275 {
kiran_mbed 0:b2ebb454185a 276 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input
kiran_mbed 0:b2ebb454185a 277
kiran_mbed 0:b2ebb454185a 278 if(value==0)
kiran_mbed 0:b2ebb454185a 279 {
kiran_mbed 0:b2ebb454185a 280 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_UP
kiran_mbed 0:b2ebb454185a 281 }
kiran_mbed 0:b2ebb454185a 282 else if(value==1)
kiran_mbed 0:b2ebb454185a 283 {
kiran_mbed 0:b2ebb454185a 284 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_DOWN
kiran_mbed 0:b2ebb454185a 285 }
kiran_mbed 0:b2ebb454185a 286 else if(value==2)
kiran_mbed 0:b2ebb454185a 287 {
kiran_mbed 0:b2ebb454185a 288 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << (((_pin.pin%16)*2)+1) ); //NEITHER
kiran_mbed 0:b2ebb454185a 289 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2)); //NEITHER
kiran_mbed 0:b2ebb454185a 290 }
kiran_mbed 0:b2ebb454185a 291 else if(value==3)
kiran_mbed 0:b2ebb454185a 292 {
kiran_mbed 0:b2ebb454185a 293 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << (((_pin.pin%16)*2)+1) ); //REPEATER
kiran_mbed 0:b2ebb454185a 294 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2)); //REPEATER
kiran_mbed 0:b2ebb454185a 295 }
kiran_mbed 0:b2ebb454185a 296 }
kiran_mbed 0:b2ebb454185a 297 }
kiran_mbed 0:b2ebb454185a 298
kiran_mbed 0:b2ebb454185a 299 Digital_InOut(Pin_Name IPin, int value, Pin_Direction IDir)
kiran_mbed 0:b2ebb454185a 300 {
kiran_mbed 0:b2ebb454185a 301 _pin.port=(IPin/100);
kiran_mbed 0:b2ebb454185a 302 _pin.pin=(IPin%100);
kiran_mbed 0:b2ebb454185a 303 const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 304 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 305
kiran_mbed 0:b2ebb454185a 306 _Dir=IDir;
kiran_mbed 0:b2ebb454185a 307 *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //GPIO
kiran_mbed 0:b2ebb454185a 308 if(_Dir)
kiran_mbed 0:b2ebb454185a 309 {
kiran_mbed 0:b2ebb454185a 310 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output
kiran_mbed 0:b2ebb454185a 311 *(uint32_t*)((_FIOSET_BASE*value) + (_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Write
kiran_mbed 0:b2ebb454185a 312 }
kiran_mbed 0:b2ebb454185a 313 }
kiran_mbed 0:b2ebb454185a 314
kiran_mbed 0:b2ebb454185a 315 void input()
kiran_mbed 0:b2ebb454185a 316 {
kiran_mbed 0:b2ebb454185a 317 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 318 int value=0;
kiran_mbed 0:b2ebb454185a 319 *(uint32_t*)((_FIOSET_BASE*value)+(_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Reset
kiran_mbed 0:b2ebb454185a 320 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) &= ~(1 << _pin.pin); //Input //Output
kiran_mbed 0:b2ebb454185a 321 _Dir=0;
kiran_mbed 0:b2ebb454185a 322 }
kiran_mbed 0:b2ebb454185a 323
kiran_mbed 0:b2ebb454185a 324 void output()
kiran_mbed 0:b2ebb454185a 325 {
kiran_mbed 0:b2ebb454185a 326 const int FIODIRx = _pin.port;
kiran_mbed 0:b2ebb454185a 327 *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin); //Output
kiran_mbed 0:b2ebb454185a 328 _Dir=1;
kiran_mbed 0:b2ebb454185a 329 }
kiran_mbed 0:b2ebb454185a 330
kiran_mbed 0:b2ebb454185a 331 int read()
kiran_mbed 0:b2ebb454185a 332 {
kiran_mbed 0:b2ebb454185a 333 return ((*(uint32_t*)(_FIOPIN_BASE +_pin.port*0x20) & (1 << _pin.pin)) >> _pin.pin); //Read
kiran_mbed 0:b2ebb454185a 334 }
kiran_mbed 0:b2ebb454185a 335
kiran_mbed 0:b2ebb454185a 336 void write(int value)
kiran_mbed 0:b2ebb454185a 337 {
kiran_mbed 0:b2ebb454185a 338 if(_Dir)
kiran_mbed 0:b2ebb454185a 339 {
kiran_mbed 0:b2ebb454185a 340 if (value != 1 && value != 0) return;
kiran_mbed 0:b2ebb454185a 341 *(uint32_t*)((_FIOSET_BASE*value)+(_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin); //Write
kiran_mbed 0:b2ebb454185a 342 }
kiran_mbed 0:b2ebb454185a 343 }
kiran_mbed 0:b2ebb454185a 344
kiran_mbed 0:b2ebb454185a 345 Digital_InOut& operator= (int value)
kiran_mbed 0:b2ebb454185a 346 {
kiran_mbed 0:b2ebb454185a 347 write(value);
kiran_mbed 0:b2ebb454185a 348 return *this;
kiran_mbed 0:b2ebb454185a 349 }
kiran_mbed 0:b2ebb454185a 350
kiran_mbed 0:b2ebb454185a 351 Digital_InOut& operator= (Digital_InOut& rhs)
kiran_mbed 0:b2ebb454185a 352 {
kiran_mbed 0:b2ebb454185a 353 write(rhs.read());
kiran_mbed 0:b2ebb454185a 354 return *this;
kiran_mbed 0:b2ebb454185a 355 }
kiran_mbed 0:b2ebb454185a 356
kiran_mbed 0:b2ebb454185a 357 void mode(Pin_Mode value)
kiran_mbed 0:b2ebb454185a 358 {
kiran_mbed 0:b2ebb454185a 359 const int PINMODEx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
kiran_mbed 0:b2ebb454185a 360
kiran_mbed 0:b2ebb454185a 361 if(!_Dir)
kiran_mbed 0:b2ebb454185a 362 {
kiran_mbed 0:b2ebb454185a 363 if(value==0)
kiran_mbed 0:b2ebb454185a 364 {
kiran_mbed 0:b2ebb454185a 365 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_UP
kiran_mbed 0:b2ebb454185a 366 }
kiran_mbed 0:b2ebb454185a 367 else if(value==1)
kiran_mbed 0:b2ebb454185a 368 {
kiran_mbed 0:b2ebb454185a 369 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1))); //PULL_DOWN
kiran_mbed 0:b2ebb454185a 370 }
kiran_mbed 0:b2ebb454185a 371 else if(value==2)
kiran_mbed 0:b2ebb454185a 372 {
kiran_mbed 0:b2ebb454185a 373 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << (((_pin.pin%16)*2)+1) ); //NEITHER
kiran_mbed 0:b2ebb454185a 374 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << ((_pin.pin%16)*2)); //NEITHER
kiran_mbed 0:b2ebb454185a 375 }
kiran_mbed 0:b2ebb454185a 376 else if(value==3)
kiran_mbed 0:b2ebb454185a 377 {
kiran_mbed 0:b2ebb454185a 378 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) &= ~(1 << (((_pin.pin%16)*2)+1) ); //REPEATER
kiran_mbed 0:b2ebb454185a 379 *(uint32_t*)(_PINMODE_BASE + (PINMODEx*4)) |= (1 << ((_pin.pin%16)*2)); //REPEATER
kiran_mbed 0:b2ebb454185a 380 }
kiran_mbed 0:b2ebb454185a 381 }
kiran_mbed 0:b2ebb454185a 382 }
kiran_mbed 0:b2ebb454185a 383
kiran_mbed 0:b2ebb454185a 384 operator int()
kiran_mbed 0:b2ebb454185a 385 {
kiran_mbed 0:b2ebb454185a 386 return read();
kiran_mbed 0:b2ebb454185a 387 }
kiran_mbed 0:b2ebb454185a 388 };
kiran_mbed 0:b2ebb454185a 389
kiran_mbed 0:b2ebb454185a 390 #endif /* DIGITAL_IN_H */