Specifications

The prototype for get_mac_addr() is in the header file <iniche path><iniche path>/inc/alt_iniche_dev.h. The
NET structure is defined in the <iniche path>/src/h/net.h file.
For demonstration purposes only, the MAC address is stored at address CUSTOM_MAC_ADDR in this
example. There is no error checking in this example. In a real application, if there is an error,
get_mac_addr() must return -1.
Example 11-2: An Implementation of get_mac_addr()
#include <alt_iniche_dev.h>
#include "includes.h"
#include "ipport.h"
#include "tcpport.h"
#include <io.h>
int get_mac_addr(NET net, unsigned char mac_addr[6])
{
int ret_code = -1;
/* Read the 6-byte MAC address from wherever it is stored */
mac_addr[0] = IORD_8DIRECT(CUSTOM_MAC_ADDR, 4);
mac_addr[1] = IORD_8DIRECT(CUSTOM_MAC_ADDR, 5);
mac_addr[2] = IORD_8DIRECT(CUSTOM_MAC_ADDR, 6);
mac_addr[3] = IORD_8DIRECT(CUSTOM_MAC_ADDR, 7);
mac_addr[4] = IORD_8DIRECT(CUSTOM_MAC_ADDR, 8);
mac_addr[5] = IORD_8DIRECT(CUSTOM_MAC_ADDR, 9);
ret_code = ERR_OK;
return ret_code;
}
Prototype for get_ip_addr()
You must write the function get_ip_addr() to assign the IP address of the protocol stack. Your program
can either assign a static address, or request the DHCP to find an IP address. The function prototype for
get_ip_addr() is:
int get_ip_addr(alt_iniche_dev* p_dev,
ip_addr* ipaddr,
ip_addr* netmask,
ip_addr* gw,
int* use_dhcp);
get_ip_addr() sets the return parameters as follows:
IP4_ADDR(&ipaddr, IPADDR0,IPADDR1,IPADDR2,IPADDR3);
IP4_ADDR(&gw, GWADDR0,GWADDR1,GWADDR2,GWADDR3);
IP4_ADDR(&netmask, MSKADDR0,MSKADDR1,MSKADDR2,MSKADDR3);
For the dummy variables IP_ADDR0-3, substitute expressions for bytes 0-3 of the IP address. For
GWADDR0-3, substitute the bytes of the gateway address. For MSKADDR0-3, substitute the bytes of the
network mask. For example, the following statement sets ip_addr to IP address 137.57.136.2:
IP4_ADDR ( ip_addr, 137, 57, 136, 2 );
To enable DHCP, include the line:
*use_dhcp = 1;
The NicheStack TCP/IP stack attempts to get an IP address from the server. If the server does not provide
an IP address within 30 seconds, the stack times out and uses the default settings specified in the
IP4_ADDR() function calls.
11-6
Prototype for get_ip_addr()
NII5V2
2015.05.14
Altera Corporation
Ethernet and the NicheStack TCP/IP Stack - Nios II Edition
Send Feedback