cancel
Showing results for 
Search instead for 
Did you mean: 

How to avoid erasing the entire program in the Flash

MA4
Associate III

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

1 ACCEPTED SOLUTION

Accepted Solutions

@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.

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

View solution in original post

8 REPLIES 8
Andrew Neil
Super User

@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:

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

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.

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.
MA4
Associate III

I will test it. Thank you for your answer


@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.

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

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.

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

The ST EEPROM Emulation does do this.

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.
MA4
Associate III

The STM32H7 has too large sectors, which is a real constraint. It restricts a lot.

Andrew Neil
Super User

@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).

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.
MA4
Associate III

Yes yes I know but I speak about erase size.