当前位置:   article > 正文

FPGA之zynq以太网(1)_zynq 以太网

zynq 以太网

学习在PS端配置以太网。本篇内容主要是vivado与SDK的使用步骤。

ZYNQ初体验千兆以太网的那些事儿(ps端)_zynq千兆网口实验-CSDN博客

vivado:

新建工程 --> 新建block design --> 选择zynq system,配置Ethernet、uart、SD卡。然后生成比特流

注意,这里不能只配置Ethernet,还要配置uart,不然在SDK新建模板的时候会报错。

生成HDL wrapper。然后export到SDK。

SDK:

新建application,选择lwip模板。

右键bsp配置以太网信息,这里没有修改任何信息。

然后main函数里就有了关于以太网配置的代码了。

那么现在来学习一下以太网相关的知识吧,不然代码看不懂。

fpga接口系列_基于zynq的以太网开发(pl到ps)_pl端采集的信息如何通过ps端以太网传输-CSDN博客

Zynq学习_____以太网三部曲(一)理论篇_zynq 以太网-CSDN博客 

 参考文档:zynq小系统板之嵌入式SDK开发指南

在创建工程选择模板时,右侧的界面已经说明MAC的地址为00:0a:35:00:01:02,静态IP地址为192.168.1.10,端口为port 7。在代码中的部分为:

 

 echo.c代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "lwip/err.h"
  4. #include "lwip/tcp.h"
  5. #if defined (__arm__) || defined (__aarch64__)
  6. #include "xil_printf.h"
  7. #endif
  8. int transfer_data() {
  9. return 0;
  10. }
  11. void print_app_header()
  12. {
  13. xil_printf("\n\r\n\r-----lwIP TCP echo server ------\n\r");
  14. xil_printf("TCP packets sent to port 6001 will be echoed back\n\r");
  15. }
  16. err_t recv_callback(void *arg, struct tcp_pcb *tpcb,
  17. struct pbuf *p, err_t err)
  18. {
  19. /* do not read the packet if we are not in ESTABLISHED state */
  20. if (!p) {
  21. tcp_close(tpcb);
  22. tcp_recv(tpcb, NULL);
  23. return ERR_OK;
  24. }
  25. /* indicate that the packet has been received */
  26. tcp_recved(tpcb, p->len);
  27. /* echo back the payload */
  28. /* in this case, we assume that the payload is < TCP_SND_BUF */
  29. if (tcp_sndbuf(tpcb) > p->len) {
  30. err = tcp_write(tpcb, p->payload, p->len, 1);
  31. } else
  32. xil_printf("no space in tcp_sndbuf\n\r");
  33. /* free the received pbuf */
  34. pbuf_free(p);
  35. return ERR_OK;
  36. }
  37. err_t accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err)
  38. {
  39. static int connection = 1;
  40. /* set the receive callback for this connection */
  41. tcp_recv(newpcb, recv_callback);
  42. /* just use an integer number indicating the connection id as the
  43. callback argument */
  44. tcp_arg(newpcb, (void*)connection);
  45. /* increment for subsequent accepted connections */
  46. connection++;
  47. return ERR_OK;
  48. }
  49. int start_application() //用户应用函数
  50. {
  51. struct tcp_pcb *pcb;
  52. err_t err;
  53. unsigned port = 7; // 端口设置为7
  54. /* create new TCP PCB structure */
  55. pcb = tcp_new();
  56. if (!pcb) {
  57. xil_printf("Error creating PCB. Out of Memory\n\r");
  58. return -1;
  59. }
  60. /* bind to specified @port */
  61. err = tcp_bind(pcb, IP_ADDR_ANY, port);
  62. if (err != ERR_OK) {
  63. xil_printf("Unable to bind to port %d: err = %d\n\r", port, err);
  64. return -2;
  65. }
  66. /* we do not need any arguments to callback functions */
  67. tcp_arg(pcb, NULL);
  68. /* listen for connections */
  69. pcb = tcp_listen(pcb);
  70. if (!pcb) {
  71. xil_printf("Out of memory while tcp_listen\n\r");
  72. return -3;
  73. }
  74. /* specify callback to use for incoming connections */
  75. tcp_accept(pcb, accept_callback);
  76. xil_printf("TCP echo server started @ port %d\n\r", port);
  77. return 0;
  78. }

