一、配置DHCP功能
void LwIP_Init( void )
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
lwip_init();
#if LWIP_DHCP //
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else //
IP4_ADDR(&ipaddr, 192, 168, 0, 18); //
IP4_ADDR(&netmask, 255, 255, 255, 0); //
IP4_ADDR(&gw, 192, 168, 0, 1); //
#endif
netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
netif_set_default(&enc28j60);
#if LWIP_DHCP
/* Creates a new DHCP client for this interface on the first call.
Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
the predefined regular intervals after starting the client.
You can peek in the netif->dhcp struct for the actual DHCP status.*/
dhcp_start(&enc28j60);
#endif
/* When the netif is fully configured this function must be called.*/
netif_set_up(&enc28j60); //
}
二、配置lwipopts.h文件
/* ---------- DHCP---------- */
#define LWIP_DHCP 1
#define DHCP_DOES_ARP_CHECK 1
可以选择开启调试模式
/*
----------------------------------------
---------- Lwip调试配置----------
----------------------------------------
*/
#define LWIP_DEBUG 1 //
#define TCP_DEBUG LWIP_DBG_OFF
#define DHCP_DEBUG LWIP_DBG_ON
三、定时轮询
#if LWIP_DHCP
/* Fine DHCP periodic process every 500ms */
if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
{
DHCPfineTimer = localtime;
dhcp_fine_tmr();//请求应答超时处理
}
/* DHCP Coarse periodic process every 60s */
if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS)
{
DHCPcoarseTimer = localtime;
dhcp_coarse_tmr();//地址租用情况到期处理
}
#endif
四、以上就是全部的配置,可以把模块连接到路由器上,路由器一般默认会开启DHCP功能。
图中的匿名主机就是stm32设备。进一步打开可以看到路由器分配的地址。
同时可以看到相应的效果:
在浏览器中输入192.168.0.103,同样可以访问到web服务器。
五、工程源码
http://download.csdn.net/detail/crjmail/9875450