3.8 Safely Storing and Validating User Settings with CRC and Internal EEPROM

In this system, user settings are retained across power-cycles. The internal EEPROM data memory within the microcontroller is used to store these settings. First, the microcontroller validates the EEPROM version ID, which is written by the application when settings are saved. On start-up, the version control byte is compared to a constant defined by the developer at compile time. If the values mismatch, then either:

  • The EEPROM formatting has changed, or
  • The EEPROM does not contain user settings

In either case, the settings are erased and reprogrammed with compile-time defaults. If the version ID passes, then a Cyclic Redundancy Check (CRC) is performed on the user settings and an embedded checksum. If the CRC is not zero, then it could be assumed that the memory is corrupted, and the settings are subsequently erased and reprogrammed with compile-time defaults. One modification made to the CRC configuration was to set an initial value of 0xFF. This ensures that if the CRC fails to run, the value will remain non-zero and can be easily detected.