Go Down

Topic: How to prevent reading unformatted data from the eeprom? (Read 430 times) previous topic - next topic


Hi guys, what the proper way to deal with this issue?

Every time my code is starting up it loads all data and structs from the eeprom that were saved on the previous execution.

To prevent it from reading unformatted, garbage data and especially non null terminated strings, every time I save the data I've added a stupid value "123" as memory check, that is then verified upon restart, and from this I assume all data is healthy.

This was my lame approach the the problem, but I'm not happy with it.


I've used https://www.arduino.cc/en/Tutorial/EEPROMCrc with success.

At startup
Calculate the CRC over the length of the eeprom minus 4 bytes.
Compare against the last 4 bytes in eeprom.

If you have a fresh eeprom (all 0xFF), the crc check will fail.

After updating data in the eeprom

Calculate the CRC over the length of the eeprom minus 4 bytes.
Store the calculated CRC in the last 4 bytes.

If after a startup the crc does not match, you have corruption.

If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.


Go Up