TA的每日心情 | 开心 2021-3-22 21:23 |
---|
新手入门
- 积分
- 15
|
楼主 |
发表于 2021-4-1 23:41:26
|
显示全部楼层
Qsys连线:
三速以太网IP核负责收发PHY芯片的数据包,通过SGDMA将数据包内容传输给C语言处理。NIOS II核负责运行C语言程序,其中包括了lwip2.1.2协议栈。Interval Timer负责提供毫秒计数器,供lwip的sys_now函数使用。
NIOS从onchip memory中启动(也就是reset和exception vector位于onchip memory),但程序运行在SDRAM中,SGDMA的描述符和缓冲区全部位于SDRAM中。
三速以太网IP核配置的是10/100Mb Small MAC模式,选择MII接口,使能了MDIO接口和半双工模式的支持。
Verilog开发环境:
C语言开发环境:
- /*
- * "Hello World" example.
- *
- * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
- * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
- * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
- * device in your system's hardware.
- * The memory footprint of this hosted application is ~69 kbytes by default
- * using the standard reference design.
- *
- * For a reduced footprint version of this template, and an explanation of how
- * to reduce the memory footprint for a given application, see the
- * "small_hello_world" template.
- *
- */
-
- #include <lwip/apps/httpd.h>
- #include <lwip/apps/netbiosns.h>
- #include <lwip/dhcp.h>
- #include <lwip/dns.h>
- #include <lwip/init.h>
- #include <lwip/netif.h>
- #include <lwip/timeouts.h>
- #include <netif/ethernetif.h>
- #include <stdio.h>
- #include <sys/alt_alarm.h>
- #include <system.h>
-
- static struct netif netif_rtl8201cp;
-
- alt_u32 sys_now(void)
- {
- // BSP Editor里面Settings -> Common -> hal -> sys_clk_timer必须选择一个定时器
- // 而且该定时器的计时间隔必须为1ms
- LWIP_ASSERT("incorrect tick rate", alt_ticks_per_second() == 1000);
- return alt_nticks();
- }
-
- static void display_ip(void)
- {
- const ip_addr_t *addr;
- static uint8_t ip_displayed = 0;
- static uint8_t ip6_displayed = 0;
- int i, ip_present;
- int dns = 0;
-
- if (netif_dhcp_data(&netif_rtl8201cp) == NULL)
- ip_present = 1; // 使用静态IP地址
- else if (dhcp_supplied_address(&netif_rtl8201cp))
- ip_present = 2; // 使用DHCP获得IP地址, 且已成功获取到IP地址
- else
- ip_present = 0; // 使用DHCP获得IP地址, 且还没有获取到IP地址
-
- // 显示IPv4地址
- if (ip_present)
- {
- if (ip_displayed == 0)
- {
- ip_displayed = 1;
-
- if (ip_present == 2)
- printf("DHCP supplied address!\r\n");
- printf("IP address: %s\r\n", ipaddr_ntoa(&netif_rtl8201cp.ip_addr));
- printf("Subnet mask: %s\r\n", ipaddr_ntoa(&netif_rtl8201cp.netmask));
- printf("Default gateway: %s\r\n", ipaddr_ntoa(&netif_rtl8201cp.gw));
- dns = 1;
- }
- }
- else
- ip_displayed = 0;
-
- // 显示IPv6地址
- for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) // 0号地址是本地链路地址, 不需要显示
- {
- if (ip6_addr_isvalid(netif_ip6_addr_state(&netif_rtl8201cp, i)))
- {
- if ((ip6_displayed & _BV(i)) == 0)
- {
- ip6_displayed |= _BV(i);
- printf("IPv6 address %d: %s\r\n", i, ipaddr_ntoa(netif_ip_addr6(&netif_rtl8201cp, i)));
- dns = 1;
- }
- }
- else
- ip6_displayed &= ~_BV(i);
- }
-
- // 显示DNS服务器地址
- // 在lwip中, IPv4 DHCP和IPv6 SLAAC获取到的DNS地址会互相覆盖
- if (dns)
- {
- addr = dns_getserver(0);
- if (ip_addr_isany(addr))
- return;
- printf("DNS Server: %s", ipaddr_ntoa(addr));
-
- addr = dns_getserver(1);
- if (!ip_addr_isany(addr))
- printf(" %s", ipaddr_ntoa(addr));
-
- printf("\r\n");
- }
- }
-
- static void net_config(int use_dhcp)
- {
- ip4_addr_t ipaddr, netmask, gw;
-
- if (use_dhcp)
- netif_add_noaddr(&netif_rtl8201cp, NULL, ethernetif_init, netif_input);
- else
- {
- IP4_ADDR(&ipaddr, 192, 168, 0, 19);
- IP4_ADDR(&netmask, 255, 255, 255, 0);
- IP4_ADDR(&gw, 192, 168, 0, 1);
- netif_add(&netif_rtl8201cp, &ipaddr, &netmask, &gw, NULL, ethernetif_init, netif_input);
- }
- netif_set_default(&netif_rtl8201cp);
- netif_set_up(&netif_rtl8201cp);
-
- if (use_dhcp)
- dhcp_start(&netif_rtl8201cp);
-
- netif_create_ip6_linklocal_address(&netif_rtl8201cp, 1);
- printf("IPv6 link-local address: %s\r\n", ipaddr_ntoa(netif_ip_addr6(&netif_rtl8201cp, 0)));
- netif_set_ip6_autoconfig_enabled(&netif_rtl8201cp, 1);
- }
-
- int main(void)
- {
- printf("Hello from Nios II!\r\n");
-
- lwip_init();
- net_config(1);
-
- httpd_init();
- netbiosns_init();
- netbiosns_set_name("EP4CE10F17C8");
-
- while (1)
- {
- ethernetif_check_link(&netif_rtl8201cp);
- display_ip();
-
- ethernetif_input(&netif_rtl8201cp);
- sys_check_timeouts();
- }
- }
- ————————————————
- 版权声明:本文为CSDN博主「巨大八爪鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
- 原文链接:https://blog.csdn.net/ZLK1214/article/details/115337628
复制代码
|
|