0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

基于米联客MA703FA开发板的MicroBlaze LWIP千兆以太网案例

C29F_xilinx_inc 来源:赛灵思 作者:赛灵思 2022-02-16 16:21 次阅读

Xilinx FPGA MicroBlaze使用AXI 1G/2.5G Ethernet Subsystem(= Tri Mode Ethernet MAC + AXI Ethernet Buffer)以太网IP核驱动RTL8211FD千兆网口,并使用lwip2.1.2协议栈建立http服务器,支持IPv6。

Xilinx Vivado 2020.1工程下载地址:https://pan.baidu.com/s/1QO49qAloPJBvY6XiKGqdxQ(提取码:a4ns)

【开发板】

开发板型号:米联客MA703FA
FPGA型号:XC7A35TFGG484-2
晶振频率:50MHz
DDR3内存型号:MT41K128M16(容量为256MB)
PHY芯片型号:RTL8211FD(RGMII接口,千兆以太网PHY芯片)

XC7A35T只有20800个LUT,资源非常紧张,建议换用XC7A50T或者XC7A100T。

poYBAGIMpdaARHFEABSYktuGcoE442.png

【网口LED灯配置】

poYBAGIMpd2ABP-hAAVuGXkrqHg016.png

插上网线后,绿灯常亮。拔掉网线后,绿灯熄灭。
链路支持EEE节能:有数据传输,黄灯闪烁,否则熄灭
链路不支持EEE节能:有数据传输,黄灯闪烁,否则常亮

具体配置方法见ethernetif.c的low_level_init函数。(https://blog.csdn.net/ZLK1214/article/details/115842136)

【程序功能展示】

1. ping通开发板的NetBIOS设备名,IPv4地址和IPv6地址:

pYYBAGIMpeGAFLfiAAEZyMSAMug268.png

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

pYYBAGIMpeOAMGMkAAJXQ_XPiuE773.png

poYBAGIMpeaAYJQ0AAJhJ4wDJ94118.png

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

pYYBAGIMpe2AZx8ZAALcTXp9ePw157.png

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

poYBAGIMpfCAYWhdAAEoivHvSBI236.png

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

poYBAGIMpfOAWO1vAAEzi5vQVQQ506.png

【开发环境】

Verilog开发环境:Xilinx Vivado 2020.1

poYBAGIMpf2AQ4T_AAPsnuMBQ7U159.png

从中可以看到,LUT资源占了将近90%。

C语言开发环境:Xilinx Vitis 2020.1

pYYBAGIMpgOAahYLAAR9M2-GDFg870.png

【Block Design连线】

poYBAGIMpgaARx2tAAJX33P0pVI881.png

pYYBAGIMpgiACtPQAAHZwsNzm1U187.png

Vitis sleep()函数所用的定时器的设置方法:

poYBAGIMpg-Aem8zAAUihjTouXQ778.png

【主要程序代码

hello_world.c:

/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
* helloworld.c: simple test application
*
* This application configures UART 16550 to baud rate 9600.
* PS7 UART (Zynq) is not initialized by this application, since
* bootrom/bsp configures it to baud rate 115200
*
* ------------------------------------------------
* | UART TYPE BAUD RATE |
* ------------------------------------------------
* uartns550 9600
* uartlite Configurable only in HW design
* ps7_uart 115200 (configured by bootrom/bsp)
*/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "platform.h"

XIntc xintc;
XTmrCtr xtmrctr;
static struct netif netif_rtl8211fd;
static uint32_t sys_ticks;

uint32_t sys_now(void)
{
return sys_ticks;
}

static void sys_now_handler(void *CallBackRef, u8 TmrCtrNumber)
{
if (TmrCtrNumber == 1)
sys_ticks++;
}

void sys_now_init(void)
{
int ret;

#ifndef XSLEEP_TIMER_IS_AXI_TIMER
// 请在platform工程的platform.spr里面, 将sleep_timer设为axi_timer_0
// 此定时器的Timer 1用于sleep()函数, Timer 2用于sys_now()函数
#error "sleep() function cannot work!"
#endif

// 初始化中断控制器
ret = XIntc_Initialize(&xintc, XPAR_INTC_0_DEVICE_ID);
LWIP_ASSERT("Intc initialized", ret == XST_SUCCESS);
ret = XIntc_Start(&xintc, XIN_REAL_MODE);
LWIP_ASSERT("Intc started", ret == XST_SUCCESS);

Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XIntc_InterruptHandler, &xintc);
Xil_ExceptionEnable();

// 初始化定时器
ret = XTmrCtr_Initialize(&xtmrctr, XPAR_TMRCTR_0_DEVICE_ID);
LWIP_ASSERT("Timer initialized", ret == XST_SUCCESS);
ret = XTmrCtr_SelfTest(&xtmrctr, 1);
LWIP_ASSERT("Timer self test passed", ret == XST_SUCCESS);

// 启动定时器
// TIMING_INTERVAL = (TLRx + 2) * AXI_CLOCK_PERIOD
XTmrCtr_SetOptions(&xtmrctr, 1, XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION | XTC_INT_MODE_OPTION);
XTmrCtr_SetResetValue(&xtmrctr, 1, XPAR_TMRCTR_0_CLOCK_FREQ_HZ / 1000 - 2); // 计时频率为1ms
XTmrCtr_SetHandler(&xtmrctr, sys_now_handler, NULL);
XTmrCtr_Start(&xtmrctr, 1);

// 开启定时器中断
ret = XIntc_Connect(&xintc, XPAR_INTC_0_TMRCTR_0_VEC_ID, (XInterruptHandler)XTmrCtr_InterruptHandler, &xtmrctr);
LWIP_ASSERT("Timer interrupt connected", ret == XST_SUCCESS);
XIntc_Enable(&xintc, XPAR_INTC_0_TMRCTR_0_VEC_ID);
}

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_rtl8211fd) == NULL)
ip_present = 1; // 使用静态IP地址
else if (dhcp_supplied_address(&netif_rtl8211fd))
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!\n");
printf("IP address: %s\n", ipaddr_ntoa(&netif_rtl8211fd.ip_addr));
printf("Subnet mask: %s\n", ipaddr_ntoa(&netif_rtl8211fd.netmask));
printf("Default gateway: %s\n", ipaddr_ntoa(&netif_rtl8211fd.gw));
dns = 1;
}
}
else
ip_displayed = 0;

