你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32F407的BootLoader程序

[复制链接]
STMCU小助手 发布时间:2022-4-13 16:00
BootLoader程序介绍
BootLoader其实就是一段启动程序,它在芯片启动的时候首先被执行,它可以用来做一些硬件的初始化,当初始化完成之后跳转到对应的应用程序中去。例如,我们可以将Flash分为两个区,一个是启动程序区(0x0800 0000 - 0x0800 2000)大小为8K Bytes,剩下的为应用程序区(0x0800 2000 - 0x0801 0000)。芯片上电时先运行启动程序,然后跳转到应用程序区执行应用程序。

源码实现

基本的BootLoader程序仅实现了代码跳转的功能,因此全部在main.c文件中实现。

源文件
  1. /*
  2. ******************************************************************************
  3. File:     main.c
  4. Info:     Generated by Atollic TrueSTUDIO(R) 9.3.0   2020-09-07

  5. The MIT License (MIT)
  6. Copyright (c) 2019 STMicroelectronics

  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:

  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.

  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.

  22. ******************************************************************************
  23. */

  24. /* Includes */
  25. #include "stm32f4xx.h"
  26. #include "delay.h"
  27. /* Private macro */

  28. //用户扇区从第5扇区的起始位置开始启动
  29. #define FLASH_APP_ADDR                        0x08010000

  30. #define MCU_LED                                                GPIOA_OUT(15)

  31. /* Private variables */
  32. typedef  void (*UserApplication)(void);                                //定义一个函数类型的参数.

  33. UserApplication userApp;

  34. /* Private function prototypes */
  35. void ConfigLED( );
  36. void SystemBooting(uint8_t times, uint16_t msDelay);
  37. void IAPLoadApp(u32 appxaddr);

  38. /* Private functions */


  39. /**
  40. **===========================================================================
  41. **
  42. **  Abstract: main program
  43. **
  44. **===========================================================================
  45. */
  46. int main(void)
  47. {


  48.         /**
  49.          *  IMPORTANT NOTE!
  50.          *  The symbol VECT_TAB_SRAM needs to be defined when building the project
  51.          *  if code has been located to RAM and interrupts are used.
  52.          *  Otherwise the interrupt table located in flash will be used.
  53.          *  See also the <system_*.c> file and how the SystemInit() function updates
  54.          *  SCB->VTOR register.
  55.          *  E.g.  SCB->VTOR = 0x20000000;
  56.          */

  57.         /* TODO - Add your application code here */

  58.         delay_init();
  59.         ConfigLED();
  60.         SystemBooting(5,100);

  61.         //从第五扇区起始位置开始加载应用程序
  62.         IAPLoadApp(FLASH_APP_ADDR);

  63.         while(1);
  64. }

  65. void ConfigLED( )
  66. {
  67.         GPIO_InitTypeDef GPIO_InitStructure;
  68.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  69.         /**
  70.          * System State LED
  71.          */
  72.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  73.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  74.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  75.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  76.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  77.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  78. }

  79. void SystemBooting(uint8_t times, uint16_t msDelay)
  80. {
  81.         for (uint8_t i = 0; i < times; i++)
  82.         {
  83.                 MCU_LED = 0;
  84.                 delay_ms(msDelay);
  85.                 MCU_LED = 1;
  86.                 delay_ms(msDelay);
  87.         }
  88. }

  89. void IAPLoadApp(u32 appxaddr)
  90. {
  91.         if(((*(vu32*)appxaddr)&0x2FF00000)==0x20000000)                                //检查栈顶地址是否合法.
  92.         {
  93.                 userApp=(UserApplication)*(__IO vu32*)(appxaddr+4);        //用户代码区第二个字为程序开始地址(复位地址)
  94.                 __set_MSP(*(__IO uint32_t*) appxaddr);                                                        //初始化APP堆栈指针(用户代码区的第一个字用于存放栈顶地址)
  95.                 userApp();                                                                                                                                                //跳转到APP.
  96.         }
  97. }
复制代码

stm32f4_flash.ld
设置BootLoader程序在flash中的位置为第1~2扇区,共32K空间。在笔者的项目中,第3~4扇区用来实现EEPROM的软件仿真,用户程序将从第5扇区开始启动。

  1. /* Specify the memory areas */
  2. MEMORY
  3. {
  4.   FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 32K
  5.   RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 128K
  6.   MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
  7.   CCMRAM (rw)     : ORIGIN = 0x10000000, LENGTH = 64K
  8. }
复制代码

使用指南
用户程序需要从第5扇区的起始位置开始下载,同时需要设置中断向量的偏移地址。

设置用户程序下载位置(stm32f4_flash.ld)

在笔者的项目中,设置BootLoader程序在flash中的位置为第1~2扇区,共32K空间。第3~4扇区用来实现EEPROM的软件仿真,共32K空间。用户程序将从第5扇区开始启动。

  1. /* Memories definition */
  2. MEMORY
  3. {
  4.   FLASH (rx)    : ORIGIN = 0x8010000,   LENGTH = 448K
  5.   RAM (xrw)            : ORIGIN = 0x20000000,  LENGTH = 128K
  6.   CCMRAM (xrw)  : ORIGIN = 0x10000000,  LENGTH = 64K
  7. }
复制代码

修改中断向量表偏移量
在"system_stm32f4xx.c"文件中,修改宏定义:

  1. /*!< Uncomment the following line if you need to relocate your vector Table in
  2.      Internal SRAM. */
  3. /* #define VECT_TAB_SRAM */
  4. #define VECT_TAB_OFFSET  0x10000 /*!< Vector Table base offset field.
  5.                                    This value must be a multiple of 0x200. */
复制代码

在void SystemInit(void)中将会根据上述宏定义,设置中断向量表的偏移量:

  1. /* Configure the Vector Table location add offset address ------------------*/
  2. #ifdef VECT_TAB_SRAM
  3.   SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
  4. #else
  5.   SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
  6. #endif
复制代码



收藏 评论0 发布时间:2022-4-13 16:00

举报

0个回答
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版