read to and write from the EEPROM on the BMU

Dependencies:   mbed

Committer:
drajan
Date:
Sat Nov 26 13:45:25 2016 +0000
Revision:
0:06386d17bb00
I2C code for writing to and reading from the EEPROM on the BMU

Who changed what in which revision?

UserRevisionLine numberNew contents of line
drajan 0:06386d17bb00 1 #include "mbed.h"
drajan 0:06386d17bb00 2 // This code works it writes a single byte to memory and receives a single byte back
drajan 0:06386d17bb00 3 // Also added page read and write and both of these work
drajan 0:06386d17bb00 4 // Now have to migrate the code over to CUER i2c
drajan 0:06386d17bb00 5
drajan 0:06386d17bb00 6 I2C i2c(p9, p10);
drajan 0:06386d17bb00 7
drajan 0:06386d17bb00 8 Serial pc(USBTX, USBRX);
drajan 0:06386d17bb00 9 const int address1 = 0xAF; // For some reason both these addresses work I believe it is something in the implementation of the I2C class, it gets rid of last byte
drajan 0:06386d17bb00 10 const int address2 = 0xAE;
drajan 0:06386d17bb00 11 //Best way to check the address is to loop through every address and check for the return condition
drajan 0:06386d17bb00 12
drajan 0:06386d17bb00 13 void i2c_read(int address);
drajan 0:06386d17bb00 14 void i2c_write(int address);
drajan 0:06386d17bb00 15 void i2c_page_write(int address);
drajan 0:06386d17bb00 16 void i2c_page_read(int address);
drajan 0:06386d17bb00 17
drajan 0:06386d17bb00 18 int main() {
drajan 0:06386d17bb00 19 /*i2c_write(address2);
drajan 0:06386d17bb00 20 wait(1);
drajan 0:06386d17bb00 21 i2c_read(address2);
drajan 0:06386d17bb00 22
drajan 0:06386d17bb00 23
drajan 0:06386d17bb00 24 wait(1);
drajan 0:06386d17bb00 25 i2c_write(address1);
drajan 0:06386d17bb00 26 wait(1);
drajan 0:06386d17bb00 27 i2c_read(address1);*/
drajan 0:06386d17bb00 28
drajan 0:06386d17bb00 29 i2c_page_write(address1);
drajan 0:06386d17bb00 30 wait(1);
drajan 0:06386d17bb00 31 i2c_page_read(address1);
drajan 0:06386d17bb00 32 wait(1);
drajan 0:06386d17bb00 33 i2c_page_write(address2);
drajan 0:06386d17bb00 34 wait(1);
drajan 0:06386d17bb00 35 i2c_page_read(address2);
drajan 0:06386d17bb00 36
drajan 0:06386d17bb00 37 }
drajan 0:06386d17bb00 38
drajan 0:06386d17bb00 39 void i2c_write(int address){
drajan 0:06386d17bb00 40
drajan 0:06386d17bb00 41 pc.printf("In i2c write .... \n");
drajan 0:06386d17bb00 42 char data[3];
drajan 0:06386d17bb00 43 data[0] = 0x00;
drajan 0:06386d17bb00 44 data[1] = 0x00;
drajan 0:06386d17bb00 45 data[2] = 50;
drajan 0:06386d17bb00 46 pc.printf("return value: %d \n", i2c.write(address, data, 3, false));
drajan 0:06386d17bb00 47 }
drajan 0:06386d17bb00 48
drajan 0:06386d17bb00 49 void i2c_read(int address){
drajan 0:06386d17bb00 50 pc.printf("In i2c read... \n"); // To read you need to write then read
drajan 0:06386d17bb00 51 char cmd[3];
drajan 0:06386d17bb00 52 cmd[0] = 0x00;
drajan 0:06386d17bb00 53 cmd[1] = 0x00;
drajan 0:06386d17bb00 54 i2c.write(address, cmd, 2, true);
drajan 0:06386d17bb00 55
drajan 0:06386d17bb00 56 char dataout = 0x00;
drajan 0:06386d17bb00 57 pc.printf("Read status: %d \n", i2c.read(address, &dataout, 1, false)); // MUST PASS THE ADDRESS TO THE READ FN (since not an array)
drajan 0:06386d17bb00 58
drajan 0:06386d17bb00 59 pc.printf("This should be the read: %d \n", dataout);
drajan 0:06386d17bb00 60 }
drajan 0:06386d17bb00 61
drajan 0:06386d17bb00 62 // Page write
drajan 0:06386d17bb00 63 void i2c_page_write(int address){
drajan 0:06386d17bb00 64 pc.printf("In i2c page write .... \n");
drajan 0:06386d17bb00 65 char data[5];
drajan 0:06386d17bb00 66 data[0] = 0x00;
drajan 0:06386d17bb00 67 data[1] = 0x02;
drajan 0:06386d17bb00 68 data[2] = 50;
drajan 0:06386d17bb00 69 data[3] = 17;
drajan 0:06386d17bb00 70 data[4] = 25;
drajan 0:06386d17bb00 71 pc.printf("return value: %d \n", i2c.write(address, data, 5, false));
drajan 0:06386d17bb00 72
drajan 0:06386d17bb00 73 }
drajan 0:06386d17bb00 74 // Page read
drajan 0:06386d17bb00 75 void i2c_page_read(int address){
drajan 0:06386d17bb00 76 pc.printf("In I2C page read.....\n");
drajan 0:06386d17bb00 77 char cmd[2];
drajan 0:06386d17bb00 78 cmd[0] = 0x00;
drajan 0:06386d17bb00 79 cmd[1] = 0x02;
drajan 0:06386d17bb00 80 i2c.write(address, cmd, 2, true);
drajan 0:06386d17bb00 81
drajan 0:06386d17bb00 82 char dataout[3];
drajan 0:06386d17bb00 83 dataout[0] = 0;
drajan 0:06386d17bb00 84 dataout[1] = 0;
drajan 0:06386d17bb00 85 dataout[2] = 0;
drajan 0:06386d17bb00 86 pc.printf("Read status: %d \n", i2c.read(address, dataout, 3, false));
drajan 0:06386d17bb00 87 pc.printf("This should be the read: %d \t %d \t %d \n", dataout[0], dataout[1], dataout[2]);
drajan 0:06386d17bb00 88 }
drajan 0:06386d17bb00 89
drajan 0:06386d17bb00 90