STM32 IAP online upgrade

Source: Internet
Author: User

One of the powerful functions of STM32 is to support online IAP upgrades, IAP (In-Application Programming), that is, Programming In Applications ", generally, the program itself can write data or modify the program into the program memory. With the IAP function, you can easily update and upgrade the program firmware of the product through the reserved Communication Ports (such as serial port, USB, and IIC) even after the product is released, you do not need to use the traditional JTAG Method for installation and update. The firmware of The IAP function generally includes two parts: Boot and
UserApp. Among them, the Boot part must be burned through JATG or ISP, and the APP part can be burned in through the IAP after the BOOT is burned in or merged with the BOOT, and then burned through JATG or ISP.

After the MCU is powered on, run BOOT and BOOT as follows:

1. Verify the APP. If the verification fails, the APP is deemed to be abnormal and enters process 3. Otherwise, the APP enters process 4;

2. Check the upgrade mark to see if the upgrade is required. If the upgrade is required, go to process 3. Otherwise, go to process 4;

3. Execute the upgrade process. After the upgrade, reset the upgrade mark and reset the software;

4. Jump to the APP for execution. When the APP needs to be upgraded, write the upgrade icon and reset the software.

Note that if the BOOT program is damaged, the product can only be burned through JATG or ISP, which is intolerable, the solution is to set write protection for the BOOT area. To prohibit programming or erasure of the BOOT region.

Before implementing IAP, first understand the Storage Architecture and Startup Process of STM32:

The Internal flash Address of STM32 starts from 0x8000000. Generally, program files are written from this address. In addition, STM32 is a microcontroller based on the Cortex-M3 kernel, which internally responds to the interrupt through an "interrupt vector table", after the program starts, the reset interrupt vector is first retrieved from the "interrupt vector table" and the reset interrupt program is executed to complete startup. The starting address of the interrupt vector table is 0x8000004. When the interrupt arrives, the internal hardware mechanism of STM32 automatically positions the PC pointer to the interrupt vector table, the interrupt service program is executed based on the interrupt vector retrieved from the interrupt source.

IAP function design:

There are two programs to write the IAP function, one is the IAP (and BOOT) project, and the other is the APP project. The IAP is designed to be placed in the 0x80000000 -- 0x80002000 area of the Internal FLASH of STM32. The size of the IAP is 8 K, and the APP is placed in the area after 0x80002000. The specific size of the internal FALSH is determined by the specific chip.

The IAP core code is as follows:

//************************************** **************************************** * **************/STM32F10x IAP OnlineUpdate Test-IAP Part // compiler: keil UV3 // 2012-08-09, by friehood //************************************ **************************************** * ***************** # define APP_ADDR (0x08002000) // APP address # define APP_CRC_ADDR (u32 *) (APP_ADDR + 28) // app crc verification code storage address, which is stored in the seventh in the interrupt vector table # d Efine APP_CRC (u32) (* (u32 *) APP_CRC_ADDR) // app crc verification code # define APP_LEN (u32) (* (u32 *) (APP_ADDR + 32 ))) // APP length, which is stored in the eighth int main (void) {void (** AppEntry) (void) = (void (**) (void) in the interrupt vector table )) 0x08002004; // APP mian function address void (** BootEntry) (void) = (void (**) (void) 0x08000004; // BOOT mian function address u32 crcCode = 0; // hardware initialization HWInit (); // CRC verification for some APP code crcCode = GetCRC32 (u8 *) APP_ADDR, APP_LEN); // read the upgrade mark, which is stored in the backup register g_bUpda TeFlag = BKP_ReadBackupRegister (BKP_DR9); if (g_bUpdateFlag) {// clear the upgrade flag BKP_WriteBackupRegister (BKP_DR9, 0x00);} // upgrade if (crcCode! = APP_CRC | g_bUpdateFlag) {// the update process printf ("go to boot mode, begin to upgrade .. \ n "); // enter the upgrade process UpdateProc (); // jump to BOOT after the upgrade is complete, and check BootEntry [0] ();} else {// jump to the APP and execute printf ("goto app mode .. \ n "); AppEntry [0] () ;}}

 

The core code of the APP is as follows:

//************************************** **************************************** * **************/Stm32f10x IAP onlineupdate test-app part // Compiler: keil UV3 // 2012-08-09, by friehood //************************************ **************************************** * ****************** int main (void) {void (** bootentry) (void) = (void (**) (void) 0x08000004; // boot Mian function address // hardware initialization hwinit (); while (1) {// whether to receive the upgrade request if (g_brevupdatereq) {// set the upgrade flag bkp_writebackupregister (bkp_dr9, 0x01 ); // jump to boot and execute bootentry [0] ();} task1 (); task2 ();//...}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.