Because I previously wrote a stm32 IAP upgrade program, I summed up the three main difficulties in doing IAP upgrades:
1, how to set the interrupt vector, that is, the redirection of the interrupt vector
2, how to configure the start address of the program
3. How to jump from IAP to App program
4, the use of library functions to pay attention to the place (to prevent being pits)
Say the article when I have completed a simple IAP upgrade program, you can receive the bin file through the serial port to write to flash inside, and then run.
1, how to set the interrupt vector, that is, the redirection of the interrupt vector
STM8 does not have a register like Stm32 that manages the address of the interrupt vector, so stm32 interrupts can be set arbitrarily (in accordance with the requirements), but the STM8 interrupt vector table is fixed
In the 0x8000 address, can not be modified, so the boot zone can not be interrupted, or will be interrupted with the app area, but the app area of a break will jump to the 0x8000 address, so jump to the boot area, so you need to use the jump command to jump back to the app area,
The interrupt vector for the app can be redirected in the following way, but Bootloader is not allowed to use interrupts, but some people on the web say they can redirect interrupts to arm and redirect them via Arm's properties in bootloader and app. can be implemented bootloader and apps are used interrupts, but I do not, if you will be able to leave a message to tell me thank you
__root Const Long reintvec[]@ ". Intvec" =
{
0x82008080,0x8200a804,0x8200a808,0x8200a80c,
0X8200A810,0X8200A814,0X8200A818,0X8200A81C,
0X8200A820,0X8200A824,0X8200A828,0X8200A82C,
0X8200A830,0X8200A834,0X8200A838,0X8200A83C,
0X8200A840,0X8200A844,0X8200A848,0X8200A84C,
0X8200A850,0X8200A854,0X8200A858,0X8200A85C,
0X8200A860,0X8200A864,0X8200A868,0X8200A86C,
0x8200a870,0x8200a874,0x8200a878,0x8200a87c,
}; This is my redirect interrupt, because STM8 flash from 0x8000 to 0xa800 is exactly the 0x2800 byte, this number is exactly 10K, that is, I gave bootloader reserved 10K space, and I use the STM8S207R8T6 is 64k
2 , how to configure the start address of the program
The size of the configuration Bootloader program space is set in a file with the ICF suffix, and this file is in the installation directory of the IAR.
I installed the following directory C:\Program Files (x86) \iar systems\embedded Workbench 7.0\stm8\config
You can see a lot of it here. ICF suffix file, and then select a and chip consistent, I will choose LNKSTM8S207R8.ICF, copy into our project inside, and then in the IAR inside the following settings
R
Then open the ICF file to modify the following
Define Region Nearfunccode = [from 0x8000 to 0XA7FF];
Define Region Farfunccode = [from 0x8000 to 0XA7FF];
// | [From 0x10000 to 0X17FFF];
Define Region Hugefunccode = [from 0x8000 to 0XA7FF];
This is all about modifying the bootloader program.
Here is the ICF file that modifies the app
Define Region Nearfunccode = [from 0xa800 to 0X17FFF];
Define Region Farfunccode = [from 0xa800 to 0xFFFF]
| [From 0x10000 to 0X17FFF];
Define Region Hugefunccode = [from 0xa800 to 0X17FFF];
Here the address of the modification is completed, the following start to jump
3. How to jump from IAP to App program
STM8 jump is easy, using the following assembly can be achieved jump
ASM ("LDW X, SP");
ASM ("LD A, $FF");
ASM ("LD XL, A");
ASM ("LDW SP, X");
ASM ("JPF $A 800");
This is the program that implements the jump to app
4, the use of library functions to pay attention to the place (to prevent being pits)
Use the library function to read and write Flash when you must pay attention to, because STM8 Flash library function A bit of a problem, he put the address are forced to convert to 16-bit, 16-bit range is 0 to 0xFFFF 64K space, but STM8 Flash use 0x8000 start, 64kflash size space to 0X17FFF, so if you do not modify the library function read and write error.
This is the library function, we want to change it to uint32_t so that we can read and write more than 0xFFFF address
How to write STM8 IAP upgrades for bootloader and apps