7. W5100S/W5500+RP2040 Raspberry Pi Pico<UDP Multicast>

Article directory

  • 1 Introduction
  • 2. Related introduction
    • 2.1 Brief description
    • 2.2 Advantages
    • 2.3 Application
  • 3. WIZnet Ethernet chip
  • 4. UDP multicast loopback test
    • 4.1 Program flow chart
    • 4.2 Test preparation
    • 4.3 Connection method
    • 4.4 Related code
    • 4.5 Test phenomena
  • 5. Precautions
  • 6. Related links

1. Preface

UDP multicast is a communication method based on the UDP protocol, which allows a computer to send information to multiple targets at the same time by sending a single UDP packet. This communication method is very useful in applications that require efficient, real-time data transmission, such as live video broadcasts, online games, etc.
This chapter will conduct UDP multicast loopback test.

W5100S/W5500 is an embedded Ethernet controller integrating 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. Related introduction

2.1 Brief description

UDP multicast is a communication method based on UDP protocol, also called multicast. It allows a computer to send information to multiple targets simultaneously by sending a single UDP packet, without the need to send separate packets to each target as in unicast mode.

Multicast addresses are a special type of IP address, ranging from 224.0.0.0 to 239.255.255.255. They are divided into different types, including local link multicast addresses, reserved multicast addresses and administrative rights multicast addresses. When using multicast, a peer-to-peer relationship needs to be established between the sender and the receiver, and a specific multicast protocol is used to ensure the reliability and sequence of data transmission.

Multicast MAC address: In order to achieve correct transmission of multicast information on the local physical network, a multicast MAC address needs to be used at the link layer. When Ethernet transmits IPv4 unicast packets, the destination MAC address uses the recipient’s MAC address. However, when transmitting multicast data, its destination is no longer a specific receiver, but a group with uncertain members, so the IPv4 multicast MAC address must be used, that is, the IPv4 multicast address is mapped to the link layer address. IANA stipulates that the upper 24 bits of the IPv4 multicast MAC address are 0x01005e, the 25th bit is 0, and the lower 23 bits are the lower 23 bits of the IPv4 multicast address. The mapping relationship is as shown in the following figure:

In multicast, the sender sends the data packet to a specific multicast group address. This address does not belong to any specific computer, but to a group of computers. Only computers subscribed to this multicast group address can receive this data packet. This communication method can effectively save network bandwidth because the sender only needs to send one data packet instead of sending one data packet to each receiver.

In contrast, unicast mode is a point-to-point communication method, and each sender needs to send a data packet to the receiver individually. In some cases, unicast mode can waste a lot of network bandwidth if the same packet needs to be sent to multiple recipients.

In general, UDP multicast is a very efficient communication method, suitable for scenarios where the same data needs to be sent to multiple receivers at the same time, such as live video broadcasts, online games, multi-user collaboration, etc.

2.2 Advantages

  • Save bandwidth: Since UDP multicast allows the sender to only send one data packet to reach multiple receivers, it can significantly save bandwidth, especially when there are many receivers.
  • Efficient transmission: Compared with unicast mode, UDP multicast can quickly transmit data packets in the network because it can be sent to multiple receivers simultaneously on one network path.
  • Real-time: UDP multicast is suitable for real-time communication because it can immediately send data packets to all receivers subscribed to the multicast group without waiting for confirmation from the receiver.
    Scalability: By using multicast addresses, one-to-many communication can be achieved, suitable for large-scale data distribution and real-time applications

2.3 Application

  • Multimedia Broadcasting: Multimedia broadcasting applications, such as television and radio broadcasts, transmit audio and video data to multiple receivers via UDP multicast.
  • Multi-user games: Multi-user games transmit game data to multiple clients through UDP multicast to realize multi-player online games.
  • Real-time collaboration: Real-time collaboration applications like online conferencing can use UDP multicast to transmit audio, video, and other collaboration data to all participants.
  • Distributed system: UDP multicast can be used in distributed systems to send data from one node to other nodes to achieve functions such as data synchronization and status reporting.
  • Network monitoring: Network monitoring applications can send monitoring data to multiple receivers through UDP multicast, such as real-time traffic monitoring, network security monitoring, etc.
  • Video conferencing: A multi-person online video conferencing, each user can share audio and video data.

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
  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. UDP multicast loopback test

4.1 Program flow chart

4.2 Test preparation

Software:

  • 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) <----> USB_TTL_RX
    • RP2040 GPIO 1 (UART0 RX) <----> USB_TTL_TX
  • When using the module to connect RP2040 for wiring
    • RP2040 GPIO 16 <----> W5100S MISO
    • RP2040 GPIO 17 <----> W5100S CS
    • RP2040 GPIO 18 <----> W5100S SCK
    • RP2040 GPIO 19 <----> W5100S MOSI
    • RP2040 GPIO 20 <----> 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_multicast.c file (path: examples/udp_multicast/udp_multicast.c) to see the specific implementation:

