File Transfer Protocol Using MPLAB Harmonyv3 TCPIP DS00003475A
File Transfer Protocol Using MPLAB Harmonyv3 TCPIP DS00003475A
File Transfer Protocol Using MPLAB Harmonyv3 TCPIP DS00003475A
Introduction
File Transfer Protocol (FTP) is an application layer protocol used to transfer files between local and remote hosts. It
runs on top of the TCP, such as HTTP.
This document focuses on the FTP implementation of the MPLAB® Harmony v3 TCP/IP stack, available in the latest
MPLAB Harmony v3 framework. It also provides an FTP client demonstration using the SAM E70. This
demonstration implements an Ethernet bootloader using FTP, through which the SAM E70 client downloads a binary
for self-programming from an FTP server.
Abbreviations
The following abbreviations are used in this document:
• FTP – File Transfer Protocol
• HTTP - Hypertext Transfer Protocol
• TCP/IP – Transmission Control Protocol/Internet Protocol
• DHCP - Dynamic Host Configuration Protocol
• DFP - Device Family Pack
• MHC - MPLAB Harmony Configurator
Table of Contents
Introduction.....................................................................................................................................................1
1. FTP Overview......................................................................................................................................... 3
1.1. Client............................................................................................................................................ 3
1.2. Server...........................................................................................................................................3
1.3. Connection................................................................................................................................... 3
1.4. Operation Modes..........................................................................................................................4
1.5. FTP Applications.......................................................................................................................... 4
4. Troubleshooting.....................................................................................................................................17
5. Appendix............................................................................................................................................... 18
5.1. Generating Application Binary Files........................................................................................... 18
5.2. Self-Programming of Downloaded Binary.................................................................................. 20
6. References............................................................................................................................................22
Customer Support........................................................................................................................................ 23
Legal Notice................................................................................................................................................. 23
Trademarks.................................................................................................................................................. 24
1. FTP Overview
FTP is built on a client/server model architecture using separate control and data connections between the client and
the server.
Figure 1-1. FTP Model
Control Connection
User PI Server PI
Data Connection
File System User DTP Server DTP File System
1.1 Client
A File Transfer Protocol client (FTP client) is a software that establishes a connection between a host computer and a
remote FTP server. An FTP client provides the dual-direction transfer of data and files between two computers over
TCP. An FTP client works on a client/server architecture, where the host computer is the client and the remote FTP
server is the central server. It works when the host computer connects to the FTP server by specifying the domain, IP
address, user name, and password of that server (in some cases, the port number also needs to be specified). After
the user authentication, a connection is established between both the systems, and the host computer can upload
data onto the FTP server. An FTP client generally supports one or multiple simultaneous file transfers. Moreover,
most FTP clients can connect to multiple FTP servers simultaneously, providing status updates of the uploading
process, and notifications about successful and failed transfers. The host computer can also download files from the
FTP server using the FTP client.
1.2 Server
The FTP server enables downloading and uploading files. There could be access restrictions, which are determined
by the FTP server administrator for downloading the various files and folders residing on the FTP server. Files
residing on FTP servers can be retrieved by common web browsers, but they may not support protocol extensions,
such as FTPS (FTP Secure). All file transfer protocol site addresses begin with ‘ ftp://’.
1.3 Connection
To transfer a file, the following TCP connections are used by the FTP in parallel: control connection and data
connection.
Control Connection: FTP uses control connection for sending control information, such as user identification,
password, commands to change the remote directory, and commands to retrieve and store files. The default port
number for control connection is 21. All control connections are initiated on this port.
Data connection: For sending the file, FTP uses data connection. The default port number for data connection is 20.
However, data connections are not always initiated on this port. Depending on the mode of operation, they may be
initiated in other port numbers. For additional information, refer to 1.4 Operation Modes.
Session:
When an FTP session is started between a client and a server, the client initiates a control TCP connection with the
server side. The client sends control information over this. When the server receives control information, it initiates a
data connection to the client side. Only one file can be sent over one data connection. But the control connection
remains active throughout the user session.
File Description
ftp_config.h Configuration of the FTP server (from symbols generated in configuration.h)
ftp.h Interface to the FTP server TCP/IP Stack library
ftp.c Implementation of FTP server
File Description
ftpc_config.h Configuration of the FTP Client (from symbols generated in configuration.h)
ftpc.h Interface to the FTP client TCP/IP stack library
ftpc.c Implementation of FTP client protocol
The following table displays some of the FTP commands supported by the FTPC module.
In addition to the implementation of FTP commands, the client module provides a user interface through the USART
terminal. This enables users to provide the required FTP command by entering into the terminal.
All FTP client commands start with the keyword ‘ftpc’, for example, ftpc login <username> <pswd> .
3.1 Prerequisites
Hardware:
• SAM E70 Xplained Ultra evaluation kit – 1 No.
• SD card – 1 No.
• Ethernet cable (RJ45) – 1 No.
• USB cable – 1 No.
Software:
• MPLAB X IDE (v5.30)
• MPLAB XC32/32++ Compiler (v2.40)
• MPLAB Harmony Configurator (v3.4.0)
– Harmony bsp repository, v3.5.0
– Harmony csp repository, v3.5.2
– Harmony core repository, v3.5.2
– Harmony dev_packs repository, 3.5.0
– Harmony net repository v3.5.1
case APP_Connect_FTP:
TCPIP_FTPC_Get_Status (ftpcHandle, &ftpcStatus);
if (ftpcStatus.isConnected)
{
SYS_CONSOLE_MESSAGE ("FTP Client connected \r\n");
appData.state = APP_Logign_FTP;
SYS_CONSOLE_MESSAGE ("Login with your credentials \r\nEg: ftpc login
username psswd\r\n");
}
break;
case APP_Login_FTP:
TCPIP_FTPC_Get_Status (ftpcHandle, &ftpcStatus);
if (ftpcStatus.isLoggedIn)
{
SYS_CONSOLE_MESSAGE ("FTP Client logged in \r\n");
appData.state = APP_SET_CURRENT_DRIVE;
}
break;
• Because this document focuses on FTP, the TCP/IP related configurations are highlighted below:
TCP/IP Harmony configuration
– For TCP/IP Basic Configurator, follow these steps:
1. Adding the TCP/IP Basic Configurator will create a BASIC CONFIGURATION view. Different views
can be selected as shown in the following figure.
2. In the Basic Configurator view, TCPIP CMD, TCPIP CORE and NETCONFIG components are
enabled.
Figure 3-4. Basic Configurator View
3. In the TCP/IP CORE configuration options, the TCP/IP Stack Dynamic RAM Size can be adjusted
per the application requirement. For this example the RAM allocated is 67,960 bytes (66 KB).
Figure 3-5. Configuration Options
4. In NETCONFIG options, the necessary network details, such as IP address and gateway address
are configured:
2. In the FTP CLIENT configuration, the Enable FTP Client Commands option allows users to interact
with the FTP client through a serial terminal. Transmit and receive buffer size can be increased as
required (that is, 1,024).
Figure 3-8. FTP CLIENT Configuration
2. Build and program the project to the target board by clicking (Make and Program device icon).
4. Click (user icon), or go to Edit > User for adding the client.
5. In the User window, click General. Add the client by clicking Add , and then enable the account and password.
This will be the user name and password for connecting from the FTP client.
6. In the Shared folders section, add the path of the folder where the binary to be downloaded is located.
Figure 3-10. Adding the Path
Note: This project is configured for a maximum binary file size of 7 KB. For binary files larger than this,
increase APP_DATA_LEN macro in app.c file of the ftp_bootloader project. If the binary file is larger than
available RAM, the file can be read in chunks (instead of reading complete file into buffer) and programmed.
7. The MCU is reset automatically after the programming is complete, and the application code starts executing.
Displaying the message: **** Executing Application code ****.
After the MCU reset, the following message is displayed, indicating that the MCU has been reprogrammed
with a new application binary.
Figure 3-12.
4. Troubleshooting
This section covers important error messages and appropriate workaround for that.
• “FTP timed out” - Server response time can be affected by the distance between the sites. If you see this
message when connecting to a server, increase the Connect Timeout setting to give the server more time to
respond during the login process.
• “Cannot open data connection” - If this error is encountered in a Windows system with the Firewall enabled, it
may be due to the firewall blocking the connection on the corresponding FTP data port. Try disabling the firewall
temporarily and see if the FTP connection succeeds. If the connection is successful with the firewall disabled,
users can configure the firewall to allow the required port and enable it.
• Client unable to connect using PASV (passive) mode - Ensure that the server is configured to accept passive
connection. For example, in the FileZilla server the passive mode can be enabled by disabling Active mode:
FileZilla Server > Server > Active.
5. Appendix
Application
Flash
Bootloader
0x0000
To generate the binary file of an application project in MPLAB X IDE, perform these actions:
1. Modify Application Start Address.
2. Execute the post build command to generate the bin file.
ROM Origin can be given as any address value which is beyond the size of the bootloader. ROM length can be the
size of the memory available from ROM origin to the end of the memory.
Building the project. The bin file output can be found in the following path: <your_project>.X\dist\<config_name>
\production\
Implementation of erasing and programing the Flash sector can be found in the APP_Flash_Task() function in the
source code of this application.
Application Code Entry
Upon every reset, the bootloader code checks for the following:
• User button press (SW0)
• A valid application code in the Flash address allocated for application
If either of these conditions is true, the execution jumps to the user application code without executing the FTP client
bootloader.
6. References
• The Microchip TCP/IP stack:
http://ww1.microchip.com/downloads/en/appnotes/00833c.pdf
®
• MPLAB Harmony v3 TCP/IP Help:
https://microchip-mplab-harmony.github.io/net/frames.html?frmname=topic&frmfile=index.html
• MPLAB Harmony v3 Installation and Usage:
https://github.com/Microchip-MPLAB-Harmony/mhc/wiki
• RFC 959:
https://tools.ietf.org/html/rfc959
• Microchip Developer Help:
https://microchipdeveloper.com/harmony3:start
• MPLAB Harmony v3:
https://www.microchip.com/mplab/mplab-harmony
• MPLAB Harmony GitHub wiki:
https://github.com/Microchip-MPLAB-Harmony/Microchip-MPLAB-Harmony.github.io/wiki
• How to Setup MPLAB Harmony v3 Software Development Framework:
http://ww1.microchip.com/downloads/en/DeviceDoc/How_to%20_Setup_MPLAB_%20Harmonyv3_
%20Software_%20Development_Framework_DS90003232A.pdf
• How to create first TCP/IP application: https://github.com/Microchip-MPLAB-Harmony/net/wiki/Create-your-first-
tcpip-pplication.
• MPLAB Harmony v2 to MPLAB Harmony v3 TCP/IP Application Migration Guide: https://github.com/Microchip-
MPLAB-Harmony/net/wiki/H2-to-H3-Migration.
Customer Support
Users of Microchip products can receive assistance through several channels:
• Distributor or Representative
• Local Sales Office
• Embedded Solutions Engineer (ESE)
• Technical Support
Customers should contact their distributor, representative or ESE for support. Local sales offices are also available to
help customers. A listing of sales offices and locations is included in this document.
Technical support is available through the website at: http://www.microchip.com/support
Legal Notice
Information contained in this publication regarding device applications and the like is provided only for your
convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with
Trademarks
The Microchip name and logo, the Microchip logo, Adaptec, AnyRate, AVR, AVR logo, AVR Freaks, BesTime,
BitCloud, chipKIT, chipKIT logo, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, HELDO, IGLOO, JukeBlox,
KeeLoq, Kleer, LANCheck, LinkMD, maXStylus, maXTouch, MediaLB, megaAVR, Microsemi, Microsemi logo, MOST,
MOST logo, MPLAB, OptoLyzer, PackeTime, PIC, picoPower, PICSTART, PIC32 logo, PolarFire, Prochip Designer,
QTouch, SAM-BA, SenGenuity, SpyNIC, SST, SST Logo, SuperFlash, Symmetricom, SyncServer, Tachyon,
TempTrackr, TimeSource, tinyAVR, UNI/O, Vectron, and XMEGA are registered trademarks of Microchip Technology
Incorporated in the U.S.A. and other countries.
APT, ClockWorks, The Embedded Control Solutions Company, EtherSynch, FlashTec, Hyper Speed Control,
HyperLight Load, IntelliMOS, Libero, motorBench, mTouch, Powermite 3, Precision Edge, ProASIC, ProASIC Plus,
ProASIC Plus logo, Quiet-Wire, SmartFusion, SyncWorld, Temux, TimeCesium, TimeHub, TimePictra, TimeProvider,
Vite, WinPath, and ZL are registered trademarks of Microchip Technology Incorporated in the U.S.A.
Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BlueSky, BodyCom,
CodeGuard, CryptoAuthentication, CryptoAutomotive, CryptoCompanion, CryptoController, dsPICDEM,
dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial Programming, ICSP,
INICnet, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, memBrain, Mindi, MiWi, MPASM, MPF,
MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient Code Generation, PICDEM,
PICDEM.net, PICkit, PICtail, PowerSmart, PureSilicon, QMatrix, REAL ICE, Ripple Blocker, SAM-ICE, Serial Quad
I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total Endurance, TSHARC, USBCheck, VariSense,
ViewSpan, WiperLock, Wireless DNA, and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A.
and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
The Adaptec logo, Frequency on Demand, Silicon Storage Technology, and Symmcom are registered trademarks of
Microchip Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip
Technology Inc., in other countries.
All other trademarks mentioned herein are property of their respective companies.
© 2020, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.
ISBN: 978-1-5224-6113-5