Skip to main content
GKerc.1
Associate II
May 19, 2020
Solved

How to program FLASH_OPTR register to survive system reset?

  • May 19, 2020
  • 4 replies
  • 4428 views

Hello,

I'm trying to control the boot behavior of my STM32L412 development board which has a floating BOOT0 hardware pin.

I'm trying to clear the SWBOOT0 bit in FLASH_OPTR using firmware. So far I am able to unlock the register and clear the bit. This works while the target is running, but as soon as I do a system reset, the FLASH_OPTR reverts back to a value of 0xFBFFF8AA.

My code to clear the SWBOOT0 bit is below and all runs fine. What can I do to make FLASH_OPTR survive a system reset?

__ramfunc void ClearSWBOOT0Pin()

{

   

  HAL_FLASH_Unlock();

   

  HAL_FLASH_OB_Unlock();

   

  CLEAR_BIT(FLASH->OPTR, FLASH_OPTR_nSWBOOT0_Msk);

   

  CLEAR_BIT(FLASH->OPTR, FLASH_OPTR_nBOOT0_Msk);

   

  SET_BIT(FLASH->OPTR, FLASH_OPTR_nBOOT1_Msk); 

  HAL_FLASH_OB_Lock();

  HAL_FLASH_Lock();

   

}

This topic has been closed for replies.
Best answer by Uwe Bonnes

I think follwoing the reference manual e.g. for the STM32L412 RM0394 "3.4.2 Option bytes programming" is quite good at explaining.

4 replies

Uwe Bonnes
Chief
May 19, 2020

when is OPTSTART set?

GKerc.1
GKerc.1Author
Associate II
May 19, 2020

Thank you for the reply, Uwe. I wasn't setting OPTSTRT in my initial attempt.

After trial and error, I found that setting the FLASH_CR->OPTSTRT bit *before* writing to the FLASH_OPTR allows the contents to survive a power cycle or a system reset.

I know ST's answer to this will be to only program the option register bits using the ST-LINK tool and a software utility, but it would be really nice if the full sequence to write these bits in a non-volatile fashion were documented somewhere.

rgds,

Gary

Uwe Bonnes
Uwe BonnesBest answer
Chief
May 20, 2020

I think follwoing the reference manual e.g. for the STM32L412 RM0394 "3.4.2 Option bytes programming" is quite good at explaining.

GKerc.1
GKerc.1Author
Associate II
May 22, 2020

Thank you again for the reply Uwe. I am following the exact recipe in "3.4.2" and now it appears to be working. I had issues with my prototype board's power supply which I believe were causing issues with reset and FLASH_OPTR values. Working with a newer set of prototype boards, I now have no problems programming FLASH_OPTR.

best,

Gary