Wiznet makers

ronpang

Published July 23, 2025 © Apache License 2.0 (Apache-2.0)

130 UCC

51 WCC

32 VAR

0 Contests

1 Followers

0 Following

Hardwired TCP/IP Chapter 2: W55MH32 DHCP Example

Hardwired TCP/IP Chapter 2: W55MH32 DHCP Example

COMPONENTS
PROJECT DESCRIPTION

Chapter 2 W55MH32 DHCP Example

In this article, we will provide a detailed explanation of the basic information, advantages, features, working principle and application scenarios of the DHCP protocol. Through practical examples, we will also explain to you how to use the W55MH32 to dynamically obtain IP information, helping readers better understand and apply this protocol.

For the initialization process of W55MH32, please refer to the relevant chapters. We will not elaborate on it here.

1 Introduction to the DHCP Protocol

DHCP (Dynamic Host Configuration Protocol) is a protocol at the application layer. It is mainly used to automatically assign IP addresses and other related network configuration parameters (such as subnet mask, default gateway, DNS server address, etc.) to clients in an IP network. This dynamic allocation method greatly simplifies the work of network administrators and can more effectively utilize the limited IP address resources.

2 Characteristics of the DHCP Protocol

  • Easy configuration and management: DHCP can automatically assign network parameters such as IP addresses, subnet masks, gateways, and DNS. Devices obtain these parameters automatically upon connecting to the network, eliminating the need for manual operation by users. Administrators can centrally manage IP allocation through the server, and when adjusting the network architecture, modifying server settings will automatically adapt the clients; static IP requires manual input and adjustment for each device, which is cumbersome and prone to errors.
  • Flexible resource utilization: DHCP dynamically allocates IP addresses. When devices go offline, their addresses are returned to the pool for reuse, which is suitable for temporary large-scale access requirements in public places and improves address utilization; it can also be flexibly allocated as needed, reserving static IP addresses for critical devices and dynamically allocating them for others. Static IP occupies a fixed position and wastes resources; its flexibility is poor.
  • Efficient maintenance and troubleshooting: DHCP automatically assigns IP addresses, avoiding manual configuration errors and IP address conflicts. The server records the allocation situation in detail. When a network failure occurs, administrators can lock the faulty device based on the server log for troubleshooting; manual configuration of static IP is prone to conflicts and lacks effective records, making the troubleshooting difficult and time-consuming.
  • Adaptation to mobility and expansion: When mobile devices cross networks, DHCP enables them to automatically obtain IP configurations without manual switching; when the network scale expands or new devices are added, DHCP automatically allocates addresses, facilitating rapid expansion. Static IP requires prior planning and is prone to address shortages and unreasonable allocation issues, and it also increases the difficulty of device movement operations.

3 The Working Principle of DHCP

The working principle of DHCP is shown in the figure below:

From the illustration, we can clearly and intuitively understand the working principle of DHCP. It generally consists of four stages:

  1. Discovery phase: When the client connects to the network, it sends a DHCP Discover message in broadcast form (with the destination IP being 255.255.255.255 and the source IP being 0.0.0.0) to search for the DHCP server. The message contains the client's MAC address. If the server and the client are not in the same subnet, the message will be forwarded through a relay agent (such as a router).
  2. Provisioning phase: After receiving the Discover message, the DHCP server selects an unallocated IP address from the IP address pool, encapsulates it along with the subnet mask, default gateway, DNS server address, etc. into a DHCP Offer message, and sends it to the client in broadcast or unicast form. There may be multiple Offer messages.
  3. Request phase: After receiving multiple Offers, the client selects one and sends a DHCP Request message in broadcast form to request the allocation of this IP address and other configuration information. It also sends an ARP request to check the uniqueness of the IP address.
  4. Confirmation phase: After receiving the Request message, the server checks if the IP address is available. If it is available, it will send a DHCP Ack message in broadcast or unicast form, and the client receives it to complete the network configuration and access the internet normally. If it is not available, it will send a DHCP Nak message, and the client receives it and restarts the Discovery process.

