STM32F072 的BootLoader 不能够进行Vector table的relocation的解决方案

在RM中给出了解决方案。

Unlike Cortex® M3 and M4, the M0 CPU does not support the vector table relocation. For application code which is located in a different address than 0x0800 0000, some additional code must be added in order to be able to serve the application interrupts. A solution will be to relocate by software the vector table to the internal SRAM:
• Copy the vector table from the Flash (mapped at the base of the application load address) to the base address of the SRAM at 0x2000 0000.
• Remap SRAM at address 0x0000 0000, using SYSCFG configuration register 1.
• Then once an interrupt occurs, the Cortex®-M0 processor will fetch the interrupt handler start address from the relocated vector table in SRAM, then it will jump to
execute the interrupt handler located in the Flash. This operation should be done at the initialization phase of the application. Please refer to
AN4065 and attached IAP code from www.st.com for more details.

自己在stm32CubeIDE中实现的代码如下,在main初始化的适当位置中可以对其进行调用。注意需要包含头文件

stm32f0xx_hal.h

void vector_table_and_remap(void)
{
    uint32_t addr = 0x08008000; //this address should be same as defined in linker file of this project.
    memcpy((void*)0x20000000, (void*)addr, VECTOR_SIZE);
    /* this feature is not the part of "STM32 Cortex-M0 processor programming
       model, instruction set and core peripherals."
       please refer to "System configuration controller (SYSCFG)" in RM.
    and refer to "Boot configuration".
    as is
    "Boot from main Flash memory: the main Flash memory is aliased in the boot memory space (0x00000000),
     but still accessible from its original memory space(0x08000000).
     In other words, the Flash memory contents can be accessed starting from address 0x00000000 or 0x08000000."

     *
     *  */
    __HAL_SYSCFG_REMAPMEMORY_SRAM(); //20200317. first tested, it works.

}

不同内核的芯片的处理方式是不一样的。在是stm32cubeide中,以一个stm32F4的芯片为例,src文件夹下面的system_stm32f4xx.c提供了SystemInit()提供了中断的vector table的offset的设置。
可以将vector table放到ram当中去即可。这种处理方式由于硬件的支持而变得更加灵活和合理。

术语表示:
Boot:CAN BootLoader的简称。由一个独立的工程创建。只有在通过can对设备进行升级的时候使用。
App: 由一个独立的工程创建,完成所有功能。由于STM32F072这款芯片的特殊性(是Cortex M0内核),存在少量对boot的依赖。

先设定STM32的BootLoader占用空间为32K。

对App的修改如下:
1、对RAM和Flash的修改
所以App的的Flash起始地址需要从 0x08008000 开始。
由于该芯片的Vector table无法实现relocation。而使用芯片的参考手册中提到的__HAL_SYSCFG_REMAPMEMORY_SRAM()可以解决这个问题。整个中断向量表的位置放置在RAM中。APP的RAM的起始地址为0x200000C0。


2、对Boot的依赖
在加入BootLoader后,第一次调试app之前,需要对MCU刷入任意版本的boot的二进制文件,否则可能导致程序无法正常启动,无法运行。
由于加入了boot之后,里面存在相互依赖。

3、需要在app的main.c中加入一行特殊的初始化的代码。
此项为重大修改。否则整个app无法正常工作。

对于Boot的修改如下:

由于boot是一个单独的工程,所以,在App已完成适配的前提下,不需要做特殊处理。除非原定的32K的Flash空间不能够容纳boot。

原文地址:https://www.cnblogs.com/praiseslow/p/12512777.html