2024-10-17 08:39 AM
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.
Solved! Go to Solution.
2024-10-17 10:42 AM
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.
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();
2024-10-17 09:23 AM
2024-10-17 10:26 AM
Thank you,
Looks like it is possible, but too complicated!
So there is no small peace of code!
2024-10-17 10:35 AM
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 ?
2024-10-17 10:42 AM
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.
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();
2024-10-17 03:30 PM
Thank you very much. I will try to make it work.