Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 1 month ago.
i2c EEprom and mbed
Hi. I have some problems on using a Microchip i2c eeprom and mbed. If I add a lot of records (for me one record means 10 bytes), sometimes I have a very strange problem. For example if I write 10 bytes with pointer= 250, the bytes writtes are ok but I have a corruption problem with bytes ad pointer= 130, written some time before. Do you have a idea non that? The code is very short:
include the mbed library with this snippet
Buffer[0]= (EEPROMPointer >> 8) & 0xff; Buffer[1]= EEPROMPointer & 0xff; Buffer[2]= Channel; Buffer[3]= (Code >> 24) & 0xff; Buffer[4]= (Code >> 16) & 0xff; Buffer[5]= (Code >> 8) & 0xff; Buffer[6]= Code & 0xff; Buffer[7]= 0xff; Buffer[8]= 0xff; Buffer[9]= 0xff; Buffer[10]= 0xff; Buffer[11]= 0xff; if (!i2c.write(SlaveAddress, (char*)Buffer, sizeof(Buffer))== 0) return false; while(i2c.write(SlaveAddress, NULL, 0));
1 Answer
8 years, 1 month ago.
Your Buffer seems to have a size of at least 12 bytes since you write a value to Buffer[11].
So the i2c.write(SlaveAddress, (char*)Buffer, sizeof(Buffer) operation will write more than 10 bytes. Maybe that causes corruption.
What is the purpose of
while(i2c.write(SlaveAddress, NULL, 0));
That NULL operation may cause a problem (eg spurious I2C Start) depending on the platform implementation of I2C.write.