Saturday, November 11, 2017

ESP-8266, EEPROM, and config registers

I'm not 100% sold on the idea that using byte-wise simulated EEPROM on the ESP - as opposed to some other SPIFFS system - is the 'best' way. But I have a lot of existing code that wouldn't need much modification, so EEPROM it is.

Differences from AVR / Arduino:

  1. It's not EEPROM, but actually Flash backed RAM under the hood.
  2. The maximum size defined by SPI_FLASH_SEC_SIZE = 4096, so that's likely the max EEPROM size.
  3. Call EEPROM.begin(size) before using.
  4. Call EEPROM.commit() to actually write it.
  5. and EEPROM.write(addr, value) are RAM based, and fast.
Other Notes
  1. For my own libraries there isn't much need to duplicate the RAM storage of things like config registers in an additional uint8_t reg[size] array, but for 32 bytes it's not catastrophic.
  2. Stick to the standard of:
    1. EEPROM_init() writes hard-coded values into EEPROM and commits them.
    2. EEPROM_close() just calls EEPROM.end() - for now.
    3. EEPROM_save(len) changes the len byte and commit() writes.
    4. EEPROM_load() does an initial read at the default length (i.e. 32 bytes) and if the len is different, then perform an EEPROM.end() and EEPROM.begin(len) to adjust.
  3. Config reg array layout is:
    1. Byte 0: Version [0..255]
    2. Byte 1: Length in bytes
    3. Bytes [2..Length] are EEPROM written
    4. Bytes above Length, i.e. [32..63], are dynamic variables and used for RAM management on space constrained systems; not written to EEPROM.

No comments:

Post a Comment