How to use the BootLoader and APP program of STM32F4

Publisher:pengbinyyyLatest update time:2018-08-19 Source: eefocusKeywords:STM32F4  BootLoader Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Concept Literacy

1.IAP


IAP is the abbreviation of In Application Programming. IAP means that the user's own program burns part of the User Flash area during operation. The purpose is to easily update the firmware program in the product through the reserved communication port after the product is released .


Usually when users need to implement the IAP function, that is, to update the user program itself during operation, they need to write two project codes when designing the firmware program. The first project program does not perform normal functional operations, but only receives programs or data through some communication channels (such as USB, USART ) to execute updates to the second part of the code; the second project code is the real functional code.

2.Bootloader

In the above IAP concept, bootloader is the first project program. The main functions of bootloader are: receiving data from serial port, USB and other media; storing and moving data; program jump and other functions.

3. App

A name for the second project code in the IAP concept


Notes on writing bootloader and App for Stm32

1. How to allocate the address space of bootloader and app

2. How to get the upgrade software data and write it into flash

3. How to jump from bootloader to app

4. How to set the interrupt vector of App

5. How to generate bin file in App

6. Program execution process


1. How to allocate the address space of bootloader and app


Figure 1 STM32 Flash partitioning


(1) Assigning addresses

I am using STM32F407, its Flash size is 512K, so I divide it as follows:


0x08000000 --- 0x08010000 is allocated to the bootloader, the size is 64k

0x08010000----0x0801F000 is allocated to the first APP, and the size is 124k

0x0801FC00----0x0801FFFF is allocated to user_flag and other flags

(2) Set the ROM size in Keil


A. Setting the ROM size in BootLoader


B. Set the ROM size in the App



2. How to get the upgrade software data and write it into flash

In my program, I receive data through the serial port, and then call iap_write_appbin(u32 appxaddr,u8 *appbuf,u32 appsize) to store the received APP program in the FLASH of STM32F4, where appxaddr is the starting address of the application, appbuf is the application code, and appsize is the application size (bytes). The code is as follows:

#define FLASH_APP1_ADDR 0x08010000 //The first application start address


if(applenth) //Serial port receives data length

{

printf("Start updating firmware...\r\n");

LCD_ShowString(30,210,200,16,16,"Copying APP2FLASH...");

  if(((*(vu32*)(0X20001000+4))&0xFF000000)==0x08000000)//Judge whether it is 0X08XXXXXX.

{  

iap_write_appbin(FLASH_APP1_ADDR,USART_RX_BUF,applenth); //Update FLASH code   

LCD_ShowString(30,210,200,16,16,"Copy APP Successed!!");

printf("Firmware update completed!\r\n");

}else 

{

LCD_ShowString(30,210,200,16,16,"Illegal FLASH APP!  ");   

printf("Non-FLASH application!\r\n");

}

  }else 

{

printf("No firmware can be updated!\r\n");

LCD_ShowString(30,210,200,16,16,"No APP!");

}

clearflag=7; //The flag updates the display and clears the display after 7*300ms

void iap_write_appbin(u32 appxaddr,u8 *appbuf,u32 appsize)