You can see that the network information is configured in dhcp mode, so after the main control and W5100S are initialized, DHCP initialization will be performed, and then a timer initialization will be added to do timing during the DHCP process for timeout processing; Then enter DHCP to configure the network information. If it succeeds, it will directly enter the loop to call the loopback test function. If it fails, we 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.

wiz_NetInfo get_info;
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {<!-- -->
    0,
}; // Send and receive cache
static uint8_t multicast_mac[6]= {<!-- -->0x01,0x00,0x5e,0x01,0x01,0x0b}; // multicast mac address
static uint8_t multicast_ip[4] = {<!-- -->224, 1, 1, 11}; // multicast ip address
static uint16_t multicast_port = 30000; // multicast port
static uint8_t dhcp_get_ip_flag = 0; // Define the DHCP acquisition flag

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

    /* 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 tcp server example.\r\
");
    network_init( & amp;net_info); // Configuring Network Information
    print_network_information( & amp;get_info); // Read back the configuration information and print it

    while(true)
    {<!-- -->
        multicast_loopback(SOCKET_ID, ethernet_buf, multicast_mac, multicast_ip, multicast_port); // Multicast loopback test
    }
}

Jump into the loopback test to see its specific implementation: This function has these parameters, socket port number, data sending and receiving buffer, multicast MAC address, multicast IP address, multicast port; fill in the parameters according to actual needs. 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, and sending back the data after receiving it; the local port is initialized directly within the function. As follows:

/**
 * @brief UDP Multicast loopback test
 * @param sn: Socket Number
 * @param buf: Data sending and receiving cache
 * @param multicast_mac: Multicast MAC address
 * @param multicast_ip: Multicast IP address
 * @param multicast_port:Multicast port
 * @return value for SOCK_ERRORs,return 1:no error
*/
int32_t multicast_loopback(uint8_t sn, uint8_t* buf, uint8_t* multicast_mac, uint8_t* multicast_ip, uint16_t multicast_port)
{<!-- -->
   int32_t ret;
   uint16_t size, sentsize;
   uint8_t destip[4];
   uint16_t destport, port=50000;
   
   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*) & amp;destport);
            buf[ret]=0x00;
            printf("recv from [%d.%d.%d.%d][%d]: %s\r\
",destip[0],destip[1],destip[2],destip[3] ,destport,buf);
            if(ret <= 0)
            {<!-- -->
#ifdef _MULTICAST_DEBUG_
               printf("%d: recvfrom error. %ld\r\
",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 _MULTICAST_DEBUG_
                  printf("%d: sendto error. %ld\r\
",sn,ret);
#endif
                  return ret;
               }
               sentsize + = ret; // Don't care SOCKERR_BUSY, because it is zero.
            }
         }

         break;
      case SOCK_CLOSED:
#ifdef _MULTICAST_DEBUG_
         printf("%d:Multicast Loopback start\r\
",sn);
#endif
         setSn_DIPR(0, multicast_ip);
         setSn_DPORT(0, multicast_port);
         setSn_DHAR(0, multicast_mac);
         if((ret = socket(sn, Sn_MR_UDP, port, Sn_MR_MULTI)) != sn)
            return ret;
#ifdef _MULTICAST_DEBUG_
         printf("%d:Opened, UDP Multicast Socket\r\
", sn);
         printf("%d:Multicast Group IP - %d.%d.%d.%d\r\
", sn, multicast_ip[0], multicast_ip[1], multicast_ip[2], multicast_ip[3]);
         printf("%d:Multicast Group Port - %d\r\
", sn, multicast_port);
#endif
         break;
      default :
         break;
   }
   return 1;
}

4.5 Test phenomenon

? 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, none Check digit, 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 the network debugging tool SocketTester, fill in the parameters in the left column: select UDP mode, fill in the local IP with the computer IP , the port is random, but try not to use special ports; fill in the corresponding multicast IP and port for the remote IP and port below, and then send the information directly. You can see that after the data is successfully sent, the serial port receives the message from the multicast member ( 192.168.1.2:8080), that is, the device as a multicast member successfully received the data sent by the computer to the broadcast group; as shown in the following figure:

In order to understand the interaction process more directly, here we use the wireshark packet capture tool to capture and analyze the packets. Every time after sending data to the broadcast group, the device as a member of the broadcast group receives it and sends it back, as shown in the following figure:

5. Notes

  • Mapping relationship between multicast MAC address and multicast IP address
  • 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 the 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!