Skip to main content
Associate
Mar 16, 2026 12:48 PM UTC
Question

Hard Fault when accessing FLASH_SIZE and NMI while reading Data Flash (EDATA) on STM32C542

Hello,

We are currently working on STM32C542CCT6 from the STM32C5 series and developing custom Flash and Data Flash drivers.

Issue 1 – Hard Fault when accessing FLASH_SIZE

While executing code generated by STM32Cube, the following check causes the system to enter the Hard Fault handler:

if (FLASH_ADDR < (FLASH_BASE + FLASH_SIZE)) /* Main User Flash */

The FLASH_SIZE macro reads the value from the device information area. However, when the code attempts to access FLASH_SIZE, the system immediately enters the Hard Fault handler.

If we manually hardcode the flash size value, the code works correctly.

Our questions are:

  • Is there any specific register configuration or initialization required before accessing FLASH_SIZE on this device?
  • Is this region restricted due to Trust Zone or privileged access on STM32C5 devices?

Issue 2 – NMI when reading Data Flash

We have enabled Data Flash by setting: EDATA_EN = 1

We are trying to read from the Data Flash base address:0x09000000

using a pointer-based read method:

uint16_t data =(*(uint16_t *)0x09000000);

However, if the location has not been written previously, the system enters the NMI handler.

Observations:

  • If we erase and write data first, the read operation works correctly.
  • If we read from a blank location, it enters the NMI handler.

Our questions are:

  1. Is this behavior expected due to ECC protection in the Data Flash region?
  2. Is there any recommended HAL API to safely read Data Flash?
  3. Is there any API available to read from data flash?
  4. Is there any API available to check whether a Data Flash page is blank before performing a read operation?

Thank you.

 

5 replies

Andrew Neil
Super User
Mar 16, 2026 12:51 PM UTC

Have you tried debugging the Hard Fault - to see exactly what is causing it?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate
Mar 16, 2026 12:57 PM UTC

yes, macro which was used FLASH_SIZE is causing the hard fault.it is trying to read data from reserved memory address whenever it is trying that address it is end up with Hard fault handler and the code is completely generated by code generator, and it is in stm32C5xx_hal_flash.c

Associate
Sep 26, 2026 11:03 AM UTC

Hello, I’ve encountered the same issue. The solution is to define the range 0x08FFE000 to 0x08FFFFFF as Read-only in the MPU, and set the range 0x09000000 to 0x0900BFFF
to Normal Non-cacheable, Read/Write, Execute Never, and specify the storage attributes for these regions. Additionally, the MPU initialization function must be placed before the ICACHE initialization.

 

*This post has been translated from Chinese to comply with the ST Community guidelines.

ST Technical Moderator
Oct 5, 2026 10:50 AM UTC

Hello ​@yun_miao_code 

Please write your comment in English. 

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om
ST Technical Moderator
Oct 5, 2026 11:01 AM UTC

Hello ​@koteswara_roa 

There is an official HAL example specifically for this scenario,hal_flash_edata_erase_program, which demonstrates programming, erasing, and safely reading back EDATA while handling the expected ECC/NMI error. 

STM32CubeC5/examples/hal/flash/edata_erase_program at main · STMicroelectronics/STM32CubeC5

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Saket_Om