{

u32 t;

u16 i=0;

u32 temp;

u32 fwaddr=appxaddr; //Currently written address

u8 *dfu=appbuf;

for(t=0;t

{   

temp=(u32)dfu[3]<<24;   

temp|=(u32)dfu[2]<<16;    

temp|=(u32)dfu[1]<<8;

temp|=(u32)dfu[0];  

dfu+=4; //Offset 4 bytes

iapbuf[i++]=temp;    

if(i==512)

{

i=0; 

STMFLASH_Write(fwaddr,iapbuf,512);

fwaddr+=2048; //Offset 2048 512*4=2048

}

if(i)STMFLASH_Write(fwaddr,iapbuf,i);//Write the last few bytes of content.  

}



3. How to jump from bootloader to app


// Jump to the application segment

//appxaddr: user code starting address.

void iap_load_app(u32 appxaddr)

{

if(((*(vu32*)appxaddr)&0x2FFE0000)==0x20000000) //Check if the top address of the stack is legal.

jump2app=(iapfun)*(vu32*)(appxaddr+4); //The second word in the user code area is the program start address (reset address)

MSR_MSP(*(vu32*)appxaddr); //Initialize APP stack pointer (the first word in the user code area is used to store the top address of the stack)

jump2app(); //Jump to APP.

}

}

4. How to set the interrupt vector of App

SCB->VTOR = FLASH_BASE | 0x10000;/*The interrupt vector table is located at FLASH_BASE (0X08000000) + 0x10000*/


  Because the starting address of APP is (0X08000000)+0x10000

5. How to generate bin file in App

We click the Options for TargetUser tab in MDK, check Run #1 in the After Build/Rebuild column, and write: D:\tools\mdk5.14\ARM\ARMCC\bin\fromelf.exe --bin -o ..\OBJ\RTC.bin..\OBJ\RTC.axf, as shown in the figure:

6. Program execution process



Summarize:

The main contents of Bootloader are

1. Set the space for bootloader and app

2. Receive the compiled app bin file and write it to flash

3. Implement jump

The main changes in the App are

1. ROM starting address and allocated space size

2. Redirect interrupt vector

3. Generate bin file


Keywords:STM32F4  BootLoader Reference address:How to use the BootLoader and APP program of STM32F4

Previous article:Summary of STM32F103 bootloader download and debugging process
Next article:STM32F103c8t6 implements IAP online firmware upgrade through serial port

Recommended ReadingLatest update time:2024-11-23 11:33

STM32F429 serial port IDLE interrupt + DMA receives serial port data
#define Rec_GPS_DATA_Unfinish     0; #define Rec_GPS_DATA_Finish       1; extern unsigned char GPS_DATA ; extern unsigned char Rec_GPS_DATA_Flag; int main(void) //main function {     SysTick_Init();     NVIC_Configuration();     Debug_USART_Config();     macUART4_Config();     DMA_Config();     while(1)     {    
[Microcontroller]
Successfully ported icore's shell serial port to STM32F4
In the example CD on the icore development board, delete the two C files FSMC.c and RCC.c and the H file \iCore_Extension-related\code\7_uart1_shell project file. Transplantation steps: First, replace the previous startup code, add two files: startup_stm32f4xx.s and system_stm32f4xx.c. system_stm32f4xx.h can be
[Microcontroller]
Successfully ported icore's shell serial port to STM32F4
The concept of STM32F4 interrupt preemption priority and response priority
Preemption priority and response priority The interrupt vector of STM32 has two attributes, one is the preemption attribute and the other is the response attribute. The smaller the attribute number, the higher its priority level. Preemption refers to the attribute of interrupting other interrupts. That is, because of
[Microcontroller]
Servo control based on STM32F4
The control of the servo generally requires a time base pulse of about 20ms. The high-level part of the pulse is generally the angle control pulse part in the range of 0.5ms-2.5ms, and the total interval is 2ms. Taking the 180-degree angle servo as an example, the corresponding control relationship is as follows:    0
[Microcontroller]
ATMEGE32 reads and writes Flash in the bootloader area
Note: This program uses the official boot library function for experiments, and the development environment is atmelstudio6 (avrstudio6). The official provides bootloader related write and erase functions.   The main function is as follows: int main(void) { USARTInits(115200); for (int i=0;i 256;i++) {
[Microcontroller]
Talking about the bus matrix and access scheduling of the STM32F4 series
    Many people may have seen the internal system architecture block diagram of the STM32F4 series. It is roughly as shown below. This diagram is very important and cannot be ignored.     The crisscrossing lines in the figure are the multi-layer AHB bus matrix, which is responsible for interconnecting the yel
[Microcontroller]
Talking about the bus matrix and access scheduling of the STM32F4 series
STM32F4xx bit-band operation
The explanation in the example of the atom on point is about the STM32F10X series. For the newer STM32F40x series, the method of implementing bit-band operations is actually similar, except that the address has changed.     #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)
[Microcontroller]
Analysis of U-Boot transplantation on S3C2410
Introduction BootLoader is the first link in embedded system software development. It closely connects software and hardware together and is crucial for the subsequent software development of an embedded device. BootLoader also involves a lot of hardware-related knowledge. For ordinary embedded development boa
[Microcontroller]
Analysis of U-Boot transplantation on S3C2410
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号