Flash Recovery

Defining the constant __FLASH_RECOVER enables the Flash recovery option for avoiding data loss in case of power failure. When Flash recovery is enabled, one Flash page will serve as a recovery buffer. The value of __FLASH_RECOVER will determine the address to the Flash page used for this purpose. This address must be a byte address pointing to an address in the application section of a Flash page and the write functions will not be able to write to this page. Flash recovery is carried out by calling the function RecoverFLASH() at program startup.

When the Flash recovery option is enabled a page write will involve pre-storing of data into a dedicated recovery page in Flash, before the actual write to a given Flash page takes place. The address for the page to be written to is stored in EEPROM together with a status byte indicating that the Flash recovery page contains data. This status byte will be cleared when the actual write to a given Flash page is completed successfully. The variables in EEPROM and the Flash recovery buffer are used by the Flash recovery function RecoverFlash() to recover data when necessary. The writing of one byte to EEPROM takes about the same time as writing an entire page to Flash. Thus, when enabling the Flash recovery option the total write time will increase considerably. EEPROM is used instead of Flash because reserving a few bytes in Flash will exclude flexible usage of the entire Flash page containing these bytes.