Hi, I faced with some problem in LwIP. I make an application with user interface with possibility to switch between static IP and DHCP. But DHCP isn't properly initialize and lwIPInit() doesn't return IP address (it returns zeroes as defaults during initialization). In some times It works correct and I receive IP from DHCP, but this case is happens absolutely randomly. Static IP initialized correctly at any request. Do you have any idea about this issue? Here is my code from lwIPInit()/* This is a peace of code from where I make a choise between static IP and DHCP*/
case MODULES_NETWORK_IP_STATIC:
/* IPaddr NetMask Gate */
//uint32_t htonl_aaddr = lwip_htonl(&network.sv_ip_addr);
ipAddr = lwIPInit(0, macAddress, network.sv_ip_addr, network.sv_ip_mask, network.sv_ip_gate, IPADDR_USE_STATIC, lwip_init_seq);
break;
case MODULES_NETWORK_IP_DHCP:
/* Initialze the lwIP library, using DHCP.*/
ipAddr = lwIPInit(0, macAddress, 0, 0, 0, IPADDR_USE_DHCP, lwip_init_seq);
break;
//////////////////////////////////////////////////////////////////////////////////////////
extern main_database_t main_database;
unsigned int lwIPInit(unsigned int instNum, unsigned char *macArray,
unsigned int ipAddr, unsigned int netMask,
unsigned int gwAddr, unsigned int ipMode, unsigned int inited)
{
struct ip_addr ip_addr;
struct ip_addr net_mask;
struct ip_addr gw_addr;
volatile unsigned char *state;
unsigned int *ipAddrPtr;
volatile unsigned int cnt = 0x3FFFFFFF;
lwip_init();
/* Setup the network address values. */
if(ipMode == IPADDR_USE_STATIC)
{
ip_addr.addr = htonl(ipAddr);
net_mask.addr = htonl(netMask);
gw_addr.addr = htonl(gwAddr);
}
else
{
ip_addr.addr = 0;
net_mask.addr = 0;
gw_addr.addr = 0;
}
hdkif_macaddrset(instNum, macArray);
/*
** Create, configure and add the Ethernet controller interface with
** default settings. ip_input should be used to send packets directly to
** the stack. The lwIP will internaly call the hdkif_init function.
*/
if(inited){
if(NULL ==
netif_add(&hdkNetIF[instNum], &ip_addr, &net_mask, &gw_addr, &instNum,
hdkif_init, ip_input))
{
return 0;
};
}else{
dhcp_stop(&hdkNetIF[instNum]);
netif_set_down(&hdkNetIF[instNum]);
netif_set_addr(&hdkNetIF[instNum], &ip_addr, &net_mask, &gw_addr);
}
netif_set_default(&hdkNetIF[instNum]);
/* Start DHCP, if enabled. */
#if LWIP_DHCP
if(ipMode == IPADDR_USE_DHCP)
{
dhcp_start(&hdkNetIF[instNum]);
}
#endif
/* Start AutoIP, if enabled and DHCP is not. */
#if LWIP_AUTOIP
if(ipMode == IPADDR_USE_AUTOIP)
{
autoip_start(&hdkNetIF[instNum]);
}
#endif
if(ipMode != IPADDR_USE_STATIC)
{
/* wait till the dynamic IP address is properly assigned */
state = &(hdkNetIF[instNum].dhcp->state);
cnt = 20;
while(*state != DHCP_BOUND){
vTaskDelay(1000);
cnt--;
if(cnt == 0)
return 0;
}
}
else
{
/* Bring the interface up */
netif_set_up(&hdkNetIF[instNum]);
}
ipAddrPtr = (unsigned int*)&(hdkNetIF[instNum].ip_addr);
main_database.sv_ip_addr = *(unsigned int*)&(hdkNetIF[instNum].ip_addr);
main_database.sv_ip_mask = *(unsigned int*)&(hdkNetIF[instNum].netmask);
main_database.sv_ip_gate = *(unsigned int*)&(hdkNetIF[instNum].gw);
return (*ipAddrPtr);
}