stm32 iap笔记

要想使用bootloader,只需下边几个步骤:

1. 分别写两个独立的程序: bootloader 和 app
2. bootloader程序栈顶地址按默认0x08000000, 假设此代码最大为4K  
3. 在bootloader程序里执行自己的代码,最后,需要跳转到app处执行,此代码直接按官方例子拷贝即可

//根据程序需要而定义的宏
#define BOOTLOADER_SIZE_MAX  4096                                  //4K
#define APP_SIZE_MAX         61440                                 //60K  60*1024
#define BOOTLADER_ADDRESS    0x08000000                            //bootloader首地址
#define APPLICATION_ADDRESS  BOOTLADER_ADDRESS+BOOTLOADER_SIZE_MAX //可执行程序首地址
typedef      void (*pFunction)(void);
pFunction    JumpToApplication;
uint32_t     JumpAddress;
void jump2app(void)
{
    /* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
    if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
    {
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
      JumpToApplication = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
      JumpToApplication();
    }
}

4.  app程序栈顶地址改为0x08001000(ps:与0x08000000地址差为4K,此4k空间留给bootloader)
5.  app程序中重置中断向量表的地址,有两种方法,一种是直接修改宏定义,另一种使用NVIC_SetVectorTable()函数。
   一种方法:  在标准库及LL库中(ps:HAL库没使用,不确定) 都有SystemInit()函数,其中VECT_TAB_OFFSET即为中断向量便宜地址,鼠标右键 "goto Definition ...." 找到定义位置修改为偏移值即可,本例子是4K (0x1000)  

#ifdef VECT_TAB_SRAM
  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */
#else
  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;/* Vector Table Relocation in Internal FLASH. */
#endif 

  另一种方法: 使用NVIC_SetVectorTable(),此函数必须放在SystemInit()函数之后。暂时只证实标准库中有此函数。

	
SystemInit();                                    //系统时钟初始化为72M
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x1000); //必须放在systemInit()之后,
                                                 //因为systemInit()中有对VTOR的初始化

6. 合并hex文件烧写到mcu即可

猜你喜欢

转载自blog.csdn.net/sierllen/article/details/81482515