cancel
Showing results for 
Search instead for 
Did you mean: 

MX_FLASH_Init does not launch option bytes loading

continuum
Associate II
MCU: STM32H563VITx

 

Stm32CubeMX version: 6.14.0

 

HAL Driver version: 1.5.0

 

When I enable EDATA option bytes in CubeMX, it generates the following code: 
void MX_FLASH_Init(void)
{

  /* USER CODE BEGIN FLASH_Init 0 */

  /* USER CODE END FLASH_Init 0 */

  FLASH_OBProgramInitTypeDef pOBInit = {0};

  /* USER CODE BEGIN FLASH_Init 1 */

  /* USER CODE END FLASH_Init 1 */
  if (HAL_FLASH_Unlock() != HAL_OK)
  {
    Error_Handler();
  }

  /* Option Bytes settings */

  if (HAL_FLASH_OB_Unlock() != HAL_OK)
  {
    Error_Handler();
  }
  pOBInit.OptionType = OPTIONBYTE_EDATA;
  pOBInit.Banks = FLASH_BANK_2;
  pOBInit.EDATASize = 2;
  if (HAL_FLASHEx_OBProgram(&pOBInit) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_FLASH_OB_Lock() != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_FLASH_Lock() != HAL_OK)
  {
    Error_Handler();
  }

  /* Launch Option Bytes Loading */
  /*HAL_FLASH_OB_Launch(); */

  /* USER CODE BEGIN FLASH_Init 2 */

  /* USER CODE END FLASH_Init 2 */

}​

 

The function does not launch option bytes loading. The 41st line suggests that just uncommenting it should enable it but this is misleading. Running `HAL_FLASH_OB_Launch` after `HAL_FLASH_Lock` will fail as the `HAL_FLASH_OB_Launch` function does not unlock flash option bytes. The function should be placed before `
HAL_FLASH_OB_Lock` or the flash option bytes should be unlocked again. 
1 REPLY 1
KDJEM.1
ST Employee

Hello @continuum;

 

Could you please try with sequence and let me know if the issue is solved or not?

  if (HAL_FLASHEx_OBProgram(&FLASH_OBInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /* Start option byte load operation after successful programming operation */
  HAL_FLASH_OB_Launch();

  /* Lock the Flash to disable the flash control register access (recommended
     to protect the FLASH memory against possible unwanted operation)
  */
  HAL_FLASH_Lock();

  /* Lock the Flash control option to restrict register access */
  HAL_FLASH_OB_Lock();

 

Also, I recommend you to look at STM32CubeH5/Projects/NUCLEO-H563ZI/Examples/FLASH/FLASH_EDATA_EraseProgram at main · STMicroelectronics/STM32CubeH5 · GitHub example may help you to check your code.

 

I hope this help you.

Thank you.

Kaouthar

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.