📄 network_utilities.c
字号:
defined(ALTERA_NIOS_DEV_BOARD_STRATIX_1S10_ES) ||\
defined(ALTERA_NIOS_DEV_BOARD_STRATIX_2S60_ES) ||\
defined(ALTERA_NIOS_DEV_BOARD_STRATIX_1S40) || \
defined(ALTERA_DSP_DEV_BOARD_STRATIX_2S60_ES)
signature = IORD_32DIRECT(LAST_FLASH_SECTOR, 0);
/* This LAST_FLASH_SECTOR region of flash is examined to see if
* valid network settings are present, indicated by a signature of 0x00005afe at
* the first address of the last flash sector. This hex value is chosen as the
* signature since it looks like the english word "SAFE", meaning that it is
* safe to use these network address values.
*/
if (signature != 0x00005afe)
{
ret_code = generate_and_store_mac_addr();
}
if (ret_code == ERR_OK)
{
netif->hwaddr[0] = IORD_8DIRECT(LAST_FLASH_SECTOR, 4);
netif->hwaddr[1] = IORD_8DIRECT(LAST_FLASH_SECTOR, 5);
netif->hwaddr[2] = IORD_8DIRECT(LAST_FLASH_SECTOR, 6);
netif->hwaddr[3] = IORD_8DIRECT(LAST_FLASH_SECTOR, 7);
netif->hwaddr[4] = IORD_8DIRECT(LAST_FLASH_SECTOR, 8);
netif->hwaddr[5] = IORD_8DIRECT(LAST_FLASH_SECTOR, 9);
printf("Your Ethernet MAC address is %02x:%02x:%02x:%02x:%02x:%02x\n",
netif->hwaddr[0],
netif->hwaddr[1],
netif->hwaddr[2],
netif->hwaddr[3],
netif->hwaddr[4],
netif->hwaddr[5]);
}
#else
ret_code = ERR_IF;
perror( "Not an Altera Nios Development Board.\n");
perror( "You need to modify the function get_mac_addr \n");
perror( "to set a MAC address for your board");
#endif
return ret_code;
}
/*
* get_ip_addr()
*
* This routine is called by lwIP to obtain an IP address for the specified
* network adapter. Like the MAC address, obtaining an IP address is very
* system-dependant and therefore this function is exported for the developer
* to control.
*
* In our system, we are either attempting DHCP auto-negotiation of IP address,
* or we are setting our own static IP, Gateway, and Subnet Mask addresses our
* self. This routine is where that happens.
*/
int get_ip_addr(alt_lwip_dev* lwip_dev, struct ip_addr* ipaddr,
struct ip_addr* netmask, struct ip_addr* gw,
int* use_dhcp)
{
int ret_code = 0;
IP4_ADDR(ipaddr, IPADDR0,IPADDR1,IPADDR2,IPADDR3);
IP4_ADDR(gw, GWADDR0,GWADDR1,GWADDR2,GWADDR3);
IP4_ADDR(netmask, MSKADDR0,MSKADDR1,MSKADDR2,MSKADDR3);
*use_dhcp = 0;
printf("Static IP Address is %d.%d.%d.%d\n",
ip4_addr1(ipaddr),
ip4_addr2(ipaddr),
ip4_addr3(ipaddr),
ip4_addr4(ipaddr));
ret_code = 1;
return ret_code;
}
/*
* NETUTILSDHCPTimeoutTask()
*
* This lwIP-called routine is responsible for checking to see whether DHCP
* was successful. Here we check to see whether a timeout (120 seconds in this
* case) has elapsed. If so, display an error, assign a static IP address, and
* bail out. If not, continue checking for DHCP success and bail out once
* a DHCP address assignment is complete.
*
* This routine also displays the assigned IP address to the user (via STDOUT
* and, if equipped, LCD screen.
*
* Note: LCD_DISPLAY_NAME is defined in telnet.h to be the LCD interface for
* the "standard" and "full-featured" example designs.
*/
#if LWIP_DHCP == 1
void NETUTILSDHCPTimeoutTask()
{
int have_address = 0, timeout_dhcp = 0;
struct ip_addr ipaddr, netmask, gw;
INT8U error_code;
error_code = OS_NO_ERR;
while(1)
{
OSTimeDlyHMSM(0,0,0,100);
/*
* For demo purposes we need to use DHCP to allocate an IP Address
* and lwIP had no mechanism to tell us one is allocated. Wait two
* minutes if we don't have one we use a static IP address
*/
if((timeout_dhcp < DHCP_TIMEOUT) && (have_address == 0))
{
timeout_dhcp++;
if (!ip_addr_isany(&adapter->ip_addr))
{
have_address = 1;
printf("Assigned IP Address is %d.%d.%d.%d\n\n",
ip4_addr1(&adapter->ip_addr),
ip4_addr2(&adapter->ip_addr),
ip4_addr3(&adapter->ip_addr),
ip4_addr4(&adapter->ip_addr));
#ifdef LCD_DISPLAY_NAME
/* Clear screen */
fprintf(LCDDevice, "\x1b");
fprintf(LCDDevice, "[2J");
fprintf(LCDDevice, "DHCP IP Addr is \n %d.%d.%d.%d",
ip4_addr1(&adapter->ip_addr),
ip4_addr2(&adapter->ip_addr),
ip4_addr3(&adapter->ip_addr),
ip4_addr4(&adapter->ip_addr));
#endif /* LCD_DISPLAY_NAME */
}
}
/*
* You're in a bit of trouble if this happens, you could assign a static
* IP address but how do we chose it?
*/
if (timeout_dhcp == DHCP_TIMEOUT)
{
dhcp_stop(adapter);
IP4_ADDR(&ipaddr, IPADDR0,IPADDR1,IPADDR2,IPADDR3);
IP4_ADDR(&gw, GWADDR0,GWADDR1,GWADDR2,GWADDR3);
IP4_ADDR(&netmask, MSKADDR0,MSKADDR1,MSKADDR2,MSKADDR3);
netif_set_addr( adapter,&ipaddr, &netmask, &gw);
printf("DHCP Failed to assign an IP Address\n\n");
printf("\nUsing static address: %d.%d.%d.%d\n\n\n",
ip4_addr1(&adapter->ip_addr),
ip4_addr2(&adapter->ip_addr),
ip4_addr3(&adapter->ip_addr),
ip4_addr4(&adapter->ip_addr));
#ifdef LCD_DISPLAY_NAME
/* Clear the LCD screen */
fprintf(LCDDevice, "\x1b");
fprintf(LCDDevice, "[2J");
fprintf(LCDDevice, "DHCP Failed using\n %d.%d.%d.%d",
ip4_addr1(&adapter->ip_addr),
ip4_addr2(&adapter->ip_addr),
ip4_addr3(&adapter->ip_addr),
ip4_addr4(&adapter->ip_addr));
#endif /* LCD_DISPLAY_NAME */
have_address = 1;
}
if (have_address == 1)
{
/*
* We have an IP address this task has served it's purpose - kill it.
*/
#ifdef LCD_DISPLAY_NAME
fclose(LCDDevice);
#endif /* LCD_DISPLAY_NAME */
error_code = OSSemPost(SSSAttainedIPAddressSem);
alt_uCOSIIErrorHandler(error_code, 0);
OSTaskDel(OS_PRIO_SELF);
}
}
}
#endif /* LWIP_DHCP */
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* *
* This agreement shall be governed in all respects by the laws of the State *
* of California and by the laws of the United States of America. *
* Altera does not recommend, suggest or require that this reference design *
* file be used in conjunction or combination with any other product. *
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -