[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [lwip-users] Problem with use DHCP and IP fixed
From: |
Diecol |
Subject: |
RE: [lwip-users] Problem with use DHCP and IP fixed |
Date: |
Wed, 22 Dec 2010 03:07:16 -0800 (PST) |
Hi,
I saw in other foruns and I try to do like theses... but didn't work too!
If I'm doing anythings wrong in my code, please tell me..
I set LWIP_DHCP 1 and LWIP_UDP 1
And in another .c I set when I want to change to Static IP (LWIP_DHCP_ON =
dhcp_off) or DHCP (LWIP_DHCP_ON = dhcp_on).
look my main.c:
struct ip_addr ipaddr, netmask, gw;
struct netif NetIf, *netif;
u8_t dhcp_state = DHCP_INIT;
Dm9161 *pDm = &gDm9161;
unsigned int errCount = 0;
#if !defined(BOARD_EMAC_POWER_ALWAYS_ON)
// clear PHY power down mode
PIO_Configure(emacPwrDn, 1);
#endif
// Init EMAC driver structure
EMAC_Init(AT91C_ID_EMAC, MacAddress, EMAC_CAF_ENABLE, EMAC_NBC_DISABLE);
// Init DM9161 driver
DM9161_Init(pDm, EMAC_PHY_ADDR);
// Setup EMAC buffers and interrupts
AIC_ConfigureIT(AT91C_ID_EMAC, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
ISR_Emac);
AIC_EnableIT(AT91C_ID_EMAC);
// Initialize system timing
sys_init_timing();
//Hardware Configuration
Hard_Init();
// Initialize lwIP modules
lwip_init();
// Initialize net interface for lwIP
emacif_setmac((u8_t*)MacAddress);
// PHY initialize
if (!DM9161_InitPhy(pDm, BOARD_MCK,
emacRstPins, PIO_LISTSIZE(emacRstPins),
emacPins, PIO_LISTSIZE(emacPins))) {
printf("P: PHY Initialize ERROR!\n\r");
return -1;
}
// Auto Negotiate
if (!DM9161_AutoNegotiate(pDm)) {
printf("P: Auto Negotiate ERROR!\n\r");
return -1;
}
while( DM9161_GetLinkSpeed(pDm, 1) == 0 ) {
errCount++;
}
printf("P: Link detected 0x%X\n\r", errCount);
IP4_ADDR(&gw, new_gateway[0], new_gateway[1], new_gateway[2],
new_gateway[3]);
IP4_ADDR(&ipaddr, new_ip[0], new_ip[1], new_ip[2], new_ip[3]);
IP4_ADDR(&netmask, new_mask[0], new_mask[1], new_mask[2], new_mask[3]);
netif = netif_add(&NetIf, &ipaddr, &netmask, &gw, NULL, emacif_init,
ip_input);
netif_set_default(netif);
netif_set_up(netif);
// Initialize http server application
if (ERR_OK != httpd_init()) {
return -1;
}
while(1) {
// Run periodic tasks
timers_update();
// Run polling tasks
emacif_poll(netif);
if (LWIP_DHCP_ON == dhcp_on){
struct dhcp *dhcp = netif->dhcp;
IP4_ADDR(&gw, 0, 0, 0, 0);
IP4_ADDR(&ipaddr, 0, 0, 0, 0);
IP4_ADDR(&netmask, 0, 0, 0, 0);
netif_set_down(netif);
dhcp_stop(netif);
netif_set_addr(netif, &ipaddr, &netmask, &gw);
dhcp_start(netif);
// Dump DHCP addresses
if (netif->dhcp) {
u8_t tmp_state = netif->dhcp->state;
if (dhcp_state != 6){
printf("DHCP: %d -> %d\n\r",
dhcp_state, tmp_state);
}
if (tmp_state != dhcp_state) {
netif->dhcp->state = DHCP_BOUND;
if (tmp_state == DHCP_BOUND) {
printf("entrou7777");
u8_t * IP;
u8_t * Mask;
u8_t * GTW;
printf("\n\r");
printf("=== DHCP Configurations
===\n\r");
IP = (u8_t*)&netif->ip_addr;
Mask = (u8_t*)&netif->netmask;
GTW = (u8_t*)&netif->gw;
memcpy(dhcp_ip, IP, 4);
printf("- DHCP IP:
%d.%d.%d.%d\n\r",
dhcp_ip[0], dhcp_ip[1],
dhcp_ip[2], dhcp_ip[3]);
memcpy(dhcp_mask, Mask, 4);
printf("- DHCP Mask:
%d.%d.%d.%d\n\r",
dhcp_mask[0], dhcp_mask[1],
dhcp_mask[2], dhcp_mask[3]);
memcpy(dhcp_gtw, GTW, 4);
printf("- DHCP Gateway:
%d.%d.%d.%d\n\r",
dhcp_gtw[0], dhcp_gtw[1],
dhcp_gtw[2], dhcp_gtw[3]);
printf("===========================\n\r\n");
LWIP_DHCP_ON = dhcp_wait;
}
dhcp_state = tmp_state;
}
}
}
else if(LWIP_DHCP_ON == dhcp_off){
dhcp_release(netif);
dhcp_stop(netif);
Read_NetConfig(&dhcpstate, new_ip, new_mask,
new_gateway, host);
IP4_ADDR(&gw, new_gateway[0], new_gateway[1],
new_gateway[2],
new_gateway[3]);
IP4_ADDR(&ipaddr, new_ip[0], new_ip[1], new_ip[2],
new_ip[3]);
IP4_ADDR(&netmask, new_mask[0], new_mask[1],
new_mask[2], new_mask[3]);
netif_set_addr(netif, &ipaddr, &netmask, &gw);
netif_set_up(netif);
LWIP_DHCP_ON = dhcp_wait;
}
}
return 0;
}
Thank you
Diego
--
View this message in context:
http://old.nabble.com/Problem-with-use-DHCP-and-IP-fixed-tp30500330p30513410.html
Sent from the lwip-users mailing list archive at Nabble.com.