前言
最近在学习zynq中的lwip协议族,找不到很好的记笔记的地方,所以就用csdn记录一下自己的学习过程。现在对lwip不熟悉,只是把官方的lwip echo server例程跑了一下,能跑通就一点点的照着学了,笔记都是根据自己的理解写的,而且部分内容可能也只针对lwip echo server例程有效,笔记可以供有缘人参考,但不敢保证全对,有不对的地方也期待有高人指点一二。
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_40356705/article/details/136824649
一、概述
- 原型
struct netif *xemac_add(struct netif *netif,
ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw,
unsigned char *mac_ethernet_address,unsigned mac_baseaddr)
- 参数
struct netif *netif
: 一个指向网络接口结构的指针。ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw
: 分别是IP地址、子网掩码和网关地址的指针。unsigned char *mac_ethernet_address
: 以太网MAC地址的指针。unsigned mac_baseaddr
: MAC设备的基地址
- 作用
这个函数的目的是根据给定的参数和MAC设备的类型,初始化并添加一个网络接口到lwIP栈中。
二、函数体
// 定义一个指向netif结构体的指针函数,该函数用于添加网络接口
struct netif *xemac_add(struct netif *netif,
ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw,
unsigned char *mac_ethernet_address,
unsigned mac_baseaddr)
{
int i;
// 如果定义了OS_IS_FREERTOS,则执行以下代码
#ifdef OS_IS_FREERTOS
/* 启动一个线程,用于定期检测链路状态,以实现热插拔自动检测 */
sys_thread_new("link_detect_thread", link_detect_thread, netif,
THREAD_STACKSIZE, tskIDLE_PRIORITY);
#endif
// 设置网络接口的MAC地址长度
/* set mac address */
netif->hwaddr_len = 6;
// 遍历MAC地址数组,并将其赋值给网络接口结构体的hwaddr字段
for (i = 0; i < 6; i++)
netif->hwaddr[i] = mac_ethernet_address[i];
// 根据MAC类型进行初始化
/* initialize based on MAC type */
switch (find_mac_type(mac_baseaddr)) {
case xemac_type_xps_emaclite:
// 如果定义了XLWIP_CONFIG_INCLUDE_EMACLITE,则执行以下代码
#ifdef XLWIP_CONFIG_INCLUDE_EMACLITE
// 添加网络接口,并返回netif指针
return netif_add(netif, ipaddr, netmask, gw,
(void*)(UINTPTR)mac_baseaddr,
xemacliteif_init,
#if NO_SYS
// 如果没有定义系统,则使用ethernet_input作为输入函数
ethernet_input
#else
// 否则,使用tcpip_input作为输入函数
tcpip_input
#endif
);
#else
// 如果没有定义XLWIP_CONFIG_INCLUDE_EMACLITE,则返回NULL
return NULL;
#endif
case xemac_type_axi_ethernet:
// 如果定义了XLWIP_CONFIG_INCLUDE_AXI_ETHERNET,则执行以下代码
#ifdef XLWIP_CONFIG_INCLUDE_AXI_ETHERNET
// 添加网络接口,并返回netif指针
return netif_add(netif, ipaddr, netmask, gw,
(void*)(UINTPTR)mac_baseaddr,
xaxiemacif_init,
#if NO_SYS
// 如果没有定义系统,则使用ethernet_input作为输入函数
ethernet_input
#else
// 否则,使用tcpip_input作为输入函数
tcpip_input
#endif
);
#else
// 如果没有定义XLWIP_CONFIG_INCLUDE_AXI_ETHERNET,则返回NULL
return NULL;
#endif
#if defined (__arm__) || defined (__aarch64__)
case xemac_type_emacps:
// 如果定义了XLWIP_CONFIG_INCLUDE_GEM,则执行以下代码
#ifdef XLWIP_CONFIG_INCLUDE_GEM
// 添加网络接口,并返回netif指针
return netif_add(netif, ipaddr, netmask, gw,
(void*)(UINTPTR)mac_baseaddr,
xemacpsif_init,
#if NO_SYS
// 如果没有定义系统,则使用ethernet_input作为输入函数
ethernet_input
#else
// 否则,使用tcpip_input作为输入函数
tcpip_input
#endif
);
#endif
#endif
default:
// 如果无法确定EMAC的类型,则打印错误信息并返回NULL
xil_printf("unable to determine type of EMAC with baseaddress 0x%08x\r\n",
mac_baseaddr);
return NULL;
}
}
# 三、调用关系
被main调用,在echo server函数中调用的是case xemac_type_emacps下的函数