15. W5100S/W5500+RP2040 Raspberry Pi Pico<TFTP Client>

Article directory

  • 1 Introduction
  • 2 Introduction
    • 2.1 What is TFTP?
    • 2.2 Advantages of TFTP
    • 2.3 Comparison between TFTP and FTP
    • 2.4 TFTP application scenarios
  • 3 WIZnet Ethernet chip
  • 4 ARP network setting example overview and usage
    • 4.1 Flowchart
    • 4.2 Core preparation work
    • 4.3 Connection method
    • 4.4 Main code overview
    • 4.5 Results demonstration
  • 5 things to note
  • 6 related links

1 Preface

Generally speaking, the primary purpose of using the Internet is to share information, and file transfer is a very important part of information sharing. Compared with complex file transfer protocols such as FTP, the TFTP protocol is simple and lightweight, and is suitable for scenarios such as embedded systems with limited resources.

W5100S/W5500 is an embedded Ethernet controller integrating a full hardware TCP/IP protocol stack. It is also an industrial-grade Ethernet control chip. This tutorial will introduce the basic principles, usage steps, application examples and precautions of W5100S/W5500 Ethernet TFTP application to help readers better master this technology.

2 Introduction

2.1 What is TFTP?

TFTP is a simple locked file transfer protocol that can transfer files between computer systems on a local network. It is a simple protocol that utilizes the User Datagram Protocol (UDP) to transfer files between computer systems on the Internet.

2.2 Advantages of TFTP

  1. Simple: The TFTP protocol is simple to implement, easy to understand and implement, and is suitable for file transfer in scenarios such as embedded systems with limited resources.
  2. Lightweight: The TFTP protocol is more lightweight than FTP and is suitable for file transfer in systems with limited resources.
  3. Plain text transmission: TFTP uses ASCII encoding for file transmission, which is suitable for the transmission of text files and is easy to parse and process.
  4. Support error handling: TFTP supports error handling mechanism. When an error occurs, error information can be sent to the other party for error handling.
  5. Suitable for small file transfers: The TFTP protocol is designed for small file transfers, so it does not have many features of the usual FTP. For example, it can only get or write files from the file server, cannot list directories, and does not Certification. This makes TFTP highly efficient when transferring small files.
  6. Supports read-only and read-write operations: TFTP supports read-only and read-write operations on files, and can be used to download files from the server or upload files to the server.

2.3 Comparison between TFTP and FTP

TFTP and FTP are two different file transfer protocols, and there are some obvious differences between them.

  1. Function: FTP is a complete, session-oriented, general-purpose file transfer protocol that provides more functions than TFTP, such as directory browsing, file renaming, and permission management. TFTP is a simple, request-based file transfer protocol that only provides the most basic file transfer functions.
  2. Ports: FTP uses TCP ports 21 and 20, while TFTP uses UDP port 69.
  3. Reliability: FTP is a reliable transmission protocol and supports resumed transmission and error recovery, while TFTP is an unreliable transmission protocol and has no error recovery or retransmission mechanism.
  4. Security: FTP supports encrypted SSL/TLS protocols to protect transmitted data, while TFTP does not provide any encryption capabilities.
  5. Packet size: FTP can transfer large files as it supports data splitting and reassembly. TFTP can only transfer smaller files because it limits the packet size.
  6. Application scenarios: TFTP is usually used for firmware upgrades or configuration file transmission in a local area network, while FTP is more suitable for downloading or uploading files from a remote server.

2.4 TFTP application scenarios

Although the TFTP protocol is relatively simple, it can still play an important role in specific scenarios.

  1. Firmware upgrade: Many hardware devices can have firmware upgrades through a TFTP server. The TFTP server can transfer firmware files to the device for updates to improve the performance of the device or fix software vulnerabilities.
  2. Network installation: The TFTP server can be used for network installation, such as presetting the system on a diskless machine so that other machines can automatically download it during PXE startup.
  3. Backup files: In Linux systems, the TFTP server can be used to back up system configuration files or runtime data. These backup files can be transferred between the server and client for troubleshooting or system recovery.
  4. Small file transfer: Because the TFTP protocol is simple and easy to implement, it is suitable for transferring small text files, especially in scenarios with limited resources such as embedded systems and network devices.

3 WIZnet Ethernet chip

WIZnet mainstream hardware protocol stack Ethernet chip parameter comparison

td>

Model Embedded Core Host I/F TX/RX Buffer HW Socket Network Performance
W5100S TCP/IPv4, MAC & PHY 8bit BUS, SPI 16KB 4 Max.25Mbps
W6100 TCP/IPv4/IPv6, MAC & PHY 8bit BUS, Fast SPI 32KB 8 Max.25Mbps
W5500 TCP/IPv4, MAC & PHY Fast SPI 32KB 8 Max 15Mbps
  1. W5100S/W6100 supports 8-bit data bus interface, and the network transmission speed will be better than W5500.
  2. W6100 supports IPV6 and is compatible with W5100S hardware. If users who already use W5100S need to support IPv6, they can be Pin to Pin compatible.
  3. W5500 has more Sockets and send and receive buffers than W5100S.

4 ARP network setting example overview and usage

4.1 Flowchart

The running block diagram of the program is as follows:

4.2 Core preparation work

Software

  • Visual Studio Code
  • WIZnet UartTool
  • Tftpd32

Hardware

  • W5100SIO module + RP2040 Raspberry Pi Pico development board or WIZnet W5100S-EVB-Pico development board
  • Micro USB interface data cable
  • TTL to USB
  • cable

4.3 Connection method

  • Connect the USB port of the PC through the data cable (mainly used for burning programs, but can also be used as a virtual serial port)

  • Convert TTL serial port to USB and connect the default pin of UART0:

    • RP2040 GPIO0 (UART0 TX) <----> USB_TTL_RX
    • RP2040 GPIO1 (UART0 RX) <----> USB_TTL_TX
  • When wiring using module connection RP2040

    • RP2040 GPIO16 <----> W5100S MISO
    • RP2040 GPIO17 <----> W5100S CS
    • RP2040 GPIO18 <----> W5100S SCK
    • RP2040 GPIO19 <----> W5100S MOSI
    • RP2040 GPIO20 <----> W5100S RST
  • Connect the PC and device to the router LAN port through network cables

4.4 Main code overview

We are using the official ioLibrary_Driver library of WIZnet. The library supports a wide range of protocols and is easy to operate. The chip integrates the TCP/IP protocol stack on the hardware. The library also encapsulates the protocols above the TCP/IP layer. We only need to simply call the corresponding function to complete the application of the protocol. .

Next see the tftp_client.c file.

Step 1: Add the required libraries

Step 2: The libraries required for macro definition and the variables, structures, etc. used in the definition.

Step 3: Write the timer callback function and network initialization function.

Step 4: In the main function, first we initialize the chip, including the serial port, SPI and its related pins, and the link status detection of the W5100S chip. Then perform DHCP to obtain network address information. If the acquisition fails, the preset static address information will be used for configuration. Finally, there is the initialization operation of the TFTP client and the TFTP request to read the file.

int main(void)
{<!-- -->
    int i;
    struct repeating_timer timer; // Define the timer structure
    wiz_NetInfo get_info;

    int tftp_state;
    uint8_t tftp_read_flag = 0;
    uint32_t tftp_server_ip = inet_addr(TFTP_SERVER_IP);
    uint8_t tftp_read_file_name[] = TFTP_SERVER_FILE_NAME;

    /* MCU init */
    stdio_init_all(); // Initialize the main control peripheral
    wizchip_initialize(); // Initialize the chip interface

    /*dhcp init*/
    DHCP_init(SOCKET_ID, ethernet_buf); // DHCP initialization
    add_repeating_timer_ms(1000, repeating_timer_callback, NULL, & timer); // Add DHCP 1s Tick Timer handler

    printf("wiznet chip tftp client example.\r\
");
    network_init( & amp;net_info); // Configuring Network Information
    print_network_information( & amp;get_info); // Read back the configuration information and print it

    TFTP_init(TFTP_SOCKET_ID, tftp_client_socket_buffer);

    while(true)
    {<!-- -->
        if (tftp_read_flag == 0)
        {<!-- -->
            printf("tftp server ip: %s, file name: %s\r\
", TFTP_SERVER_IP, TFTP_SERVER_FILE_NAME);
            printf("send request\r\
");
            TFTP_read_request(tftp_server_ip, TFTP_SERVER_FILE_NAME);
            tftp_read_flag = 1;
        }
        else
        {<!-- -->
            tftp_state = TFTP_run();
            if (tftp_state == TFTP_SUCCESS)
            {<!-- -->
                printf("tftp read success, file name: %s\r\
", tftp_read_file_name);

                while (1)
                {<!-- -->
                }
            }
            else if (tftp_state == TFTP_FAIL)
            {<!-- -->
                printf("tftp read fail, file name: %s\r\
", tftp_read_file_name);
                while (1)
                {<!-- -->
                }
            }
        }
    }
}

4.5 Result Demonstration


5 Notes

  • A directory must be set on the Tftpd32 tool, and the requested file must be in the directory, otherwise the request will fail.
  • If we want to use WIZnet’s W5500 to implement the example in this chapter, we only need to modify two places:

? (1) Find the header file wizchip_conf.h under library/ioLibrary_Driver/Ethernet/, and change the _WIZCHIP_ macro definition to W5500.

? (2) Find the CMakeLists.txt file under library and set COMPILE_SEL to ON. OFF is W5100S and ON is W5500.

6 Related links

WIZnet official website

WIZnet official library link

Routine link in this chapter

If you want to know more, leave a comment!