main.c代码:

  1. #include <stdio.h>
  2. #include "xparameters.h"
  3. #include "netif/xadapter.h"
  4. #include "platform.h"
  5. #include "platform_config.h"
  6. #if defined (__arm__) || defined(__aarch64__)
  7. #include "xil_printf.h"
  8. #endif
  9. #include "lwip/tcp.h"
  10. #include "xil_cache.h"
  11. #if LWIP_DHCP==1
  12. #include "lwip/dhcp.h"
  13. #endif
  14. /* defined by each RAW mode application */
  15. void print_app_header();
  16. int start_application();
  17. int transfer_data();
  18. void tcp_fasttmr(void);
  19. void tcp_slowtmr(void);
  20. /* missing declaration in lwIP */
  21. void lwip_init();
  22. #if LWIP_DHCP==1
  23. extern volatile int dhcp_timoutcntr;
  24. err_t dhcp_start(struct netif *netif);
  25. #endif
  26. extern volatile int TcpFastTmrFlag;
  27. extern volatile int TcpSlowTmrFlag;
  28. static struct netif server_netif;
  29. struct netif *echo_netif;
  30. void
  31. print_ip(char *msg, struct ip_addr *ip)
  32. {
  33. print(msg);
  34. xil_printf("%d.%d.%d.%d\n\r", ip4_addr1(ip), ip4_addr2(ip),
  35. ip4_addr3(ip), ip4_addr4(ip));
  36. }
  37. void
  38. print_ip_settings(struct ip_addr *ip, struct ip_addr *mask, struct ip_addr *gw)
  39. {
  40. print_ip("Board IP: ", ip);
  41. print_ip("Netmask : ", mask);
  42. print_ip("Gateway : ", gw);
  43. }
  44. #if defined (__arm__) || defined(__aarch64__)
  45. #if XPAR_GIGE_PCS_PMA_SGMII_CORE_PRESENT == 1 || XPAR_GIGE_PCS_PMA_1000BASEX_CORE_PRESENT == 1
  46. int ProgramSi5324(void);
  47. int ProgramSfpPhy(void);
  48. #endif
  49. #endif
  50. int main()
  51. {
  52. #if __aarch64__
  53. Xil_DCacheDisable();
  54. #endif
  55. struct ip_addr ipaddr, netmask, gw;
  56. /* the mac address of the board. this should be unique per board */
  57. unsigned char mac_ethernet_address[] =
  58. { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };
  59. echo_netif = &server_netif;
  60. #if defined (__arm__) || defined(__aarch64__)
  61. #if XPAR_GIGE_PCS_PMA_SGMII_CORE_PRESENT == 1 || XPAR_GIGE_PCS_PMA_1000BASEX_CORE_PRESENT == 1
  62. ProgramSi5324();
  63. ProgramSfpPhy();
  64. #endif
  65. #endif
  66. init_platform();
  67. #if LWIP_DHCP==1
  68. ipaddr.addr = 0;
  69. gw.addr = 0;
  70. netmask.addr = 0;
  71. #else
  72. /* initliaze IP addresses to be used */
  73. IP4_ADDR(&ipaddr, 192, 168, 1, 10);
  74. IP4_ADDR(&netmask, 255, 255, 255, 0);
  75. IP4_ADDR(&gw, 192, 168, 1, 1);
  76. #endif
  77. print_app_header();
  78. lwip_init();
  79. /* Add network interface to the netif_list, and set it as default */
  80. if (!xemac_add(echo_netif, &ipaddr, &netmask,
  81. &gw, mac_ethernet_address,
  82. PLATFORM_EMAC_BASEADDR)) {
  83. xil_printf("Error adding N/W interface\n\r");
  84. return -1;
  85. }
  86. netif_set_default(echo_netif);
  87. /* now enable interrupts */
  88. platform_enable_interrupts();
  89. /* specify that the network if is up */
  90. netif_set_up(echo_netif);
  91. #if (LWIP_DHCP==1)
  92. /* Create a new DHCP client for this interface.
  93. * Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
  94. * the predefined regular intervals after starting the client.
  95. */
  96. dhcp_start(echo_netif);
  97. dhcp_timoutcntr = 24;
  98. while(((echo_netif->ip_addr.addr) == 0) && (dhcp_timoutcntr > 0))
  99. xemacif_input(echo_netif);
  100. if (dhcp_timoutcntr <= 0) {
  101. if ((echo_netif->ip_addr.addr) == 0) {
  102. xil_printf("DHCP Timeout\r\n");
  103. xil_printf("Configuring default IP of 192.168.1.10\r\n");
  104. IP4_ADDR(&(echo_netif->ip_addr), 192, 168, 1, 10);
  105. IP4_ADDR(&(echo_netif->netmask), 255, 255, 255, 0);
  106. IP4_ADDR(&(echo_netif->gw), 192, 168, 1, 1);
  107. }
  108. }
  109. ipaddr.addr = echo_netif->ip_addr.addr;
  110. gw.addr = echo_netif->gw.addr;
  111. netmask.addr = echo_netif->netmask.addr;
  112. #endif
  113. print_ip_settings(&ipaddr, &netmask, &gw);
  114. /* start the application (web server, rxtest, txtest, etc..) */
  115. start_application();
  116. /* receive and process packets */
  117. while (1) {
  118. if (TcpFastTmrFlag) {
  119. tcp_fasttmr();
  120. TcpFastTmrFlag = 0;
  121. }
  122. if (TcpSlowTmrFlag) {
  123. tcp_slowtmr();
  124. TcpSlowTmrFlag = 0;
  125. }
  126. xemacif_input(echo_netif);
  127. transfer_data();
  128. }
  129. /* never reached */
  130. cleanup_platform();
  131. return 0;
  132. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/742046
推荐阅读
相关标签
  

闽ICP备14008679号