cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F10 Flash byte erase and write

Stef Roelants
Associate
Posted on March 29, 2017 at 17:47

Dear

I'm new with STM32 and ARM. I am using the nucleo STM32F103RB and using STM32CubeMX and HAL libraries. 

I want to save 10 to 100 uint32 size register in the flash that will be loaded on startup to have the correct settings. 

In the run time some register can change and need to be saved in the flash. 

In this moment I can write and read  registers to the Flash. Problem is that the HAL flash code always erase the whole page. (1028 bytes). I just need to erase 4 bytes and rewrite them.

But If I unstand okay there need to be a way to erase just one or 4 bytes so I can change only one register. 

I hope somebody can help me with explaining the steps I need to take. 

I really appreciate any help you can provide.

Kind regard 

Stef 
2 REPLIES 2
Posted on March 30, 2017 at 01:28

No, the FLASH is always erased in large pieces. There's no bytewise-erasable memory in STM32s (this would be called EEPROM).

There are several possible workarounds:

  • you still use one whole page from the FLASH, but don't erase it every time you need to write a word, but write the new word into the unwritten portion of memory sequentially, together with its address. Upon powerup, scan the whole page to find the newest value for the given address. There are numerious variations to this method; there are also 'EEPROM simulation' libraries around, maybe there's one in Cube too, I don't Cube.
  • use battery-backup RAM. There are a couple of registers in the RTC which are maintained from VBAT, or with a bigger battery the whole mcu could be kept under power in some of the low-power modes
  • use external EEPROM, e.g. a cheap 24C02 or similar

JW

Posted on March 30, 2017 at 09:16

Thank you very much. 

Also for the tips!

Kind regards 

Stef