7 years, 6 months 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

7 years, 6 months 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.

That line of code is necessary to wait till the end of the i2c bus operations. There is a problem with the new mbed revision (127). The i2c bus does not work! Downgrading to 126 now the i2c bus works. The problem is evident with ST Nucleo F446RE

posted by Denis Gottardello 04 Oct 2016

Hi Denis with mbed_130, I don't think that the line below is needed anymore ... while(i2c.write(SlaveAddress, NULL, 0)); actually, this could even cause an error in the I2C lib, I will test and send a correction for this

posted by Laurent Meunier 29 Nov 2016