1. Introduction to IAP IAP is the abbreviation of "in applicatin programming", which means that the MCU can obtain new code in the system and reprogram itself, that is, change the application. It is different from the ISP programming we are familiar with. The ISP programming interface of LPC1768 is serial port 1. If other serial ports or other buses are used, it cannot be programmed. The IAP we are talking about here downloads a bootloader program. If we want to update the application from serial port 2 or the network port, initialize the corresponding serial port or network port in the Bootloader to receive the application, write the received application into the Flash, and jump to the application entry to execute the application after IAP is completed. So the current IAP program involves two concepts: Bootloader and application. Bootloader: BootLoader is a small program that runs before the operating system kernel runs. Through this small program, we can initialize the hardware devices and establish a memory space mapping map, so as to bring the system's hardware and software environment to a suitable state, so as to prepare the correct environment for the final call to the operating system kernel. The Bootloader we are talking about here is also a small program before the system starts up. Its main task is to initialize the serial port and IAP port (network port CAN interface, etc.), and judge whether the status needs to update the application from the IAP port. If it needs to be updated, the application is received from the port and stored in the specified Flash. After the update is completed, it jumps into the specified Flash to execute the application. Application: That is, the program that we need to develop the board to realize the function. There are two main types of applications: hex files and bin files. In the KEIL that we often use, the executable file (application) compiled by default is in hex format. If you need to compile and generate bin format, you need to make the following changes, add "D:\Keil\ARM\ARMCC\bin\fromelf.exe --bin --output ./Obj/Can_Updata.bin ./Obj/test.axf", and recompile the generated Can_Updata.bin file and store it in the Obj folder. 2. The difference between bin format files and hex format files Bin format files are pure binary files. When they are downloaded to the development board, their contents remain unchanged. Therefore, it is more convenient to use bin format files for IAP downloads. The following figure shows the contents of the bin file and the contents of the Flash stored in the emulator after being written to the development board (of course, this program can be executed). Hex format file: Hex file (Intel HEX) is an ASCII text file consisting of lines of text that conform to the Intel HEX file format. In an Intel HEX file, each line contains a HEX record. These records consist of hexadecimal numbers corresponding to machine language codes and/or constant data. The following figure shows part of the data in a hex file, which consists of ":CCAAAARR...ZZ", CC=10 represents a length of 16 bytes, AAAA=0000 the starting address of the data in this record in the storage area, RR=00, data area, ZZ=38 is the checksum, which will not be explained in detail here. 3. LPC1768 IAP Principle LPC1768 starts to execute the Boot code after reset. The Boot code can execute the ISP program or the user's application code. After a hardware reset occurs, the P2.10 pin is low, which is treated as an external hardware request to start the ISP command processor. Assuming that the correct signal appears on the power pin when the rising edge appears on the /RESET pin, there is 3ms before sampling P2.10 to decide whether to execute the user code or the ISP handler. If P2.10 is low and the watchdog overflow flag is set, the external hardware request to start the ISP command processor is ignored. In the absence of a request from the ISP command processor (the P2.10 pin is high after a hardware reset), a valid user program will be searched. If a valid user program is found, execution control is transferred to the user program. If no valid user program is found, the auto-baud rate program will be called. ISP download and commands are not discussed here. Interested friends can refer to Chapter 32 ISP Commands in the LPC1768 Technical Manual. In IAP upgrade, the normal execution of the program is user code (the user code here is what we call IAP boot program). The following is the IAP upgrade flow chart. The program will reserve the port (serial port and CAN bus interface are provided here) to receive the APP program bin file, and write the received data to the specified Flash area (the routine APP address is 0x0001 0000). The program writes the data to the Flash through the IAP command. LPC1768 provides a series of IAP commands to erase and write the on-chip Flash. 4. IAP command LPC1768 operates the on-chip Flash through the IAP function. The IAP function is a function with input parameters and return parameters solidified at 0x1FFF1FF1. There is a detailed description of the IAP command in Chapter 32 of the LPC1768 Technical Manual. The main commands provided are as follows: prepare the operation sector, copy RAM content to Flash, clear sector, check sector empty, read device ID, read boot version, compare, re-call ISP, etc. 5. Serial port IAP upgrade This routine is modified based on the official serial port IAP update image, and directly uses the official IAP.c file, which provides various functions of the IAP command as shown above. For its specific parameters, please refer to the IAP command. According to the official routine, the bmp image is sent to the development board through the serial port using the Xmodem1K protocol and stored at address 0x0001 0000. The following figure shows the LPC1768 Flash allocation address, and sectors 16 to 21 are the application storage space. Here we change the bmp picture to be transmitted to the transmission application bin file 6. Serial port IAP program analysis The routine controls the development board through buttons. The INT0 key erases the Flash, the confirm key waits for the serial port IAP, the up key displays the menu, the down key executes the application, and the LCD is used to develop the board status When the program is fully written to the Flash, press the down key to jump to the application. First modify the interrupt vector table and then enter the application void Boot( void ) { SCB->VTOR = IMG_START_SECTOR & 0x1FFFFF80; //Modify the interrupt vector table JMP_Boot(IMG_START_SECTOR); } Stack address updated, PC address updated __asm void JMP_Boot( uint32_t address ){ LDR SP,[R0] ;Stack address update LDR PC, [R0, #4] ;Enter the application } 7. Operation steps and experimental phenomena 1. Download the "BMW development board serial port IAP upgrade" routine, plug in the USB to serial port cable, open the hyperterminal, and reset the development board. 2. Press the INT0 button--erase the sector 3. Press the direction key to confirm (ie press down)--wait to receive the serial port program 4. The serial port prints the 'C' character and waits for data to be received 5. The serial port sends the file, selects the "1K Xmodem" protocol, and selects the application bin file to be downloaded. Here, the DAC routine is used as a test. 6. Click "Send" to send the file. 7. Sending completed. 8. Press the down arrow key to start executing the application. At this time, we can use the oscilloscope to test the output of the sine wave signal at the P0.26 port. Bin file generation method and settings: Open the application project to be updated. Here, the "IAP upgrade DAC conversion" program is used to set the ROM space address (the address where the program is downloaded to the Flash). This is also the entry address of our application 0x10000. Open the User option and use Keil's built-in fromelf.exe to generate a bin file. The bin file is saved in the Obj folder. As shown in the figure below, add "D:\Keil\ARM\ARMCC\bin\fromelf.exe --bin --output ./Obj/app.bin ./Obj/app.axf", the input file is app.axf, so the project compilation generates the output file name set to app, the command is executed to generate the app.bin file Open the Asm option and define "NO_CRP". We can open the startup file. When "NO_CRP" is defined, the following code will not work. Therefore, when encryption is required, the code read protection must not be defined before, that is, the encryption keyword. After encryption, the chip can no longer be erased. Since our program here needs to use IAP upgrade, this definition is added