4 DHCP Protocol Messages

The message format of DHCP is as follows:

0~7 bit

8~15 bit

16~23 bit

24~31 bit

op(1)

htype(1)

hlen(1)

hops(1)

Xid(4)

secs(2)

flags(2)

ciaddr(4)

yiaddr(4)

siaddr(4)

giaddr(4)

chaddr(16)

sname(64)

file(128)

options(variable)

The descriptions of each field in the DHCP message are as follows:

Field

len

Meaning

op(op code)

1byte

Indicates the type of the message, with values of 1 or 2. The meanings are as follows:

1: Client request message.

2: Server response message.

htype(hardware type)

1byte

Indicates the hardware type. Different hardware types have different values. Common values include 1, which represents Ethernet (10Mb).

hlen(hardware length)

1byte

Indicates the length of the hardware address. The value for Ethernet is 6.

hops

1byte

Indicates the number of DHCP relay nodes that the DHCP message passes through. The client or server is initially set to 0. For each relay node passed through, the value is incremented by 1. This is used to limit the number of relay nodes that the DHCP message passes through. The number of relay nodes between the server and the client cannot exceed 16 (the Hops value cannot be greater than 16), otherwise the message will be discarded.

xid

4byte

Represents the random number selected by the DHCP client, enabling the DHCP server's response to be associated with the DHCP client's message.

secs(seconds)

2byte

Indicates the time taken by the client from the moment it starts obtaining the address or after the renewal of the address lease, measured in seconds.

flags

2 byte

Indicates the flag field. Only the most significant bit of the flag field is meaningful, and the remaining 15 bits are all set to 0. The most significant bit is interpreted as the unicast or broadcast response flag, as follows:

0: The client requests the server to send the response message in unicast form.

1: The client requests the server to send the response message in broadcast form.

ciaddr(client ip address)

4 byte

Indicates the IP address of the client. It can be the IP address assigned by the server to the client or the existing IP address of the client. The client does not have an IP address in the initial state, and this field is 0.0.0.0.

The IP address 0.0.0.0 is only allowed for this host to use it for temporary communication when the system adopts the DHCP method. It is not a valid destination address.

yiaddr(your client ip address)

4 byte

Indicates the IP address allocated by the server to the client. When the server responds with DHCP, fill in the IP address allocated to the client in this field.

siaddr(server ip address)

4 byte

The DHCP client obtains the IP address of the server that provides the startup configuration information.

giaddr(gateway ip address)

4 byte

Indicates the IP address of the first DHCP relay. When a client sends a DHCP request, if the server and the client are not on the same network segment, the first relay will fill in its own IP address when forwarding the request message to the server. Based on this, the server determines the client's network segment address, selects the address pool to allocate the corresponding IP address, and then sends a response message back to this relay with this address. The relay forwards it to the client. If there are multiple relays, this field serves as the client's network segment identifier. After filling in the IP address of the first relay, it will not be changed. For each relay passed through, the value of the "hops" field will increase by 1.

chaddr(client hardware address)

16 byte

Indicates the MAC address of the client. This field is consistent with the previous "hardware type" and "hardware length". When the client sends a DHCP request, it fills in its own hardware address in this field. For Ethernet, when "hardware type" and "hardware length" are respectively "1" and "6", this field must be filled with a 6-byte Ethernet MAC address.

sname(server host name)

64 byte

Indicates the name of the server from which the client obtains configuration information. This field is filled in by the DHCP server and is optional. If filled in, it must be a string ending with 0.

file(file name)

128 byte

Indicates the name of the startup configuration file that the client needs to obtain. It is filled in by the DHCP server and is distributed to the client along with the DHCP address allocation. This field is optional. If filled in, it must be a string ending with 0.

options

Variable

