cancel
Showing results for 
Search instead for 
Did you mean: 

How to program FLASH_OPTR register to survive system reset?

GKerc.1
Associate II

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

   

}

1 ACCEPTED SOLUTION

Accepted Solutions
Uwe Bonnes
Principal II

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

View solution in original post

4 REPLIES 4
Uwe Bonnes
Principal II

when is OPTSTART set?

GKerc.1
Associate II

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
Principal II

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
Associate II

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