2025-06-02 2:34 AM - last edited on 2025-06-02 4:57 AM by mƎALLEm
Hello,
I'm working on stm32h7 with the STM32CubeIDE IDE and I'd like to know how to make sure that when the debug starts it doesn't delete the whole program. Let me explain:
in my flash memory I have data in a specific location and specific size that I've written via the program and that I want to keep.
The problem is that when I launch the debug, it deletes the entire flash memory to write the new program.
Is it possible not to erase a specific memory location?
(I've tried with KEEP and NOLOAD in the linker script but it doesn't work).
thank you
Solved! Go to Solution.
2025-06-02 4:28 AM
@Andrew Neil wrote:PS:
The smallest unit of erasing is a single page; so you do have to ensure that your data is in a page of its own - separate from any part of the application.
Exactly. The STM32H7 has 128k sectors. So the size of the reserved part of memory has to be a multiple of a whole sector and aligned with a whole sector. I often use the last sector of the flash for data. Or the first sector after a bootloader and before the application.
@Andrew Neil wrote:
You could try not including it in the linker script at all - which is what the EEPROM Emulation does.
I've had an application with external flash and I had to remove it from the elf file to prevent it from being flashed. Changing the linker file did not work in my case. It always wanted to write it. And when disabling writing it gave an error that certain data from the elf file was not written. So the simplest is not to include it in the elf file, but simply have that sector not defined in the linker file.
2025-06-02 2:52 AM - edited 2025-06-02 2:54 AM
@MA4 wrote:The problem is that when I launch the debug, it deletes the entire flash memory
It should only erase the amount of Flash necessary for the size of the program:
You could try not including it in the linker script at all - which is what the EEPROM Emulation does.
PS:
The smallest unit of erasing is a single page; so you do have to ensure that your data is in a page of its own - separate from any part of the application.
2025-06-02 4:06 AM
I will test it. Thank you for your answer
2025-06-02 4:28 AM
@Andrew Neil wrote:PS:
The smallest unit of erasing is a single page; so you do have to ensure that your data is in a page of its own - separate from any part of the application.
Exactly. The STM32H7 has 128k sectors. So the size of the reserved part of memory has to be a multiple of a whole sector and aligned with a whole sector. I often use the last sector of the flash for data. Or the first sector after a bootloader and before the application.
@Andrew Neil wrote:
You could try not including it in the linker script at all - which is what the EEPROM Emulation does.
I've had an application with external flash and I had to remove it from the elf file to prevent it from being flashed. Changing the linker file did not work in my case. It always wanted to write it. And when disabling writing it gave an error that certain data from the elf file was not written. So the simplest is not to include it in the elf file, but simply have that sector not defined in the linker file.
2025-06-06 1:55 AM
Also another tip:
If you don't want data loss during writing to FLASH in case of power loss or reset, then use 2 sectors and alternate between them. You can use a byte as a counter(check for overflow) and CRC for integrity. A side effect of this is that you are automatically doing some wear leveling. Using more sectors will increase the number of cycles you can write the data. Though with sectors this large it is not practical . For EEPROM this trick is very useful.
2025-06-06 2:03 AM
The ST EEPROM Emulation does do this.
2025-06-06 2:16 AM
The STM32H7 has too large sectors, which is a real constraint. It restricts a lot.
2025-06-06 4:37 AM
@MA4 wrote:when the debug starts it doesn't delete the whole program.
It's not the debug per se which erases the flash - it's the flash programming.
You can start a debug session without programming (assuming, of course, that the code you want to debug is already programmed into the target).
2025-06-06 4:41 AM
Yes yes I know but I speak about erase size.