STM32F0 IAP实现关键点说明

目录​​​​​​​

一、bootloader关键代码

二、application关键代码


​​​​​​​

一、bootloader关键代码

#define APPLICATION_ADDRESS        (uint32_t)0x80004000    // app所在地址

typedef void (*pFunction)(void);

pFunction JumpToApplication;
uint32_t JumpAddress;


int main(void)
{
    ...

    if (((*(__IO uint32_t *)APPLICATION_ADDRESS) & 0xFFFF8000) == 0x20000000) {
        JumpAddress = *(__IO uint32_t *)(APPLICATION_ADDRESS + 4);
        JumpToApplication = (pFunction)JumpAddress;
        // 设置堆栈
        __set_MSP(*(__IO uint32_t *)APPLICATION_ADDRESS);
        // 跳转到app
        JumpToApplication();
    }

    return 0;
}

这里if条件判断主要是为了确保堆栈的地址在内存大小的地址范围内。

二、application关键代码

#define APPLICATION_ADDRESS     (uint32_t)0x08004000

#if (defined ( __CC_ARM ))
__IO uint32_t VectorTable[48] __attribute__((at(0x20000000)));
#elif (defined (__ICCARM__))
#pragma location = 0x20000000
__no_init __IO uint32_t VectorTable[48];
#elif defined (  __GNUC__  )
__IO uint32_t VectorTable[48] __attribute__((section(".RAMVectorTable")));
#endif

int main(void)
{
    ...

    /* Copy the vector table from the Flash (mapped at the base of the application
     load address 0x08004000) to the base address of the SRAM at 0x20000000. */
    for(i = 0; i < 48; i++) {
        VectorTable[i] = *(__IO uint32_t*)(APPLICATION_ADDRESS + (i<<2));
    }

    /* Enable the SYSCFG peripheral clock*/
    __HAL_RCC_SYSCFG_CLK_ENABLE(); 

    /* Remap SRAM at 0x00000000 */
    __HAL_SYSCFG_REMAPMEMORY_SRAM();

    ...

    return 0;
}

这里比较关键的是中断向量表的搬移,因为Cortex M0没有VTOR这个寄存器,所以需要将__Vectors至__Vectors_End之间的内容拷贝到0x20000000处。

再一个就是将内存映射到0x00000000处,即执行__HAL_SYSCFG_REMAPMEMORY_SRAM()调用。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值