STM32 gotchas
265. STM32 with ECC in FLASH may freeze due to unexpected NMI when FLASH is corrupted

Newer STM32 (starting with 'L0/'L4 roughly 10 years ago) have error-check-and-correction (ECC) circuitry implemented within their FLASH controller. This is a useful feature to ensure FLASH integrity especially in long term, and almost a necessity these days due to the rapidly decreasing feature size thus increasing impact of FLASH leakage and crosstalk on its retention.

The FLASH ECC operates on the whole width of the FLASH word, i.e. the chunk of FLASH written/read at the same time (the actual width depends on the particular family/model, e.g. in 'L4 it is 64 bits). The FLASH array contains extra data (e.g. in case of 'L4 the actual FLASH width is 72 bits, i.e. there is an extra ECC byte for every 8 bytes), into whith the ECC checksum is stored. The extra bits are not accessible to the user.

The ECC operation is transparent to the user and occurs automatically. Upon each write hardware calculates the ECC checksum and writes in into the extra bits. Upon read the hardware uses the extra bits to determine if the data bits are unaltered. If there is a single-bit error, the hardware corrects it automatically; there is an optional (maskable and by default disabled) interrupt which can be triggered upon such event. If there is a double-bit (or more) error, this is not correctable, and hardware invokes a non-maskable interrupt (NMI) to notify the user.

In older STM32 families produced using well-matured 90nm technology (such as 'L4), FLASH is probably robust enough so that no FLASH faults occur within the normal operating envelope. It's the same technology node used in some non-ECC FLASH models (e.g. 'F4 and 'F7 - although it's low-power version), after all. That means, that users can ignore the ECC in such mcu in many applications.

However, if FLASH corruption results in a double-or-more-bit fault, reading from such faulty location will lead to NMI 1. And, if NMI is unhandled, default handler usually contains an infinite loop, which then leads to a surprising mcu "freeze". The NMI handler has to clear the flag indicating ECC error leading to NMI (usually FLASH_ECCR.ECCD) 2, otherwise the NMI handler would tail-chain into itself forever.

One way how such FLASH corruption may occur is in applications which use the FLASH to store configuration or status data, i.e. use it in an EEPROM fashion. If during erasing or writing to FLASH power failure occurs, the FLASH may remain in inconsistent, corrupted state, subsequently detected by ECC and leading to the NMI.

See also g266 and g267.


1. Only double-bit-errors (i.e. where two or more bits within the FLASH word including ECC data, are corrupted) will lead to NMI. However, the most common scenario resulting in FLASH ECC error outlined above (i.e. FLASH corrupted due to power removal during FLASH erase/programming in EEPROM-like operation) is most likely to result in double-bit-error. Single-bit-errors are automatically corrected by the hardware during FLASH read, and they will result in a dedicated FLASH interrupt only if that is enabled.

2. In some STM32 families with two-FLASH-bank models, e.g. STM32G0, there are two separate FLASH_ECCRx registers for the two FLASH banks. Thus, the NMI handler has to check both, and clear that ECCD bit which is set in either of them, to avoid the infinite NMI tail-chaining.