The option field of DHCP, with a maximum length of 1200 bytes, contains the DHCP message type and the configuration information assigned by the server to the terminal (such as gateway IP address, DNS server IP address, client IP address validity period, etc.). For details, please refer to the description in the "DHCP Options" section on the IETF official website.

Discovery message example: The client sends the DHCP discovery message via UDP broadcast. The message contains information such as the client's MAC address, hostname, and the requested IP address.

|Message parsing |

Message type: Boot Request (1) op code1Client request message

Hardware type: Ethernet (0x01)

Hardware address length: 6

Hops: 0

Transaction ID: 0xbf600589

Seconds elapsed: 0

Bootp flags: 0x0000 (Unicast)

Client IP address: 0.0.0.0

Your (client) IP address: 0.0.0.0

Next server IP address: 0.0.0.0

Relay agent IP address: 0.0.0.0

Client MAC address: HP_b1:37:11 (64:4e:d7:b1:37:11)

Client hardware address padding: 00000000000000000000

Server host name not given

Boot file name not given

Magic cookie: DHCP

Option: (53) DHCP Message Type (Discover)

Option: (61) Client identifier

Option: (50) Requested IP Address (192.168.1.115)

Option: (12) Host Name

Option: (60) Vendor class identifier

Option: (55) Parameter Request List

Option: (255) End

Padding: 0000000000000000

 

| Original message |

01 01 06 00 bf 60

05 89 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 64 4e d7 b1 37 11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 63 82 53 63 35 01 01 3d 07 01 64 4e d7 b1 37 11 32 04 c0 a8 01 73 0c 05 46 41 45 5f 33 3c 08 4d 53 46 54 20 35 2e 30 37 0e 01 03 06 0f 1f 21 2b 2c 2e 2f 77 79 f9 fc ff 00 00 00 00 00 00 00 00

Example of an Offer message: After receiving a Discover message, the DHCP server selects an unallocated IP address from the IP address pool, encapsulates it along with subnet mask, default gateway and other information into an Offer message, and broadcasts or unicasts (in this case, it is broadcast) the message to the client.

|Message parsing |

Message type: Boot Reply (2) op code2Server response message

Hardware type: Ethernet (0x01)

Hardware address length: 6

Hops: 0

Transaction ID: 0xbf600589

Seconds elapsed: 0

Bootp flags: 0x0000 (Unicast)

Client IP address: 0.0.0.0

Your (client) IP address: 192.168.1.115

Next server IP address: 0.0.0.0

Relay agent IP address: 0.0.0.0

Client MAC address: HP_b1:37:11 (64:4e:d7:b1:37:11)

Client hardware address padding: 00000000000000000000

Server host name not given

 Boot file name not given

Magic cookie: DHCP

Option: (53) DHCP Message Type (Offer)

Option: (54) DHCP Server Identifier (192.168.1.1)

 Option: (51) IP Address Lease Time

 Option: (6) Domain Name Server

Option: (1) Subnet Mask (255.255.255.0)

Option: (3) Router

Option: (255) End

| Original message |

02 01 06 00 bf 60

05 89 00 00 00 00 00 00 00 00 c0 a8 01 73 00 00 00 00 00 00 00 00 64 4e d7 b1 37 11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 63 82 53 63 35 01 02 36 04 c0 a8 01 01 33 04 00 00 1c 20 06 08 ca 60 86 21 ca 60 80 56 01 04 ff ff ff 00 03 04 c0 a8 01 01 ff

The other message information will not be presented one by one here. Interested friends can capture and view it by themselves.

5 DHCP Application Scenarios

The application scenarios of DHCP are mainly concentrated in local area network environments where IP addresses need to be dynamically allocated. For instance, in large office environments or schools, due to the large number of network devices that need to be connected to the network, manually assigning and managing IP addresses for each device would be extremely cumbersome and prone to errors. Using DHCP can centrally manage the allocation of IP addresses, enhance the efficiency of network administrators, reduce the occurrence of errors, and be adaptable to network changes.

6 Implementation Process

