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 TargetUser 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
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
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- How to prevent wage subsidy fraud? All Sohu employees were exposed to wage subsidy fraud
- How to use a power amplifier to amplify and output a pulse train signal? How to use the Burst function of a signal generator?
- 【DIY Creative LED】LED lights and holes
- Surge arrester explanation and working principle
- HT6221 Remote Control Chip Introduction
- Is there any error in the schematic diagram of the electrostatic generator?
- A Brief Introduction to Low Dropout Linear Regulator (LDO)
- Cheap_Flash_FS (SPI_Flash version) -- embedded SPI_FLASH file system free source code, please download
- How to write interrupt function after using library function in MSP430F5529
- PWM