IAP Overview
Working Principle
In-Application Programming (IAP) is a way to perform firmware upgrades in the field via MCU communication interfaces (e.g. USART, USB, CAN, and Ethernet).
When starting the microcontroller, you can choose to have it run in any of the following modes.
IAP mode for executing IAP code
Normal mode for executing application code
Both the IAP code and the application code are located in the built-in FLASH of the microcontroller. The IAP code is usually stored in the first page of the MCU FLASH, while the user application code occupies the remaining FLASH area.
Figure 1 describes the IAP operation flow
Figure 1 IAP operation flow
Implementing IAP with MCU Ethernet Interface
If Ethernet is available, it is usually the interface of choice for implementing IAP functionality in embedded systems, and its advantages include
High-speed communication interface (10/100 Mbps)
Remote programming over a network (LAN or WAN)
IAP can be implemented using standard TCP/IP stack-based application protocols such as FTP, TFTP, HTTP, etc.
Implementing IAP on AT32F437 via Ethernet
This application note will describe two solutions for implementing IAP on the AT32F437 using an Ethernet communication peripheral.
IAP using TFTP (Simple File Transfer Protocol)
IAP using HTTP (Hypertext Transfer Protocol)
Both solutions are based on the LwIP stack (2.1.2), which is a lightweight TCP/IP protocol stack
IAP method using TFTP
The IAP method using TFTP is widely used in embedded system applications that require firmware upgrade capabilities (e.g., in embedded Linux bootloaders).
TFTP is a simple file transfer protocol executed over the UDP transport layer. This protocol is well suited for use in a LAN environment. It is based on a client/server architecture in which the client sends file transfer requests (read or write operations) to the server.
To implement IAP, a simple TFTP server needs to be implemented on the LwIP stack, and the server only has to handle write requests from the TFTP client of the PC.
IAP method using HTTP
Firmware upgrades using the HTTP protocol are not as common as using TFTP, but this solution is extremely useful when remote programming over the Internet is required. In this case, the HTTP service needs to be implemented using the TCP transport protocol.
HTTP runs on the TCP protocol and provides a way to send a binary file from a Web client (Mozilla Firefox or Microsoft Internet Explorer) in the form of an HTML form. This is called HTTP File Upload (RFC1867).
Subsequent sections in this document will describe the implementation of these two IAP methods in detail and will provide instructions on how to use the software.
Implementing IAP with TFTP
TFTP Overview
TFTP is a simple UDP-based file transfer protocol. File transfer is initiated by a TFTP client, which sends a read or write request to a TFTP server. Once the server acknowledges the request, the file data transfer will begin. Data will be sent in fixed size blocks as much as possible (e.g. 512 bytes per block).
Each outgoing block must be acknowledged by the receiver before the next block can be sent. This acknowledgement mechanism is implemented by the block number that is sent with each data block. A data block smaller than the fixed block size indicates the end of the file transfer.
Figure 2 depicts the various TFTP packet formats.
Figure 2 TFTP packet
Table 1 lists the TFTP opcodes. Table 1 TFTP opcodes
IAP implementation for AT32F437 using TFTP
This IAP implementation consists of a TFTP server based on the LwIP TCP/IP stack.
This server responds to write requests sent from a remote TFTP client (PC).
TFTP read requests are ignored.
TFTP normally writes the received files to the file system, but this server does not, it writes the received data blocks to the MCU FLASH (in the user FLASH area).
Note: In this implementation, the data block size is fixed at 512 bytes.
Figure 3 outlines the process of implementing IAP operations using TFTP.
Figure 3 Flowchart of IAP implementation using TFTP
Using the software
To test the IAP via TFTP, perform the following steps.
- In the iap.h file, uncomment the USE_IAP_TFTP option.
- Recompile the software. Using the generated mapping file, ensure that there is no overlap between the IAP code areas (starting at address 0x0) and that the user FLASH area starts at the following address: APP_START_SECTOR_ADDR (defined in iap.h).
- Write and run the software program in AT32 FLASH.
- To enter the IAP mode, you need to press and hold the USER Key on the development board.
- After assigning the IP (can be static or dynamic address), the user can start the IAP process.
- On the PC side, open the TFTP client (e.g. Tftpd64) and then configure the TFTP server address (host address in Tftpd64)
- Click the Put (Write) button in the Tftpd64 utility to initiate a file write request
- At the end of the IAP operation, you can reset the development board and run the application you just wrote in AT32 FLASH
Figure 4 TFTP64 dialog box
Implementing IAP using HTTP
Overview of HTTP File Uploads
File uploading using HTTP is defined in RFC1867. This file upload method is based on HTTP forms. When sending raw binary data, use the HTTP POST method instead of the GET method.
The following is an HTML code example for implementing a form-based file upload.
Figure 5 Browser screen of the file upload HTML form
Note: Before sending file data, the Web client first sends HTTP header data, which contains information such as the name of the file and the length of its contents, and the Web server must parse some of this information.
Web clients do not always use the same HTTP header file format. Figure 6 shows the HTTP header file format for Internet Explorer in a POST request. Figure 7 shows the HTTP header file format for Mozilla Firefox.
The HTTP web server must be able to handle these different formats.
Figure 6 IE11 HTTP header file format
Figure 7 Mozilla Firefox HTTP header file format
IAP implementation on AT32F437 using HTTP
This IAP implementation consists of an HTTP web server based on the LwIP stack.
After entering the IP address of the AT32 in the browser, the login web page will be displayed (Figure 8). This login web page allows only authorized users to use the IAP file upload function.
Figure 8 Login Web Page
Note: 1. The default User ID is user, Password is at32. 2.
- If User ID or Password is not correct, the login web page will be reloaded.
After successful login, browse and select the binary file you want to upload to AT32 FLASH
Note: Make sure the binary file size does not exceed the total size of the AT32 user FLASH area.
After clicking the Upload button (see Figure x), a POST request will be sent to the server. At this point, the server starts to erase the entire contents of the user’s FLASH area and waits to accept the raw binary file data. Then the data received by the street is written to the user FLASH area.
Note that the information about the total length of the data to be received will be extracted from the HTTP header data sent at the beginning of the transfer.
At the end of the IAP operation, the web page will just be the IAP operation success and a button that can be used to reset the MCU will be displayed.
Figure 9 File upload completion web page
Figure 10 summarizes the IAP implementation method using HTTP
Figure 10 Flowchart of IAP implementation using HTTP
Using the software
To test the IAP using HTTP, perform the following steps.
- In the iap.h file, uncomment the option USE_IAP_HTTP.
- Recompile the software, using the generated mapping file, ensuring that there is no overlap between the IAP area codes (starting at address 0x0) and that the user FLASH area starts at the following address: APP_START_SECTOR_ADDR (defined in iap.h).
- Write and run the software program in AT32 FLASH.
- To enter IAP mode, you need to press and hold the USER Key on the development board again.
- After assigning the IP address (can be static or dynamic), the user can start the IAP process
- Open the web client (Mozilla Firefox or Internet Explorer) and enter the AT32 IP address. 7.
- The login web page will be displayed. Enter “user” in the User ID field and “at32” in the Password field, then press the Login button. 8.
When the IP operation is finished, a new web page will be loaded, only the file upload operation has been completed successfully. 9.
- You can reset the MCU by pressing the Reset MCU button, and then run the application you just wrote in AT32 FLASH.
Note: Test the software with the following Web clients: Microsoft Internet Explorer 11 and Mozilla Firefox 80.0
Known limitations
Extra bytes added to the binary file
Internet Explorer (Microsoft Internet Explorere or Mozilla Firefox) adds a random boundary marker to the end of the uploaded binary file (this marker must not exceed 72 bytes according to RFC1521). In the latest version of the IAP software, this boundary marker is not removed, but stored in FLASH if there is enough space. If there is not enough space, no additional bytes will be written in FLASH and no error will be returned.
Environment
Hardware Configuration
- DM9162 Ethernet module
- AT-START-F407 development board
- Ethernet cable
Software source code
uTIliTIesAT32F437_emac_iap_demosource_codeootloader, emac iap source program, run iap upgrade program
MAC address and IP address settings
The MAC address is defined in the netconf.h file.
The default MAC address is fixed as: 004456:01.
The IP address is defined in the netconf.h file.
The IP address can be set as a static address or as a dynamic address assigned by a DHCP server. The default static address is: 192.168.81.37.
DHCP mode can be selected by enabling LWIP_DHCP in the lwipopts.h file.
Software file composition
Table 2 describes the project source files
Note: The table does not list the files used in the standard firmware library and LwIP stack.
Building the IAP image
In order to build the IAP image (which will be loaded using the IAP software), the following should be ensured.
- the compiled/linked software must run from the start address of the user FLASH area (this address should be the same as the address defined in APP_START_SECTOR_ADDR in iap.h).
- Configure the start address of the vector table to be the start address of the user FLASH area.
A. In the application code, use the NVIC_SetVectorTable function of misc.h/.c driver to relocate the vector table of the application load address.
For example, to set the vector table base location to 0x08010000.
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x10000);
B. By modifying the value of the VECT_TAB_OFFSET constant defined in the system_at32f4xx.c file.
For example, to set the vector table base location to 0x08010000.
define VECT_TAB_OFFSET 0x10000
- The size of the compiled software does not exceed the total capacity of the user FLASH area.