本文基于恩智浦的i.MX RT1170 EVK及SDK,讨论如何在一个以太网口上同时实现IPv6和IPv4通信。
测试环境: SDK:2.10.1 IDE: MCUXpresso 11.4.0 EVK: i.MX RT1170 PC: Windows10
1. 实现步骤 1、下载i.MX RT1170 SDK 2.10.1导入lwip_udpecho_bm_cm7工程到MCUXpresso。 2、在lwipopts.h文件中, 参考本文提供的代码链接, 添加以下代码:
3、在lwip_udpecho_bm.c文件中, 参考本文提供的代码链接,添加以下代码:
- ip6_addr_t ipaddr_v6;
- s8_t chosen_idx;
- // IPv6
- IP6_ADDR(&ipaddr_v6,PP_HTONL(0xFE800000),PP_HTONL(0x0),PP_HTONL(0x3DD79303),PP_HTONL(0x126c0df0));
- netif_add_ip6_address(&netif, &ipaddr_v6, &chosen_idx);
- netif.ip6_addr_state[chosen_idx] = IP6_ADDR_VALID;
- ip6_addr_assign_zone(ip_2_ip6(&netif.ip6_addr[0]), IP6_UNICAST, &netif);
- netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_TENTATIVE);
复制代码
4、编译并下载到目标板运行。 5、参考下图用网线连接PC机和目标板。
<ignore_js_op>
6、ICMP测试 打开CMD窗口,参照下面的log分别进行IPv6和IPv4的测试。
- C:\Users\nxa16038>ping 192.168.0.102
- Pinging 192.168.0.102 with 32 bytes of data:
- Reply from 192.168.0.102: bytes=32 time=2ms TTL=255
- Reply from 192.168.0.102: bytes=32 time=1ms TTL=255
- Reply from 192.168.0.102: bytes=32 time=1ms TTL=255
- Reply from 192.168.0.102: bytes=32 time=1ms TTL=255
- Ping statistics for 192.168.0.102:
- Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
- Approximate round trip times in milli-seconds:
- Minimum = 1ms, Maximum = 2ms, Average = 1ms
- C:\Users\nxa16038>ping -6 FE80::3DD7:9303:126c:0df0
- Pinging fe80::3dd7:9303:126c:df0 with 32 bytes of data:
- Reply from fe80::3dd7:9303:126c:df0: time=1ms
- Reply from fe80::3dd7:9303:126c:df0: time=1ms
- Reply from fe80::3dd7:9303:126c:df0: time=1ms
- Reply from fe80::3dd7:9303:126c:df0: time=2ms
- Ping statistics for fe80::3dd7:9303:126c:df0:
- Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
- Approximate round trip times in milli-seconds:
- Minimum = 1ms, Maximum = 2ms, Average = 1ms
复制代码
7、UDP测试
UDP测试需要使用相关的工具,在本文提供的代码链接中附带了此工具,测试命令和log如下所示:
- C:\Users\nxa16038\Desktop\tt2\udp_test_tool\x64\Debug>udp_test.exe -6 FE80::3DD7:9303:126c:df0 7 5001 test_string_12345
- Build date and time: Oct 22 2021, 18:50:48
- arc = 6
- arg[0] = udp_test.exe
- arg[1] = -6
- arg[2] = FE80::3DD7:9303:126c:df0
- arg[3] = 7
- arg[4] = 5001
- arg[5] = test_string_12345
- Send ok.
- Receiving...
- Get connection.
- Get string: test_string_12345
- Remote addr: fe80:0000:0000:0000:3dd7:9303:126c:0df0
- C:\Users\nxa16038\Desktop\tt2\udp_test_tool\x64\Debug>udp_test.exe -4 192.168.0.102 7 5001 test_string_12345
- Build date and time: Oct 22 2021, 18:50:48
- arc = 6
- arg[0] = udp_test.exe
- arg[1] = -4
- arg[2] = 192.168.0.102
- arg[3] = 7
- arg[4] = 5001
- arg[5] = test_string_12345
- Send ok.
- Receiving...
- Get connection.
- Get string: test_string_12345, len = 17
- Remote addr: 192.168.0.102
复制代码
2. 获取源代码 本文的原代码可以从github.com进行下载,无需密码,下载链接为: https://github.com/jiaguonxpcom/lwip_ipv6_ipv4 该代码编译后可以直接下载到i.MX RT1170 EVK运行。 |