About STM32 's IAP and app jump to each other

Source: Internet
Author: User

about the STM32 of the IAP with the APP jump to each other

Before doing a non-system of IAP and app to each other, after looking for information on the Internet, very smooth completed, and later in the IAR integrated development environment, IAP No system, the app with Ucos system to jump to each other appeared a lot of problems. Now summarize the IAP learning process and the actual problems encountered.

First say what is IAP. IAP (in application programming) is the application programming, IAP is the user's own program in the process of running a portion of user flash to write, The purpose is to update and upgrade the firmware in the product after the release of the product, conveniently through the reserved communication port. When the IAP function is usually implemented, that is, the user program is running in its own update operation, need to write two project code in the design of the firmware program, the first project program does not perform normal function operation, but only through some means of communication (such as USB, USART) to receive the program or data, the second part of the code to perform the update The second project code is the real function code.

The above is an excerpt from the Development Guide for atoms. In a more popular sense, to do the IAP function (also known as the remote upgrade function), need to have two segments of the program is an IAP program (also called bootloader), and the other is the app (main application). Via USB, serial port, can and other means of communication to the STM32 to upgrade the program file data (according to a custom protocol), the IAP program will receive the data written to the address of the app program to upgrade the app. This is the approximate process.

The summary of this document only jumps to each other and does not include operations such as receiving data, flash writing, and so on.

Speaking of IAP upgrades have to say two charts (image from the development Guide of Atoms)

The first one is the flowchart of the normal run time

The STM32 Flash address starts at 0x08000000 and the program file is written from this address. In addition STM32 internal through the "interrupt vector table" to respond to interrupts, after the program starts, will first remove the reset interrupt vector from the "Interrupt vector table" to perform the reset interrupt program to complete the start, while the "Interrupt vector table" Starting address is 0x08000004, when the interrupt comes, The internal hardware mechanism of the STM32 also automatically locates the PC pointer at the interrupt vector table and executes the interrupt service program based on the interrupt source to remove the corresponding interrupt vector.

Based on the analysis of the start and run process,

①stm32 after reset, the address of the reset interrupt vector is removed from the 0x08000004 address, and the Reset Interrupt service program is redirected to

② after the reset interrupt service program executes, jumps to the main function (such as the use of Keil MDK debugging when loading the program, you will find the need to run a few steps to jump to the location of the main function)

The ③main function is generally a hyper-loop (while (1) dead Loop), and in the execution of the main function, if an interrupt request (a heavy interrupt occurs) is received, the STM32 forces the PC pointer back to the interrupt vector table

④ into the appropriate interrupt service according to the source of the interrupt,

⑤ after executing the interrupt service program, the program returns to the main function execution again.

The second one is the flowchart after adding the IAP function

Starting and running process after adding IAP according to analysis

①stm32 Reset, or remove the address of the reset interrupt vector from the 0x08000004 address, and jump to the reset interrupt service program, after running the Reset interrupt service program to jump to the main function of the IAP, such as the IAP as an app, then this part and normal start is the same, This step is equivalent to the normal operation of ① and ②.

② after the IAP has been executed (such as Flash that needs to write the new app code to STM32 or no update to jump directly.) App reset interrupt vector start address is 0x08000004+n+m), jump to app reset vector table

③ Remove the address of the app reset interrupt vector and jump to execute the new program's Reset interrupt service program, then jump to the app's main function

④ also the main function is a hyper-loop, and note that at this time STM32 Flash, in different locations, there are two interrupt vector tables. During the main function execution, if the CPU gets an interrupt request, the PC pointer still forces the jump to the address 0x08000004 interrupt vector table instead of the app's interrupt vector table.

The ⑤ program then jumps to the interrupt Source app interrupt Service program according to the Interrupt vector table offset we set.

⑥ after executing the interrupt service program, the program returns to the main function to continue running.

Note: The IAP and app jump process, is through the PC pointer positioning to jump, all the registers are maintained in the original state, the jump process is not to do a reset.

Here are the IAP and app jump programs

/******************************************************************************

IAP Jump to APP jump function

******************************************************************************/

void Iap_app_jump (void)

{

INT32U Spinitval; To jump to the program's SP initial value.

INT32U jumpaddr; The address of the program to jump to. That is, to jump to the entry of the program

void (*pfun) (void); Defines a function pointer. Used to point to the app entry

Rcc_deinit (); Turn off peripherals

Nvic_deinit (); Restores the Nvic to the reset state. Causes the interrupt to no longer occur.

Spinitval = * (INT32U *) iap_addr;//iap_addr the stack top address of the IAP (0x08000000)

App_addr AAP's stack top address when jumping to APP (e.g. 0x08003800)

JUMPADDR = * (INT32U *) (IAP_ADDR + 4); Set the reset interrupt vector (e.g. above process analysis)

__set_msp (Spinitval); Set SP., Stack stack top address

Pfun = (void (*) (void)) jumpaddr; Generates a jump function. The reset interrupt vector address as a function pointer

(*pfun) (); Executes a function that implements a jump. No longer returns.

}

Interrupt vector table settings are required in both IAP and app, as in normal programs.

Introduce the application start address setting in the Integrated development environment

