cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G474RE write to OPTR Register because BOOT0-Pin is floating

ABach.4
Associate

My hardware design doesn't connect the BOOT0-Pin to a specific voltage level. That's why I need to write to the OPTR Register so that the BOOT0-Pin is not read at the boot sequence.

I check the OPTR Register:

FLASH->OPTR = ffeff8aa
  RDP         = aa
  BOR_LEV     = 0
  nRST_STOP   = 1
  nRST_STDBY  = 1
  nRST_SHDW   = 1
  IWDG_SW     = 1
  IWDG_STOP   = 1
  IWDG_STDBY  = 1
  WWDG_SW     = 1
  BFB2        = 0
  DBANK       = 1
  nBOOT1      = 1
  SRAM_PE     = 1
  CCMSRAM_RST = 1
  nSWBOOT0    = 1
  nBOOT0      = 1
  NRST_MODE   = 3
  IRHEN       = 1

I need to clear the nSWBOOT0 so that the BOOT0-pin is not been evaluated anymore.

0693W00000FBoS3QAL.pngI wrote a code sequence to clear the nSWBOOT0...

FLASH_OBProgramInitTypeDef FLASH_OBInitStruct;
 
HAL_FLASHEx_OBGetConfig(&FLASH_OBInitStruct);
 
FLASH_OBInitStruct.OptionType = OPTIONBYTE_USER;
FLASH_OBInitStruct.USERType = OB_USER_nSWBOOT0;
 
CLEAR_BIT(FLASH_OBInitStruct.USERConfig, FLASH_OPTR_nSWBOOT0);
 
printf("FLASH_OBProgramInitTypeDef\n");
printf("  OptionType    = %" PRIx32 "\n", FLASH_OBInitStruct.OptionType);
printf("  RDPLevel      = %" PRIx32 "\n", FLASH_OBInitStruct.RDPLevel);
printf("  USERType      = %" PRIx32 "\n", FLASH_OBInitStruct.USERType);
printf("  USERConfig    = %" PRIx32 "\n", FLASH_OBInitStruct.USERConfig);
printf("  PCROPConfig   = %" PRIx32 "\n", FLASH_OBInitStruct.PCROPConfig);
printf("\n");
 
/* Unlock the option bytes block access */
HAL_FLASH_OB_Unlock();
 
HAL_FLASHEx_OBProgram(&FLASH_OBInitStruct);
 
/* Launch the option byte loading */
HAL_FLASH_OB_Launch();
 
/* Locks the option bytes block access */
HAL_FLASH_OB_Lock();

... that outputs the right value...

FLASH_OBProgramInitTypeDef
  OptionType    = 4
  RDPLevel      = aa
  USERType      = 2000
  USERConfig    = fbeff800
  PCROPConfig   = 0

... but it goes to the HardFault_Handler:

0693W00000FBoVkQAL.pngMy question: why?

1 ACCEPTED SOLUTION

Accepted Solutions
ABach.4
Associate

I found something related here: Unlock readout protection

It first unlocks the Flash itself with HAL_FLASH_Unlock()

Now it works!

Here is the almost working example (it writes to OPTR Register but also reloads the program somehow):

FLASH_OBProgramInitTypeDef FLASH_OBInitStruct;
 
HAL_FLASHEx_OBGetConfig(&FLASH_OBInitStruct);
 
FLASH_OBInitStruct.OptionType = OPTIONBYTE_USER;
FLASH_OBInitStruct.USERType = OB_USER_nSWBOOT0;
 
CLEAR_BIT(FLASH_OBInitStruct.USERConfig, FLASH_OPTR_nSWBOOT0);
 
printf("FLASH_OBProgramInitTypeDef\n");
printf("  OptionType    = %" PRIx32 "\n", FLASH_OBInitStruct.OptionType);
printf("  RDPLevel      = %" PRIx32 "\n", FLASH_OBInitStruct.RDPLevel);
printf("  USERType      = %" PRIx32 "\n", FLASH_OBInitStruct.USERType);
printf("  USERConfig    = %" PRIx32 "\n", FLASH_OBInitStruct.USERConfig);
printf("  PCROPConfig   = %" PRIx32 "\n", FLASH_OBInitStruct.PCROPConfig);
printf("\n");
 
/* Unlock the FLASH control register access */
HAL_FLASH_Unlock();
 
/* Unlock the option bytes block access */
HAL_FLASH_OB_Unlock();
 
HAL_FLASHEx_OBProgram(&FLASH_OBInitStruct);
 
/* Launch the option byte loading */
HAL_FLASH_OB_Launch();
 
/* Locks the option bytes block access */
HAL_FLASH_OB_Lock();
 
/* Lock the FLASH control register access */
HAL_FLASH_Lock();

Here is an other example from ST itself:

C:\Users\andreas\STM32Cube\Repository\STM32Cube_FW_G4_V1.4.0\Projects\NUCLEO-G474RE\Examples\FLASH\FLASH_WriteProtection\Src

For me the problem is solved!

View solution in original post

1 REPLY 1
ABach.4
Associate

I found something related here: Unlock readout protection

It first unlocks the Flash itself with HAL_FLASH_Unlock()

Now it works!

Here is the almost working example (it writes to OPTR Register but also reloads the program somehow):

FLASH_OBProgramInitTypeDef FLASH_OBInitStruct;
 
HAL_FLASHEx_OBGetConfig(&FLASH_OBInitStruct);
 
FLASH_OBInitStruct.OptionType = OPTIONBYTE_USER;
FLASH_OBInitStruct.USERType = OB_USER_nSWBOOT0;
 
CLEAR_BIT(FLASH_OBInitStruct.USERConfig, FLASH_OPTR_nSWBOOT0);
 
printf("FLASH_OBProgramInitTypeDef\n");
printf("  OptionType    = %" PRIx32 "\n", FLASH_OBInitStruct.OptionType);
printf("  RDPLevel      = %" PRIx32 "\n", FLASH_OBInitStruct.RDPLevel);
printf("  USERType      = %" PRIx32 "\n", FLASH_OBInitStruct.USERType);
printf("  USERConfig    = %" PRIx32 "\n", FLASH_OBInitStruct.USERConfig);
printf("  PCROPConfig   = %" PRIx32 "\n", FLASH_OBInitStruct.PCROPConfig);
printf("\n");
 
/* Unlock the FLASH control register access */
HAL_FLASH_Unlock();
 
/* Unlock the option bytes block access */
HAL_FLASH_OB_Unlock();
 
HAL_FLASHEx_OBProgram(&FLASH_OBInitStruct);
 
/* Launch the option byte loading */
HAL_FLASH_OB_Launch();
 
/* Locks the option bytes block access */
HAL_FLASH_OB_Lock();
 
/* Lock the FLASH control register access */
HAL_FLASH_Lock();

Here is an other example from ST itself:

C:\Users\andreas\STM32Cube\Repository\STM32Cube_FW_G4_V1.4.0\Projects\NUCLEO-G474RE\Examples\FLASH\FLASH_WriteProtection\Src

For me the problem is solved!