---
title: "W6100 DHCP Sample code for IPV4"
url: "https://maker.wiznet.io/forum/13552"
markdown_url: "https://maker.wiznet.io/forum/13552/md"
type: "Forum topic"
category: "Ethernet Chips"
author: "embeddedcoder"
created: "2019-10-27T17:46:22+09:00"
last_activity: "2020-01-03T15:37:19+09:00"
language: "en"
views: 2246
replies: 8
source: "WIZnet Makers forum (https://maker.wiznet.io/forum/)"
---

# W6100 DHCP Sample code for IPV4

## Question

Asked by embeddedcoder on 2019-10-27 in Ethernet Chips.

Hi Team,

I am using W6100 ethernet chip with STM32 controller through SPI inorder to connect to cloud. I want to use W6100 to establish connectivity using IPV4. W6100EVB-AddressAutoConfiguration repo has only DHCP code for IPV6. So for that can some provide me sample code for DHCP IPV4 and sample code to ping one of the IP over the internet using IPV4. Using STM32CubeIDE for development.

Thanks.

## Replies

### Reply 1 by becky, 2019-10-28

I think that you can use the io6Libray.

![](https://github.githubassets.com/favicons/favicon.svg) [github.com](https://github.com/Wiznet/io6Library/tree/master/Internet/DHCP4)

### [io6Library/Internet/DHCP4 at master · Wiznet/io6Library](https://github.com/Wiznet/io6Library/tree/master/Internet/DHCP4)

[master/Internet/DHCP4](https://github.com/Wiznet/io6Library/tree/master/Internet/DHCP4)

io6Library is the integrated library for dual TCP/IP statck contorller such as W6100.

And call this code in main().

```
DHCPv4_init(0, test_buf);
reg_dhcpv4_cbfunc(dhcpv4_assign, dhcpv4_update, dhcpv4_conflict);
if (gWIZNETINFO.ipmode == NETINFO_DHCP_V4) {       // CHEP DHCP
	printf("Start DHCP\r\n");
	while (1) {
		ret = DHCPv4_run();

		if (ret == DHCP_IPV4_LEASED) {
			printf("DHCP Success\r\n");
			break;
		}
		 else if (ret == DHCPV4_FAILED) {
			 dhcp_retry++;
		}

		if (dhcp_retry > 3) {
			printf("DHCP Fail\r\n");
			break;
		}
	}
}
ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);
```

Callback function is same as below.

```
void dhcpv4_assign(void)
{
    getIPfromDHCPv4(gWIZNETINFO.ip);
    getGWfromDHCPv4(gWIZNETINFO.gw);
    getSNfromDHCPv4(gWIZNETINFO.sn);
    getDNSfromDHCPv4(gWIZNETINFO.dns);

    ctlnetwork(CN_SET_NETINFO, (void*) &gWIZNETINFO);
}

void dhcpv4_update(void)
{
    ;
}


void dhcpv4_conflict(void)
{
    ;
}
```

### Reply 2 by embeddedcoder, 2019-10-28 (in reply to reply 1)

Hi Becky,

Thanks for a quick response. As you suggested I have integrated the above code in my STM32 code I not getting a response.

I am using W6100 Ethernet Shield with STM32discovery board. I am using SPI interface for communication between them. I have able to read CIDR and VER register of W6100 chip. But somehow not able to get IP using DHCP. Here I am attaching my project code [W6100_Ping_Test.zip](https://maker.wiznet.io/forum/legacyFile.asp?path=cRYI0br4wYQOI7XSbDw1zZEENqp.zip) (3.5 MB) and logs which we are getting.

Debug Logs:

Starting W6100 Chip
Register value after W6100 initialize!
Starting DHCP check
Start DHCP

> Send DHCP_DISCOVER

Also, one more observation that I have connected the ethernet cable to RJ45 port of the W6100 Ethernet Shield and another end of it connected to a modem. After connection, there is no LEDs blinking on either of those.

Thanks in advance for the help.

### Reply 3 by Eugeny, 2019-10-28

![](https://avatars.discourse-cdn.com/v4/letter/e/b38774/48.png) embeddedcoder:

> After connection, there is no LEDs blinking on either of those.

No LEDs blinking **and** turned on?

Did you turn both devices on?
Are both operational?

### Reply 4 by embeddedcoder, 2019-10-28

Eugeny:

> No LEDs blinking **and** turned on?
>
> Did you turn both devices on?
> Are both operational?

Yes, both devices are operational. I am able to read CIDR and VER register of the W6100 chip. Both device is able to communicate properly over SPI. But LEDs on the RJ-45 connector are off.

### Reply 5 by embeddedcoder, 2019-10-30

Hi Becky,

Please help us on this. We are still facing the same issue. The code got stuck after Send DHCP_DISCOVER. Please suggest that what will be the next steps to run this code as described.

Thank you.

### Reply 6 by becky, 2019-10-30

1. It is the case that DHCP cannot check DISCOVER message because DISCOVER message is sent before Phy link is established.
   You can solve this by adding a delay before the DHCP process or adding code to check the PHY LInk.

2. The DHCP server cannot be found. You should check your network environment again.

In addition I hope you try to ping the w6100 from your PC after setting IP to static.

### Reply 7 by embeddedcoder, 2019-11-01

Hi becky,

becky:

> You can solve this by adding a delay before the DHCP process

So how much delay I should add before the DHCP process? Should I add that delay before DHCPv4_run() API?

becky:

> adding code to check the PHY LInk.

I need to configure the PHY in PHY_MODE_AUTONEGO mode. Correct? And what will be max timeout for PHY Link up? Based on that I will add logic in code for timeout before called off DHCP process.

becky:

> I hope you try to ping the w6100 from your PC after setting IP to static.

Sorry, but I didn’t get ur point here. Will you please give more details on this.

Thanks.

### Reply 8 by becky, 2019-11-04

![](https://avatars.discourse-cdn.com/v4/letter/e/b38774/48.png) embeddedcoder:

> So how much delay I should add before the DHCP process? Should I add that delay before DHCPv4_run() API?

Yes, 3 seconds seems to be enough.

![](https://avatars.discourse-cdn.com/v4/letter/e/b38774/48.png) embeddedcoder:

> I need to configure the PHY in PHY_MODE_AUTONEGO mode. Correct? And what will be max timeout for PHY Link up? Based on that I will add logic in code for timeout before called off DHCP process.

What I mean that was to add the following code:

```
/* Wait Phy Link */
	while(1){
        ctlwizchip(CW_GET_PHYLINK, &tmp1 );
        ctlwizchip(CW_GET_PHYLINK, &tmp2 );
        if(tmp1==PHY_LINK_ON && tmp2==PHY_LINK_ON) break;
	}
```

---

Source: https://maker.wiznet.io/forum/13552