Keil MDK Environment Click the Options for Targetàtarget tab

When the IAP and app are all non-system, the above program can be very good to jump to each other. However, there are a lot of problems with the IAP (IAR Environment) when the app uses the Ucos system without the system. The process of solving various problems is documented as follows.

1, in the program normal application program and vector table, when the IAP directly call Jump app, the program does not jump to the app normally, jump into the region, the situation is now recalled. But the analysis is because the interrupt vector table is not found after the jump, so there is no normal access to the app's main () function.

However, the reset interrupt vector is indeed set in the Bsp_init () function.

Nvic_setvectortable (Nvic_vecttab_flash, (app1_addr+4));

Note that the interrupt vector table is set to (app1_addr+4) (offset 4 bytes is the reset interrupt vector), and some programs are set to APP1_ADDR. In fact, the two settings are the same

Execute the following sentence in the nvic_setvectortable () function

Scb->vtor = Nvic_vecttab | (Offset & (uint32_t) 0x1fffff80); So you can see if the offset of 4 bytes is the same.

The test finds that the reset interrupt vector must be re-set in the previous sentence of the call to Osstart () to be set correctly.

2, after the modification of the previous step, IAP can jump to the app, the app can also jump to the IAP, but then the re-IAP can no longer jump back to the app. Modify Jump function

/***********************************************************************

function function: Jump to IAP function

***********************************************************************/

void Jumptoiap (void)

{

INT32U Iapspinitval;

INT32U iapjumpaddr;

void (*piapfun) (void);

Rcc_deinit ();

Nvic_deinit ();

__DISABLE_IRQ (); Off interrupt ()

App such as jump front off interrupt, jump to IAP, after IAP initialization to open interrupt

Iapspinitval = * (INT32U *) iap_addr;

IAPJUMPADDR = * (INT32U *) (IAP_ADDR + 4);

__set_control (0);

Enter the user-level threading mode to enter a soft interrupt before returning to privileged-level threading mode

If the app is using a system such as Ucos, you must have this procedure or skip to the app after jumping to the IAP

__set_msp (Iapspinitval);

Piapfun = (void (*) (void)) iapjumpaddr;

(*piapfun) ();

}

/***********************************************************************

IAP Jump to APP function

***********************************************************************/

void Jumpto_app (void)

{

INT32U Iapspinitval;

INT32U iapjumpaddr;

void (*piapfun) (void);

Rcc_deinit ();//Turn off peripherals

Nvic_deinit ();

__DISABLE_IRQ (); Off interrupt () if the IAP off interrupt app does not use the Ucos system, the app

After initialization, the interrupt will be interrupted after the start of the task with Ucos.

Iapspinitval = * (INT32U *) app1_addr;

IAPJUMPADDR = * (INT32U *) (APP1_ADDR + 4);

if ((Iapspinitval & 0x2ffe0000) ==0x20000000)//check whether the top address of the stack is legal.

{

__set_msp (Iapspinitval);

Piapfun = (void (*) (void)) iapjumpaddr;

(*piapfun) ();

}

}

This adjustment can normally jump to each other.

3, add IAP 3 seconds delay and then execute the Jump app function (use Timer update interrupt (update interrupt) timing), but an execution jump into the app's Hardfault_handler ().

Because there is an initialization timer in the IAP and interrupts are not initialized in the app, there is no interrupt vector table to initialize the timer and initialize the timer, so the timer still works in the app, and there is an error when there is no corresponding interrupt vector table after the interrupt is generated. (You understand, anyway, whatever the reason, it's wrong.) The timer updates interrupt deactivation before executing the Jump app function, and the timer loses the energy to resolve!

/***************************************************************************

Better: Turn off the timer

Input parameters: num Timer number

***************************************************************************/

void Closetim (U8 num)

{

U32 Tim = 0;

Tim = (Apb1periph_base + (num-2) *0x0400); Calculate the timer address

Tim_itconfig ((tim_typedef*) tim,tim_it_update,disable);

Tim_cmd ((tim_typedef*) TIM, DISABLE);

}

In the event of an update interruption with the STM32 timer, it is found that in some cases, an interrupt is entered immediately as soon as the timer is turned on. Accurately, as long as the Enable update interrupt allow bit to respond to an update interrupt immediately "of course, the relevant Nvic is also configured." In other words, as long as the relevant timer update interrupt, no matter how long you interval or whether you start the relevant timer, it will immediately enter a Timer update interrupt service program. This problem is relatively easy to ignore, in some cases it does not matter, but some situations may cause problems for the application.

The workaround is to clear the update interrupt flag bit before the Enable Timer update is interrupted.

Use the following code when initializing the timer:

Tim_clearitpendingbit (tim4,tim_it_update);

Tim_itconfig (Tim4,tim_it_update, ENABLE);

Tim_cmd (TIM4, ENABLE); Turn on the clock

Record their work encountered problems, one is to facilitate their own search for learning, the second is to facilitate a beginner to encounter similar problems, to provide solutions. Previous work or study encountered problems not recorded in a timely manner, and then summed up, the phenomenon was not clear. In time to record, timely summary!

About STM32 's IAP and app jump to each other

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.