11. W5100S/W5500+RP2040 Raspberry Pi Pico<ARP Address Resolution>

Article directory

  • 1 Introduction
  • 2 Introduction
    • 2.1 What is ARP?
    • 2.2 Advantages of ARP
    • 2.3 How ARP works
    • 2.4 ARP 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

As network security receives more and more attention, the market demand for ARP attack protection software is also increasing. ARP also provides decentralized application distribution solutions, including completely open source code, mutual openness of orders, and profit sharing. These characteristics make ARP have broad application prospects in future development.

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 ARP application to help readers better master this technology.

2 Introduction

2.1 What is ARP?

ARP (Address Resolution Protocol) is an address resolution protocol, a TCP/IP protocol that obtains a physical address based on an IP address. It is used to resolve IP addresses on the network into specific hardware addresses (MAC addresses). In a computer network, when a host needs to send a data packet to another host, it needs to know the MAC address of the target host. However, in most cases, the host only knows the IP address of the target host, so the ARP protocol is needed to convert the IP address to a MAC address.

2.2 Advantages of ARP

The advantages of ARP mainly include:

  1. IP addresses can be resolved to Ethernet MAC addresses, thus facilitating communication between network devices.
  2. Measures such as restrictions and inspections on ARP table learning and ARP packet processing can ensure the security of network devices.
  3. ARP is simple and easy to use.

2.3 ARP working principle

The working principle of ARP can be summarized as follows:

  1. Each host will establish an ARP list (address translation table) in its own ARP buffer to store the correspondence between IP addresses and MAC addresses.
  2. When the source host needs to send data to the destination host, it will first check its own ARP list to see if the MAC address corresponding to the IP address of the destination host already exists. If it exists, the data packet will be sent directly to the MAC address; if it does not exist, an ARP request broadcast packet will be sent to the local network segment to query the MAC address corresponding to the destination host.
  3. The ARP request packet will contain the IP address of the source host, the hardware address, and the IP address of the destination host. After receiving this ARP request, all hosts in the network will check whether the destination IP address in the data packet is consistent with its own IP address.
  4. If the source host has not received an ARP response packet, it means that the ARP query failed. If the source host receives the ARP response packet, it will add the obtained IP address and MAC address of the destination host to its own ARP list, and use this information to start data transmission.

2.4 ARP application scenarios

The ARP protocol is used in many network scenarios. The most common application scenarios include:

  1. Network communication: In a local area network, the ARP protocol can help computers obtain their corresponding MAC addresses through IP addresses, thereby enabling communication between computers.
  2. Routing: ARP protocol can help the router find the MAC address of the target device, which is very useful in routing.
  3. Switch learns MAC address: The switch can learn the MAC address of each computer through the ARP protocol and establish a MAC address table, so that it can directly forward data packets and improve the communication efficiency of the LAN.

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

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

Step 1: Add the corresponding .h file to the arp_run.c file.

Step 2: Define the macros required for DHCP configuration.

Step 3: Configure network information and turn on DHCP mode.

Step 4: Write a timer callback processing function for the DHCP 1s tick timer processing function.

Step 5: The main function first defines a timer structure parameter to trigger the timer callback function, initializes the serial port and SPI, then writes the network configuration parameters of W5100S, initializes DHCP and starts DHCP to obtain IP, and obtains Just print the obtained IP. When the number of acquisitions exceeds the maximum number of acquisitions, a static IP will be used. Then the main loop initiates an ARP request. The required parameters are the socket number, cache buff, and target IP. Obtain the MAC address of the target IP.

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
#include "hardware/spi.h"

#include "wizchip_conf.h"
#include "bsp_spi.h"
#include "dhcp.h" // Use dhcp
#include "socket.h" // Use socket
#include "arp.h" // Use arp

#define SOCKET_ID 0 // Socket number
#define ETHERNET_BUF_MAX_SIZE (1024 * 2) // Send and receive cache size
#define DHCP_RETRY_COUNT 5 // DHCP retry times


/**
 * @brief Timer callback processing function, used for dhcp timing processing
 * @param repeating :Timer structure
 * @return bool
 */
bool repeating_timer_callback(struct repeating_timer *t);

/**
 * @brief Initialization of chip network information
 * @param conf_info:Static configuration information
 * @return none
 */
void network_init(wiz_NetInfo *conf_info);

/* 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 uint8_t destip[4]={192, 168, 1, 2}; // udp destination ip
static uint8_t dest_ip[4] = {<!-- -->192, 168, 1, 2}; // UDP IP address
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;
    /* 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)
    {<!-- -->
        do_arp(SOCKET_ID, ethernet_buf, dest_ip); //run arp
    }
}

void network_init(wiz_NetInfo *conf_info)
{<!-- -->
    int count = 0;
    uint8_t dhcp_retry = 0;

    if (conf_info->dhcp == NETINFO_DHCP)
    {<!-- -->
        while(true)
        {<!-- -->
            switch (DHCP_run()) // Do the DHCP client
            {<!-- -->
            case DHCP_IP_LEASED: // DHCP resolves the domain name successfully
            {<!-- -->
                if (breakout_flag == 0)
                {<!-- -->
                    printf("DHCP success\r\\
");
                    getIPfromDHCP((*conf_info).ip);
                    getGWfromDHCP((*conf_info).gw);
                    getSNfromDHCP((*conf_info).sn);
                    getDNSfromDHCP((*conf_info).dns);
                    wizchip_setnetinfo(conf_info); // Configuring Network Information
                    close(SOCKET_ID); // After dhcp close the socket, avoid errors in later use
                    breakout_flag = 1;
                }
                break;
            }
            case DHCP_FAILED:
            {<!-- -->
                printf("DHCP failed \r\\
");
                count + + ;
                if (count <= DHCP_RETRY_COUNT) // If the number of times is less than or equal to the maximum number of times, try again
                {<!-- -->
                    printf("DHCP timeout occurred and retry %d \r\\
", count);
                }
                else if (count > DHCP_RETRY_COUNT) // If the number of times is greater than DHCP fails
                {<!-- -->
                    breakout_flag = 1; // if DHCP fail, use the static
                    DHCP_stop(); // Stop processing DHCP protocol
                    conf_info->dhcp = NETINFO_STATIC;
                    wizchip_setnetinfo(conf_info); // Configuring Network Information
                    break;
                }
                break;
            }
            }
            if (breakout_flag)
            {<!-- -->
                printf("config succ\r\\
");
                break;
            }
        }
    }
    else
    {<!-- -->
        wizchip_setnetinfo(conf_info); // Configuring Network Information
    }
}

bool repeating_timer_callback(struct repeating_timer *t)
{<!-- -->
    DHCP_time_handler(); // DHCP 1s Tick Timer handler
    return true;
}

4.5 Result Demonstration

1. Open WIZ UartTool and fill in the parameters: select the com port corresponding to the serial port, baud rate 115200, 8 data bits, 1 stop bit, no check bit, no flow control. After filling in the parameters, click open.

2. After opening the serial port, press the reset button to see the serial port printing the information obtained by DHCP, in which the IP is 192.168.1.123.

3. Then start ARP request 192.168.1.2 to obtain the MAC. After obtaining the MAC address, it indicates success.

5 Notes

  • The socket number must be socket0 and make it work in MACRAM mode.
  • 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

Related routine links in this chapter

If you want to know more, leave a comment!