1

Do I understand the code in EEPROM.cpp / write() correctly, that val is only written to the EEPROM, if val differs from the current EEPROM contents at the given address?

And if the answer is yes, what is commit() doing?

3

From library Library, see fragment:

EEPROM.write does not write to flash immediately, instead you must call EEPROM.commit() whenever you wish to save changes to flash. EEPROM.end() will also commit, and will release the RAM copy of EEPROM contents.

Probably to prevent too many writes into the same page (mostly EEPROM is divided in pages), the write stores it into some temporary buffer and the commit actually writes it to EEPROM. This decreases the number of writes, and since the number of writes has a maximum, the lifetime of the EEPROM is increased.

Update: Majenko's comment mentions no EEPROM is used, but flash, see below.

  • 2
    Note that the ESP8266 doesn't have any EEPROM. It uses EEPROM emulation in Flash memory. Since Flash memory can only be erased in blocks you can't change just one value - you have to wipe the whole lot and re-write it. Doing that for every value when you want to change multiple values would quickly cripple the flash (and be slow as a dead dog), so commit() batches those changes up into just one single erase/write sequence. – Majenko Oct 15 '17 at 9:59
  • I have a function that writes ~500 byte to EEPROM if one of these bytes had changed. If write() works on a buffer in RAM, it could be better to have a write-wrapper that only writes changed data to that buffer - correct? ...and then hope, that this SPI_FLASH_SEC_SIZE is below my 500 bytes. – dede Oct 15 '17 at 10:01
  • Actually, the write itself does not hurt. However, it might be good to keep a so called 'dirty' flag, which is initially false and only true when at least one value of the page/block has been changed. Than you know a commit is needed. – Michel Keijzers Oct 15 '17 at 10:16
  • 1
    I already have this dirty-flag for my data structure in RAM. If one of my 500 byte was changed, I do a EEPROM.write() for all 500 byte and a EEPROM.commit() after it. My idea was, that it might be better, to call EEPROM.write() only for changed bytes. But now I found a #define SPI_FLASH_SEC_SIZE 4096 in spi_flash.h. And that should mean: it makes no difference, because EEPROM.commit() always works on the full buffer. – dede Oct 15 '17 at 10:30
  • True ... it does not matter much to do an additional write, this is purely CPU and will be very fast and does not reduce the lifetime. Only commits do (probably even when the same values are overwritten). – Michel Keijzers Oct 15 '17 at 11:09
1

I read that after the EEPROM.commit() you need to add a delay(500). I had several issues with the EEPROM data gone after several restarts of the esp8266.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.