Src/main.c
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
Private user code
USER CODE BEGIN 0
Private user code USER CODE BEGIN 0
static void EnableBootLoader(void)
{
  FLASH_OBProgramInitTypeDef obInit = {0};
  HAL_FLASHEx_OBGetConfig(&obInit);
  if (((obInit.USERConfig & OB_USER_nBOOT1) == OB_BOOT1_SYSTEM) &&
      ((obInit.USERConfig & OB_USER_nBOOT_SEL) == OB_BOOT0_FROM_PIN))
  {
    printf("Boot config OK\n");
    return;
  }
  printf("Enabling BOOT0\n");
  obInit.USERConfig &= ~OB_USER_nBOOT1;
  obInit.USERConfig &= ~OB_USER_nBOOT_SEL;
  obInit.USERConfig |= OB_BOOT1_SYSTEM;
  obInit.USERConfig |= OB_BOOT0_FROM_PIN;
  obInit.OptionType = OPTIONBYTE_USER;
  HAL_StatusTypeDef result = HAL_FLASH_Unlock();
  if (result == HAL_OK)
  {
    result = HAL_FLASH_OB_Unlock();
    if (result == HAL_OK)
    {
      result = HAL_FLASHEx_OBProgram(&obInit);
      if (result == HAL_OK)
      {
        printf("Reset and reload\n");
        HAL_FLASH_OB_Launch();
      }
      HAL_FLASH_OB_Lock();
    }
    HAL_FLASH_Lock();
  }
  printf("BOOT0 enable failed\n");
}
static void EnableBootLoader ( void ) { FLASH_OBProgramInitTypeDef obInit = { 0 } ; HAL_FLASHEx_OBGetConfig ( & obInit ) ; if ( ( ( obInit . USERConfig & OB_USER_nBOOT1 ) == OB_BOOT1_SYSTEM ) && ( ( obInit . USERConfig & OB_USER_nBOOT_SEL ) == OB_BOOT0_FROM_PIN ) ) { printf ( " \n " ) ; return ; } printf ( " \n " ) ; obInit . USERConfig &= ~ OB_USER_nBOOT1 ; obInit . USERConfig &= ~ OB_USER_nBOOT_SEL ; obInit . USERConfig |= OB_BOOT1_SYSTEM ; obInit . USERConfig |= OB_BOOT0_FROM_PIN ; obInit . OptionType = OPTIONBYTE_USER ; HAL_StatusTypeDef result = HAL_FLASH_Unlock ( ) ; if ( result == HAL_OK ) { result = HAL_FLASH_OB_Unlock ( ) ; if ( result == HAL_OK ) { result = HAL_FLASHEx_OBProgram ( & obInit ) ; if ( result == HAL_OK ) { printf ( " \n " ) ; HAL_FLASH_OB_Launch ( ) ; } HAL_FLASH_OB_Lock ( ) ; } HAL_FLASH_Lock ( ) ; } printf ( " \n " ) ; }
Private user code
USER CODE BEGIN 0
Private user code USER CODE BEGIN 0
/*
     * Boot config bits are OK and setting BOOT0 pin (PA14) high will
     * enter bootloader.
     */ // The HAL_FLASH_OB_Launch should reset the device
 // so if we end up here something failed.
