---
title: "6. W5100S/W5500+RP2040 Raspberry Pi Pico＜UDP Server Data Loopback Test＞"
url: "https://maker.wiznet.io/ronpang/projects/6-w5100s-w5500-rp2040-raspberry-pi-picoudp-server-data-loopback-test/"
markdown_url: "https://maker.wiznet.io/ronpang/projects/6-w5100s-w5500-rp2040-raspberry-pi-picoudp-server-data-loopback-test/md"
type: "UCC: User Created Content"
author: "WIZnet HK"
author_url: "https://blog.csdn.net/WIZnet2012/article/details/134140991?spm=1001.2014.3001.5502"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "WIZnet HK"
original_url: "https://blog.csdn.net/WIZnet2012/article/details/134140991?spm=1001.2014.3001.5502"
published: "2023-11-17"
language: "en"
hardware: ["WIZnet W5100S-EVB-Pico", "WIZnet W5500-EVB-Pico"]
likes: 0
views: 291
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# 6. W5100S/W5500+RP2040 Raspberry Pi Pico＜UDP Server Data Loopback Test＞

> 6. W5100S/W5500+RP2040 Raspberry Pi Pico＜UDP Server Data Loopback Test＞

Original author: WIZnet HK (source: https://blog.csdn.net/WIZnet2012/article/details/134140991?spm=1001.2014.3001.5502)

## Components

- **WIZnet W5100S-EVB-Pico** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5100S/w5100s-evb-pico))
- **WIZnet W5500-EVB-Pico** x 1 ([docs](https://docs.wiznet.io/Product/Chip/Ethernet/W5500/w5500-evb-pico))

## Documents and links

- [Code for this article](https://gitee.com/wiznet-hk/W5100_W5500_C_SDK.git) (code)
- [WIZnet Official Website](https://www.wiznet.io/)
- [WIZnet Official Library](https://github.com/Wiznet/ioLibrary_Driver)
- [YouTube video](https://youtu.be/NbOVNrV1jOg)

## Article

## 1 Introduction

UDP is a connectionless network protocol that provides a simple, unreliable way to transmit data. Although it does not guarantee the integrity and sequence of data transmission, UDP has unique advantages in certain scenarios, such as in real-time applications or online games.

W5100S/W5500 is an embedded Ethernet controller that integrates a full hardware TCP/IP protocol stack. It is also an industrial-grade Ethernet control chip. Using the W5100S/W5500 in Ethernet applications makes it easier for users to connect and communicate remotely between devices.

## 2. Introduction to the protocol

### 2.1 Brief description

A UDP server is a server that communicates using the UDP protocol. Unlike TCP servers, UDP servers do not need to establish a connection to send and receive datagrams. This makes UDP servers more efficient for applications that need to handle large numbers of concurrent connections.

### 2.2 Advantages

The advantages of UDP server mainly include:

**Efficiency: **UDP does not require the establishment of a connection, thus reducing the overhead of establishing a connection, making data transmission faster.

**Real-time: **UDP is suitable for applications that require high real-time performance because it can transmit data faster.

**Broadcast and multicast transmission: **UDP can implement broadcast and multicast transmission, which means that a message can be sent to multiple or all destinations.

**Simplicity: **The UDP protocol is relatively simple and easy to understand and implement.

### 2.3 UDP Server steps

![](https://maker.wiznet.io/upload/ckeditor5/911276871%5F1700237557%2Epng)

Creating a UDP server typically involves the following steps:

**Create a socket:** This is usually done using system calls such as socket().

**Bind address and port: **Select a listening location for the server by binding the socket to the local IP address and port number.

**Start listening: **Bind the socket to the local address and port number by calling the bind() function, and then use the recvfrom() function to start listening for datagrams from the client.

**Processing Datagrams:** When a datagram is received, the server can send a response back to the client using the sendto() function.

**Close the socket:** Finally, close the socket to release system resources.

### 2.4 Application

UDP servers are widely used in various application scenarios. The following are some common applications:

**Real-time communication: **UDP is widely used in real-time communication due to its fast data transmission characteristics, such as real-time stock trading systems, real-time market quotation systems, real-time quotation systems, etc.

**Online games: **Online games have high requirements for real-time performance and smoothness. The UDP protocol can provide faster speeds and less delays, so it is widely used in online games.

**Audio and video transmission: **Audio and video transmission has high requirements for real-time performance and smoothness. The UDP protocol can provide faster speed and less delay, so it is widely used in audio and video transmission.

**Internet of Things:** In the field of Internet of Things, UDP is widely used in the communication of various devices because of its simplicity, speed and efficiency.

**Log collection: **Many servers use UDP to send log data because UDP can provide faster transmission speeds and does not have strict requirements on the order and integrity of data packets.

## 3. WIZnet Ethernet chip

**WIZnet mainstream hardware protocol stack Ethernet chip parameter comparison**

| **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 |

W5100S/W6100 supports 8-bit data bus interface, and the network transmission speed will be better than W5500.

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.

W5500 has more Sockets and send and receive buffers than W5100S.

## 4. UDP Server loopback test

### 4.1 Program flow chart

![](https://maker.wiznet.io/upload/ckeditor5/911276871%5F1700238041%2Epng)

## **4.2 Test preparationSoftware:**

Visual Studio Code

WIZnet UartTool

SocketTester

**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 GPIO 0 (UART0 TX) &lt;----> USB_TTL_RX

RP2040 GPIO 1 (UART0 RX) &lt;----> USB_TTL_TX

When using the module to connect RP2040 for wiring

RP2040 GPIO 16 &lt;----> W5100S MISO

RP2040 GPIO 17 &lt;----> W5100S CS

RP2040 GPIO 18 &lt;----> W5100S SCK

RP2040 GPIO 19 &lt;----> W5100S MOSI

RP2040 GPIO 20 &lt;----> W5100S RST

Directly connect to the PC network port through a network cable (or: both the PC and the device are connected to the switch or router LAN port through a network cable)

### 4.4 Related code

We directly open the udp_server.c file (path: examples/udp_client/udp_client.c) to see the specific implementation:

You can see that the network information is configured in DHCP mode. Therefore, after the master control and W5100S are initialized, DHCP initialization will be performed, and then a timer initialization will be added to time the DHCP process for timeout processing; then enter DHCP configures network information. If it succeeds, it will directly enter the loop to call the loopback test function. If it fails, it will use the static network information we initialized to configure, and then enter the loop to call the loopback test function, as shown below:

```
/* Network information to be configured. */
wiz_NetInfo net_info = {
    .mac = {0x00, 0x08, 0xdc, 0x1e, 0xed, 0x2e}, // Configured MAC address
    .ip = {192, 168, 1, 10},                     // Configured IP address
    .sn = {255, 255, 255, 0},                    // Configured subnet mask
    .gw = {192, 168, 1, 1},                      // Configured gateway
    .dns = {8, 8, 8, 8},                         // Configured domain address
    .dhcp = NETINFO_DHCP};                       // Configured dhcp model,NETINFO_DHCP:use dhcp; NETINFO_STATIC: use static ip.
​
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
    0,
};                                           // Send and receive cachestatic 
​
static uint16_t local_port = 8080;             // UDP port
static uint8_t breakout_flag = 0;         // Define the DHCP acquisition flag
​
int main()
{
    struct repeating_timer timer; // Define the timer structure
    wiz_NetInfo get_info;         // Stores the read configuration information
​
    /* MCU init */
    stdio_init_all();     // Initialize the main control peripheral
    wizchip_initialize(); // Initialize the chip interface
    wizchip_setnetinfo(&net_info); // Configure once first
    
    /*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 udp server example.\r\n");
    network_init(&net_info);              // Configuring Network Information
    print_network_information(&get_info); // Read back the configuration information and print it
​
    while (true)
    {
        loopback_udps(SOCKET_ID, ethernet_buf, local_port); // udp loopback test
    }
}
```

Jump into the loopback test to see its specific implementation: This function has these parameters, socket port number, data sending and receiving cache, target IP address, and target port; you can fill in the parameters as needed. The whole process polls the socket status through a switch state machine, performs corresponding processing according to the difference, and sequentially completes the operations of initialization, opening the socket port, connecting to the server, and sending back the data after receiving the data; the local port is initialized directly within the function. . As follows:

```
/**
 * @brief   udp server mode loopback test
 * @param   sn:    socket number
 * @param   buf:   Data sending and receiving cache
 * @param   port:  Local port
 * @return  value for SOCK_ERRORs,return 1:no error
*/
​
int32_t loopback_udps(uint8_t sn, uint8_t* buf, uint16_t port)
{
   int32_t  ret;
   uint16_t size, sentsize;
   uint8_t  destip[4];
   uint16_t destport;
​
   switch(getSn_SR(sn))
   {
      case SOCK_UDP :
         if((size = getSn_RX_RSR(sn)) > 0)
         {
            if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
            ret = recvfrom(sn, buf, size, destip, (uint16_t*)&destport);
            buf[ret]=0x00;
            printf("recv form[%d.%d.%d.%d][%d]: %s\n", destip[0],destip[1],destip[2],destip[3],destport,buf);
            if(ret <= 0)
            {
#ifdef _LOOPBACK_DEBUG_
               printf("%d: recvfrom error. %ld\r\n",sn,ret);
#endif
               return ret;
            }
            size = (uint16_t) ret;
            sentsize = 0;
            while(sentsize != size)
            {
               ret = sendto(sn, buf+sentsize, size-sentsize, destip, destport);
               if(ret < 0)
               {
#ifdef _LOOPBACK_DEBUG_
                  printf("%d: sendto error. %ld\r\n",sn,ret);
#endif
                  return ret;
               }
               sentsize += ret; // Don't care SOCKERR_BUSY, because it is zero.
            }
         }
         break;
      case SOCK_CLOSED:
#ifdef _LOOPBACK_DEBUG_
         //printf("%d:UDP loopback start\r\n",sn);
#endif
         if((ret = socket(sn, Sn_MR_UDP, port, 0x00)) != sn)
            return ret;
#ifdef _LOOPBACK_DEBUG_
         printf("%d:Opened, UDP loopback, port [%d]\r\n", sn, port);
#endif
         printf("local port:%d\n",port);
         break;
      default :
         break;
   }
   return 1;
}
```

## **4.5 Test phenomena**

After the hardware connection is correct, compile the burning program (for details, please refer to Chapter 1), open WIZ UartTool, select the corresponding COM port, and fill in the parameters: baud rate 115200, 8 data bits, 1 stop bit, no correction Verification, no flow control, click open after filling in the parameters, observe the information printed by the serial port to obtain the device running status; open SocketTester, fill in the corresponding parameters in the left column, UDP mode, local IP fill in the IP of the computer, local The port can be filled in randomly, but try not to use special ports; then fill in the device IP and device port in the remote IP address column below based on the IP and other information obtained by the device through DHCP. Because UDP is connectionless, you can send the information directly. You can see the return phenomenon, as shown in the figure below:

![](https://maker.wiznet.io/upload/ckeditor5/911276871%5F1700238576%2Epng)

## 5. Precautions

UDP is connectionless. The client can only see the phenomenon after the server sends the message.

If we want to use WIZnet's W5500 to implement the example in this chapter, we only need to modify two places:

Find the wizchip_conf.h header file under library/ioLibrary_Driver/Ethernet/ and modify the WIZCHIP macro definition to W5500;

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

---

Source: https://maker.wiznet.io/ronpang/projects/6-w5100s-w5500-rp2040-raspberry-pi-picoudp-server-data-loopback-test/
