什么是ISP
ISP是STM32单片机使用串口下载升级程序的下载协议。通过上电检测Boot0和Boot1的电平从不同的位置运行程序。
本文实现的功能
通过在用户的应用程序中触发更新标志,将程序跳转到System memory中去执行32的ISP固件升级代码
应用场景
在stm32的在线升级中使用,例如使用arm-linux做主控,stm32做从机。通过上位机下发升级命令进入ISP,使用stm32flash将程序文件下载到stm32中。
上代码
/**
* Function to perform jump to system memory boot from user application
*
* Call function when you want to jump to system memory
*/
void JumpToBootloader(void) {
void (*SysMemBootJump)(void);
// volatile uint32_t addr = 0x1FFFEC00; //System memory首地址,此地址为stm32f030f4的首地址,
volatile uint32_t addr = 0x1FFFF000; //此地址为STM32F103RE的地址,其余型号地址自行在数据手册中进行查找
/**
* Step: Disable RCC, set it to default (after reset) settings
* Internal clock, no PLL, etc.
*/
#if defined(USE_HAL_DRIVER)
HAL_RCC_DeInit();
#endif /* defined(USE_HAL_DRIVER) */
#if defined(USE_STDPERIPH_DRIVER)
RCC_DeInit();
#endif /* defined(USE_STDPERIPH_DRIVER) */
/**
* Step: Disable systick timer and reset it to default values
*/
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
/**
* Step: Disable all interrupts
*/
__disable_irq();
/**
* Step: Remap system memory to address 0x0000 0000 in address space
* For each family registers may be different.
* Check reference manual for each family.
*
* For STM32F4xx, MEMRMP register in SYSCFG is used (bits[1:0])
* For STM32F0xx, CFGR1 register in SYSCFG is used (bits[1:0])
* For others, check family reference manual
*/
//Remap by hand... {
#if defined(STM32F4)
SYSCFG->MEMRMP = 0x01;
#endif
#if defined(STM32F0)
SYSCFG->CFGR1 = 0x01;
#endif
//} ...or if you use HAL drivers
//__HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH(); //Call HAL macro to do this for you
/**
* Step: Set jump memory location for system memory
* Use address with 4 bytes offset which specifies jump location where program starts
*/
SysMemBootJump = (void (*)(void)) (*((uint32_t *)(addr + 4)));
/**
* Step: Set main stack pointer.
* This step must be done last otherwise local variables in this function
* don't have proper value since stack pointer is located on different position
*
* Set direct address location which specifies stack pointer in SRAM location
*/
__set_MSP(*(uint32_t *)addr);
/**
* Step: Actually call our function to jump to set location
* This will start system memory execution
*/
SysMemBootJump();
/**
* Step: Connect USB<->UART converter to dedicated USART pins and test
* and test with bootloader works with STM32 Flash Loader Demonstrator software
*/
}
结果
本文已在STM32F103RE上进行验证,亲测可用,其余单片机自行验证。
但是stm32f030f4上不能实现,具体原因尚在寻找,若有大神知道敬请指教。
STM32F103RE验证源码下载
https://download.csdn.net/download/cooper1024/11139789
测试过程
- 1、将源码下载到片子中
- 2、重启程序通过串口调试助手可以看到程序中的打印。
3、关闭串口调试助手,打开Flymcu,配置串口下载文件,选择文件点击“开始编程”。发现程序直接就下载片子中去了。可见程序已经进入ISP固件升级了。此方法省去了用Boot0和Boot1的高低电平进入ISP固件升级模式的操作。