[其他ST产品] STM32G474 Nucleo-64 评测之二 双BankFlash一键切换

[复制链接]
1531|0
 楼主| BinWin 发表于 2020-8-6 23:26 | 显示全部楼层 |阅读模式
本帖最后由 BinWin 于 2020-8-6 23:32 编辑

      
       STM32G4提供了双BankFlash,支持同时在两个不同的Bank上加载应用程序,用户只需设置一个寄存器就可以实现瞬间切换这两个Bank间的地址,从而实现在线固件升级。安全存储区域可配置大小,用于存放密钥等敏感信息;当退出时可配置为锁定,应用程序无法再读取或调试,适合用于存储关键数据。而且保护固件安全实时升级;编程后调试访问禁用功能可以降低威胁隐患。接下来就来试试这个Flash的在线切换功能。
      首先要设计出两个不同的用户程序,在例程基础上修改为这样的:Bank1 中的程序间隔50ms串口发送字符串,LED闪烁。Bank2中的程序间隔1s串口发送字符串,LED闪烁,频率明显不同来区别当前运行的程序。
      这里也要借助ST的编程工具STM32CubeProgrammer来烧录bank2区域的用户程序,利用MDK条件编译生成Bin文件供烧录,可以选择这样的配置方式,不讲究路径

       a.png
      编译后就会在工程默认的FLASH_DualBoot.axf同目录下生成bin文件。打开STM32CubeProgrammer,选择好烧录文件,点击编程即可
       b.png
      这部分程序选择地址为0x8040000,接下来继续用MDK烧录0x8000000起始的程序文件
      c.png
     完成以上步骤,即可看到结果,打开串口助手查看字符串信息,同时观察板卡上的LED灯闪烁的频率,是和串口助手的接受频率一致的
      d.png
     可以看到时间戳显示间隔1秒钟,说明程序运行正常,此时按下蓝色的按键,会看到如下
     e.png
   此时的频率就快了很多,程序中写的是50毫秒,主循环中的代码部分如下,通过按键触发禁止或使能Bank2启动程序。
  1.   while (1)
  2.   {
  3.     /* Wait for User push-button is released */
  4.     if (BSP_PB_GetState(BUTTON_USER) == SET)
  5.     {
  6.       while (BSP_PB_GetState(BUTTON_USER) == SET);

  7.       /* Allow Access to Flash control registers and user Flash */
  8.       HAL_FLASH_Unlock();

  9.       /* Clear OPTVERR bit set on virgin samples */
  10.       __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

  11.       /* Allow Access to option bytes sector */
  12.       HAL_FLASH_OB_Unlock();

  13.       /* Get the Dual boot configuration status */
  14.       HAL_FLASHEx_OBGetConfig(&OBInit);

  15.       /* Enable/Disable dual boot feature */
  16.       OBInit.OptionType = OPTIONBYTE_USER;   
  17.       OBInit.USERType   = OB_USER_BFB2;

  18.       if (((OBInit.USERConfig) & (OB_BFB2_ENABLE)) == OB_BFB2_ENABLE)
  19.       {
  20.         OBInit.USERConfig = OB_BFB2_DISABLE;
  21.       }
  22.       else
  23.       {
  24.         OBInit.USERConfig = OB_BFB2_ENABLE;
  25.       }

  26.       if(HAL_FLASHEx_OBProgram (&OBInit) != HAL_OK)
  27.       {
  28.         /*
  29.         Error occurred while setting option bytes configuration.
  30.         User can add here some code to deal with this error.
  31.         To know the code error, user can call function 'HAL_FLASH_GetError()'
  32.         */
  33.         /* Infinite loop */
  34.         while (1)
  35.         {
  36.           /* Make LED2 blink (100ms on, 2s off) to indicate error */
  37.           BSP_LED_On(LED2);
  38.           HAL_Delay(100);
  39.           BSP_LED_Off(LED2);
  40.           HAL_Delay(2000);
  41.         }
  42.       }

  43.       /* Start the Option Bytes programming process */
  44.       if (HAL_FLASH_OB_Launch() != HAL_OK)
  45.       {
  46.         /*
  47.         Error occurred while reloading option bytes configuration.
  48.         User can add here some code to deal with this error.
  49.         To know the code error, user can call function 'HAL_FLASH_GetError()'
  50.         */
  51.         /* Infinite loop */
  52.         while (1)
  53.         {
  54.           /* Make LED2 blink (100ms on, 2s off) to indicate error */
  55.           BSP_LED_On(LED2);
  56.           HAL_Delay(100);
  57.           BSP_LED_Off(LED2);
  58.           HAL_Delay(2000);
  59.         }
  60.       }

  61.       /* Prevent Access to option bytes sector */
  62.       HAL_FLASH_OB_Lock();

  63.       /* Disable the Flash option control register access (recommended to protect
  64.       the option Bytes against possible unwanted operations) */
  65.       HAL_FLASH_Lock();

  66.     }
  67.     else
  68.     {
  69. #ifdef FLASH_BANK1
  70.       /* Toggle LED1 */
  71.       BSP_LED_Toggle(LED2);
  72.           HAL_UART_Transmit(&hlpuart1,(uint8_t*)"this is bank1",13,5000);
  73.           HAL_Delay(50);
  74. #else

  75.     /* USER CODE END WHILE */
  76.         
  77.     /* USER CODE BEGIN 3 */
  78.         BSP_LED_Toggle(LED2);
  79.         HAL_UART_Transmit(&hlpuart1,(uint8_t*)"this is bank2",13,5000);
  80.         HAL_Delay(1000);
  81. #endif
  82.         }
  83.   }
       Demo板载芯片是512K的FLASH,因此双Bank下每个区域也有256K,一般的应用程序搓搓有余,问题来了,假设Bank1程序突发状况进入HardFault中断,可否中断处理中使能Bank2运行程序,从而直接运行备份呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

20

主题

65

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部