📄 network_utilities.c
字号:
/******************************************************************************
* Copyright ?2004 Altera Corporation, San Jose, California, USA. *
* All rights reserved. All use of this software and documentation is *
* subject to the License Agreement located at the end of this file below. *
*******************************************************************************
* Author - S. O'Reilly *
* Date - July 4, 2004 *
* Module - network_utilities.c *
* *
* Based on Telnet example by JRK and lwIP & HTTP server ports by PRR *
******************************************************************************/
/*
* Simple Socket Server (SSS) example.
*
* Please refer to the Nios II lwIP Tutorial documentation for details on this
* software example, as well as details on how to configure the lwIP TCP/IP
* networking stack and MicroC/OS-II Real-Time Operating System. The Nios II
* lwIP Tutorial, along with the rest of the Nios II documentation is published
* on the Altera web-site. See:
* Start Menu -> Programs -> Nios II Development Kit -> Nios II Documentation.
*/
/*
* Include files:
*
* <stdio.h>: Standard IO
* includes.h: This is the default MicroC/OS-II include file.
* alt_lwip_dev.h: Defines lwIP stack initialization calls.
* alt_types.h: Nios II CPU data type definitions.
* io.h: Nios II CPU I/O access macro definitions.
* simple_socket_server.h: Our simple socket server app's declarations.
* alt_error_handler.h: Altera Error Handler suite of development error
* handling functions.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <alt_lwip_dev.h>
#include <alt_types.h>
#include <sys/alt_flash.h>
#include "includes.h"
#include "io.h"
#include "simple_socket_server.h"
#include "alt_error_handler.h"
/*
* The "adapter" storage here is declared so that when lwIP calls the
* get_ip_addr() routine, the adapter struct will be populated with the
* network device (Ethernet MAC) that lwIP is currently trying to setup an IP
* address for. This is later used in the dhcp_timeout_task() routine to
* determine if DHCP was successful for the particular adapter.
*/
struct netif* adapter;
/*
* Handle to our Nios Development board LCD panel.
*/
FILE *LCDDevice;
/*
* generate_and_store_mac_addr()
*
* This routine is called when, upon program initialization, we discover
* that there is no valid network settings (including MAC address) programmed
* into flash memory at the last flash sector. If it is not safe to use the
* contents of this last sector of flash, the user is prompted to
* enter the serial number at the console. A MAC address is then
* generated using 0xFF followed by the last 2 bytes of the serial number
* appended to Altera's Vendor ID, an assigned MAC address range with the first
* 3 bytes of 00:07:ED. For example, if the Nios Development Board serial
* number is 040800017, the corresponding ethernet number generated will be
* 00:07:ED:FF:8F:11.
*
* It should be noted that this number, while unique, will likely differ from
* the also unique (but now lost forever) MAC address programmed into the
* development board on the production line.
*
* As we are erasing the entire flash sector, we'll re-program it with not
* only the MAC address, but static IP, subnet, gateway, and "Use DHCP"
* sections. These fail-safe static settings are compatible with previous
* Nios Ethernet designs, and allow the "factory-safe" design to behave
* as expected if the last flash sector is erased.
*/
err_t generate_and_store_mac_addr()
{
err_t ret_code = ERR_IF;
alt_u32 ser_num = 0;
char serial_number[9], flash_content[32];
alt_flash_fd* flash_handle;
int i = 0;
printf("Can't read the MAC address from your board (this probably means\n");
printf("that your flash was erased). We will assign you a MAC address and\n");
printf("static network settings\n\n");
while(!ser_num)
{
printf("Please enter your 9-digit serial number. This is printed on a \n");
printf("label under your Nios dev. board. The first 3 digits of the \n");
printf("label are ASJ and the serial number follows this.\n -->");
for(i=0; i<9; i++)
{
serial_number[i] = getchar();
putchar(serial_number[i]);
/* Handle backspaces. How civilized. */
if ((serial_number[i] == 0x08) && (i >= 0))
{
i--;
}
}
printf("\n");
for(i=0; i<9; i++)
{
if (isdigit(serial_number[i]))
{
ser_num *= 10;
ser_num += serial_number[i] - '0';
}
else
{
ser_num = 0;
printf("Serial number only contains decimal digits and is non-zero\n");
break;
}
}
if (ser_num)
{
/* This says the image is safe */
flash_content[0] = 0xfe;
flash_content[1] = 0x5a;
flash_content[2] = 0x0;
flash_content[3] = 0x0;
/* This is the Altera Vendor ID */
flash_content[4] = 0x0;
flash_content[5] = 0x7;
flash_content[6] = 0xed;
/* Reserverd Board identifier for erase boards */
flash_content[7] = 0xFF;
flash_content[8] = (ser_num & 0xff00) >> 8;
flash_content[9] = ser_num & 0xff;
/* Then comes a 16-bit "flags" field (not used with lwIP) */
flash_content[10] = 0xFF;
flash_content[11] = 0xFF;
/* Then comes the static IP address */
flash_content[12] = IPADDR0;
flash_content[13] = IPADDR1;
flash_content[14] = IPADDR2;
flash_content[15] = IPADDR3;
/* Then comes the static nameserver address (not used with lwIP) */
flash_content[16] = 0xFF;
flash_content[17] = 0xFF;
flash_content[18] = 0xFF;
flash_content[19] = 0xFF;
/* Then comes the static subnet mask */
flash_content[20] = MSKADDR0;
flash_content[21] = MSKADDR1;
flash_content[22] = MSKADDR2;
flash_content[23] = MSKADDR3;
/* Then comes the static gateway address */
flash_content[24] = GWADDR0;
flash_content[25] = GWADDR1;
flash_content[26] = GWADDR2;
flash_content[27] = GWADDR3;
/* And finally whether to use DHCP - set all bits to be safe */
flash_content[28] = 0xFF;
flash_content[29] = 0xFF;
flash_content[30] = 0xFF;
flash_content[31] = 0xFF;
/* Write the MAC address to flash */
/*
flash_handle = alt_flash_open_dev(EXT_FLASH_NAME);
if (flash_handle)
{
alt_write_flash(flash_handle, LAST_SECTOR_OFFSET, flash_content, 32);
alt_flash_close_dev(flash_handle);
ret_code = ERR_OK;
}
*/
}
}
return ret_code;
}
/*
* get_mac_addr
*
* Read the MAC address in a board specific way
*
*/
err_t get_mac_addr(alt_lwip_dev* lwip_dev)
{
err_t ret_code = ERR_OK;
alt_u32 signature;
struct netif* netif = lwip_dev->netif;
netif->hwaddr[0] = 0x00;
netif->hwaddr[1] = 0x88;
netif->hwaddr[2] = 0x88;
netif->hwaddr[3] = 0x5b;
netif->hwaddr[4] = 0x93;
netif->hwaddr[5] = 0x31;
return ret_code;
#if defined(ALTERA_NIOS_EVAL_BOARD_CYCLONE_1C12) ||\
defined(ALTERA_NIOS_DEV_BOARD_CYCLONE_2C35) ||\
defined(ALTERA_NIOS_DEV_BOARD_STRATIX_1S10) ||\
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -