cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with writing the DBANK option byte on STM32G474RE

DGrgic
Associate II

Hello,

I'm using the NUCLEO-G474RE development board and trying to set the DBANK option byte. I can do that using STM32CubeProgrammer without any issues, and everything works as it should.

But when I try to do that in the firmware using the HAL function, I get stacked and the debugger gives me this message:

Error in executing 'cont' command ... 

Target is not responding, retrying...

I've tried to do this in two ways, and both gave me the same results:

1.

HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
 
OB.OptionType = OPTIONBYTE_USER;
OB.USERType = OB_USER_DBANK;
OB.USERConfig = OB_DBANK_128_BITS;
 
if ( HAL_FLASHEx_OBProgram(&OB) != HAL_OK )
{
	// Error in programming
        HAL_FLASH_OB_Lock();
        HAL_FLASH_Lock();
}
 
HAL_FLASH_OB_Launch();
 
/* We should not make it past the Launch, so lock
 * flash memory and return an error from function
 */
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();

2.

  FLASH_OBProgramInitTypeDef flash_ob = {0};
  HAL_FLASH_Unlock();
  HAL_FLASH_OB_Unlock();
  HAL_FLASHEx_OBGetConfig(&flash_ob);
 
  printf("HAL_FLASHEx_OBGetConfig\r\n\tOptionType %.4x\r\n\tWRPArea "
         "%.4x\r\n\tWRPStartOffset %.4x\r\n\tWRPEndOffset %.4x\r\n\tRDPLevel "
         "%.4x\r\n\tUSERType %.4x\r\n\tUSERConfig %.4x\r\n\tPCROPConfig "
         "%.4x\r\n\tPCROPStartAddr %.4x\r\n\tPCROPEndAddr "
         "%.4x\r\n\tBootEntryPoint "
         "%.4x\r\n\tSecBank %.4x\r\n\tSecSize %.4x\r\r\n",
         flash_ob.OptionType, flash_ob.WRPArea, flash_ob.WRPStartOffset,
         flash_ob.WRPEndOffset, flash_ob.RDPLevel, flash_ob.USERType,
         flash_ob.USERConfig, flash_ob.PCROPConfig, flash_ob.PCROPStartAddr,
         flash_ob.PCROPEndAddr, flash_ob.BootEntryPoint, flash_ob.SecBank,
         flash_ob.SecSize);
  HAL_FLASH_OB_Lock();
  HAL_FLASH_Lock();
 
  if(flash_ob.USERConfig & 0x01 << 22)
  {
      HAL_FLASH_Unlock();
      HAL_FLASH_OB_Unlock();
      printf("DBANK == 1, Need to set single bank\r\n");
 
      HAL_FLASHEx_OB_DBankConfig(OB_DBANK_128_BITS);
      HAL_FLASH_OB_Launch();
      HAL_FLASH_OB_Lock();
      HAL_FLASH_Lock();
  }
  else
  {
      printf("DBANK == 0, Single bank SET\r\n");
  }

As you can see in the second example, I've even succeeded in reading option bytes and everything is OK until I call HAL_FLASH_OB_Launch(); or SET_BIT(FLASH->CR, FLASH_CR_OBL_LAUNCH);.

After that debugger gives me those two messages (Error in executing 'cont' command ... Target is not responding, retrying...)

I can't even reset the board and need to upload firmware again, and interesting thing is that after re-upload of a firmware option byte is set to SingleBank.

Thank you in advance and best regards,

Dragan

2 REPLIES 2
Amel NASRI
ST Employee

Hi @DGrgic​ ,

A power reset is required so that the option bytes take effect.

One tip shared in this article (How to program STM32 Option Bytes with the HAL API) is to add "return HAL_ERROR;" after HAL_FLASH_Lock.

Try to put in place a similar implementation as what is suggested in the article. Does it work this way?

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

DGrgic
Associate II

Amel thanks for your answer.

Yes, I forgot to add return in that case indeed, but the thing is that I'm using a debugger, and I see there that HAL_FLASHEx_OBProgram returns HAL_OK and continues where it should go.

It seems that I'm missing some steps before calling HAL_FLASH_OB_Launch.

Dragan