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: ICE-Application/src/Drivers/i2c_eeprom.cpp
- Revision:
- 0:61364762ee0e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ICE-Application/src/Drivers/i2c_eeprom.cpp Tue Jan 24 19:05:33 2017 +0000 @@ -0,0 +1,96 @@ +/** @file i2c_eeprom.cpp */ +/*CPP************************************************************************** + * FILENAME : i2c_eeprom.cpp * + * * + * DESCRIPTION : * + * Simple library for external I2C EEEPROM. * + * * + * AUTHOR : Olli Vanhoja START DATE : 2011-02-17 * + *****************************************************************************/ + +#include "mbed.h" +#include "i2c_eeprom.h" +#include "global.h" + +I2C i2c_instance(I2C_SDA, I2C_SCL); + +i2c_eeprom::i2c_eeprom(int hwAddr, int speed) +{ + i_i2c_address = hwAddr; +// i2c->frequency(speed); +} + +void i2c_eeprom::write(char *data, uint16_t iAddr, unsigned int n) +{ + char *pi2c_data[3]; // Pointers for CW items + char i2c_data[3]; // Final CW + + /* Convert address to hi and low byte array + * This is really pointless even though they are + * called pointers it would be lot easier to do this + * conversion without any pointers */ + uint16_t *piAddr = &iAddr; + pi2c_data[0] = (char *)piAddr+1; + pi2c_data[1] = (char *)piAddr; + + for (uint16_t i=0; i < n; i++) + { + pi2c_data[2] = &data[i]; + + // Apply actual values from pointer + //for (int n=0; n < 3; n++) + // i2c_data[n] = *pi2c_data[n]; + i2c_data[0] = *pi2c_data[0]; + i2c_data[1] = *pi2c_data[1]; + i2c_data[2] = *pi2c_data[2]; + + // Send write command + strwrite: +// printf("(%s:%d): iAddr=0x%x, data=0x%x\r\n", __func__, __LINE__, iAddr, data[0]); + if(i2c->write(i_i2c_address, i2c_data, 3)) + { + autoreset(); + goto strwrite; + } + + iAddr++; // increment address counter + + // Wait for ACK + while(i2c->write(i_i2c_address, NULL, 0)){} + } +} + +void i2c_eeprom::read(uint16_t iAddr, uint16_t n, char *out) +{ + char *pi2c_data[2]; // Pointers for CW items + char i2c_data[2]; // Final CW + + uint16_t *piAddr = &iAddr; + pi2c_data[0] = (char *)piAddr+1; + pi2c_data[1] = (char *)piAddr; + + // Apply actual values from pointer + //for (int i=0; i < 2; i++) + // i2c_data[i] = *pi2c_data[i]; + i2c_data[0] = *pi2c_data[0]; + i2c_data[1] = *pi2c_data[1]; + + // Send read command + strread: + if(i2c->write(i_i2c_address, i2c_data, 2)) + { + autoreset(); + goto strread; + } + if(i2c->read(i_i2c_address, out, n)) + { + autoreset(); + goto strread; + } +// printf("(%s:%d): iAddr=0x%x, data=0x%x\r\n", __func__, __LINE__, iAddr, *out); +} + +void i2c_eeprom::autoreset() +{ +} +