This is the DDRO software we write to operate the chip

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers master_i2c.cpp Source File

master_i2c.cpp

00001 //master
00002 #include "mbed.h"
00003 #include <stdlib.h>
00004 
00005 I2C master(p28, p27);        // sda, scl
00006 Serial pc_master(USBTX, USBRX); // tx, rx
00007 DigitalOut success(LED1);
00008 DigitalOut fail(LED2);
00009 DigitalIn read(p29);
00010 
00011 const int SLAVEADDR = 0x88; // define the I2C Slave Address (mbed 2)
00012 char cmd[2];
00013 char buf[50];
00014 
00015 void i2c_write(char * data)
00016 {
00017     int ack = master.write(SLAVEADDR, cmd, 2); // Send command string
00018     if (ack)
00019         fail=!fail;
00020     else {
00021         pc_master.printf("Master is writing..\n");
00022         success=!success;
00023     }
00024 }
00025 
00026 void i2c_read(char * buf)
00027 {
00028     /*
00029     int ack = master.read(SLAVEADDR, buf, 6);
00030     if (ack)
00031         fail=!fail;
00032     else {
00033         pc_master.printf("Slave sent: %s\n", buf);
00034         success=!success;
00035     }
00036     */
00037     int ack, data, scan_counter;
00038     scan_counter = 0;
00039     while(1) {
00040         data=master.read(ack);
00041         if(data==0xFF) {
00042             pc_master.printf("Scan finish..\n");
00043             break;
00044         } else {
00045             pc_master.printf("data %d reads %d\n",scan_counter++, data);
00046         }
00047     }
00048 }
00049 
00050 void i2c_test()
00051 {
00052     master.start();
00053     cmd[0] = 'U';
00054     cmd[1] = 'S';
00055     while (1) {
00056         if (read) {
00057             i2c_read(buf);
00058         } else {
00059             i2c_write(cmd);
00060         }
00061         wait(0.07);
00062     }
00063 }
00064 
00065 void i2c_test2()
00066 {
00067     //send signal to mbed2 and wait for response
00068     master.start();
00069     cmd[0] = 'C';
00070     while (master.write(SLAVEADDR, cmd, 2)) {
00071         fail=!fail;
00072     }
00073     int ack = 1;
00074     while (1) {
00075         if (read) {
00076             while (ack) {
00077                 ack = master.read(SLAVEADDR, buf, 2);
00078             }
00079             success = 1;
00080             //pc_master.printf("Slave sent: $s\n", buf);
00081             break;
00082         }
00083     }
00084 }
00085 
00086 void master_write()
00087 {
00088     char cmd[] = "scan";
00089     while (master.write(SLAVEADDR, cmd, 4)) {
00090         fail=1;
00091     }
00092     fail = 0;
00093 }
00094 
00095 void master_read(int* ro_reading)
00096 {
00097     char* data_read;
00098     data_read = new char [64*5+8];
00099     for(int i=0; i<64*5+8; i++) {
00100         data_read[i]=(char)47;
00101     }
00102     while(data_read[0]==47) {
00103         wait(10);
00104         master.read(SLAVEADDR|1, data_read, 64*5+8, true);
00105     }
00106     for(int i=0; i<64; i++) {
00107         int out = 0;
00108         out += data_read[i*5+4]-48;
00109         out *= 10;
00110         out += data_read[i*5+3]-48;
00111         out *= 10;
00112         out += data_read[i*5+2]-48;
00113         out *= 10;
00114         out += data_read[i*5+1]-48;
00115         out *= 10;
00116         out += data_read[i*5+0]-48;
00117         ro_reading[i] = out;
00118     }
00119     ro_reading[64] = data_read[64*5]+data_read[64*5+1]*0x100;
00120     ro_reading[65] = data_read[64*5+2]+data_read[64*5+3]*0x100;
00121     ro_reading[66] = data_read[64*5+4]+data_read[64*5+5]*0x100; //HVTP
00122     ro_reading[67] = data_read[64*5+6]+data_read[64*5+7]*0x100; //HVTN
00123 }