2025-04-23 3:11 AM
Hi everybody,
I am currently working on an STM32F446RE
I need to save some bytes like 10 for example. I need to save some bytes like 10 for example, in flash memory to save my HMI configuration.
I have read articles and done research but I don't understand everything.
The memory is divided into sectors. I think I have to write my data to the end of the memory. However, the last sector is 128kB. Do I have to erase 128kB each time?
Isn't there a simpler and less resource-intensive method? Can't we just delete a 2KB page?
I really need to save only a few variables
Thank you to reading me
Solved! Go to Solution.
2025-04-23 12:20 PM
If that's resolved it for, please mark that as the solution:
https://community.st.com/t5/community-guidelines/help-others-to-solve-their-issues/ta-p/575256
2025-04-23 3:28 AM - edited 2025-04-23 4:46 AM
@JJoao.1 wrote:The memory is divided into sectors.
Correct.
@JJoao.1 wrote:I think I have to write my data to the end of the memory.
You don't have to.
By default, CubeIDE uses memory from the beginning - but you could reconfigure that.
@JJoao.1 wrote:Can't we just delete a 2KB page?
Sure you can - you just need to make sure that the sector isn't used for anything else
Correction: The smallest sector is 16K - not 2K - see the post by @Hl_st, below.
Have you seen
STSW-STM32066, EEPROM emulation in STM32F40x/STM32F41x microcontrollers (AN3969) :
https://www.st.com/en/embedded-software/stsw-stm32066.html
2025-04-23 3:52 AM - edited 2025-04-23 4:00 AM
Hello,
I attached easy example of reading and writing to flash memory for STM32C031 but it can be easily reconfigured to your MCU. Example writes to the last page of flash (user program is saved to the beginning of flash by default). Address of the last flash page is defined by these constants (you should find it in reference manual for your device):
#define FLASH_USER_START_ADDR 0x08007800 /* Start @ of user Flash area */
#define FLASH_USER_END_ADDR 0x08007FFF /* End @ of user Flash area */
Write function in the example erase memory by pages (2 kB) and then saves data by doubleworlds (8 B).
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2025-04-23 4:31 AM
> The memory is divided into sectors. I think I have to write my data to the end of the memory. However, the last sector is 128kB. Do I have to erase 128kB each time?
If you want to use the same locations, yes.
The term "sector" in regard to Flash denominates individually erasable segments.
> Isn't there a simpler and less resource-intensive method? Can't we just delete a 2KB page?
If the MCU variant contains no explicit 2kB Flash sector, no.
And you will invariably come across another term (and problem) related to Flash - the "bank".
All sectors in one Flash bank can only be set to programming mode at once(especially, applying programming voltage internally). And you cannot execute code from a Flash bank while under programming voltage.
Most MCUs are single-bank designs. This means, you either accept to stall during the actual flash programming, or run the flashing code from RAM.
An alternative solution is a cheap external EEPROM (I2C or SPI).
2025-04-23 5:33 AM
Thank you fot you quick reply !
@Andrew Neil: I took a look at your EEPROM simulation link. It's too complicated for me at the moment.
Can you confirm that for me :
I want to write my data on the last sector.
Sector 7 0x0806 0000 - 0x0807 FFFF 128 Kbytes .
In this 128Kb in would like use only the 2 last Kb . Can I erase only 2Kb on the sector 7 ?
So at this adress 0x807 FFFF - 2048 = 0x807F7FF
0x807F7FF is my start adress
Or maybe it's more simple to change the start adress of my program like 0x0800 0000 to 0x800 0000 + 16Kb to reserved for me the sector 0 ?
2025-04-23 5:41 AM - edited 2025-04-23 6:12 AM
Yes, you can erase only 2 kB on sector 7 at the address 0x807F7FF. Try to follow the example I have send in my last reply there is also erased only 2 kB. Full sector have to be erased, 128 kB for the last one.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2025-04-23 5:54 AM - edited 2025-04-23 5:59 AM
@Hl_st wrote:Yes, you can erase only 2 kB on sector 7 at the address 0x807F7FF.
Really?
Surely the whole point of the "Sector" is that it defines the size which can be erased? You can only ever erase a whole sector - not a part thereof.
Surely, that's what the RM means by, "Sector erase":
And, explicitly:
@Hl_st wrote:Try to follow the example I have send in my last reply there is also erased only 2 kB.
But that is for an entirely different processor
2025-04-23 5:58 AM
@JJoao.1 wrote:Can I erase only 2Kb on the sector 7 ?
No. The RM seems pretty clear to me - the Sector is the minimum unit of erasing.
Is it actually a problem to you to reserve 128K for your data storage?
@JJoao.1 wrote:Or maybe it's more simple to change the start address of my program
Probably.
But remember that the reset vectors (at least) will have to remain at the start of Flash...
2025-04-23 6:02 AM
@Ozone wrote:And you cannot execute code from a Flash bank while under programming voltage.
@JJoao.1 see the RM:
2025-04-23 6:03 AM
Sorry, I was missed in terms, there have to be erased full sector. The last sector is 128 kB, so couldn´t be erased only 2 KB.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.