Kiran George Vetteth / Mbed 2 deprecated Steering_Display

Dependencies:   Digital_InOut KS0108 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Digital_Out.h Source File

Digital_Out.h

00001 /*
00002 Test Code
00003 #include "Digital_Out.h"
00004 
00005 //Serial pc(USBTX,USBRX);
00006 
00007 int main()
00008 {
00009     Digital_Out ledPin1(p1_18,1);
00010     Digital_Out ledPin2(p1_20,0);
00011     Digital_Out ledPin3(p1_21,1);
00012     Digital_Out ledPin4(p1_23,0);
00013      
00014     
00015     wait(0.5);
00016     while(1)
00017     {
00018         ledPin1= !ledPin1;
00019         ledPin2= !ledPin2;
00020         ledPin3= !ledPin3;
00021         ledPin4= !ledPin4;
00022          
00023         wait(0.5);
00024         ledPin1= !ledPin1;
00025         ledPin2= !ledPin2;
00026         ledPin3= !ledPin3;
00027         ledPin4= !ledPin4;
00028         
00029         wait(0.5);
00030     }
00031 
00032 Digital_Out pin(p0_0);
00033 
00034 DigitalOut myled(LED1);
00035 
00036 Timer t;
00037 int main() {
00038    t.reset();
00039    t.start();
00040    for (int i = 0; i < 1000000; i++){
00041     pin = 1;
00042     pin = 0;
00043    }
00044        t.stop();
00045 
00046    unsigned int time1 = t.read_us(); 
00047     
00048     t.reset();
00049     t.start();
00050     for (int i = 0; i < 1000000; i++) {
00051     myled = 1;
00052     myled = 0;
00053     }
00054     t.stop();
00055     unsigned int time2 = t.read_us();
00056     printf("Library time: %d, mbed time: %d\r\n", time1, time2);
00057     while(1) {
00058 
00059     }
00060 }
00061 */
00062 
00063 #ifndef DIGITAL_OUT_H
00064     #define DIGITAL_OUT_H
00065     
00066     #include"mbed.h"
00067         
00068     #define _PINSEL_BASE 0x4002C000
00069     #define _FIODIR_BASE 0x2009C000
00070     #define _FIOSET_BASE 0x2009C018
00071     #define _FIOCLR_BASE 0x2009C01C
00072     #define _FIOPIN_BASE 0x2009C014
00073     
00074     #ifndef _PIN_
00075         #define _PIN_
00076         typedef struct
00077         {
00078             int port;
00079             int pin;
00080         }Pin;
00081     #endif /* _PIN_ */
00082     
00083     #ifndef _PIN_NAME_
00084         #define _PIN_NAME_
00085         // Note many pins are not available as fully functional GPIOs, they are ommitted below
00086         enum Pin_Name
00087         {
00088             p0_0 = 0,
00089             p0_1 = 1,
00090             p0_2 = 2,
00091             p0_3 = 3,
00092             p0_4 = 4,
00093             p0_5 = 5,
00094             p0_6 = 6,
00095             p0_7 = 7,
00096             p0_8 = 8,
00097             p0_9 = 9,
00098             p0_10 = 10,
00099             p0_11 = 11,
00100             /*p0_12 = 12,
00101             p0_13 = 13,
00102             p0_14 = 14,*/
00103             p0_15 = 15,
00104             p0_16 = 16,
00105         
00106             p0_17 = 17,
00107             p0_18 = 18,
00108             p0_19 = 19,
00109             p0_20 = 20,
00110             p0_21 = 21,
00111             p0_22 = 22,
00112             p0_23 = 23,
00113             p0_24 = 24,
00114             p0_25 = 25,
00115             p0_26 = 26,
00116           /*  p0_27 = 27,
00117             p0_28 = 28,
00118             p0_29 = 29,
00119             p0_30 = 30,*/
00120         
00121         
00122             p1_0 = 100,
00123             p1_1 = 101,
00124            /* p1_2 = 102,
00125             p1_3 = 103,*/
00126             p1_4 = 104,
00127            /* p1_5 = 105,
00128             p1_6 = 106,
00129             p1_7 = 107,*/
00130             p1_8 = 108,
00131             p1_9 = 109,
00132             p1_10 = 110,
00133            /* p1_11 = 111,
00134             p1_12 = 112,
00135             p1_13 = 113,*/
00136             p1_14 = 114,
00137             p1_15 = 115,
00138         
00139             p1_16 = 116,
00140             p1_17 = 117,
00141             p1_18 = 118,
00142             p1_19 = 119,
00143             p1_20 = 120,
00144             p1_21 = 121,
00145             p1_22 = 122,
00146             p1_23 = 123,
00147             p1_24 = 124,
00148             p1_25 = 125,
00149             p1_26 = 126,
00150             p1_27 = 127,
00151             p1_28 = 128,
00152             p1_29 = 129,
00153             p1_30 = 130,
00154         
00155         
00156             p2_0 = 200,
00157             p2_1 = 201,
00158             p2_2 = 202,
00159             p2_3 = 203,
00160             p2_4 = 204,
00161             p2_5 = 205,
00162             p2_6 = 206,
00163             p2_7 = 207,
00164             p2_8 = 208,
00165             p2_9 = 209,
00166             p2_10 = 210,
00167             p2_11 = 211,
00168             p2_12 = 212,
00169             p2_13 = 213,
00170            /* p2_14 = 214,
00171             p2_15 = 215,
00172         
00173             p2_16 = 216,
00174             p2_17 = 217,
00175             p2_18 = 218,
00176             p2_19 = 219,
00177             p2_20 = 220,
00178             p2_21 = 221,
00179             p2_22 = 222,
00180             p2_23 = 223,
00181             p2_24 = 224,
00182             p2_25 = 225,
00183             p2_26 = 226,
00184             p2_27 = 227,
00185             p2_28 = 228,
00186             p2_29 = 229,
00187             p2_30 = 230,*/
00188         
00189         
00190            /* p3_0 = 300,
00191             p3_1 = 301,
00192             p3_2 = 302,
00193             p3_3 = 303,
00194             p3_4 = 304,
00195             p3_5 = 305,
00196             p3_6 = 306,
00197             p3_7 = 307,
00198             p3_8 = 308,
00199             p3_9 = 309,
00200             p3_10 = 310,
00201             p3_11 = 311,
00202             p3_12 = 312,
00203             p3_13 = 313,
00204             p3_14 = 314,
00205             p3_15 = 315,
00206         
00207             p3_16 = 316,
00208             p3_17 = 317,
00209             p3_18 = 318,
00210             p3_19 = 319,
00211             p3_20 = 320,
00212             p3_21 = 321,
00213             p3_22 = 322,
00214             p3_23 = 323,
00215             p3_24 = 324,*/
00216             p3_25 = 325,
00217             p3_26 = 326,
00218             /*p3_27 = 327,
00219             p3_28 = 328,
00220             p3_29 = 329,
00221             p3_30 = 330,*/
00222         
00223             /*p4_0 = 400,
00224             p4_1 = 401,
00225             p4_2 = 402,
00226             p4_3 = 403,
00227             p4_4 = 404,
00228             p4_5 = 405,
00229             p4_6 = 406,
00230             p4_7 = 407,
00231             p4_8 = 408,
00232             p4_9 = 409,
00233             p4_10 = 410,
00234             p4_11 = 411,
00235             p4_12 = 412,
00236             p4_13 = 413,
00237             p4_14 = 414,
00238             p4_15 = 415,
00239         
00240             p4_16 = 416,
00241             p4_17 = 417,
00242             p4_18 = 418,
00243             p4_19 = 419,
00244             p4_20 = 420,
00245             p4_21 = 421,
00246             p4_22 = 422,
00247             p4_23 = 423,
00248             p4_24 = 424,
00249             p4_25 = 425,
00250             p4_26 = 426,
00251             p4_27 = 427,*/
00252             p4_28 = 428,
00253             p4_29 = 429,
00254             /*p4_30 = 430*/
00255         
00256         };
00257     #endif /* _PIN_NAME_ */
00258     
00259     
00260     class Digital_Out
00261     {
00262         private:    Pin _pin;
00263         public:     Digital_Out(Pin_Name IPin)
00264                     {
00265                         _pin.port=(IPin/100);
00266                         _pin.pin=(IPin%100);
00267                         const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
00268                         const int FIODIRx = _pin.port;
00269                         
00270                         *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1)));          //GPIO
00271                         *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin);                                        //Output
00272                     }
00273                     
00274                     Digital_Out(Pin_Name IPin, int value)
00275                     {
00276                         _pin.port=(IPin/100);
00277                         _pin.pin=(IPin%100);
00278                         const int PINSELx = ((_pin.port*2) + (1* (_pin.pin > 15) ? 1:0));
00279                         const int FIODIRx = _pin.port;
00280                         
00281                         *(uint32_t*)(_PINSEL_BASE + (PINSELx*4)) &= ~(1 << ((_pin.pin%16)*2) | (1 << (((_pin.pin%16)*2)+1)));           //GPIO
00282                         *(uint32_t*)(_FIODIR_BASE + (FIODIRx*0x20)) |= (1 << _pin.pin);                                        //Output
00283                         *(uint32_t*)((_FIOSET_BASE*value) + (_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin);   //Write     
00284                     }
00285                     
00286                     void write(int value)
00287                     {
00288                         if (value != 1 && value != 0) return;
00289                         *(uint32_t*)((_FIOSET_BASE*value)+(_FIOCLR_BASE*(1-value)) + (_pin.port*0x20)) |= (1 << _pin.pin);     //Write     
00290                     }
00291                     
00292                     int read()
00293                     {
00294                         return ((*(uint32_t*)(_FIOPIN_BASE +_pin.port*0x20) & (1 << _pin.pin)) >> _pin.pin);                          //Read
00295                     }
00296                     
00297                     Digital_Out& operator= (int value)
00298                     {
00299                         write(value);
00300                         return *this;
00301                     }
00302                     
00303                     Digital_Out& operator= (Digital_Out& rhs)
00304                     {
00305                         write(rhs.read());
00306                         return *this;
00307                     }
00308                     
00309                     operator int()
00310                     {
00311                         return read();
00312                     }
00313     };
00314 
00315 #endif /*DIGITAL_OUT_H*/