WIZnet Developer Forum

W6100 DHCP Sample code for IPV4

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.

I think that you can use the io6Libray.

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)
{
    ;
}

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

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.

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.

  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.

Hi becky,

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

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.

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

Thanks.

Yes, 3 seconds seems to be enough.

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;
	}

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.