Question
MX_FLASH_Init does not launch option bytes loading
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.
