stm32下lwip使用DHCP实现自动获取IP地址的设计
lwip_v1.4.1 首先在lwip找到
opt.h 里将 #define LWIP_ARP 1 //值1使能
/*
----------------------------------
---------- DHCP options ----------
----------------------------------
*/
/**
* LWIP_DHCP==1: Enable DHCP module.
*/
#ifndef LWIP_DHCP
#define LWIP_DHCP 1 //为1 使能
#endif
在sourceinsight 下进入定义中在lwipopts.h中也要使能。
#define LWIP_DHCP 1
//#define DHCP_DOES_ARP_CHECK 1
#define USE_DHCP 1 定义这个
初始化
ETH_BSP_Config();
LwIP_Init();
在LwIP_Init函数中 会自动完成
#ifdef USE_DHCP
ipaddr.addr = 0;
netmask.addr = 0;
gw.addr = 0;
#else
IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
#endif
#endif
/* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
struct ip_addr *netmask, struct ip_addr *gw,
void *state, err_t (* init)(struct netif *netif),
err_t (* input)(struct pbuf *p, struct netif *netif))
Adds your network interface to the netif_list. Allocate a struct
netif and pass a pointer to this structure as the first argument.
Give pointers to cleared ip_addr structures when using DHCP,
or fill them with sane numbers otherwise. The state pointer may be NULL.
The init function pointer must point to a initialization function for
your ethernet netif interface. The following code illustrates it's use.*/
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);
/* Registers the default network interface.*/
netif_set_default(&gnetif);
if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
{
/* Set Ethernet link flag */
gnetif.flags |= NETIF_FLAG_LINK_UP;
/* When the netif is fully configured this function must be called */
netif_set_up(&gnetif);
最后在netconf.c中找到
void LwIP_DHCP_Process_Handle()
{struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
uint8_t iptab[4] = {0};
uint8_t iptxt[20];
switch (DHCP_state)
{
case DHCP_START:
{
DHCP_state = DHCP_WAIT_ADDRESS;
dhcp_start(&gnetif);
/* IP address should be set to 0
every time we want to assign a new DHCP address */
IPaddress = 0;
#ifdef USE_LCD
LCD_DisplayStringLine(Line4, (uint8_t*)" Looking for ");
LCD_DisplayStringLine(Line5, (uint8_t*)" DHCP server ");
LCD_DisplayStringLine(Line6, (uint8_t*)" please wait... ");
#endif
}
break;
case DHCP_WAIT_ADDRESS:
{
/* Read the new IP address */
IPaddress = gnetif.ip_addr.addr;
if (IPaddress!=0)
{
DHCP_state = DHCP_ADDRESS_ASSIGNED;
/* Stop DHCP */
dhcp_stop(&gnetif);
#ifdef USE_LCD
iptab[0] = (uint8_t)(IPaddress >> 24);
iptab[1] = (uint8_t)(IPaddress >> 16);
iptab[2] = (uint8_t)(IPaddress >> 8);
iptab[3] = (uint8_t)(IPaddress);
sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
LCD_ClearLine(Line4);
LCD_ClearLine(Line5);
LCD_ClearLine(Line6);
/* Display the IP address */
LCD_DisplayStringLine(Line7, (uint8_t*)"IP address assigned ");
LCD_DisplayStringLine(Line8, (uint8_t*)" by a DHCP server ");
LCD_DisplayStringLine(Line9, iptxt);
#endif
STM_EVAL_LEDOn(LED1);
}
else
{
/* DHCP timeout */
if (gnetif.dhcp->tries > MAX_DHCP_TRIES)
{
DHCP_state = DHCP_TIMEOUT;
/* Stop DHCP */
dhcp_stop(&gnetif);
/* Static address used */
IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
netif_set_addr(&gnetif, &ipaddr , &netmask, &gw);
#ifdef USE_LCD
LCD_DisplayStringLine(Line7, (uint8_t*)" DHCP timeout ");
iptab[0] = IP_ADDR3;
iptab[1] = IP_ADDR2;
iptab[2] = IP_ADDR1;
iptab[3] = IP_ADDR0;
sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);
LCD_ClearLine(Line4);
LCD_ClearLine(Line5);
LCD_ClearLine(Line6);
LCD_DisplayStringLine(Line8, (uint8_t*)" Static IP address ");
LCD_DisplayStringLine(Line9, iptxt);
#endif
STM_EVAL_LEDOn(LED1);
}
}
}
break;
default: break;
}
}