// 显示IPv6地址
for (i = 1; i {
if (ip6_addr_isvalid(netif_ip6_addr_state(&netif_rtl8211fd, i)))
{
if ((ip6_displayed & _BV(i)) == 0)
{
ip6_displayed |= _BV(i);
printf("IPv6 address %d: %s\n", i, ipaddr_ntoa(netif_ip_addr6(&netif_rtl8211fd, 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("\n");
}
}

static void net_config(int use_dhcp)
{
ip4_addr_t ipaddr, netmask, gw;
struct netif *netif;

if (use_dhcp)
netif_add_noaddr(&netif_rtl8211fd, 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_rtl8211fd, &ipaddr, &netmask, &gw, NULL, ethernetif_init, netif_input);
}
netif_set_default(&netif_rtl8211fd);
netif_set_up(&netif_rtl8211fd);

if (use_dhcp)
dhcp_start(&netif_rtl8211fd);

netif_create_ip6_linklocal_address(&netif_rtl8211fd, 1);
printf("IPv6 link-local address: %s\n", ipaddr_ntoa(netif_ip_addr6(&netif_rtl8211fd, 0)));
netif_set_ip6_autoconfig_enabled((netif = &netif_rtl8211fd), 1);
}

int main()
{
char ch;

init_platform();

printf("Hello World\n");
printf("Successfully ran Hello World application\n");
sys_now_init();

lwip_init();
net_config(1);

httpd_init();
netbiosns_init();
netbiosns_set_name("XC7A35TFGG484-2");

while (1)
{
ethernetif_check_link(&netif_rtl8211fd);
display_ip();

ethernetif_input(&netif_rtl8211fd);
sys_check_timeouts();

if (!XUartLite_IsReceiveEmpty(XPAR_UARTLITE_0_BASEADDR))
{
ch = getchar();
if (ch == 't')
printf("sys_now()=%lu\n", sys_now());
}
}

cleanup_platform();
return 0;
}

ethernetif.c(网口驱动):
备注:官网lwip2.1版本的压缩包(lwip-2.1.0.zip)中的src/netif文件夹下没有ethernetif.c了,那个文件是被移动到contrib-2.1.0.zip的examples/ethernetif文件夹下去了。

/**
* @file
* Ethernet Interface Skeleton
*
*/

/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Adam Dunkels
*
*/

/*
* This file is a skeleton for developing Ethernet network interface
* drivers for lwIP. Add code to the low_level functions and do a
* search-and-replace for the word "ethernetif" to replace it with
* something that better describes your network interface.
*/

#include "lwip/opt.h"

#if 1 /* don't build, this is only a skeleton, see previous comment */

#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/pbuf.h"
#include "lwip/stats.h"
#include "lwip/snmp.h"
#include "lwip/ethip6.h"
#include "lwip/etharp.h"
#include "netif/ppp/pppoe.h"

#include
#include
#include

/* Define those to better describe your network interface. */
#define IFNAME0 'e'
#define IFNAME1 'n'

/**
* Helper struct to hold private data used to operate your ethernet interface.
* Keeping the ethernet address of the MAC in this struct is not necessary
* as it is already kept in the struct netif.
* But this is only an example, anyway...
*/
struct ethernetif {
struct eth_addr *ethaddr;
/* Add whatever per-interface state that is needed here. */
XAxiDma_Config *xaxidma_cfg;
XAxiEthernet_Config *xaxieth_cfg;
XAxiDma_BdRing *rxring;
XAxiDma_BdRing *txring;
int rx_i;
int tx_i;
};

/* Forward declarations. */
//static void ethernetif_input(struct netif *netif);

XAxiDma xaxidma;
XAxiEthernet xaxieth;

// 收发描述符和缓冲区
#define RX_NUM 10
#define TX_NUM 10
static __attribute__((aligned(XAXIDMA_BD_MINIMUM_ALIGNMENT))) uint8_t dma_bdring_rx[RX_NUM][XAXIDMA_BD_MINIMUM_ALIGNMENT];
static __attribute__((aligned(XAXIDMA_BD_MINIMUM_ALIGNMENT))) uint8_t dma_bdring_tx[TX_NUM][XAXIDMA_BD_MINIMUM_ALIGNMENT];
static __attribute__((aligned(4))) uint8_t dma_buffer_rx[RX_NUM][1600];
static __attribute__((aligned(4))) uint8_t dma_buffer_tx[TX_NUM][1600];

// 网口LED灯的序号
#define GREEN_LED 2 // 绿灯是LED2
#define YELLOW_LED 1 // 黄灯是LED1

/**
* In this function, the hardware should be initialized.
* Called from ethernetif_init().
*
* @param netif the already initialized lwip network interface structure
* for this ethernetif
*/
static void
low_level_init(struct netif *netif)
{
struct ethernetif *ethernetif = netif->state;
int i, ret;
uint16_t temp;
XAxiDma_Bd *bd, *p;

/* set MAC hardware address length */
netif->hwaddr_len = ETHARP_HWADDR_LEN;

/* set MAC hardware address */
// 设置MAC地址
netif->hwaddr[0] = 0x00;
netif->hwaddr[1] = 0x0a;
netif->hwaddr[2] = 0x35;
netif->hwaddr[3] = 0x00;
netif->hwaddr[4] = 0x01;
netif->hwaddr[5] = 0x02;
printf("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n", netif->hwaddr[0], netif->hwaddr[1], netif->hwaddr[2], netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]);

/* maximum transfer unit */
netif->mtu = 1500;

/* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP/* | NETIF_FLAG_LINK_UP*/; // 初始状态下认为网线还没有插
netif->flags |= NETIF_FLAG_MLD6; // 启用IPv6多播

#if LWIP_IPV6 && LWIP_IPV6_MLD
/*
* For hardware/netifs that implement MAC filtering.
* All-nodes link-local is handled by default, so we must let the hardware know
* to allow multicast packets in.
* Should set mld_mac_filter previously. */
if (netif->mld_mac_filter != NULL) {
ip6_addr_t ip6_allnodes_ll;
ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
}
#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */

/* Do whatever else is needed to initialize interface. */
// 初始化DMA
ethernetif->xaxidma_cfg = XAxiDma_LookupConfig(XPAR_AXIDMA_0_DEVICE_ID);
XAxiDma_CfgInitialize(&xaxidma, ethernetif->xaxidma_cfg);
ret = XAxiDma_Selftest(&xaxidma);
LWIP_ASSERT("DMA self test passed", ret == XST_SUCCESS);

if (!XAxiDma_HasSg(&xaxidma))
{
printf("XPAR_AXIDMA_0_INCLUDE_SG=%d\n", XPAR_AXIDMA_0_INCLUDE_SG);
printf("Please recreate and build Vitis platform project!\n");
LWIP_ASSERT("DMA has SG", 0);
}

// 初始化ETH
ethernetif->xaxieth_cfg = XAxiEthernet_LookupConfig(XPAR_AXIETHERNET_0_DEVICE_ID);
XAxiEthernet_CfgInitialize(&xaxieth, ethernetif->xaxieth_cfg, ethernetif->xaxieth_cfg->BaseAddress);
XAxiEthernet_SetMacAddress(&xaxieth, netif->hwaddr);
// 请注意: 必须先初始化DMA, 后初始化ETH, 顺序不能反
// 这是因为DMA在复位的时候, ETH会跟着复位

// 启用IPv4和IPv6多播
XAxiEthernet_SetOptions(&xaxieth, XAE_MULTICAST_OPTION);
LWIP_ASSERT("NumTableEntries >= 2", ethernetif->xaxieth_cfg->NumTableEntries >= 2);
// IPv4多播MAC地址: 01:00:5E:*:*:*
XAxiEthernet_MulticastAdd(&xaxieth, "\x01\x00\x5e\x00\x00\x00", 0);
XAxiEthernet_WriteReg(ethernetif->xaxieth_cfg->BaseAddress, 0x750, 0xffffff);
XAxiEthernet_WriteReg(ethernetif->xaxieth_cfg->BaseAddress, 0x754, 0);
// IPv6多播MAC地址: 33:33:*:*:*:*
XAxiEthernet_MulticastAdd(&xaxieth, "\x33\x33\x00\x00\x00\x00", 1);
XAxiEthernet_WriteReg(ethernetif->xaxieth_cfg->BaseAddress, 0x750, 0xffff);
XAxiEthernet_WriteReg(ethernetif->xaxieth_cfg->BaseAddress, 0x754, 0);

// 配置网口LED灯
XAxiEthernet_PhyWrite(&xaxieth, PHY_ADDR, 31, 0xd04);
temp = 0x0b temp |= 0x1b XAxiEthernet_PhyWrite(&xaxieth, PHY_ADDR, 0x10, temp);
temp = _BV(YELLOW_LED + 1); // 绿灯不指示EEE节能状态, 插了网线后灯要常亮
// 但黄灯要指示EEE节能状态, 没有数据包时灯要熄灭
XAxiEthernet_PhyWrite(&xaxieth, PHY_ADDR, 0x11, temp);
XAxiEthernet_PhyWrite(&xaxieth, PHY_ADDR, 31, 0);
// 插上网线后, 绿灯常亮; 拔掉网线后, 绿灯熄灭
// 链路支持EEE节能: 有数据传输, 黄灯闪烁, 否则熄灭
// 链路不支持EEE节能: 有数据传输, 黄灯闪烁, 否则常亮

// 配置DMA发送描述符
ethernetif->tx_i = 0;
ethernetif->txring = XAxiDma_GetTxRing(&xaxidma);
ret = XAxiDma_BdRingCreate(ethernetif->txring, (uintptr_t)dma_bdring_tx, (uintptr_t)dma_bdring_tx, XAXIDMA_BD_MINIMUM_ALIGNMENT, TX_NUM);
LWIP_ASSERT("DMA txring created", ret == XST_SUCCESS);
ret = XAxiDma_BdRingStart(ethernetif->txring);
LWIP_ASSERT("DMA txring started", ret == XST_SUCCESS);

// 配置DMA接收描述符
ethernetif->rx_i = 0;
ethernetif->rxring = XAxiDma_GetRxRing(&xaxidma);
ret = XAxiDma_BdRingCreate(ethernetif->rxring, (uintptr_t)dma_bdring_rx, (uintptr_t)dma_bdring_rx, XAXIDMA_BD_MINIMUM_ALIGNMENT, RX_NUM);
LWIP_ASSERT("DMA rxring created", ret == XST_SUCCESS);
ret = XAxiDma_BdRingStart(ethernetif->rxring);
LWIP_ASSERT("DMA rxring started", ret == XST_SUCCESS);

// 启用所有的接收描述符
ret = XAxiDma_BdRingAlloc(ethernetif->rxring, RX_NUM, &bd);
LWIP_ASSERT("DMA rxring allocated all", ret == XST_SUCCESS);
p = bd;
for (i = 0; i {
XAxiDma_BdSetBufAddr(p, (uintptr_t)dma_buffer_rx[i]);
XAxiDma_BdSetLength(p, sizeof(dma_buffer_rx[i]), ethernetif->rxring->MaxTransferLen);
XAxiDma_BdSetCtrl(bd, 0);
XAxiDma_BdSetId(p, i);
p = (XAxiDma_Bd *)XAxiDma_BdRingNext(ethernetif->rxring, p);
}
ret = XAxiDma_BdRingToHw(ethernetif->rxring, RX_NUM, bd);
LWIP_ASSERT("DMA rxring brought all", ret == XST_SUCCESS);

printf("DMA Rx descriptor: %p\n", dma_bdring_rx);
printf("DMA Tx descriptor: %p\n", dma_bdring_tx);
printf("DMA Rx buffer: %p\n", dma_buffer_rx);
printf("DMA Tx buffer: %p\n", dma_buffer_tx);
}

/**
* This function should do the actual transmission of the packet. The packet is
* contained in the pbuf that is passed to the function. This pbuf
* might be chained.
*
* @param netif the lwip network interface structure for this ethernetif
* @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
* @return ERR_OK if the packet could be sent
* an err_t value if the packet couldn't be sent
*
* @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
* strange results. You might consider waiting for space in the DMA queue
* to become available since the stack doesn't retry to send a packet
* dropped because of memory failure (except for the TCP timers).
*/

static err_t
low_level_output(struct netif *netif, struct pbuf *p)
{
struct ethernetif *ethernetif = netif->state;
struct pbuf *q;
int ret;
uint8_t *data = dma_buffer_tx[ethernetif->tx_i];
XAxiDma_Bd *bd = NULL;

//initiate transfer();
LWIP_ASSERT("tx packet too big", p->tot_len if (netif_is_link_up(netif))
{
// 释放不用的描述符
do
{
ret = XAxiDma_BdRingFromHw(ethernetif->txring, XAXIDMA_ALL_BDS, &bd);
if (ret > 0)
{
//printf("%d packet(s) have been sent!\n", ret);
ret = XAxiDma_BdRingFree(ethernetif->txring, ret, bd);
LWIP_ASSERT("DMA txring freed some", ret == XST_SUCCESS);
}
} while (XAxiDma_BdRingGetFreeCnt(ethernetif->txring) == 0);

// 分配一个新描述符
ret = XAxiDma_BdRingAlloc(ethernetif->txring, 1, &bd);
LWIP_ASSERT("DMA txring allocated one", ret == XST_SUCCESS);
}

#if ETH_PAD_SIZE
pbuf_remove_header(p, ETH_PAD_SIZE); /* drop the padding word */
#endif

for (q = p; q != NULL; q = q->next) {
/* Send the data from the pbuf to the interface, one pbuf at a
time. The size of the data in each pbuf is kept in the ->len
variable. */
//send data from(q->payload, q->len);
memcpy(data, q->payload, q->len);
data += q->len;
}

//signal that packet should be sent();
if (bd != NULL)
{
printf("[Send] len=%d, id=%d\n", p->tot_len, ethernetif->tx_i);
XAxiDma_BdSetBufAddr(bd, (uintptr_t)dma_buffer_tx[ethernetif->tx_i]);
XAxiDma_BdSetLength(bd, p->tot_len, ethernetif->txring->MaxTransferLen);
XAxiDma_BdSetCtrl(bd, XAXIDMA_BD_CTRL_TXSOF_MASK | XAXIDMA_BD_CTRL_TXEOF_MASK);
XAxiDma_BdSetId(bd, ethernetif->tx_i);
ret = XAxiDma_BdRingToHw(ethernetif->txring, 1, bd);
LWIP_ASSERT("DMA txring brought one", ret == XST_SUCCESS);
ethernetif->tx_i = (ethernetif->tx_i + 1) % TX_NUM;
}
else
printf("[Send] len=%d (failed)\n", p->tot_len);

MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
if (((u8_t *)p->payload)[0] & 1) {
/* broadcast or multicast packet*/
MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
} else {
/* unicast packet */
MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
}
/* increase ifoutdiscards or ifouterrors on error */

#if ETH_PAD_SIZE
pbuf_add_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif

LINK_STATS_INC(link.xmit);

return ERR_OK;
}

/**
* Should allocate a pbuf and transfer the bytes of the incoming
* packet from the interface into the pbuf.
*
* @param netif the lwip network interface structure for this ethernetif
* @return a pbuf filled with the received packet (including MAC header)
* NULL on memory error
*/
static struct pbuf *
low_level_input(struct netif *netif)
{
struct ethernetif *ethernetif = netif->state;
struct pbuf *p, *q;
u16_t len;
int ret;
uint8_t *data;
uint32_t id;
XAxiDma_Bd *bd;

/* Obtain the size of the packet and put it into the "len"
variable. */
ret = XAxiDma_BdRingFromHw(ethernetif->rxring, 1, &bd);
if (ret return NULL;
len = XAxiDma_BdGetActualLength(bd, ethernetif->rxring->MaxTransferLen);
LWIP_ASSERT("rx packet too big", len id = XAxiDma_BdGetId(bd);
printf("[Recv] len=%d, id=%lu\n", len, id);
data = (uint8_t *)XAxiDma_BdGetBufAddr(bd);

#if ETH_PAD_SIZE
len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
#endif

/* We allocate a pbuf chain of pbufs from the pool. */
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);

if (p != NULL) {

#if ETH_PAD_SIZE
pbuf_remove_header(p, ETH_PAD_SIZE); /* drop the padding word */
#endif

/* We iterate over the pbuf chain until we have read the entire
* packet into the pbuf. */
for (q = p; q != NULL; q = q->next) {
/* Read enough bytes to fill this pbuf in the chain. The
* available data in the pbuf is given by the q->len
* variable.
* This does not necessarily have to be a memcpy, you can also preallocate
* pbufs for a DMA-enabled MAC and after receiving truncate it to the
* actually received size. In this case, ensure the tot_len member of the
* pbuf is the sum of the chained pbuf len members.
*/
memcpy(q->payload, data, q->len);
data += q->len;
}
//acknowledge that packet has been read();

MIB2_STATS_NETIF_ADD(netif, ifinoctets, p->tot_len);
if (((u8_t *)p->payload)[0] & 1) {
/* broadcast or multicast packet*/
MIB2_STATS_NETIF_INC(netif, ifinnucastpkts);
} else {
/* unicast packet*/
MIB2_STATS_NETIF_INC(netif, ifinucastpkts);
}
#if ETH_PAD_SIZE
pbuf_add_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
#endif

LINK_STATS_INC(link.recv);
} else {
//drop packet();
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
MIB2_STATS_NETIF_INC(netif, ifindiscards);
}

// 继续接收后续数据包
ret = XAxiDma_BdRingFree(ethernetif->rxring, 1, bd);
LWIP_ASSERT("DMA rxring freed one", ret == XST_SUCCESS);
ret = XAxiDma_BdRingAlloc(ethernetif->rxring, 1, &bd);
LWIP_ASSERT("DMA rxring allocated one", ret == XST_SUCCESS);
XAxiDma_BdSetBufAddr(bd, (uintptr_t)dma_buffer_rx[ethernetif->rx_i]);
XAxiDma_BdSetLength(bd, sizeof(dma_buffer_rx[ethernetif->rx_i]), ethernetif->rxring->MaxTransferLen);
XAxiDma_BdSetCtrl(bd, 0);
XAxiDma_BdSetId(bd, ethernetif->rx_i);
ret = XAxiDma_BdRingToHw(ethernetif->rxring, 1, bd);
LWIP_ASSERT("DMA rxring brought one", ret == XST_SUCCESS);
ethernetif->rx_i = (ethernetif->rx_i + 1) % RX_NUM;

return p;
}

/**
* This function should be called when a packet is ready to be read
* from the interface. It uses the function low_level_input() that
* should handle the actual reception of bytes from the network
* interface. Then the type of the received packet is determined and
* the appropriate input function is called.
*
* @param netif the lwip network interface structure for this ethernetif
*/
/*static */void
ethernetif_input(struct netif *netif)
{
//struct ethernetif *ethernetif;
//struct eth_hdr *ethhdr;
struct pbuf *p;

//ethernetif = netif->state;

/* move received packet into a new pbuf */
p = low_level_input(netif);
/* if no packet could be read, silently ignore this */
if (p != NULL) {
/* pass all packets to ethernet_input, which decides what packets it supports */
if (netif->input(p, netif) != ERR_OK) {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pbuf_free(p);
p = NULL;
}
}
}

/**
* Should be called at the beginning of the program to set up the
* network interface. It calls the function low_level_init() to do the
* actual setup of the hardware.
*
* This function should be passed as a parameter to netif_add().
*
* @param netif the lwip network interface structure for this ethernetif
* @return ERR_OK if the loopif is initialized
* ERR_MEM if private data couldn't be allocated
* any other err_t on error
*/
err_t
ethernetif_init(struct netif *netif)
{
struct ethernetif *ethernetif;

LWIP_ASSERT("netif != NULL", (netif != NULL));

ethernetif = mem_malloc(sizeof(struct ethernetif));
if (ethernetif == NULL) {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
return ERR_MEM;
}

#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "XC7A35TFGG484-2_ETH"; // 路由器里面看到的设备名称
#endif /* LWIP_NETIF_HOSTNAME */

/*
* Initialize the snmp variables and counters inside the struct netif.
* The last argument should be replaced with your link speed, in units
* of bits per second.
*/
MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);

netif->state = ethernetif;
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
/* We directly use etharp_output() here to save a function call.
* You can instead declare your own function an call etharp_output()
* from it if you have to do some checks before sending (e.g. if link
* is available...) */
#if LWIP_IPV4
netif->output = etharp_output;
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
netif->output_ip6 = ethip6_output;
#endif /* LWIP_IPV6 */
netif->linkoutput = low_level_output;

ethernetif->ethaddr = (struct eth_addr *) & (netif->hwaddr[0]);

/* initialize the hardware */
low_level_init(netif);

return ERR_OK;
}

/* 网线插拔检测 */
void ethernetif_check_link(struct netif *netif)
{
uint16_t value;

XAxiEthernet_PhyRead(&xaxieth, PHY_ADDR, PHY_BMSR, &value);
if (value & PHY_LINKED_STATUS)
{
/* 已插入网线 */
if (!netif_is_link_up(netif))
{
if (value & PHY_AUTONEGO_COMPLETE) // 自动协商完毕
{
// 配置速率和双工模式
printf("Link is up!\n");
XAxiEthernet_PhyRead(&xaxieth, PHY_ADDR, PHY_PHYSR, &value);
if (value & PHY_SPEED1_STATUS)
{
printf("Speed: 1000Mbps\n");
XAxiEthernet_SetOperatingSpeed(&xaxieth, XAE_SPEED_1000_MBPS);
}
else if (value & PHY_SPEED0_STATUS)
{
printf("Speed: 100Mbps\n");
XAxiEthernet_SetOperatingSpeed(&xaxieth, XAE_SPEED_100_MBPS);
}
else
{
printf("Speed: 10Mbps\n");
XAxiEthernet_SetOperatingSpeed(&xaxieth, XAE_SPEED_10_MBPS);
}

// AXI 1G/2.5G Ethernet Subsystem IP核不支持半双工模式, 所以这里不做设置
if (value & PHY_DUPLEX_STATUS)
printf("Duplex: full\n");
else
printf("Duplex: half\n");

XAxiEthernet_Start(&xaxieth);
netif_set_link_up(netif);
}
}
}
else
{
/* 已拔出网线 */
if (netif_is_link_up(netif))
{
printf("Link is down!\n");
XAxiEthernet_Stop(&xaxieth);
netif_set_link_down(netif);
}
}
}

#endif /* 0 */

ethernetif.h:

#ifndef ETHERNETIF_H
#define ETHERNETIF_H

#ifndef _BV
#define _BV(n) (1ull #endif

#define PHY_ADDR 1

#define PHY_PHYSR 0x1a
#define PHY_SPEED1_STATUS 0x20
#define PHY_SPEED0_STATUS 0x10
#define PHY_DUPLEX_STATUS 0x08

#define PHY_BMSR 0x01
#define PHY_AUTONEGO_COMPLETE 0x20
#define PHY_LINKED_STATUS 0x04

err_t ethernetif_init(struct netif *netif);
void ethernetif_input(struct netif *netif);
void ethernetif_check_link(struct netif *netif);

#endif

lwipopts.h:

#ifndef LWIP_LWIPOPTS_H
#define LWIP_LWIPOPTS_H

#define NO_SYS 1 // 无操作系统
#define SYS_LIGHTWEIGHT_PROT 0 // 不进行临界区保护

#define LWIP_NETCONN 0
#define LWIP_SOCKET 0

#define MEM_ALIGNMENT 4 // 4字节对齐方式
#define MEM_SIZE (64 * 1024) // lwip的mem_malloc函数使用的堆内存的大小
#define MEM_OVERFLOW_CHECK 1
#define MEM_SANITY_CHECK 1

// 配置TCP
#define TCP_MSS 1500
#define LWIP_TCP_SACK_OUT 1 // 允许批量确认

// 配置DHCP
#define LWIP_DHCP 1
#define LWIP_NETIF_HOSTNAME 1

// 配置DNS
#define LWIP_DNS 1

// 配置HTTP服务器
#define HTTPD_FSDATA_FILE "fsdata_custom.h"

// 广播包过滤器
#define IP_SOF_BROADCAST 1
#define IP_SOF_BROADCAST_RECV 1

// 配置IPv6
#define LWIP_IPV6 1
#define LWIP_ND6_RDNSS_MAX_DNS_SERVERS LWIP_DNS // 允许SLAAC获取DNS服务器的地址

#endif

arch/cc.h:

#ifndef LWIP_ARCH_CC_H
#define LWIP_ARCH_CC_H

#define LWIP_RAND() ((u32_t)rand())
#define PACK_STRUCT_BEGIN __attribute__((packed, aligned(1)))

#endif

【程序运行结果】

Hello World
Successfully ran Hello World application
MAC address: 00:0A:35:00:01:02
DMA Rx descriptor: 0x800666c0
DMA Tx descriptor: 0x80066940
DMA Rx buffer: 0x80066bc0
DMA Tx buffer: 0x8006aa40
IPv6 link-local address: FE80::20A:35FF:FE00:102
[Recv] len=52, id=0
Link is up!
Speed: 1000Mbps
Duplex: full
[Send] len=350, id=0
[Send] len=86, id=1
[Send] len=78, id=2
[Send] len=86, id=3
[Send] len=86, id=4
[Send] len=350, id=5
[Recv] len=60, id=1
[Recv] len=208, id=2
[Recv] len=146, id=3
[Send] len=70, id=6
[Recv] len=142, id=4
[Send] len=86, id=7
[Recv] len=86, id=5
[Recv] len=590, id=6
[Send] len=350, id=8
[Recv] len=60, id=7
[Recv] len=60, id=8
[Recv] len=590, id=9
[Send] len=42, id=9
[Send] len=42, id=0
[Send] len=42, id=1
[Send] len=78, id=2
[Send] len=42, id=3
DHCP supplied address!
IP address: 192.168.1.8
Subnet mask: 255.255.255.0
Default gateway: 192.168.1.1
DNS Server: 192.168.1.1
[Send] len=42, id=4
IPv6 address 1: 2409:8A62:367:4EA0:20A:35FF:FE00:102
DNS Server: 192.168.1.1
[Recv] len=208, id=0
[Send] len=86, id=5
[Send] len=42, id=6
[Send] len=42, id=7
[Recv] len=86, id=1
[Send] len=86, id=8
[Recv] len=208, id=2
[Recv] len=60, id=3
[Recv] len=147, id=4
[Recv] len=208, id=5

【程序固化方法】

https://blog.csdn.net/ZLK1214/article/details/115875025

【未修复的Bug】

2021年4月26日:(必现)程序中用raw API建立一个TCP服务器,固化后运行。在电脑上运行两个网络调试助手同时连接这个TCP服务器,发送大文件。发送了大约十几分钟后,从串口中看到程序突然就无法接收数据包了。只见随后发送了几个数据包(显示了几个[Send]),程序就卡死了,串口发送t无反应(不打印sys_now()的值)。估计是以太网IP核出问题了,无法收发数据包。程序卡在了low_level_output函数的do while循环里面,无法申请到DMA发送描述符而卡死。

审核编辑:符乾江

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 以太网
    +关注

    关注

    40

    文章

    5419

    浏览量

    171592
  • MicroBlaze
    +关注

    关注

    3

    文章

    68

    浏览量

    21548
收藏 人收藏

    评论

    相关推荐

    利用LWIP 2.2实现以太网的DHCP功能

    最近学习了LWIP,了解到目前LWIP的版本已经更新到了2.2版本。LWIP 2.2相较于之前的版本,在协议支持、性能、安全性等方面都有了显著的改进,我将在本帖中探讨如何利用LWIP
    的头像 发表于 11-26 14:37 483次阅读
    利用<b class='flag-5'>LWIP</b> 2.2实现<b class='flag-5'>以太网</b>的DHCP功能

    269元买开源以太网硬件?N2L EtherKit重磅发布!

    端口千兆以太网端口;支持EtherCAT、PROFINET RT/IRT、EtherNet/IP、Modbus、OPC UA等主流工业以太网通信协议,该开发板能轻松满足各种工业场景对工
    的头像 发表于 11-23 01:02 159次阅读
    269元买开源<b class='flag-5'>以太网</b>硬件?N2L EtherKit重磅发布!

    RT-Thread 发布 EtherKit开源以太网硬件!

    千兆以太网端口;支持EtherCAT、PROFINETRT/IRT、EtherNet/IP、Modbus、OPCUA等主流工业以太网通信协议,该开发板能轻松满足各
    的头像 发表于 11-20 01:07 482次阅读
    RT-Thread 发布 EtherKit开源<b class='flag-5'>以太网</b>硬件!

    千兆以太网解决方案:MX2000核心,高性能不高价

    导读在嵌入式开发中,千兆以太网的需求日益增长,特别是在工业自动化、视频监控和云计算等场景。MX2000核心以其高性能和经济性,为您提供了满足高速数据传输和成本效益双重需求的理想解决方
    的头像 发表于 11-15 01:06 166次阅读
    <b class='flag-5'>千兆</b><b class='flag-5'>以太网</b>解决方案:MX2000核心<b class='flag-5'>板</b>,高性能不高价

    为什么通过SFP连接开发板,电脑显示以太网未连接呢?

    想与开发板ZC706进行以太网通信,为什么通过SFP连接开发板,电脑显示以太网未连接呢?但是连接到RJ45就显示未识别网络(证明连上了)?
    发表于 10-11 15:10

    以太网端口和千兆端口的区别

    以太网端口和千兆端口在多个方面存在显著的区别,这些区别主要体现在速度、应用范围、技术规格以及网络性能等方面。
    的头像 发表于 08-07 15:42 1211次阅读

    TNETE2201B 1.25千兆以太网收发器数据表

    电子发烧友网站提供《TNETE2201B 1.25千兆以太网收发器数据表.pdf》资料免费下载
    发表于 07-03 11:42 0次下载
    TNETE2201B 1.25<b class='flag-5'>千兆</b><b class='flag-5'>以太网</b>收发器数据表

    Banana Pi BPI-M4 Berry 开源硬件开发板以太网口和WiFi测试

    Banana Pi BPI-M4 Berry 开源硬件开发板以太网口和WiFi测试
    的头像 发表于 07-02 16:16 881次阅读
    Banana Pi BPI-M4 Berry 开源硬件<b class='flag-5'>开发板</b><b class='flag-5'>以太网</b>口和WiFi测试

    【PHYTEC AM62x开发板试用】ETHRNET以太网口调试过程

    现在将我调试PHYTEC AM62X以太网口过程发表一下。 通过认真看了开发板接口Interface章节内容,一步一步掌握了许多接囗操作方法,比如HDMI,UART,USB,I2C等,这里着重介绍
    发表于 06-25 11:45

    DP83561-SP千兆以太网PHY数据表

    电子发烧友网站提供《DP83561-SP千兆以太网PHY数据表.pdf》资料免费下载
    发表于 06-22 10:19 0次下载
    DP83561-SP<b class='flag-5'>千兆</b>位<b class='flag-5'>以太网</b>PHY数据表

    智嵌物100/1000BASE-T1千兆车载以太网交换机

    ZQWL-AES6502是智嵌物自主研发生产的一款2+5端口千兆车载以太网(100/1000BASE-T1)交换机。具有2个RJ45千兆
    的头像 发表于 06-06 09:29 1111次阅读
    智嵌物<b class='flag-5'>联</b>100/1000BASE-T1<b class='flag-5'>千兆</b>车载<b class='flag-5'>以太网</b>交换机

    如何实现以太网功能的,是用F107或F407芯片自己移植Lwip还是采用SPI转以太网模块?

    一般大家是如何实现以太网功能的,是用F107或F407芯片自己移植Lwip还是采用SPI转以太网模块?
    发表于 05-17 06:49

    【米尔-瑞派兼容树莓派扩展模块-试用体验】初始开发板_米尔-瑞派兼容树莓派扩展模块

    支持摄像头接口(MIPI-CSI/Parallel-IF)、显示器接口(MIPI-DSI/Parallel-IF)、USB2.0、UART、CAN接口、千兆以太网接口等,特别适用于入门级工业人机界面
    发表于 04-29 11:42

    如何使用Vitis自带的LWIP模板进行PS端千兆以太网TCP通信?

    开发板有两路千兆以太网,通过RGMII接口连接,本实验演示如何使用Vitis自带的LWIP模板进行PS端千兆
    的头像 发表于 04-28 10:44 3374次阅读
    如何使用Vitis自带的<b class='flag-5'>LWIP</b>模板进行PS端<b class='flag-5'>千兆</b><b class='flag-5'>以太网</b>TCP通信?

    WT32-ETH02 plus 串口转以太网开发,WT32-ETH01开发板升级款!

    广受欢迎的WT32-ETH01开发板迎来了升级。就是这款启明云端新推出的嵌入式串口转以太网开发板——WT32-ETH02plus。应广大客户的需求,在WT32-ETH01的基础上增
    的头像 发表于 03-16 08:03 755次阅读
    WT32-ETH02 plus 串口转<b class='flag-5'>以太网</b><b class='flag-5'>开发</b>,WT32-ETH01<b class='flag-5'>网</b>关<b class='flag-5'>开发板</b>升级款!