cancel
Showing results for 
Search instead for 
Did you mean: 

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

GS2
Senior

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.

1 ACCEPTED SOLUTION

Accepted Solutions

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();

 

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

View solution in original post

5 REPLIES 5

Thank you,

Looks like it is possible, but too complicated!

So there is no small peace of code!

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

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();

 

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

Thank you very much. I will try to make it work.