3、IAP源码说明
(1)主函数:
- int main(void)
- {
- FLASH_Unlock(); /* FLASH解锁 */
- KEY_Init(); /* 按键初始化 */
- IAP_Init(); /* IAP初始化 */
-
- /* 按键按下则进入主菜单开始更新程序(注意:需要按下复位键的同时按下该按键触发程序更新) */
- if (GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2) == 0x00)
- {
- SerialPutString("\r\n======================================================================");
- SerialPutString("\r\n= (C) COPYRIGHT 2010 STMicroelectronics =");
- SerialPutString("\r\n= =");
- SerialPutString("\r\n= In-Application Programming Application (Version 3.3.0) =");
- SerialPutString("\r\n= =");
- SerialPutString("\r\n= By MCD Application Team =");
- SerialPutString("\r\n======================================================================");
- SerialPutString("\r\n\r\n");
- Main_Menu ();
- }
- /* 保持运行用户应用程序 */
- else
- {
- /* Test if user code is programmed starting from address "ApplicationAddress" */
- if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
- {
- /* Jump to user application */
- JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
- Jump_To_Application = (pFunction) JumpAddress;
- /* Initialize user application's Stack Pointer */
- __set_MSP(*(__IO uint32_t*) ApplicationAddress);
- Jump_To_Application();
- }
- }
- while (1){}
- return0;
- }
可见,我们按下复位的同时按下PE2对应的按键即可触发应用程序更新操作,否则跳转到应用程序的起始地址执行应用程序(断点重启也是默认执行应用程序),这里的ApplicationAddress就是我们上面设置的0x08003000。
(2)主菜单函数Main_Menu
这个函数里就是该IAP程序的功能,根据其中的打印信息:
可以知道,该IAP有三个功能(分别输入键盘上的数字1、2、3进行选择):一是下载程序(电脑->STM32);二是上传程序(STM32->电脑);三是执行新程序(即刚下载完成的程序)。
(3)下载程序功能函数SerialDownload
首先,上位机使用Ymodem协议进行数据下发,STM32根据协议解析数据,拿到有用的数据,并把这些数据写入FLASH对应的地址中,即ApplicationAddress。关于Ymodem协议及SerialDownload函数这里不展开讨论。
|