Recently, I took APM32F407 as an example,trained the demo code of the Geehy IAP upgrade program based on the MDK platform.
First, we need to download the APM32F407 demo code from Geehy’s official website, and the IAP demo contains 3 demos. APP1 and APP2 are user programs and represent different applications of the customer. The IAP demo is a user upgrade program.
In practical applications, IAP programs and user programs can be programmed to flash together at production time. When an app needs to be upgraded, it can be upgraded via IAP.
一、APM32F407 Flash distribution table
Table as follows, IAP program and user program have the same flash functional structure, only have different starting address. Now take IAP program as an example to introduce its flash allocation.
The stack top address is stored at the top. Next we store the NVIC vector table. When an interrupt occurs in the program, the program looks up the entry address of the interrupt vector table at this entry address, and then executes the ISP program. The interrupt service procedure is different for each user, but the entry address of the interrupt vector table is the same. The top address of the IAP program stack is 0×08000000, and the entry address of the IAP program interrupt vector table is 0×08000004.
Then there’s your interrupt service routine. The last part houses the application.
二、Program operation process
We can see that after the system is reset, the stack top address is obtained and stored in the SP. Then put the next pointer address into the PC program counter and start executing the reset interrupt service program Reset_Handler. After executing the program, jump to the SystemInit() function, and finally execute the main() function.
The following is the contents of the startup.s file.
三、IAP upgrade process
The IAP program is also a user program, and its function is to upgrade the user program.
After the MCU reset, it will run to the user-customized IAP program, and if it does not receive the upgrade command, it will jump to the user application function program. When the application received the program upgrade command, MCU reset, the program will jump to the IAP program again, receives data from PC side to get data. After erected flash, and then jump to the user program area.
四、Setting the start address of the program
In the user program Settings of APP1 and APP2, you need to perform the following Settings
In uses program, the first instruction in the program,
SCB->VTOR = FMC_BASE | 0×4000;
SCB->VTOR = FMC_BASE | 0×8000;
Used to move the vector table to the new address above.
With the above Settings, the top stack address is set to:
APP1: SCB->VTOR = FMC_BASE | 0×4000;
APP2: SCB->VTOR = FMC_BASE | 0×8000;
Entry address of interrupt vector table:
APP1: SCB->VTOR = FMC_BASE | 0×4004;
APP2: SCB->VTOR = FMC_BASE | 0×8004;
The bin file will be programmed in the IAP program to the address you defined.
五、Explanation to the part of IAP demo program
void Jump_to_App(uint8_t Application)
{
uint32_t address;
/** Lock the Program memory */
FMC_Lock();
if(Application == APP1)
{
address = USER_APP1_ADDRESS;
}
else
{
address = USER_APP2_ADDRESS;
}
/** Jump to user application */
JumpAddress = *(__IO uint32_t *) (address + 4);
Jump_To_Application = (pFunction) JumpAddress;
/** Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t *) address);
/** Jump to application */
Jump_To_Application();
}
In this code, explain as below
1) FMC_Lock(); Used to lock falsh. After data is written, you need to perform this operation immediately to protect flash code data. To erase flash, run FMC_Unlock(). Operate.
2) JumpAddress = *(__IO uint32_t ) (address + 4); Is used to jump to the entry address of the interrupt vector table and execute the user program.
3) set_MSP((IO uint32_t *) address); Used to set a new stack top address.
4) Jump_To_Application(); The user jumps to the function pointer address and executes the new user program.
This IAP code is just a demo. In fact, the IAP upgrade program is very flexible, you can customize any communication interface you need, communication protocol, file format, and whether to keep the source program design, etc., according to your needs, and even you can upgrade your IAP program.