cancel
Showing results for 
Search instead for 
Did you mean: 

How to program the Option Bytes on a G0?

JKaz.1
Associate III

I'm trying to program the option bytes on an STM32G0B1 but it keeps timing out at the end of the program sequence and never actually succeeds, no matter how many times I try.

I enable the Flash peripheral

I (successfully) unlock the flash

I (successfully) unlock the option bytes

I program the option bytes

... and it times out 10 times

then it calls OB_Launch and hard faults.

.. what am I missing? This was easy on the H7...

{	
	__HAL_RCC_FLASH_CLK_ENABLE();
 
	FLASH_OBProgramInitTypeDef flashDef;
	memset(&flashDef, 0, sizeof(FLASH_OBProgramInitTypeDef));	
	HAL_FLASHEx_OBGetConfig(&flashDef);
	
	if ((flashDef.USERConfig & OB_BOR_ENABLE) == 0)
	{
		for (int i = 0; i < 10; i++)
		{
			if (HAL_FLASH_Unlock() == HAL_OK)
				break;
		}		
		
		for (int i = 0; i < 10; i++)
		{
			if (HAL_FLASH_OB_Unlock() == HAL_OK)
				break;
		}	
	
		memset(&flashDef, 0, sizeof(FLASH_OBProgramInitTypeDef));
		flashDef.OptionType = OPTIONBYTE_USER;
		flashDef.USERType = OB_USER_BOR_EN;
		flashDef.USERConfig = OB_BOR_ENABLE;
	
		for (int i = 0; i < 10; i++)
		{
			if (HAL_FLASHEx_OBProgram(&flashDef) == HAL_OK)
				break;
		}
		
		HAL_FLASH_OB_Launch();
	
		for (int i = 0; i < 10; i++)
		{
			if (HAL_FLASH_OB_Lock() == HAL_OK)
				break;
		}
	
		for (int i = 0; i < 10; i++)
		{
			if (HAL_FLASH_Lock() == HAL_OK)
				break;
		}
	}
}

2 REPLIES 2
Pavel A.
Evangelist III

Same thing as here?

Yup, looks like it. I didn't find that post while searching the forums to see if anyone had similar difficulties... Kinda surprised this doesn't have an answer yet...