mbed2, collect sensor data

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //slave
00002 //note: main() function is at the bottom
00003 #include <mbed.h>
00004 #include <sstream>
00005 using namespace std;
00006 
00007 I2CSlave slave(p9, p10);
00008 DigitalOut led1(LED1);
00009 DigitalOut led2(LED2);
00010 DigitalOut led3(LED3);
00011 DigitalOut led4(LED4);
00012 Serial pc(USBTX, USBRX);
00013 
00014 Ticker CLK;
00015 
00016 DigitalOut leakage_enable(p15);
00017 AnalogIn leakage1(p17);
00018 AnalogIn leakage2(p18);
00019 
00020 DigitalOut ref_clk(p5);
00021 DigitalOut count(p6);
00022 DigitalOut read(p7);
00023 DigitalOut enable(p8);
00024 DigitalOut S0(p11);
00025 DigitalOut S1(p12);
00026 DigitalOut S2(p13);
00027 DigitalOut S3(p14);
00028 DigitalOut S4(p22);
00029 DigitalOut S5(p21);
00030 DigitalOut OS(p16);
00031 DigitalIn O0(p23);
00032 DigitalIn O1(p24);
00033 DigitalIn O2(p25);
00034 DigitalIn O3(p29);
00035 DigitalIn O4(p28);
00036 DigitalIn O5(p27);
00037 DigitalIn O6(p26);
00038 DigitalIn O7(p30);
00039 AnalogIn core(p19);
00040 AnalogIn sram(p20);
00041 
00042 char buf[50] = {};
00043 char msg[] = "Slave!";
00044 const int SLAVEADDR = 0x88; // define the I2C Slave Address (mbed 2)
00045 
00046 void clk_gen()
00047 {
00048     ref_clk = !ref_clk;
00049 }
00050 
00051 void assign_s(int selection)
00052 {
00053     int remainder = selection;
00054     S0 = selection % 2;
00055     selection = selection / 2;
00056     S1 = selection % 2;
00057     selection = selection / 2;
00058     S2 = selection % 2;
00059     selection = selection / 2;
00060     S3 = selection % 2;
00061     selection = selection / 2;
00062     S4 = selection % 2;
00063     selection = selection / 2;
00064     S5 = selection % 2;
00065 }
00066 
00067 unsigned int read_out()
00068 {
00069     unsigned int out;
00070     out = 0;
00071     OS = 1;
00072     wait(0.001);
00073     out += O7.read();
00074     out *= 2;
00075     out += O6.read();
00076     out *= 2;
00077     out += O5.read();
00078     out *= 2;
00079     out += O4.read();
00080     out *= 2;
00081     out += O3.read();
00082     out *= 2;
00083     out += O2.read();
00084     out *= 2;
00085     out += O1.read();
00086     out *= 2;
00087     out += O0.read();
00088     out *= 2;
00089     OS = 0;
00090     wait(0.001);
00091     out += O7.read();
00092     out *= 2;
00093     out += O6.read();
00094     out *= 2;
00095     out += O5.read();
00096     out *= 2;
00097     out += O4.read();
00098     out *= 2;
00099     out += O3.read();
00100     out *= 2;
00101     out += O2.read();
00102     out *= 2;
00103     out += O1.read();
00104     out *= 2;
00105     out += O0.read();
00106     return out;
00107 }
00108 
00109 void scan_slave(char* ro_data)
00110 {
00111     pc.printf("Testing Starts here\n");
00112     O0.mode(PullUp);
00113     O1.mode(PullUp);
00114     O2.mode(PullUp);
00115     O3.mode(PullUp);
00116     O4.mode(PullUp);
00117     O5.mode(PullUp);
00118     O6.mode(PullUp);
00119     O7.mode(PullUp);
00120 
00121     count = 0;
00122     read = 0;
00123     enable = 0;
00124     int i;
00125 
00126     for (i=0; i<64; i++) {
00127         //measure using 0.01
00128         CLK.detach();
00129         CLK.attach(&clk_gen, 0.01);
00130         int select = i;
00131         enable = 0;
00132         count = 0;
00133         read = 0;
00134         assign_s(select);
00135         wait_us(100);
00136         enable = 1;
00137         wait_us(100);
00138         count = 1;
00139         wait_us(100);
00140         count = 0;
00141         wait(0.2);
00142         read = 0;
00143         wait_us(100);
00144         read = 1;
00145         wait_us(100);
00146         read = 0;
00147         enable = 0;
00148         unsigned int out;
00149         out = read_out();
00150         ro_data[5*i] = (char)(out%10+48);
00151         out = out / 10;
00152         ro_data[5*i+1] = (char)(out%10+48);
00153         out = out / 10;
00154         ro_data[5*i+2] = (char)(out%10+48);
00155         out = out / 10;
00156         ro_data[5*i+3] = (char)(out%10+48);
00157         out = out / 10;
00158         ro_data[5*i+4] = (char)(out%10+48);
00159         CLK.detach();
00160     }
00161 }
00162 
00163 void read_power(char* ro_data)
00164 {
00165     unsigned short core_reading = core.read_u16();
00166     unsigned short sram_reading = sram.read_u16();
00167     ro_data[64*5] = (core_reading%0x100);
00168     ro_data[64*5+1] = (core_reading/0x100);
00169     ro_data[64*5+2] = (sram_reading%0x100);
00170     ro_data[64*5+3] = (sram_reading/0x100);
00171 }
00172 
00173 void read_leakage(char* ro_data)
00174 {
00175     double reading1;
00176     CLK.detach();
00177     leakage_enable = 1;
00178 
00179     S4 = 0;
00180     S5 = 0;
00181     ref_clk=0;
00182     wait(0.01);
00183 
00184     int i0;
00185     i0=0;
00186     ref_clk = 1;
00187     S5 = 1;
00188     do {
00189         i0++;
00190         if(i0>10000) {
00191             break;
00192         }
00193         wait(0.001);
00194         reading1 = leakage1.read();
00195         reading1 *= 3.3;
00196     } while (reading1 >0.01);
00197     ref_clk = 0;
00198     S5 = 0;
00199     wait(0.01);
00200 
00201     ro_data[64*5+4] = (i0%0x100);
00202     ro_data[64*5+5] = (i0/0x100);
00203 
00204     S4 = 1;
00205     wait(0.01);
00206     i0=0;
00207     ref_clk = 1;
00208     S5 = 1;
00209     do {
00210         i0++;
00211         if(i0>10000) {
00212             break;
00213         }
00214         wait(0.001);
00215         reading1 = leakage2.read();
00216         reading1 *= 3.3;
00217     } while (reading1 <3.29);
00218     ref_clk = 0;
00219     S5 = 0;
00220     wait(0.1);
00221     ro_data[64*5+6] = (i0%0x100);
00222     ro_data[64*5+7] = (i0/0x100);
00223     
00224     leakage_enable = 0;
00225 }
00226 
00227 void scan_test()
00228 {
00229     slave.address(SLAVEADDR);
00230     char* ro_data;
00231     ro_data = new char[64*5+8];
00232     bool if_ready = false;
00233     while (1) {
00234         int i = slave.receive();
00235         //pc.printf("receive = %i\r\n", i);
00236         switch (i) {
00237             case I2CSlave::ReadAddressed:
00238                 if (!if_ready) {
00239                     pc.printf("Slave is not ready...\n");
00240                     break;
00241                 }
00242                 slave.write(ro_data, 64*5+8);
00243                 pc.printf("Slave is writing..\n");
00244                 break;
00245             case I2CSlave::WriteGeneral:
00246                 led2 = !led2;
00247                 break;
00248             case I2CSlave::WriteAddressed:
00249                 slave.read(buf, 4);
00250                 pc.printf("Slave gets: %s\n", buf);
00251                 led3 = !led3;
00252                 char cmd1[] = "scan";
00253                 char cmd2[] = "getp";
00254                 char cmd3[] = "getl";
00255                 if (!strcmp(cmd1, buf)) {
00256                     pc.printf("Scanning....\n");
00257                     scan_slave(ro_data);
00258                     read_power(ro_data);
00259                     read_leakage(ro_data);
00260                     if_ready = true;
00261                 } else if (!strcmp(cmd2, buf)) {
00262                     pc.printf("Measuring power....\n");
00263                     read_power(ro_data);
00264                 } else if (!strcmp(cmd3, buf)) {
00265                     pc.printf("Measuring leakage.....\n");
00266                     read_leakage(ro_data);
00267                 }
00268                 break;
00269         }
00270         for (int i = 0; i < 50; i++) buf[i] = 0;   // Clear buffer
00271         wait(0.002);
00272     }
00273 }
00274 
00275 
00276 //// MAIN FUNCTION
00277 int main ()
00278 {
00279     //scan_slave(); //main() function from the original DDRO_scan code
00280     pc.printf("SlaveMBED starts...\n");
00281     scan_test();
00282 
00283     return 0;
00284 }