Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 0:2a52ec2fb0e0
- Child:
- 1:90a2068dd130
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sat Sep 29 19:23:08 2012 +0000
@@ -0,0 +1,332 @@
+//slave
+//note: main() function is at the bottom
+#include <mbed.h>
+#include <sstream>
+using namespace std;
+
+I2CSlave slave(p9, p10);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+Serial pc(USBTX, USBRX);
+
+Ticker CLK;
+
+DigitalOut leakage_enable(p15);
+AnalogIn leakage1(p17);
+AnalogIn leakage2(p18);
+
+DigitalOut ref_clk(p5);
+DigitalOut count(p6);
+DigitalOut read(p7);
+DigitalOut enable(p8);
+DigitalOut S0(p11);
+DigitalOut S1(p12);
+DigitalOut S2(p13);
+DigitalOut S3(p14);
+DigitalOut S4(p22);
+DigitalOut S5(p21);
+DigitalOut OS(p16);
+DigitalIn O0(p23);
+DigitalIn O1(p24);
+DigitalIn O2(p25);
+DigitalIn O3(p29);
+DigitalIn O4(p28);
+DigitalIn O5(p27);
+DigitalIn O6(p26);
+DigitalIn O7(p30);
+AnalogIn core(p19);
+AnalogIn sram(p20);
+
+char buf[50] = {};
+char msg[] = "Slave!";
+const int SLAVEADDR = 0x88; // define the I2C Slave Address (mbed 2)
+
+void clk_gen()
+{
+ ref_clk = !ref_clk;
+}
+
+void assign_s(int selection)
+{
+ int remainder = selection;
+ S0 = selection % 2;
+ selection = selection / 2;
+ S1 = selection % 2;
+ selection = selection / 2;
+ S2 = selection % 2;
+ selection = selection / 2;
+ S3 = selection % 2;
+ selection = selection / 2;
+ S4 = selection % 2;
+ selection = selection / 2;
+ S5 = selection % 2;
+}
+
+unsigned int read_out()
+{
+ unsigned int out;
+ out = 0;
+ OS = 1;
+ wait(0.001);
+ out += O7.read();
+ out *= 2;
+ out += O6.read();
+ out *= 2;
+ out += O5.read();
+ out *= 2;
+ out += O4.read();
+ out *= 2;
+ out += O3.read();
+ out *= 2;
+ out += O2.read();
+ out *= 2;
+ out += O1.read();
+ out *= 2;
+ out += O0.read();
+ out *= 2;
+ OS = 0;
+ wait(0.001);
+ out += O7.read();
+ out *= 2;
+ out += O6.read();
+ out *= 2;
+ out += O5.read();
+ out *= 2;
+ out += O4.read();
+ out *= 2;
+ out += O3.read();
+ out *= 2;
+ out += O2.read();
+ out *= 2;
+ out += O1.read();
+ out *= 2;
+ out += O0.read();
+ return out;
+}
+
+void scan_slave(char* ro_data)
+{
+ pc.printf("Testing Starts here\n");
+ O0.mode(PullUp);
+ O1.mode(PullUp);
+ O2.mode(PullUp);
+ O3.mode(PullUp);
+ O4.mode(PullUp);
+ O5.mode(PullUp);
+ O6.mode(PullUp);
+ O7.mode(PullUp);
+
+ count = 0;
+ read = 0;
+ enable = 0;
+ int i;
+
+ for (i=0; i<64; i++) {
+ //measure using 0.01
+ CLK.detach();
+ CLK.attach(&clk_gen, 0.01);
+ int select = i;
+ enable = 0;
+ count = 0;
+ read = 0;
+ assign_s(select);
+ wait_us(100);
+ enable = 1;
+ wait_us(100);
+ count = 1;
+ wait_us(100);
+ count = 0;
+ wait(0.2);
+ read = 0;
+ wait_us(100);
+ read = 1;
+ wait_us(100);
+ read = 0;
+ enable = 0;
+ unsigned int out;
+ out = read_out();
+ ro_data[5*i] = (char)(out%10+48);
+ out = out / 10;
+ ro_data[5*i+1] = (char)(out%10+48);
+ out = out / 10;
+ ro_data[5*i+2] = (char)(out%10+48);
+ out = out / 10;
+ ro_data[5*i+3] = (char)(out%10+48);
+ out = out / 10;
+ ro_data[5*i+4] = (char)(out%10+48);
+ CLK.detach();
+ }
+}
+
+void read_power(char* ro_data)
+{
+ unsigned short core_reading = core.read_u16();
+ unsigned short sram_reading = sram.read_u16();
+ ro_data[64*5] = (core_reading%0x100);
+ ro_data[64*5+1] = (core_reading/0x100);
+ ro_data[64*5+2] = (sram_reading%0x100);
+ ro_data[64*5+3] = (sram_reading/0x100);
+}
+
+void read_leakage(char* ro_data)
+{
+ pc.putc('1');
+ pc.getc();
+
+ CLK.detach();
+ leakage_enable = 1;
+ S4 = 0;
+ S5 = 0;
+ ref_clk=0;
+
+ pc.putc('2');
+ pc.getc();
+
+ wait_us(10);
+ ref_clk=1;
+
+ pc.putc('3');
+ pc.getc();
+
+ wait(0.1);
+ ref_clk=0;
+
+ pc.putc('4');
+ pc.getc();
+
+ unsigned short RVTP=leakage1.read_u16();
+ S4 = 0;
+ S5 = 1;
+ ref_clk=0;
+
+ pc.putc('5');
+ pc.getc();
+
+ wait_us(10);
+ ref_clk=1;
+
+ pc.putc('6');
+ pc.getc();
+
+ wait(0.1);
+ ref_clk=0;
+
+ pc.putc('7');
+ pc.getc();
+
+ unsigned short HVTP=leakage1.read_u16();
+ S4 = 1;
+ S5 = 0;
+ ref_clk=0;
+
+ pc.putc('8');
+ pc.getc();
+
+ wait_us(10);
+ ref_clk=1;
+ wait(0.1);
+
+ pc.putc('9');
+ pc.getc();
+
+ ref_clk=0;
+
+ pc.putc('A');
+ pc.getc();
+
+ unsigned short RVTN=leakage2.read_u16();
+ S4 = 1;
+ S5 = 1;
+ ref_clk=0;
+
+ pc.putc('B');
+ pc.getc();
+
+ wait_us(10);
+ ref_clk=1;
+ wait(0.1);
+
+ pc.putc('C');
+ pc.getc();
+
+ ref_clk=0;
+
+ pc.putc('D');
+ pc.getc();
+
+ unsigned short HVTN=leakage2.read_u16();
+ leakage_enable = 0;
+
+ pc.putc('E');
+ pc.getc();
+
+ ro_data[64*5+4] = (RVTP%0x100);
+ ro_data[64*5+5] = (RVTP/0x100);
+ ro_data[64*5+6] = (HVTP%0x100);
+ ro_data[64*5+7] = (HVTP/0x100);
+ ro_data[64*5+8] = (RVTN%0x100);
+ ro_data[64*5+9] = (RVTN/0x100);
+ ro_data[64*5+10] = (HVTN%0x100);
+ ro_data[64*5+11] = (HVTN/0x100);
+}
+
+void scan_test()
+{
+ slave.address(SLAVEADDR);
+ char* ro_data;
+ ro_data = new char[64*5+12];
+ bool if_ready = false;
+ while (1) {
+ int i = slave.receive();
+ //pc.printf("receive = %i\r\n", i);
+ switch (i) {
+ case I2CSlave::ReadAddressed:
+ if (!if_ready) {
+ pc.printf("Slave is not ready...\n");
+ break;
+ }
+ slave.write(ro_data, 64*5+12);
+ pc.printf("Slave is writing..\n");
+ break;
+ case I2CSlave::WriteGeneral:
+ led2 = !led2;
+ break;
+ case I2CSlave::WriteAddressed:
+ slave.read(buf, 4);
+ pc.printf("Slave gets: %s\n", buf);
+ led3 = !led3;
+ char cmd1[] = "scan";
+ char cmd2[] = "getp";
+ char cmd3[] = "getl";
+ if (!strcmp(cmd1, buf)) {
+ pc.printf("Scanning....\n");
+ scan_slave(ro_data);
+ read_power(ro_data);
+ read_leakage(ro_data);
+ if_ready = true;
+ } else if (!strcmp(cmd2, buf)) {
+ pc.printf("Measuring power....\n");
+ read_power(ro_data);
+ } else if (!strcmp(cmd3, buf)) {
+ pc.printf("Measuring leakage.....\n");
+ read_leakage(ro_data);
+ }
+ break;
+ }
+ for (int i = 0; i < 50; i++) buf[i] = 0; // Clear buffer
+ wait(0.002);
+ }
+}
+
+
+//// MAIN FUNCTION
+int main ()
+{
+ //scan_slave(); //main() function from the original DDRO_scan code
+ pc.printf("SlaveMBED starts...\n");
+ scan_test();
+
+ return 0;
+}
\ No newline at end of file