cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G070 flash option bytes (for bootloader) reset after power cycle

jdesjardin
Associate

Hi,

I want ot use the UART bootloader to program a STM32G070RB (with the Nucleo board). I've used the following code to activate the pin Boot0 to access the bootloader (some of it may be useless):

while(FLASH -> SR & FLASH_SR_BSY1);
FLASH->KEYR = KEY1;
FLASH->KEYR = KEY2;
while(FLASH -> SR & FLASH_SR_BSY1);
FLASH-> OPTKEYR = OPTKEY1;
FLASH-> OPTKEYR = OPTKEY2;
while(FLASH -> SR & FLASH_SR_BSY1);
FLASH->CR |= FLASH_CR_OPTSTRT;
while(FLASH -> SR & FLASH_SR_BSY1);
FLASH->OPTR |= FLASH_OPTR_nBOOT0;
FLASH->OPTR |= FLASH_OPTR_nBOOT1;
FLASH->OPTR &= ~FLASH_OPTR_nBOOT_SEL;
for(unsigned long i = 0;i<2000000;i++)
  if(!(FLASH -> SR & FLASH_SR_BSY1))
    break;

FLASH->CR |= FLASH_CR_OBL_LAUNCH;

 

It works fine, and I can program the board several times, even after a reset, without having to rerun this code since the option bytes stay set.

However, when I power down and back up the board, I have to run the code again to activate the Boot0 pin. But I don't see any difference when looking at the flash registers in the debugger: in particular, BOOTSEL stays at 0 even after powering up, so the Boot0 pin should already be active. Running the sequence seems to reactivate the Boot0 pin, but without changing the registers.

What is the reason for this? Is there a way to ensure the Boot0 pin stays active forever, even after a power cycle? In my final application the UART will be the only way to program the STM32, so I don't want to accidentally brick it by programming it with a bugged program that can't properly do the Flash option bytes sequence.

Thanks

2 REPLIES 2
TDK
Guru

By "activate boot pin" do you mean "set nBOOT_SEL = 0"?

> However, when I power down and back up the board, I have to run the code again to activate the Boot0 pin. But I don't see any difference when looking at the flash registers in the debugger:

If the flash registers reflect nBOOT_SEL = 0, what is leading you to believe that BOOT0 is not being read?

If you feel a post has answered your question, please click "Accept as Solution".

By "activate boot pin" do you mean "set nBOOT_SEL = 0"?

 

 

 

 

Yes

 

 

If the flash registers reflect nBOOT_SEL = 0, what is leading you to believe that BOOT0 is not being read?


After a power cycle, when I reset with BOOT0 tied to high, the board doesn't enter bootloader and runs its flashed program normally. Only when I've run the sequence again can I enter bootloader by resetting with BOOT0 tied to high.