芯路恒电子技术论坛

 找回密码
 立即注册
热搜: 合集
查看: 4303|回复: 3

基于AC620的NIOS II LWIP例程(RTL8201百兆网口)

[复制链接]
  • TA的每日心情
    开心
    2021-3-22 21:23
  • 1

    主题

    4

    帖子

    15

    积分

    新手入门

    Rank: 1

    积分
    15
    QQ
    发表于 2021-4-1 23:35:25 | 显示全部楼层 |阅读模式
    Altera FPGA NIOS使用Triple-Speed Ethernet三速以太网IP核驱动RTL8201CP百兆网口,并使用lwip2.1.2协议栈建立http服务器,支持IPv6
    Quartus II 13.0工程下载地址:https://pan.baidu.com/s/17CQIZfkZJD9BzDyH6gTRLQ(提取码:3a6o,内含三速以太网license.dat)
    【程序功能展示】
    1. ping通开发板的NetBIOS设备名,IPv4地址和IPv6地址

    2. 访问开发板上的http服务器(设备名方式、IPv6方式):


    3. 在路由器管理页面看到开发板的信息:

    4. PHY芯片自动协商网口速率和双工模式,程序带网口热插拔检测:

    5. DHCP获取IPv4地址,SLAAC获取IPv6地址:
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2021-3-22 21:23
  • 1

    主题

    4

    帖子

    15

    积分

    新手入门

    Rank: 1

    积分
    15
    QQ
     楼主| 发表于 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语言开发环境:





    1. /*
    2. * "Hello World" example.
    3. *
    4. * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
    5. * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
    6. * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
    7. * device in your system's hardware.
    8. * The memory footprint of this hosted application is ~69 kbytes by default
    9. * using the standard reference design.
    10. *
    11. * For a reduced footprint version of this template, and an explanation of how
    12. * to reduce the memory footprint for a given application, see the
    13. * "small_hello_world" template.
    14. *
    15. */

    16. #include <lwip/apps/httpd.h>
    17. #include <lwip/apps/netbiosns.h>
    18. #include <lwip/dhcp.h>
    19. #include <lwip/dns.h>
    20. #include <lwip/init.h>
    21. #include <lwip/netif.h>
    22. #include <lwip/timeouts.h>
    23. #include <netif/ethernetif.h>
    24. #include <stdio.h>
    25. #include <sys/alt_alarm.h>
    26. #include <system.h>

    27. static struct netif netif_rtl8201cp;

    28. alt_u32 sys_now(void)
    29. {
    30.         // BSP Editor里面Settings -> Common -> hal -> sys_clk_timer必须选择一个定时器
    31.         // 而且该定时器的计时间隔必须为1ms
    32.         LWIP_ASSERT("incorrect tick rate", alt_ticks_per_second() == 1000);
    33.         return alt_nticks();
    34. }

    35. static void display_ip(void)
    36. {
    37.         const ip_addr_t *addr;
    38.         static uint8_t ip_displayed = 0;
    39.         static uint8_t ip6_displayed = 0;
    40.         int i, ip_present;
    41.         int dns = 0;

    42.         if (netif_dhcp_data(&netif_rtl8201cp) == NULL)
    43.                 ip_present = 1; // 使用静态IP地址
    44.         else if (dhcp_supplied_address(&netif_rtl8201cp))
    45.                 ip_present = 2; // 使用DHCP获得IP地址, 且已成功获取到IP地址
    46.         else
    47.                 ip_present = 0; // 使用DHCP获得IP地址, 且还没有获取到IP地址

    48.         // 显示IPv4地址
    49.         if (ip_present)
    50.         {
    51.                 if (ip_displayed == 0)
    52.                 {
    53.                         ip_displayed = 1;

    54.                         if (ip_present == 2)
    55.                                 printf("DHCP supplied address!\r\n");
    56.                         printf("IP address: %s\r\n", ipaddr_ntoa(&netif_rtl8201cp.ip_addr));
    57.                         printf("Subnet mask: %s\r\n", ipaddr_ntoa(&netif_rtl8201cp.netmask));
    58.                         printf("Default gateway: %s\r\n", ipaddr_ntoa(&netif_rtl8201cp.gw));
    59.                         dns = 1;
    60.                 }
    61.         }
    62.         else
    63.                 ip_displayed = 0;

    64.         // 显示IPv6地址
    65.         for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) // 0号地址是本地链路地址, 不需要显示
    66.         {
    67.                 if (ip6_addr_isvalid(netif_ip6_addr_state(&netif_rtl8201cp, i)))
    68.                 {
    69.                         if ((ip6_displayed & _BV(i)) == 0)
    70.                         {
    71.                                 ip6_displayed |= _BV(i);
    72.                                 printf("IPv6 address %d: %s\r\n", i, ipaddr_ntoa(netif_ip_addr6(&netif_rtl8201cp, i)));
    73.                                 dns = 1;
    74.                         }
    75.                 }
    76.                 else
    77.                         ip6_displayed &= ~_BV(i);
    78.         }

    79.         // 显示DNS服务器地址
    80.         // 在lwip中, IPv4 DHCP和IPv6 SLAAC获取到的DNS地址会互相覆盖
    81.         if (dns)
    82.         {
    83.                 addr = dns_getserver(0);
    84.                 if (ip_addr_isany(addr))
    85.                         return;
    86.                 printf("DNS Server: %s", ipaddr_ntoa(addr));

    87.                 addr = dns_getserver(1);
    88.                 if (!ip_addr_isany(addr))
    89.                         printf(" %s", ipaddr_ntoa(addr));

    90.                 printf("\r\n");
    91.         }
    92. }

    93. static void net_config(int use_dhcp)
    94. {
    95.         ip4_addr_t ipaddr, netmask, gw;

    96.         if (use_dhcp)
    97.                 netif_add_noaddr(&netif_rtl8201cp, NULL, ethernetif_init, netif_input);
    98.         else
    99.         {
    100.                 IP4_ADDR(&ipaddr, 192, 168, 0, 19);
    101.                 IP4_ADDR(&netmask, 255, 255, 255, 0);
    102.                 IP4_ADDR(&gw, 192, 168, 0, 1);
    103.                 netif_add(&netif_rtl8201cp, &ipaddr, &netmask, &gw, NULL, ethernetif_init, netif_input);
    104.         }
    105.         netif_set_default(&netif_rtl8201cp);
    106.         netif_set_up(&netif_rtl8201cp);

    107.         if (use_dhcp)
    108.                 dhcp_start(&netif_rtl8201cp);

    109.         netif_create_ip6_linklocal_address(&netif_rtl8201cp, 1);
    110.         printf("IPv6 link-local address: %s\r\n", ipaddr_ntoa(netif_ip_addr6(&netif_rtl8201cp, 0)));
    111.         netif_set_ip6_autoconfig_enabled(&netif_rtl8201cp, 1);
    112. }

    113. int main(void)
    114. {
    115.         printf("Hello from Nios II!\r\n");

    116.         lwip_init();
    117.         net_config(1);

    118.         httpd_init();
    119.         netbiosns_init();
    120.         netbiosns_set_name("EP4CE10F17C8");

    121.         while (1)
    122.         {
    123.                 ethernetif_check_link(&netif_rtl8201cp);
    124.                 display_ip();

    125.                 ethernetif_input(&netif_rtl8201cp);
    126.                 sys_check_timeouts();
    127.         }
    128. }
    129. ————————————————
    130. 版权声明:本文为CSDN博主「巨大八爪鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    131. 原文链接:https://blog.csdn.net/ZLK1214/article/details/115337628
    复制代码


    回复 支持 反对

    使用道具 举报

    该用户从未签到

    0

    主题

    1

    帖子

    2

    积分

    新手入门

    Rank: 1

    积分
    2
    发表于 2021-10-13 17:34:05 | 显示全部楼层
    失效了,能否再发一下呢?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    闭嘴
    2022-7-11 09:42
  • 0

    主题

    6

    帖子

    146

    积分

    初级会员

    Rank: 3Rank: 3

    积分
    146
    发表于 2021-12-16 09:11:10 | 显示全部楼层
    百度盘失效了。
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|小黑屋|Archiver|芯路恒电子技术论坛 |鄂ICP备2021003648号

    GMT+8, 2024-3-29 15:49 , Processed in 0.113444 second(s), 35 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

    快速回复 返回顶部 返回列表