Next, let's take a look at how to achieve dynamic acquisition of network address information via DHCP on the W55MH32.

Note: When using DHCP to dynamically obtain IP addresses, it is necessary to change the value of dhcp in the network structure configuration to NETINFO_DHCP so that the DHCP mode can be run.

Step 1: Register the DHCP timer interrupt to the 1-second timer:

 1. /**
 2.  * @brief  1ms timer IRQ Handler
 3.  * @param  none
 4.  * @return none
 5.  */
 6. void TIM3_IRQHandler(void)
 7. {
 8.     static uint32_t tim3_1ms_count = 0;
 9.     if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
10.     {
11.         tim3_1ms_count++;
12.         if (tim3_1ms_count >= 1000)
13.         {
14.             DHCP_time_handler();
15.                                tim3_1ms_count = 0;
16.         }
17.         TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
18.     }
19. }

The registration of the DHCP timer interrupt is mainly for handling DHCP timeouts.

In the dhcp.h file, the DHCP timeout time, retry count, port number, and host name, etc. are defined:

1. #ifndef _DHCP_H_
 2. #define _DHCP_H_
 3. 
 4. #ifdef __cplusplus
 5. extern "C" {
 6. #endif
 7. 
 8. /*
 9.  * @brief 
10.  * @details If you want to display debug & processing message, Define _DHCP_DEBUG_ 
11.  * @note    If defined, it depends on <stdio.h>
12.  */
13. //#define _DHCP_DEBUG_
14. 
15. 
16. /* Retry to processing DHCP */
17. #define   MAX_DHCP_RETRY          2        ///< Maximum retry count
18. #define   DHCP_WAIT_TIME          10       ///< Wait Time 10s
19. 
20. 
21. /* UDP port numbers for DHCP */
22. #define DHCP_SERVER_PORT                            67                         ///< DHCP server port number
23. #define DHCP_CLIENT_PORT         68                      ///< DHCP client port number
24. 
25. 
26. #define MAGIC_COOKIE             0x63825363  ///< You should not modify it number.
27. 
28. #define DCHP_HOST_NAME           "WIZnet\0"

Step 2: Enable DHCP dynamic acquisition:

First, you need to change the mode of the default network address information structure to DHCP mode.

Step 3: Obtain network address information via DHCP

First, after initializing the hardware and TOE, the network_init function is called to configure the network address information.

1.     network_init(ethernet_buf, &default_net_info);

This function requires the cache array used for DHCP processing and the default network address information to be passed in. The specific content of the function is as follows:

1. /**
 2.  * @brief  set network information
 3.  *
 4.  * First determine whether to use DHCP. If DHCP is used, first obtain the Internet Protocol Address through DHCP.
 5.  * When DHCP fails, use static IP to configure network information. If static IP is used, configure network information directly
 6.  *
 7.  * @param  sn: socketid
 8.  * @param  ethernet_buff:
 9.  * @param  net_info: network information struct
10.  * @return  none
11.  */
12. void network_init(uint8_t *ethernet_buff, wiz_NetInfo *conf_info)
13. {
14.     int ret;
15.     wizchip_setnetinfo(conf_info); // Configuring Network Information
16.     if (conf_info->dhcp == NETINFO_DHCP)
17.     {
18.         ret = wiz_dhcp_process(0, ethernet_buff);
19.         if (ret == 0)
20.         {
21.             conf_info->dhcp = NETINFO_STATIC;
22.             wizchip_setnetinfo(conf_info);
23.         }
24.     }
25.     print_network_information();
26. }

In this function, the network address will first be set in the W55MH32. Then, it will check whether the mode is the DHCP mode. If it is the DHCP mode, the wiz_dhcp_process function will be used to execute the DHCP process. After successfully obtaining the network address through the DHCP method, it will be updated in the W55MH32. Finally, the network address information will be printed out. The content of the wiz_dhcp_process function is as follows:

1. /**
 2.  * @brief DHCP process
 3.  * @param sn :socket number
 4.  * @param buffer :socket buffer
 5.  */
 6. static uint8_t wiz_dhcp_process(uint8_t sn, uint8_t *buffer)
 7. {
 8.     wiz_NetInfo conf_info;
 9.     uint8_t     dhcp_run_flag = 1;
10.     uint8_t     dhcp_ok_flag  = 0;
11.     /* Registration DHCP_time_handler to 1 second timer */
12.     DHCP_init(sn, buffer);
13.     printf("DHCP running\r\n");
14.     while (1)
15.     {
16.         switch (DHCP_run())  // Do the DHCP client
17.         {
18.         case DHCP_IP_LEASED: // DHCP Acquiring network information successfully
19.        
20.             if (dhcp_ok_flag == 0)
21.             {
22.                 dhcp_ok_flag  = 1;
23.                 dhcp_run_flag = 0;
24.             }
25.             break;
26.        
27.         case DHCP_FAILED: 
28.             dhcp_run_flag = 0;
29.             break;
30.        
31.         }
32.         if (dhcp_run_flag == 0)
33.         {
34.             printf("DHCP %s!\r\n", dhcp_ok_flag ? "success" : "fail");
35.             DHCP_stop();
36. 
37.             if (dhcp_ok_flag)
38.             {
39.                 getIPfromDHCP(conf_info.ip);
40.                 getGWfromDHCP(conf_info.gw);
41.                 getSNfromDHCP(conf_info.sn);
42.                 getDNSfromDHCP(conf_info.dns);
43.                 conf_info.dhcp = NETINFO_DHCP;
44.                 getSHAR(conf_info.mac);
45.                 wizchip_setnetinfo(&conf_info); // Update network information to network information obtained by DHCP
46.                 return 1;
47.             }
48.             return 0;
49.         }
50.     }
51. }

In this function body, the DHCP_init function will be called first to initialize the DHCP configuration:

1. void DHCP_init(uint8_t s, uint8_t * buf)
 2. {
 3.    uint8_t zeroip[4] = {0,0,0,0};
 4.    getSHAR(DHCP_CHADDR);
 5.    if((DHCP_CHADDR[0] | DHCP_CHADDR[1]  | DHCP_CHADDR[2] | DHCP_CHADDR[3] | DHCP_CHADDR[4] | DHCP_CHADDR[5]) == 0x00)
 6.    {
 7.       // assigning temporary mac address, you should be set SHAR before call this function. 
 8.       DHCP_CHADDR[0] = 0x00;
 9.       DHCP_CHADDR[1] = 0x08;
10.       DHCP_CHADDR[2] = 0xdc;     
11.       DHCP_CHADDR[3] = 0x00;
12.       DHCP_CHADDR[4] = 0x00;
13.       DHCP_CHADDR[5] = 0x00; 
14.       setSHAR(DHCP_CHADDR);     
15.    }
16.  
17.                  DHCP_SOCKET = s; // SOCK_DHCP
18.                  pDHCPMSG = (RIP_MSG*)buf;
19.                  DHCP_XID = 0x12345678;
20.                  {
21.                                        DHCP_XID += DHCP_CHADDR[3];
22.                                        DHCP_XID += DHCP_CHADDR[4];
23.                                        DHCP_XID += DHCP_CHADDR[5];
24.                                        DHCP_XID += (DHCP_CHADDR[3] ^ DHCP_CHADDR[4] ^ DHCP_CHADDR[5]);
25.                  }
26.                  // WIZchip Netinfo Clear
27.                  setSIPR(zeroip);
28.                  setGAR(zeroip);
29.  
30.                  reset_DHCP_timeout();
31.                  dhcp_state = STATE_DHCP_INIT;
32. }

Then, the DHCP_run function is executed within the DHCP main loop. Its main function is to generate DHCP packets, send discovery, request, etc. messages, parse the contents such as the provision and response of the server, and handle timeouts. Here, only corresponding processing needs to be carried out based on the return value of the DHCP_run function. The content of the DHCP_run function is as follows:

  1. uint8_t DHCP_run(void)
  2. {
  3.                  uint8_t  type;
  4.                  uint8_t  ret;
  5.  
  6.                  if(dhcp_state == STATE_DHCP_STOP) return DHCP_STOPPED;
  7.  
  8.                  if(getSn_SR(DHCP_SOCKET) != SOCK_UDP)
  9.                     socket(DHCP_SOCKET, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00);
 10.  
 11.                 ret = DHCP_RUNNING;
 12.                 type = parseDHCPMSG();
 13.  
 14.                 switch ( dhcp_state ) {
 15.                    case STATE_DHCP_INIT     :
 16.          DHCP_allocated_ip[0] = 0;
 17.          DHCP_allocated_ip[1] = 0;
 18.          DHCP_allocated_ip[2] = 0;
 19.          DHCP_allocated_ip[3] = 0;
 20.                                       send_DHCP_DISCOVER();
 21.                                       dhcp_state = STATE_DHCP_DISCOVER;
 22.                                       break;
 23.                                       case STATE_DHCP_DISCOVER :
 24.                                                              if (type == DHCP_OFFER){
 25. #ifdef _DHCP_DEBUG_
 26.                                                                                     printf("> Receive DHCP_OFFER\r\n");
 27. #endif
 28.             DHCP_allocated_ip[0] = pDHCPMSG->yiaddr[0];
 29.             DHCP_allocated_ip[1] = pDHCPMSG->yiaddr[1];
 30.             DHCP_allocated_ip[2] = pDHCPMSG->yiaddr[2];
 31.             DHCP_allocated_ip[3] = pDHCPMSG->yiaddr[3];
 32.  
 33.                                                                                     send_DHCP_REQUEST();
 34.                                                                                     dhcp_state = STATE_DHCP_REQUEST;
 35.                                                              } else ret = check_DHCP_timeout();
 36.          break;
 37.  
 38.                                       case STATE_DHCP_REQUEST :
 39.                                                              if (type == DHCP_ACK) {
 40.  
 41. #ifdef _DHCP_DEBUG_
 42.                                                                                     printf("> Receive DHCP_ACK\r\n");
 43. #endif
 44.                                                                                     if (check_DHCP_leasedIP()) {
 45.                                                                                                            // Network info assignment from DHCP
 46.                                                                                                            dhcp_ip_assign();
 47.                                                                                                            reset_DHCP_timeout();
 48.  
 49.                                                                                                            dhcp_state = STATE_DHCP_LEASED;
 50.                                                                                     } else {
 51.                                                                                                            // IP address conflict occurred
 52.                                                                                                            reset_DHCP_timeout();
 53.                                                                                                            dhcp_ip_conflict();
 54.                                                                                         dhcp_state = STATE_DHCP_INIT;
 55.                                                                                     }
 56.                                                              } else if (type == DHCP_NAK) {
 57.  
 58. #ifdef _DHCP_DEBUG_
 59.                                                                                     printf("> Receive DHCP_NACK\r\n");
 60. #endif
 61.  
 62.                                                                                     reset_DHCP_timeout();
 63.  
 64.                                                                                     dhcp_state = STATE_DHCP_DISCOVER;
 65.                                                              } else ret = check_DHCP_timeout();
 66.                                       break;
 67.  
 68.                                       case STATE_DHCP_LEASED :
 69.                                          ret = DHCP_IP_LEASED;
 70.                                                              if ((dhcp_lease_time != INFINITE_LEASETIME) && ((dhcp_lease_time/2) < dhcp_tick_1s)) {
 71.                                                                                     
 72. #ifdef _DHCP_DEBUG_
 73.                                                                                     printf("> Maintains the IP address \r\n");
 74. #endif
 75.  
 76.                                                                                     type = 0;
 77.                                                                                     OLD_allocated_ip[0] = DHCP_allocated_ip[0];
 78.                                                                                     OLD_allocated_ip[1] = DHCP_allocated_ip[1];
 79.                                                                                     OLD_allocated_ip[2] = DHCP_allocated_ip[2];
 80.                                                                                     OLD_allocated_ip[3] = DHCP_allocated_ip[3];
 81.                                                                                     
 82.                                                                                     DHCP_XID++;
 83.  
 84.                                                                                     send_DHCP_REQUEST();
 85.  
 86.                                                                                     reset_DHCP_timeout();
 87.  
 88.                                                                                     dhcp_state = STATE_DHCP_REREQUEST;
 89.                                                              }
 90.                                       break;
 91.  
 92.                                       case STATE_DHCP_REREQUEST :
 93.                                          ret = DHCP_IP_LEASED;
 94.                                                              if (type == DHCP_ACK) {
 95.                                                                                     dhcp_retry_count = 0;
 96.                                                                                     if (OLD_allocated_ip[0] != DHCP_allocated_ip[0] || 
 97.                                                                                         OLD_allocated_ip[1] != DHCP_allocated_ip[1] ||
 98.                                                                                         OLD_allocated_ip[2] != DHCP_allocated_ip[2] ||
 99.                                                                                         OLD_allocated_ip[3] != DHCP_allocated_ip[3]) 
100.                                                                                    {
101.                                                                                                           ret = DHCP_IP_CHANGED;
102.                                                                                                           dhcp_ip_update();
103.                #ifdef _DHCP_DEBUG_
104.                   printf(">IP changed.\r\n");
105.                #endif
106.                                                                                                           
107.                                                                                    }
108.          #ifdef _DHCP_DEBUG_
109.             else printf(">IP is continued.\r\n");
110.          #endif                                                                                     
111.                                                                                    reset_DHCP_timeout();
112.                                                                                    dhcp_state = STATE_DHCP_LEASED;
113.                                                             } else if (type == DHCP_NAK) {
114.  
115. #ifdef _DHCP_DEBUG_
116.                                                                                    printf("> Receive DHCP_NACK, Failed to maintain ip\r\n");
117. #endif
118.  
119.                                                                                    reset_DHCP_timeout();
120.  
121.                                                                                    dhcp_state = STATE_DHCP_DISCOVER;
122.                                                             } else ret = check_DHCP_timeout();
123.                                       break;
124.                                      default :
125.                                      break;
126.                }
127.  
128.                return ret;
129. }

8 Operating Results

After the burning routine was executed, it first printed the results of the PHY link detection and the DHCP log information, then printed the network address information. Here, it can be seen that the configuration method was DHCP, the IP address was 192.168.1.117, and finally there was a PING prompt message.

Then, on the PC side, open CMD and PING the IP address of W55MH32, and it can be successfully PINGed.

7 Summary

This article introduces the DHCP protocol, covering its functions of automatically allocating parameters in IP networks, the convenience of configuration, working principle, message format, and application scenarios. Through the W55MH32 practical example, it demonstrates the process of dynamically obtaining network address information, including steps such as registration timer interrupt, enabling mode, and obtaining information. After burning, it can complete detection and information printing, and the PC end can ping the device.

The next article will explain how to implement the TCP client mode on the W55MH32 chip, analyze the core principle and application of TCP client connecting to the server for loopback testing, and at the same time, through the practical example, explain the specific implementation steps and key points. Please stay tuned!

 

 

WIZnet is a non-fabrication semiconductor company founded in 1998. Its products include the Internet processor iMCU™, which adopts TOE (TCP/IP Offloading Engine) technology and is based on a unique patented fully hardwired TCP/IP. iMCU™ is designed for embedded Internet devices in various applications.

WIZnet has over 70 distributors worldwide, with offices in Hong Kong, South Korea, and the United States, providing technical support and product marketing.

The region managed by the Hong Kong office includes: Australia, India, Turkey, and Asia (excluding South Korea and Japan).

Documents
Comments Write