ST AN3226 Application note

AN3226
Application note
STM32F107 In-Application Programming (IAP) over Ethernet
Introduction
This application note is intended for developers using the STM32F107 microcontroller. It provides implementation solutions for In-Application Programming (IAP) using the STM32F107 Ethernet communications interface.
IAP using TFTP (Trivial File Transfer Protocol)
IAP using HTTP (Hypertext Transfer Protocol)
July 2010 Doc ID 17570 Rev 1 1/18
www.st.com
Contents AN3226
Contents
1 IAP overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1 Theory of operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 IAP using the MCU’s Ethernet interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Implementing IAP over Ethernet on the STM32F107 . . . . . . . . . . . . . . . . . 4
1.3.1 IAP method using TFTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3.2 IAP method using HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 IAP using TFTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1 TFTP overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Implementing IAP for the STM32F107 using TFTP . . . . . . . . . . . . . . . . . . 6
2.3 Using the software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 IAP using HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.1 HTTP file upload overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Implementing IAP using HTTP on the STM32F107 . . . . . . . . . . . . . . . . . 11
3.3 Using the software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.4 Known limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.4.1 Extra bytes added to binary file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.1 MAC and IP address settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2 Jumper settings on the STM3210C_EVAL board . . . . . . . . . . . . . . . . . . . 14
4.3 Software file organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.4 Code size measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.5 Building an image for IAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2/18 Doc ID 17570 Rev 1
AN3226 IAP overview
MCU reset
Enter IAP
mode?
IAP initialization
No
Ye s
IAP
Request ?
Receive binary image and
program it into user Flash
area
No
Ye s
Execute application code

1 IAP overview

1.1 Theory of operation

In-Application Programming (IAP) is a means of upgrading firmware in the field using the MCU communication interfaces like UART, USB, CAN, Ethernet,etc...
When you boot the microcontroller, you can choose to put it in IAP mode in order to execute the IAP code or else, in normal mode, start to execute the application code. Both the IAP code and the application code are reside in the embedded Flash memory of the microcontroller. Usually the IAP code is stored in the first pages of the MCU Flash, and the user application code occupies the remaining Flash area.
The IAP operation flow is described in the following figure:

Figure 1. IAP operation flow

1.2 IAP using the MCU’s Ethernet interface

When it is available, Ethernet is often the preferred interface for implementing IAP capability in an embedded application. The advantages are:
High speed communication interface (10/100Mbit/s)
Remote programming through the network (LAN or WAN)
Standardized application protocols on top of the TCP/IP stack that can be used for
implementing the IAP like: FTP, TFTP, HTTP,...
Doc ID 17570 Rev 1 3/18
IAP overview AN3226

1.3 Implementing IAP over Ethernet on the STM32F107

This application note describes two solutions that implement IAP for the STM32F107 using the Ethernet communication peripheral:
IAP using TFTP (Trivial File Transfer Protocol)
IAP using HTTP (Hypertext Transfer Protocol)
Both solutions run on top of the LwIP stack (v1.3.1) which is a light-weight implementation of the TCP/IP protocol suite.

1.3.1 IAP method using TFTP

The IAP method using TFTP, is widely used in embedded applications that require a firmware upgrade capability (for example in embedded Linux bootloaders).
TFTP is a simple file transfer protocol working on top of the UDP transport layer and it is mainly intended to be used on a LAN environment. It is based on a client/server architecture, where a client will request a file transfer (read or write operation) from a file server.
In our case, a simple TFTP server is implemented on top of the LwIP stack, this server only processes write requests coming from a PC TFTP client.

1.3.2 IAP method using HTTP

Firmware upgrade through HTTP protocol is less common than with TFTP, but it can be an useful solution, when remote programming over the Internet is needed. In this case TCP transport protocol is needed to ensure optimum operation.
HTTP which works on top of TCP, offers a way of sending a binary file from a web client (example: Mozilla Firefox or Microsoft Internet Explorer) using HTML Forms, it is called HTTP File-upload (RFC 1867).
The next sections of this document, give more details about the implementation of both IAP methods and an explanation of how to use the software.
4/18 Doc ID 17570 Rev 1
AN3226 IAP using TFTP
2 bytes String 1 byte String 1 byte
Opcode File name 0 Mode 0
Opcode Block# Data
2 bytes 2 bytes n bytes
2 bytes 2 bytes
Opcode Block#
2 bytes 2 bytes String 1 byte
Opcode
Error code
Error
message
0
RRQ/WRQ packet
Data packet
ACK packet
Error packet

2 IAP using TFTP

2.1 TFTP overview

Trivial File Transfer Protocol (TFTP) is a simple file transfer protocol, working on top of UDP.
A file transfer is initiated from a TFTP client which sends a Read or Write request to a TFTP server.
When the server acknowledges the request, file data transfers start. The data is sent in fixed size blocks (for example in blocks of 512 bytes).
Each transferred data block must be acknowledged by the recepient, before the next block can be sent. The acknowledge mechanism is based on the block number sent with each data block.
A data block with less than the fixed block size indicates the termination of the file transfer.
Figure 2 describes the format of the various TFTP packets:

Figure 2. TFTP packets

The TFTP opcodes are listed in Tab le 1 .

Table 1. TFTP opcodes

Opcode Operation
0x1 Read request (RRQ)
0x2 Write request (WRQ)
0x3 Data
0x4 Acknowledgment (ACK)
0x5 Error
Doc ID 17570 Rev 1 5/18
IAP using TFTP AN3226

2.2 Implementing IAP for the STM32F107 using TFTP

This IAP implementation consists of a basic TFTP server on top of the LwIP TCP/IP stack. This server responds to file WRITE requests received from a remote TFTP client (PC). TFTP READ requests are ignored.
Instead of writing received files to a filesystem which is normally what TFTP is used for, the server writes the received data blocks into the MCU Flash (in the user Flash area).
Note: In this implementation, the data block size is fixed to 512 bytes.
The following flowchart gives an overview of the IAP operation using TFTP.
6/18 Doc ID 17570 Rev 1
Loading...
+ 12 hidden pages