Ground Embedded KO Subscribe

Notes / MCU Firmware

Storing settings in Flash and EEPROM

Values that must survive power-off go to Flash or EEPROM. Sector erase, write endurance, and a storage scheme that is safe against power loss.

GROUND EMBEDDED · NOTEMCU Firmware 11Writing Flash and EEPROMAvg 30%D1 period

Properties of Flash

  • It must be erased before writing, and the erase unit is a sector (a few KB up to 128 KB)
  • Erasing sets every bit to 1; writing can only change 1 to 0
  • Write/erase endurance is around 10,000 cycles
  • During an erase (tens of ms up to 1 s), code execution from the same bank may stall

A safe storage pattern

Sector Arecords 1, 2, 3 …FullMove to sector Bnewest onlyErase sector A
New values always go to a free slot. If power is lost during a write, the previous record survives.
VersionSettings dataCRCdetects corruption
The shape of one record. When reading, pick the highest version among records whose CRC is valid.
  1. Reserve two dedicated sectors outside the code area in the linker script
  2. Store the data with a version number and a CRC
  3. Write new values to the other sector (or to a free slot in the same sector), and read back the newest record whose CRC is valid
  4. When a sector fills up, move to the other sector and erase the previous one

This way the old value survives if power is lost during a write, and wear is spread out. ST's EEPROM emulation application note uses this method.

External EEPROM / FRAM

An I2C EEPROM is written byte by byte and lasts on the order of a million cycles. FRAM has practically unlimited endurance and writes complete instantly, which suits values that change often (distance travelled, hours of use).

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →