# STM32 - Reading EEPROM via I2C Delay Problem

Im trying to read/write EEPROM byte by byte but if i dont put an enough delay(~1ms) between read/write tasks, im getting or writing wrong value to the EEPROM. But this delay is taking a significant time when there is a many bytes to read/write and 400kHz losing its meaning. Am i missing something ? or its a nature of byte by byte process is slow. Thanks for your time and help.

MCU = STM32F072C8Tx

EEPROM = 24LC64

I2C Settings:

 Speed = 400 kHz
Rise Time = 300 ns
Fall Time = 300 ns
Analog Filter = Disable
Digital Filter Coefficent = 0


Basic R/W Code Sample:

 HAL_I2C_Mem_Write(&hi2c1, device_addr, mem_addr, I2C_MEMADD_SIZE_16BIT, data, 1, 500);
Hal_Delay(1);


Yes, you are missing the time it takes to write anything, even if the bus is 400kHz.

The first page of 24LC64 says that a page write is max 5ms. You can write one byte or up to 32 bytes (a full page) at once but it can still take up to 5ms.

So after a write operation, the EEPROM will be busy writing the data and will not respond to any operation until it is finished with the write.

• It seems your answer approves the "nature of byte by byte process is slow" ? Mar 9 at 11:46
• Yes. And for every single byte you write, the chip must internally erase a full 32-byte page, and write back the whole page with modified contents. So if you write single bytes in a for loop, the whole page gets erased and rewritten many times. Mar 9 at 12:04

EEProms require some minimum amount of time to write a page or single byte to memory. Most production code will poll the device (WB bit) to see when this write has finished. In your case, the 1ms probably allows this time to finish.

It is going to be more efficient to write by pages rather than bytes, you only have to wait one time, rather than for each byte. Watch that you don't wrap pages. Page size is going to be some power of 2, see datasheet for exact size.

• Page writes are not only faster, but will wear out the chip slower. EEPROMs have a limited number of writes before they wear out. Mar 9 at 13:41