I2C EEPROM library from 24C01 to 24C1025
Dependents: Digitalni_Sat_MG_AN EEROM_1768 EEPROM_kamal EEPROM_MY ... more
Revision 3:925096a4c7f0, committed 2015-12-21
- Comitter:
- bborredon
- Date:
- Mon Dec 21 23:26:42 2015 +0000
- Parent:
- 2:79ed7ff7c23d
- Commit message:
- Revision 1.3 : Correct write for eeprom >= T24C32.; Tested with 24C02, 24C08, 24C16, 24C64, 24C256, 24C512, 24C1025 on LPC1768.
Changed in this revision
eeprom.cpp | Show annotated file Show diff for this revision Revisions of this file |
eeprom.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 79ed7ff7c23d -r 925096a4c7f0 eeprom.cpp --- a/eeprom.cpp Mon Nov 02 23:20:56 2015 +0000 +++ b/eeprom.cpp Mon Dec 21 23:26:42 2015 +0000 @@ -1,5 +1,10 @@ /*********************************************************** Author: Bernard Borredon +Version: 1.3 + - Correct write(uint32_t address, int8_t data[], uint32_t length) for eeprom >= T24C32. + Tested with 24C02, 24C08, 24C16, 24C64, 24C256, 24C512, 24C1025 on LPC1768 (mbed online and µVision V5.16a). + - Correct main test. + Date : 12 decembre 2013 Version: 1.2 - Update api documentation @@ -27,6 +32,16 @@ const char * const EEPROM::_name[] = {"24C01","24C02","24C04","24C08","24C16","24C32", "24C64","24C128","24C256","24C512","24C1024","24C1025"}; +/** + * EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type) : _i2c(sda, scl) + * + * Constructor, initialize the eeprom on i2c interface. + * @param sda sda i2c pin (PinName) + * @param scl scl i2c pin (PinName) + * @param address eeprom address, according to eeprom type (uint8_t) + * @param type eeprom type (TypeEeprom) + * @return none +*/ EEPROM::EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type) : _i2c(sda, scl) { @@ -117,8 +132,17 @@ // Set I2C frequency _i2c.frequency(400000); + } +/** + * void write(uint32_t address, int8_t data) + * + * Write byte + * @param address start address (uint32_t) + * @param data byte to write (int8_t) + * @return none +*/ void EEPROM::write(uint32_t address, int8_t data) { uint8_t page; @@ -126,7 +150,7 @@ uint8_t cmd[3]; int len; int ack; - + // Check error if(_errnum) return; @@ -166,7 +190,7 @@ // Data cmd[2] = (uint8_t) data; } - + ack = _i2c.write((int)addr,(char *)cmd,len); if(ack != 0) { _errnum = EEPROM_I2cError; @@ -178,6 +202,15 @@ } +/** + * void write(uint32_t address, int8_t data[], uint32_t length) + * + * Write array of bytes (use the page mode) + * @param address start address (uint32_t) + * @param data bytes array to write (int8_t[]) + * @param size number of bytes to write (uint32_t) + * @return none +*/ void EEPROM::write(uint32_t address, int8_t data[], uint32_t length) { uint8_t page; @@ -187,7 +220,7 @@ uint8_t i,j,ind; uint8_t cmd[129]; int ack; - + // Check error if(_errnum) return; @@ -209,7 +242,7 @@ // Compute remaining bytes remain = length - blocs * _page_write; - + for(i = 0;i < blocs;i++) { // Compute page number page = 0; @@ -323,7 +356,7 @@ } } - if(remain) { + if(remain) { // Compute page number page = 0; if(_type < T24C32) @@ -402,37 +435,45 @@ // Wait end of write ready(); } - } + } } - } - else { - // Fist word address (MSB) - cmd[0] = (uint8_t) (address >> 8); + else { + // Fist word address (MSB) + cmd[0] = (uint8_t) (address >> 8); - // Second word address (LSB) - cmd[1] = (uint8_t) address; + // Second word address (LSB) + cmd[1] = (uint8_t) address; - // Add data for the current page - for(j = 0;j < remain;j++) - cmd[j + 2] = (uint8_t) data[blocs * _page_write + j]; - - // Write data for the current page - ack = _i2c.write((int)addr,(char *)cmd,remain + 2); - if(ack != 0) { - _errnum = EEPROM_I2cError; - return; - } + // Add data for the current page + for(j = 0;j < remain;j++) + cmd[j + 2] = (uint8_t) data[blocs * _page_write + j]; + + // Write data for the current page + ack = _i2c.write((int)addr,(char *)cmd,remain + 2); + if(ack != 0) { + _errnum = EEPROM_I2cError; + return; + } - // Wait end of write - ready(); + // Wait end of write + ready(); + } } } +/** + * void write(uint32_t address, int16_t data) + * + * Write short + * @param address start address (uint32_t) + * @param data short to write (int16_t) + * @return none +*/ void EEPROM::write(uint32_t address, int16_t data) { int8_t cmd[2]; - + // Check error if(_errnum) return; @@ -449,10 +490,18 @@ } +/** + * void write(uint32_t address, int32_t data) + * + * Write long + * @param address start address (uint32_t) + * @param data long to write (int32_t) + * @return none +*/ void EEPROM::write(uint32_t address, int32_t data) { int8_t cmd[4]; - + // Check error if(_errnum) return; @@ -469,10 +518,18 @@ } +/** + * void write(uint32_t address, float data) + * + * Write float + * @param address start address (uint32_t) + * @param data float to write (float) + * @return none +*/ void EEPROM::write(uint32_t address, float data) { int8_t cmd[4]; - + // Check error if(_errnum) return; @@ -489,10 +546,19 @@ } +/** + * void write(uint32_t address, void *data, uint32_t size) + * + * Write anything (use the page write mode) + * @param address start address (uint32_t) + * @param data data to write (void *) + * @param size number of bytes to write (uint32_t) + * @return none +*/ void EEPROM::write(uint32_t address, void *data, uint32_t size) { int8_t *cmd = NULL; - + // Check error if(_errnum) return; @@ -517,6 +583,14 @@ } +/** + * void read(uint32_t address, int8_t& data) + * + * Random read byte + * @param address start address (uint32_t) + * @param data byte to read (int8_t&) + * @return none +*/ void EEPROM::read(uint32_t address, int8_t& data) { uint8_t page; @@ -524,7 +598,7 @@ uint8_t cmd[2]; uint8_t len; int ack; - + // Check error if(_errnum) return; @@ -534,7 +608,7 @@ _errnum = EEPROM_OutOfRange; return; } - + // Compute page number page = 0; if(_type < T24C32) @@ -575,6 +649,15 @@ } +/** + * void read(uint32_t address, int8_t *data, uint32_t size) + * + * Sequential read byte + * @param address start address (uint32_t) + * @param data bytes array to read (int8_t[]&) + * @param size number of bytes to read (uint32_t) + * @return none +*/ void EEPROM::read(uint32_t address, int8_t *data, uint32_t size) { uint8_t page; @@ -582,7 +665,7 @@ uint8_t cmd[2]; uint8_t len; int ack; - + // Check error if(_errnum) return; @@ -639,11 +722,18 @@ } +/** + * void read(int8_t& data) + * + * Current address read byte + * @param data byte to read (int8_t&) + * @return none + */ void EEPROM::read(int8_t& data) { uint8_t addr; int ack; - + // Check error if(_errnum) return; @@ -660,10 +750,18 @@ } +/** + * void read(uint32_t address, int16_t& data) + * + * Random read short + * @param address start address (uint32_t) + * @param data short to read (int16_t&) + * @return none +*/ void EEPROM::read(uint32_t address, int16_t& data) { int8_t cmd[2]; - + // Check error if(_errnum) return; @@ -680,10 +778,18 @@ } +/** + * void read(uint32_t address, int32_t& data) + * + * Random read long + * @param address start address (uint32_t) + * @param data long to read (int32_t&) + * @return none +*/ void EEPROM::read(uint32_t address, int32_t& data) { int8_t cmd[4]; - + // Check error if(_errnum) return; @@ -700,10 +806,18 @@ } +/** + * void read(uint32_t address, float& data) + * + * Random read float + * @param address start address (uint32_t) + * @param data float to read (float&) + * @return none + */ void EEPROM::read(uint32_t address, float& data) { int8_t cmd[4]; - + // Check error if(_errnum) return; @@ -720,6 +834,15 @@ } +/** + * void read(uint32_t address, void *data, uint32_t size) + * + * Random read anything + * @param address start address (uint32_t) + * @param data data to read (void *) + * @param size number of bytes to read (uint32_t) + * @return none +*/ void EEPROM::read(uint32_t address, void *data, uint32_t size) { int8_t *cmd = NULL; @@ -749,6 +872,13 @@ } +/** + * void clear(void) + * + * Clear eeprom (write with 0) + * @param none + * @return none + */ void EEPROM::clear(void) { int32_t data; @@ -761,6 +891,13 @@ } } +/** + * void ready(void) + * + * Wait eeprom ready + * @param none + * @return none +*/ void EEPROM::ready(void) { int ack; @@ -784,11 +921,25 @@ } +/** + * uint32_t getSize(void) + * + * Get eeprom size in bytes + * @param none + * @return size in bytes (uint32_t) +*/ uint32_t EEPROM::getSize(void) { return(_size); } +/** + * const char* getName(void) + * + * Get eeprom name + * @param none + * @return name (const char*) + */ const char* EEPROM::getName(void) { uint8_t i = 0; @@ -835,11 +986,25 @@ return(_name[i]); } +/** + * uint8_t getError(void) + * + * Get the current error number (EEPROM_NoError if no error) + * @param none + * @return none +*/ uint8_t EEPROM::getError(void) { return(_errnum); } +/** + * bool checkAddress(uint32_t address) + * + * Check if address is in the eeprom range address + * @param address address to check (uint32_t) + * @return true if in eeprom range, overwise false (bool) +*/ bool EEPROM::checkAddress(uint32_t address) { bool ret = true;
diff -r 79ed7ff7c23d -r 925096a4c7f0 eeprom.h --- a/eeprom.h Mon Nov 02 23:20:56 2015 +0000 +++ b/eeprom.h Mon Dec 21 23:26:42 2015 +0000 @@ -3,6 +3,12 @@ /*********************************************************** Author: Bernard Borredon +Date : 21 decembre 2015 +Version: 1.3 + - Correct write(uint32_t address, int8_t data[], uint32_t length) for eeprom >= T24C32. + Tested with 24C02, 24C08, 24C16, 24C64, 24C256, 24C512, 24C1025 on LPC1768 (mbed online and µVision V5.16a). + - Correct main test. + Date : 12 decembre 2013 Version: 1.2 - Update api documentation @@ -55,21 +61,21 @@ void eeprom_test(void) { - EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C16); // 24C16 eeprom with sda = p9 and scl = p10 + EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C64); // 24C64 eeprom with sda = p9 and scl = p10 uint8_t data[256],data_r[256]; int8_t ival; - uint16_t s; - int16_t sdata,sdata_r; - int32_t ldata[1024],ldata_r[1024]; - int32_t eeprom_size,max_size; + uint16_t s; + int16_t sdata,sdata_r; + int32_t ldata[1024]; + int32_t eeprom_size,max_size; uint32_t addr; int32_t idata,idata_r; - uint32_t i,j,k,l,t; + uint32_t i,j,k,l,t,id; float fdata,fdata_r; MyData md,md_r; - eeprom_size = ep.getSize(); - max_size = MIN(eeprom_size,256); + eeprom_size = ep.getSize(); + max_size = MIN(eeprom_size,256); printf("Test EEPROM I2C model %s of %d bytes\n\n",ep.getName(),eeprom_size); @@ -81,7 +87,7 @@ myerror(ep.getErrorMessage()); } - printf("Test sequential read %d first bytes :\n\n",max_size); + printf("Test sequential read %d first bytes :\n",max_size); for(i = 0;i < max_size/16;i++) { for(j = 0;j < 16;j++) { addr = i * 16 + j; @@ -99,7 +105,7 @@ myerror(ep.getErrorMessage()); } - printf("Test sequential read %d last bytes :\n\n",max_size); + printf("\nTest sequential read %d last bytes :\n",max_size); for(i = 0;i < max_size/16;i++) { for(j = 0;j < 16;j++) { addr = i * 16 + j; @@ -126,7 +132,7 @@ myerror(ep.getErrorMessage()); } - printf("Test write and read %d first bytes :\n\n",max_size); + printf("\nTest write and read %d first bytes :\n",max_size); for(i = 0;i < max_size/16;i++) { for(j = 0;j < 16;j++) { addr = i * 16 + j; @@ -148,7 +154,7 @@ myerror(ep.getErrorMessage()); } - printf("Test current address read %d first bytes :\n\n",max_size); + printf("\nTest current address read %d first bytes :\n",max_size); for(i = 0;i < max_size/16;i++) { for(j = 0;j < 16;j++) { addr = i * 16 + j; @@ -162,7 +168,7 @@ if(ep.getError() != 0) myerror(ep.getErrorMessage()); - printf("Test sequential read %d first bytes :\n\n",max_size); + printf("\nTest sequential read %d first bytes :\n",max_size); for(i = 0;i < max_size/16;i++) { for(j = 0;j < 16;j++) { addr = i * 16 + j; @@ -191,7 +197,7 @@ myerror(ep.getErrorMessage()); // Test read short, long, float - printf("Test write and read short (%d), long (%d), float (%f) :\n", + printf("\nTest write and read short (%d), long (%d), float (%f) :\n", sdata,idata,fdata); ep.read((uint32_t)(eeprom_size - 16),(int16_t&)sdata_r); @@ -207,7 +213,7 @@ ep.read((uint32_t)(eeprom_size - 8),fdata_r); if(ep.getError() != 0) myerror(ep.getErrorMessage()); - printf("fdata %f\n\n",fdata_r); + printf("fdata %f\n",fdata_r); // Test read and write a structure md.sdata = -15203; @@ -218,7 +224,7 @@ if(ep.getError() != 0) myerror(ep.getErrorMessage()); - printf("Test write and read a structure (%d %d %f) :\n\n",md.sdata,md.idata,md.fdata); + printf("\nTest write and read a structure (%d %d %f) :\n",md.sdata,md.idata,md.fdata); ep.read((uint32_t)(eeprom_size - 32),(void *)&md_r,sizeof(md_r)); if(ep.getError() != 0) @@ -226,7 +232,7 @@ printf("md.sdata %d\n",md_r.sdata); printf("md.idata %d\n",md_r.idata); - printf("md.fdata %f\n\n",md_r.fdata); + printf("md.fdata %f\n",md_r.fdata); // Test read and write of an array of the first max_size bytes for(i = 0;i < max_size;i++) @@ -240,7 +246,7 @@ if(ep.getError() != 0) myerror(ep.getErrorMessage()); - printf("Test write and read an array of the first %d bytes :\n",max_size); + printf("\nTest write and read an array of the first %d bytes :\n",max_size); for(i = 0;i < max_size/16;i++) { for(j = 0;j < 16;j++) { addr = i * 16 + j; @@ -252,54 +258,90 @@ // Test write and read an array of int32 s = eeprom_size / 4; // size of eeprom in int32 - int ldata_size = sizeof(ldata) / 4; // size of data array in int32 + int ldata_size = sizeof(ldata) / 4; // size of data array in int32 l = s / ldata_size; // loop index - // size of read / write in bytes - t = eeprom_size; + // size of read / write in bytes + t = eeprom_size; if(t > ldata_size * 4) t = ldata_size * 4; - printf("Test write and read an array of %d int32 (write entire memory) :\n",t); + printf("Test write and read an array of %d int32 (write entire memory) :\n",t/4); - // Write entire eeprom - for(k = 0;k <= l;k++) { - for(i = 0;i < ldata_size;i++) - ldata[i] = ldata_size * k + i; + // Write entire eeprom + if(l) { + for(k = 0;k < l;k++) { + for(i = 0;i < ldata_size;i++) + ldata[i] = ldata_size * k + i; - addr = k * ldata_size * 4; - ep.write(addr,(void *)ldata,t); - if(ep.getError() != 0) - myerror(ep.getErrorMessage()); - } + addr = k * ldata_size * 4; + ep.write(addr,(void *)ldata,t); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + } + + printf("Write OK\n"); // Read entire eeprom - for(k = 0;k <= l;k++) { - addr = k * s * 4; - - ep.read(addr,(void *)ldata_r,t); - if(ep.getError() != 0) - myerror(ep.getErrorMessage()); + id = 0; + for(k = 0;k < l;k++) { + addr = k * ldata_size * 4; + ep.read(addr,(void *)ldata,t); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); - // format outputs with 16 words rows - for(i = 0;i < s / 16;i++) { - printf("%3d ",i+1); - for(j = 0;j < 16;j++) { - addr = i * 16 + j; - printf("%4d ",ldata_r[addr]); - } - printf("\n"); - } + // format outputs with 8 words rows + for(i = 0;i < ldata_size / 8;i++) { + id++; + printf("%4d ",id); + for(j = 0;j < 8;j++) { + addr = i * 8 + j; + printf("%5d ",ldata[addr]); + } + printf("\n"); + } + } } + else { + for(i = 0;i < s;i++) + ldata[i] = i; + + addr = 0; + ep.write(addr,(void *)ldata,t); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + printf("Write OK\n"); + + // Read entire eeprom + id = 0; + + addr = 0; + ep.read(addr,(void *)ldata,t); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + // format outputs with 8 words rows + for(i = 0;i < s / 8;i++) { + id++; + printf("%4d ",id); + for(j = 0;j < 8;j++) { + addr = i * 8 + j; + printf("%5d ",ldata[addr]); + } + printf("\n"); + } + } // clear eeprom - printf("Clear eeprom\n"); + printf("\nClear eeprom\n"); ep.clear(); if(ep.getError() != 0) myerror(ep.getErrorMessage()); - printf("End\n"); + printf("End\n"); + } int main() @@ -332,7 +374,8 @@ "Memory allocation error" }; -// Class +/** EEPROM Class +*/ class EEPROM { public: enum TypeEeprom {T24C01=128,T24C02=256,T24C04=512,T24C08=1024,T24C16=2048, @@ -341,160 +384,160 @@ /** * Constructor, initialize the eeprom on i2c interface. - * @param sda : sda i2c pin (PinName) - * @param scl : scl i2c pin (PinName) - * @param address : eeprom address, according to eeprom type (uint8_t) - * @param type : eeprom type (TypeEeprom) - * @return : none + * @param sda sda i2c pin (PinName) + * @param scl scl i2c pin (PinName) + * @param address eeprom address, according to eeprom type (uint8_t) + * @param type eeprom type (TypeEeprom) + * @return none */ EEPROM(PinName sda, PinName scl, uint8_t address, TypeEeprom type); /** * Random read byte - * @param address : start address (uint32_t) - * @param data : byte to read (int8_t&) - * @return : none + * @param address start address (uint32_t) + * @param data byte to read (int8_t&) + * @return none */ void read(uint32_t address, int8_t& data); /** * Random read short - * @param address : start address (uint32_t) - * @param data : short to read (int16_t&) - * @return : none + * @param address start address (uint32_t) + * @param data short to read (int16_t&) + * @return none */ void read(uint32_t address, int16_t& data); /** * Random read long - * @param address : start address (uint32_t) - * @param data : long to read (int32_t&) - * @return : none + * @param address start address (uint32_t) + * @param data long to read (int32_t&) + * @return none */ void read(uint32_t address, int32_t& data); /** * Random read float - * @param address : start address (uint32_t) - * @param data : float to read (float&) - * @return : none + * @param address start address (uint32_t) + * @param data float to read (float&) + * @return none */ void read(uint32_t address, float& data); /** * Random read anything - * @param address : start address (uint32_t) - * @param data : data to read (void *) - * @param size : number of bytes to read (uint32_t) - * @return : none + * @param address start address (uint32_t) + * @param data data to read (void *) + * @param size number of bytes to read (uint32_t) + * @return none */ void read(uint32_t address, void *data, uint32_t size); /** * Current address read byte - * @param data : byte to read (int8_t&) - * @return : none + * @param data byte to read (int8_t&) + * @return none */ void read(int8_t& data); /** * Sequential read byte - * @param address : start address (uint32_t) - * @param data : bytes array to read (int8_t[]&) - * @param size : number of bytes to read (uint32_t) - * @return : none + * @param address start address (uint32_t) + * @param data bytes array to read (int8_t[]&) + * @param size number of bytes to read (uint32_t) + * @return none */ void read(uint32_t address, int8_t *data, uint32_t size); /** * Write byte - * @param address : start address (uint32_t) - * @param data : byte to write (int8_t) - * @return : none + * @param address start address (uint32_t) + * @param data byte to write (int8_t) + * @return none */ void write(uint32_t address, int8_t data); /** * Write short - * @param address : start address (uint32_t) - * @param data : short to write (int16_t) - * @return : none + * @param address start address (uint32_t) + * @param data short to write (int16_t) + * @return none */ void write(uint32_t address, int16_t data); /** * Write long - * @param address : start address (uint32_t) - * @param data : long to write (int32_t) - * @return : none + * @param address start address (uint32_t) + * @param data long to write (int32_t) + * @return none */ void write(uint32_t address, int32_t data); /** * Write float - * @param address : start address (uint32_t) - * @param data : float to write (float) - * @return : none + * @param address start address (uint32_t) + * @param data float to write (float) + * @return none */ void write(uint32_t address, float data); /** * Write anything (use the page write mode) - * @param address : start address (uint32_t) - * @param data : data to write (void *) - * @param size : number of bytes to write (uint32_t) - * @return : none + * @param address start address (uint32_t) + * @param data data to write (void *) + * @param size number of bytes to write (uint32_t) + * @return none */ void write(uint32_t address, void *data, uint32_t size); /** * Write array of bytes (use the page mode) - * @param address : start address (uint32_t) - * @param data : bytes array to write (int8_t[]) - * @param size : number of bytes to write (uint32_t) - * @return : none + * @param address start address (uint32_t) + * @param data bytes array to write (int8_t[]) + * @param size number of bytes to write (uint32_t) + * @return none */ void write(uint32_t address, int8_t data[], uint32_t size); /** * Wait eeprom ready - * @param : none - * @return : none + * @param none + * @return none */ void ready(void); /** * Get eeprom size in bytes - * @param : none - * @return : size in bytes (uint32_t) + * @param none + * @return size in bytes (uint32_t) */ uint32_t getSize(void); /** * Get eeprom name - * @param : none - * @return : name (const char*) + * @param none + * @return name (const char*) */ const char* getName(void); /** * Clear eeprom (write with 0) - * @param : none - * @return : none + * @param none + * @return none */ void clear(void); /** * Get the current error number (EEPROM_NoError if no error) - * @param : none - * @return : current error number (uint8_t) + * @param none + * @return none */ uint8_t getError(void); /** * Get current error message - * @param : none - * @return : current error message(std::string) + * @param none + * @return current error message(std::string) */ std::string getErrorMessage(void) {