read to and write from the EEPROM on the BMU
Dependencies: mbed
i2C_write.cpp
- Committer:
- drajan
- Date:
- 2016-11-26
- Revision:
- 0:06386d17bb00
File content as of revision 0:06386d17bb00:
#include "mbed.h" // This code works it writes a single byte to memory and receives a single byte back // Also added page read and write and both of these work // Now have to migrate the code over to CUER i2c I2C i2c(p9, p10); Serial pc(USBTX, USBRX); 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 const int address2 = 0xAE; //Best way to check the address is to loop through every address and check for the return condition void i2c_read(int address); void i2c_write(int address); void i2c_page_write(int address); void i2c_page_read(int address); int main() { /*i2c_write(address2); wait(1); i2c_read(address2); wait(1); i2c_write(address1); wait(1); i2c_read(address1);*/ i2c_page_write(address1); wait(1); i2c_page_read(address1); wait(1); i2c_page_write(address2); wait(1); i2c_page_read(address2); } void i2c_write(int address){ pc.printf("In i2c write .... \n"); char data[3]; data[0] = 0x00; data[1] = 0x00; data[2] = 50; pc.printf("return value: %d \n", i2c.write(address, data, 3, false)); } void i2c_read(int address){ pc.printf("In i2c read... \n"); // To read you need to write then read char cmd[3]; cmd[0] = 0x00; cmd[1] = 0x00; i2c.write(address, cmd, 2, true); char dataout = 0x00; pc.printf("Read status: %d \n", i2c.read(address, &dataout, 1, false)); // MUST PASS THE ADDRESS TO THE READ FN (since not an array) pc.printf("This should be the read: %d \n", dataout); } // Page write void i2c_page_write(int address){ pc.printf("In i2c page write .... \n"); char data[5]; data[0] = 0x00; data[1] = 0x02; data[2] = 50; data[3] = 17; data[4] = 25; pc.printf("return value: %d \n", i2c.write(address, data, 5, false)); } // Page read void i2c_page_read(int address){ pc.printf("In I2C page read.....\n"); char cmd[2]; cmd[0] = 0x00; cmd[1] = 0x02; i2c.write(address, cmd, 2, true); char dataout[3]; dataout[0] = 0; dataout[1] = 0; dataout[2] = 0; pc.printf("Read status: %d \n", i2c.read(address, dataout, 3, false)); pc.printf("This should be the read: %d \t %d \t %d \n", dataout[0], dataout[1], dataout[2]); }