Skip to main content
Senior
October 17, 2024
Solved

Code example - how to write 2 bytes to permanent memory.

  • October 17, 2024
  • 1 reply
  • 1243 views

Hello,

Is there code example for writing 2 bytes to the permanent memory at runtime, so it will stay there after power off?

For any STM32 or better STM32WB55.

Best answer by Tesla DeLorean

Small Piece? If using HAL it's likely to pull in a metric shed-ton of other clutter, but at a register level can be done relatively concisely if necessary.

https://github.com/STMicroelectronics/STM32CubeWB/blob/master/Projects/NUCLEO-WB15CC/Examples/FLASH/FLASH_EraseProgram/Src/main.c#L168

Expectations of Address alignment compatible with the flash array / write-lines

 /* Unlock the Flash to enable the flash control register access *************/
 HAL_FLASH_Unlock();

 /* Clear OPTVERR bit set on virgin samples */
 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
 ...
 if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, Address, DATA_64) == HAL_OK)
 ...

 /* Lock the Flash to disable the flash control register access (recommended
 to protect the FLASH memory against possible unwanted operation) *********/
 HAL_FLASH_Lock();

 

1 reply

Andrew Neil
Super User
October 17, 2024
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.
GS2Author
Senior
October 17, 2024

Thank you,

Looks like it is possible, but too complicated!

So there is no small peace of code!

Tesla DeLorean
Guru
October 17, 2024

The Cube repo's should have "FLASH" examples, and "EEPROM" where appropriate.

The "Write" code can be fairly concise, usually UNLOCK, SET PROGRAM MODE, WRITE WORD[S], CHECK STATUS/COMPLETION, CLEAR MODE, LOCK

How "permanent" are we talking about? OTP?

2-bytes doesn't seem like enough for a KEY

With the FLASH, the minimum ERASE tends to be quite large, and costly in time. For small items one can journal multiple small writes over the page/sector. Write lines might impose further limits, to implement ECC in hidden bits. So 32-bit or 64-bit minimums might apply. Check part's RM

BKPRAM / NVRAM ?

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