network_utilities.c
来自「91c111芯片的网络模块的原理图以及和ep1c6fpga连线相关的例子程序」· C语言 代码 · 共 482 行 · 第 1/2 页
C
482 行
/******************************************************************************* 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 - PRR/JRK ** ** File: network_utilities.c ** ** This file contains network utilities that work in conjunction with the LWIP ** stack to bring up a design's IP address (using DHCP if available) and MAC ** address. ** ** Please refer to file readme.txt for notes on this software example. *******************************************************************************/#include <stdio.h> #include <ctype.h>#include <string.h>#include "alt_lwip_dev.h"#include "alt_types.h"#include "sys/alt_flash.h"#include "includes.h"#include "io.h"#include "user.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;/* * MicroC/OS-II semaphores are used to signal when an IP address has been * aquired (declared in web_serer.c), and when the LCD may be opened for * write. */extern OS_EVENT *attained_ip_address_sem;#ifdef LCD_DISPLAY_NAME OS_EVENT *lcd_sem;/* File handle for writing to LCD display. Defined in web_server.c */extern FILE* lcdDevice;#endif /* LCD_DISPLAY_NAME *//* * die_with_error() * * This routine is just called when a blocking error occurs with the example * design. It deletes the current task. */void die_with_error(char err_msg[DIE_WITH_ERROR_BUFFER]){ printf("\n%s\n", err_msg); OSTaskDel(OS_PRIO_SELF); while(1);}/* * 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. The user is prompted for their Nios development board's * serial number, and a unique MAC address within the Altera pool generated * based on that number. * * 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(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. This routine must be provided * by the LWIP user as each board will have its MAC address stored in a * different 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; /* * Our boards have the MAC address stored in the last sector of flash. The * format is in byte order: fe 5a 00 00 <6-byte MAC Address> */#if defined(ALTERA_NIOS_EVAL_BOARD_CYCLONE_1C12) || \ defined(ALTERA_NIOS_DEV_BOARD_STRATIX_1S10) || \ defined(ALTERA_NIOS_DEV_BOARD_STRATIX_1S10_ES) || \ defined(ALTERA_NIOS_DEV_BOARD_STRATIX_2S60_ES) || \ defined(ALTERA_NIOS_DEV_BOARD_STRATIX_2S60) || \ defined(ALTERA_NIOS_DEV_BOARD_STRATIX_1S40) || \ defined(ALTERA_DSP_DEV_BOARD_STRATIX_2S60_ES) || \ defined(ALTERA_NIOS_DEV_BOARD_CYCLONE_2C35) signature = IORD_32DIRECT(LAST_FLASH_SECTOR, 0); 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]);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?