---
title: "2. W5100S/W5500+RP2040 Raspberry Pi Pico＜DHCP＞"
url: "https://maker.wiznet.io/ronpang/projects/2-w5100s-w5500-rp2040-raspberry-pi-picodhcp/"
markdown_url: "https://maker.wiznet.io/ronpang/projects/2-w5100s-w5500-rp2040-raspberry-pi-picodhcp/md"
type: "UCC: User Created Content"
author: "WIZnet HK"
author_url: "https://blog.csdn.net/WIZnet2012/article/details/134043644?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/134043644?spm=1001.2014.3001.5502"
published: "2023-11-13"
language: "en"
hardware: ["WIZnet W5100S-EVB-Pico", "WIZnet W5500-EVB-Pico"]
likes: 0
views: 375
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# 2. W5100S/W5500+RP2040 Raspberry Pi Pico＜DHCP＞

> 2. W5100S/W5500+RP2040 Raspberry Pi Pico＜DHCP＞

Original author: WIZnet HK (source: https://blog.csdn.net/WIZnet2012/article/details/134043644?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) (code)
- [WIZnet offical website](https://www.wiznet.io/)
- [WIZnet Official library github](https://github.com/Wiznet/ioLibrary_Driver)

## Article

## 1 Introduction

  With the promotion and popularity of cloud computing, more and more network devices and services need to be connected to the network, which means that more IP addresses and other network configuration information are needed. The DHCP server can dynamically allocate IP addresses and other configuration information, simplifying network configuration management and improving the availability and efficiency of network equipment.

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

## 2 Introduction

### 2.1 What is DHCP?

DHCP stands for Dynamic Host Configuration Protocol, a network management protocol used to dynamically assign Internet Protocol addresses to any device or node on the network so that they can communicate using IP. DHCP automates and centrally manages these configurations, rather than requiring network administrators to manually assign IP addresses to all network devices. DHCP can be implemented on small local networks as well as large enterprise networks.

### 2.2 Why use DHCP?

In an IP network, each device connected to the Internet needs to be assigned a unique IP address. DHCP enables network administrators to monitor and assign IP addresses from a central node. When a computer is moved to another location on the network, it can automatically receive a new IP address. The automated allocation of IP addresses implemented by DHCP not only reduces the time for configuring and deploying devices, but also reduces the possibility of configuration errors. In addition, the DHCP server can manage the configuration information of multiple network segments. When the configuration of a certain network segment changes, the administrator only needs to update the relevant configuration on the DHCP server, achieving centralized management.

Overall, DHCP brings the following advantages compared to setting a static IP address:

Accurate IP configuration: IP address configuration parameters must be accurate, and it is easy to make mistakes when processing inputs like "192.168.XXX.XXX". In addition, typographical errors are often difficult to resolve, and using a DHCP server can minimize this risk.

Reduce IP address conflicts: Every connected device must have an IP address. However, each address can only be used once, and duplicate addresses will cause a conflict in which one or both devices cannot be connected. This can happen when addresses are assigned manually, especially when there are a large number of endpoints that only connect periodically (such as mobile devices). The use of DHCP ensures that each address is used only once.

Automation of IP address management: Without DHCP, network administrators would need to manually assign and revoke addresses. Keeping track of which device has what address can be a futile exercise because it's nearly impossible to understand when a device needs to access the network and when it needs to leave the network. DHCP allows it to be automated and centralized so network professionals can manage all locations from one location.

Efficient change management: The use of DHCP makes changing addresses, ranges or endpoints very simple. For example, an organization may want to change its IP addressing scheme from one range to another. The DHCP server is configured with new information, which will be propagated to the new endpoints. Likewise, if you upgrade and replace network devices, no network configuration is required.

## **2.3 How DHCP works**

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

#### General steps:

The DHCP Client sends a DHCP Discover message in broadcast mode to request an IP address.

The DHCP Server sends a DHCP Offer message to the DHCP Client, providing the IP address and other network settings.

The DHCP Client will send a broadcast DHCP Request message to the DHCP Server, which contains the IP address of the selected DHCP Server and the required IP address.

The DHCP Server responds to the DHCP Client with a DHCP ACK message and officially delivers the IP address and other network settings. At this point, the DHCP Client can use this IP address.

### 2.4 DHCP application scenarios

The application scenarios of DHCP are usually concentrated in LAN environments that require dynamic allocation of IP addresses. For example, in a large office environment or school, since there are a large number of network devices that need to be connected to the network, manually assigning and managing IP addresses for each device can be very cumbersome and error-prone. Using DHCP can centrally manage the allocation of IP addresses, improve the work efficiency of network administrators, and reduce the occurrence of errors.

## 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 DHCP network setting example overview and usage

### 4.1 Flowchart

The flowchart of the program is as follows:

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

### 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) &lt;----> USB_TTL_RX

RP2040 GPIO1 (UART0 RX) &lt;----> USB_TTL_TX

When wiring using module connection RP2040

RP2040 GPIO16 &lt;----> W5100S MISO

RP2040 GPIO17 &lt;----> W5100S CS

RP2040 GPIO18 &lt;----> W5100S SCK

RP2040 GPIO19 &lt;----> W5100S MOSI

RP2040 GPIO20 &lt;----> 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 rich 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 dhcp_client.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 initializes the serial port and SPI, and then writes the network configuration parameters of W5100S. After initializing DHCP, the main loop starts DHCP to obtain the IP. When obtained, it prints the obtained IP. When the number of acquisitions exceeds the maximum number of acquisitions, Use a static 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"
​
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
#define SOCKET_DHCP 0
#define DHCP_RETRY_COUNT 5
​
wiz_NetInfo net_info = {
    .mac = {0x00, 0x08, 0xdc, 0x16, 0xed, 0x2e}, // Define MAC variables
    .ip = {192, 168, 1, 10},                     // Define IP variables
    .sn = {255, 255, 255, 0},                    // Define subnet variables
    .gw = {192, 168, 1, 1},                      // Define gateway variables
    .dns = {8, 8, 8, 8},                         // Define DNS  variables
    .dhcp = NETINFO_DHCP};                       // Define the DNCP mode
static uint8_t dhcp_get_ip_flag = 0;             // Define the DHCP acquisition flag
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {
    0,
};
​
/*
    @brief Callback processing after triggering the timer.
    @param Timer struct.
    @return True.
 */
bool repeating_timer_callback(struct repeating_timer *t);
​
int main()
{
    wiz_NetInfo get_info;
    int dhcp_state = 0;
    int count = 0;
    struct repeating_timer timer; // Define the timer structure
​
    /*mcu init*/
    stdio_init_all();     // Initialize the main control peripheral
    wizchip_initialize(); // spi initialization
​
    /*wiznet chip init*/
    wizchip_setnetinfo(&net_info); // Write configuration information
​
    /*dhcp init*/
    add_repeating_timer_ms(1000, repeating_timer_callback, NULL, &timer); // Add  DHCP 1s Tick Timer handler
    DHCP_init(SOCKET_DHCP, ethernet_buf); // DHCP initialization
    printf("wiznet chip dhcp example start.\r\n");
​
    while (true)
    {
        dhcp_state = DHCP_run(); // Do the DHCP client
​
        switch (dhcp_state)
        {
        case DHCP_IP_LEASED: // DHCP resolves the domain name successfully
            if (dhcp_get_ip_flag == 0)
            {
                dhcp_get_ip_flag = 1;
​
                getSHAR(get_info.mac);
                getIPfromDHCP(get_info.ip);
                getGWfromDHCP(get_info.gw);
                getSNfromDHCP(get_info.sn);
                getDNSfromDHCP(get_info.dns);
                get_info.dhcp = NETINFO_DHCP;
​
                /* Network initialize */
                network_initialize(get_info); // apply from DHCP
​
                print_network_information(&get_info); // Read back the configuration information and print it
                printf(" DHCP leased time : %ld seconds\n", getDHCPLeasetime());
            }
            break;
        case DHCP_FAILED:
            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\n", count);
            }
            else if (count > DHCP_RETRY_COUNT) // If the number of times is greater than DHCP fails
            {
                printf(" DHCP failed \r\n");
​
                DHCP_stop(); // Stop processing DHCP protocol
​
                net_info.dhcp = NETINFO_STATIC;
                wizchip_setnetinfo(&net_info); // Write configuration information
​
                print_network_information(&get_info); // Read back the configuration information and print it
            }
        }
    }
}
​
bool repeating_timer_callback(struct repeating_timer *t)
{
    DHCP_time_handler(); // DHCP 1s Tick Timer handler
    return true;
}
​
```

### 4.5 Results demonstration

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

1. Open WIZ UartTool, 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, click open after filling in the parameters.

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

3. The IP obtained through PING on the PC terminal is found to be able to PING successfully, so DHCP is successful.

## 5 things to note

To obtain a dynamic IP, you must change the dhcp value in the network structure configuration to NETINFO_DHCP so that you can run DHCP 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 wizchip_conf.h header file under library/ioLibrary_Driver/Ethernet/ and modify 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.

---

Source: https://maker.wiznet.io/ronpang/projects/2-w5100s-w5500-rp2040-raspberry-pi-picodhcp/
