9.4. 有线网络
本章节讲解如何在 zx-rtt 系统中配置有线网络,需要配置的主要有两部分:
配置 GMAC 接口;
配置 LwIP 协议栈;
9.4.1. 配置 GMAC 接口
使用 GMAC 接口前,需将 对应 GMAC 接口选中,以 ZXM3C7D000M4R64 开发板为例,使用 scons --meunconfig
命令进行配置:
Board options --->
[*] Using Gmac0
gmac0 parameter --->
(PE.6) gmac0 phy reset gpio # 原理图中的 ETH0_RST 引脚
Select gmac0 bus (RMII) --->
Select phy clk (external clk) --->
(0) gmac0 rx delay (NEW)
(0) gmac0 tx delay (NEW)
(192.168.10.200) gmac0 ip addr (NEW)
(192.168.10.1) gmac0 gateway addr (NEW)
(255.255.255.0) gmac0 netmask (NEW)
(002244887766) gmac0 mac addr (NEW)
(1) gmac0 phy mdio addr (NEW)
Clocks options --->
......
[ ] Enable CLK_OUT0
[ ] Enable CLK_OUT1
[*] Enable CLK_OUT2 # 使能 GMAC 接口时钟输出
[ ] Enable CLK_OUT3
9.4.2. 配置 LwIP
9.4.2.1. 静态 IP 设置
使用 scons --menuconfig
命令进入配置页面,配置如下:
Local packages options --->
Third-party packages options --->
[*] LwIP: light weight TCP/IP stack --->
--- LwIP: light weight TCP/IP stack
lwIP version (lwIP v2.1.3) --->
[ ] IPV6 protocol
[*] LwIP Examples --->
[*] example test
[*] IGMP protocol
(8) Memory alignment
-*- ICMP protocol
[ ] SNMP protocol
[ ] Enble DNS for name resolution
[ ] Enable alloc ip address through DHCP
......
[*] Enable ping features --->
......
使能 ping 功能,需要修改 packages/third-party/lwip/src/include/lwip/sockets.h
文件中
......
#include <sys/time.h>
#ifndef _TIMEVAL_DEFINED
#if LWIP_TIMEVAL_PRIVATE
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
#endif /* LWIP_TIMEVAL_PRIVATE */
#endif /* _TIMEVAL_DEFINED */
......
9.4.2.2. 动态IP 设置
使用 scons --menuconfig
命令进入配置页面,配置如下:
Local packages options --->
Third-party packages options --->
[*] LwIP: light weight TCP/IP stack --->
--- LwIP: light weight TCP/IP stack
lwIP version (lwIP v2.1.3) --->
[ ] IPV6 protocol
[*] LwIP Examples --->
[*] example test
[*] IGMP protocol
(8) Memory alignment
-*- ICMP protocol
[ ] SNMP protocol
[ ] Enble DNS for name resolution
[*] Enable alloc ip address through DHCP
(1) SOF broadcast
(1) SOF broadcast recv
......
[*] Enable ping features --->
......
使能 ping 功能,需要修改 packages/third-party/lwip/src/include/lwip/sockets.h
文件
#include <sys/time.h>
#ifndef _TIMEVAL_DEFINED
#if LWIP_TIMEVAL_PRIVATE
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* and microseconds */
};
#endif /* LWIP_TIMEVAL_PRIVATE */
#endif /* _TIMEVAL_DEFINED */
9.4.3. pinmux配置
在 target/soc name/board name/pinmux.c 中设置 MAC 端口的 pinmux, 需要根据开发板板的不同进行不同的设置
#ifdef AIC_USING_GMAC0
/* gmac0 */
{2, PIN_PULL_DIS, 3, "PE.0"},
{2, PIN_PULL_DIS, 3, "PE.1"},
{2, PIN_PULL_DIS, 3, "PE.2"},
{2, PIN_PULL_DIS, 3, "PE.3"},
{2, PIN_PULL_DIS, 3, "PE.4"},
{2, PIN_PULL_DIS, 3, "PE.5"},
{2, PIN_PULL_DIS, 3, "PE.7"},
{2, PIN_PULL_DIS, 3, "PE.8"},
{2, PIN_PULL_DIS, 3, "PE.9"},
/* phy0 reset GPIO */
{1, PIN_PULL_DIS, 3, "PE.6"},
/* clk_out2 */
{2, PIN_PULL_DIS, 3, "PE.10"},
#endif
配置完成后,需要配置复位引脚为输出模式,并在 void aic_board_pinmux_init(void)
中
void aic_board_pinmux_init(void)
{
......
/* phy0 reset gpio */
pin = hal_gpio_name2pin("PE.6");
g = GPIO_GROUP(pin);
p = GPIO_GROUP_PIN(pin);
hal_gpio_set_func(g, p, 1);
hal_gpio_direction_output(g, p);
hal_gpio_clr_output(g, p);
aic_mdelay(50);
hal_gpio_set_output(g, p);
......
}
9.4.4. 编译烧录
配置完成后,保存退出,编译、烧录
9.4.5. 验证
9.4.5.1. 系统启动
烧录完成后,重启,检查设备,看到 LwIP 启动、网卡获取 IP 为配置后的 IP:
aic /> Starting lwIP, local interface IP is 192.168.10.200
[I] aicphy_poll_thread()635 Port 0 link UP! autoneg mode: speed 100M, full duplex, flow control on.
9.4.5.2. ping 包
使用 PC 进行 ping 包验证, 配置同网段 IP ,ping 包结果如下:
C:\Users\guojun.dong>ping 192.168.10.200 -t
正在 Ping 192.168.10.200 具有 32 字节的数据:
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255
来自 192.168.10.200 的回复: 字节=32 时间<1ms TTL=255