Skip to main content
Associate
February 26, 2025
Question

How to prevent overwriting of user data inside flash when flashing?

  • February 26, 2025
  • 1 reply
  • 1043 views

Hello everyone,

I want to store configuration data in a flash section that should be retained after updating the code on the controller / flashing a new program. Is this possible using the CubeIDE? If yes, how is it done. If no, can this be done otherwise? As far as I can tell, when downloading the code onto the microcontroller, the whole flash storage will be erased.

Thanks for your help!

1 reply

Andrew Neil
Super User
February 26, 2025

CubeIDE will only erase as much flash as it needs for the programming:

https://community.st.com/t5/stm32cubeide-mcus/partial-or-full-chip-erase-before-programming/m-p/734869/highlight/true#M31829

So just make sure that your "user data" is outside that range

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.
-__itAuthor
Associate
February 26, 2025

My observation sadly does not match with this description. I'm using a STM32H753 Nucleo board. I modified the linker script as follows:

MEMORY
{
RAM_D1 (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
DTCMRAM (xrw) : ORIGIN = 0x24000000, LENGTH = 512K
RAM_D2 (xrw) : ORIGIN = 0x30000000, LENGTH = 288K
RAM_D3 (xrw) : ORIGIN = 0x38000000, LENGTH = 64K
ITCMRAM (xrw) : ORIGIN = 0x00000000, LENGTH = 64K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1920K
USER_FLASH(rw) : ORIGIN = 0x81E0000, LENGTH = 128K
}

 So, I reserved Bank 2 Sector 7 as my own flash memory. Furthermore, I declared it as NOLOAD:

.user_flash(NOLOAD) : 
 { 
 *(.user_flash*); 
 } > USER_FLASH

 

But when trying to debug the program using the CubeIDE, the data from these sections is overwritten with 224 bytes of zeros followed by 228 Bytes of ones. (I might have mixed up the amount of zeros and ones here)

Tesla DeLorean
Guru
February 26, 2025

Perhaps use OBJDUMP to establish WHAT in the .ELF file is being put into a NOBITS section.

You can look at the .MAP too, but I tend to use the .ELF as the arbiter of what exactly the linked object is telling the programmer/loader to do.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..