Panel For Example Panel For Example Panel For Example

Preventing Data Loss on STM32 During Power Failures

Author : Adrian April 08, 2026

 

Overview

In embedded device development, it is often necessary to preserve some non-volatile data when power is lost. Adding an external ROM IC (for example, I2C-based 24C02) for system configuration or user-defined information increases PCB area and hardware cost and may make the product less compact. For STM32-based applications, two practical approaches are commonly used and are described below as reference.

 

Method 1: Using Backup Registers

Principle: Some MCU families provide backup registers in the backup domain. Larger STM32 MCUs may have 42 16-bit backup registers, while small and medium devices may only have 10 16-bit backup registers. For example, on the STM32F103C8T6 the 42 backup registers correspond to 84 bytes of storage. Backup registers are powered by the backup domain supply. When external VDD is lost, the contents of the backup domain registers remain valid as long as VBAT is present. Software notes: - This method is suitable for saving a small amount of data, such as common configuration parameters in wearable devices. - Typical initialization and access functions include the following prototypes:

void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data); uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR);

 

Method 2: Using Internal Flash

Principle: Flash memory is reprogrammable and is categorized into NOR and NAND types. NAND flash is typically used for large data storage, such as USB drives and SSDs. On STM32 devices, flash programming and erasing operate on pages. For example, on the STM32F103C8T6 each flash page is 2 KB. Software notes: - Write protection must be handled appropriately. This discussion assumes that flash read/write is permitted and that write operations are allowed. Option bytes and flash protection mechanisms are not covered here. - Writing a single uint32_t value involves the standard flash program sequence using the MCU's flash API. - Reading a single 32-bit value can be done directly via a pointer read, for example:

uint32_t rddata = *(__IO uint32_t *)dataAddr; Because flash operations involve multiple APIs and additional considerations (erase granularity, wear, and protection settings), this method is more complex than using backup registers. However, since flash pages provide larger storage (up to 2048 bytes per page on the example device), it is suitable for preserving larger data sets across power loss. Consider the flash read/write protection and wear-leveling aspects when using this method.

 

Summary

Backup registers provide a simple solution for small amounts of critical data, while internal flash can store larger datasets at the cost of increased complexity. These two approaches are common options for mitigating data loss on STM32-based systems during power failures.