使用microblaze和lwip进行简单的数据传输

使用microblaze和lwip进行简单的数据传输,c,lwip,microblaze,C,Lwip,Microblaze,因此,我在互联网上找不到比echo程序更简单的例子,它适用于microblaze和lwip,对我来说效果很好,以下是它们的文件: echo.c: /* * Copyright (c) 2009 Xilinx, Inc. All rights reserved. * * Xilinx, Inc. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A * COURTESY TO YOU. BY PROV

因此,我在互联网上找不到比echo程序更简单的例子,它适用于microblaze和lwip,对我来说效果很好,以下是它们的文件: echo.c:

/*
 * Copyright (c) 2009 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <stdio.h>
#include <string.h>

#include "lwip/err.h"
#include "lwip/tcp.h"


int transfer_data() {
    return 0;
}

void print_app_header()
{
    xil_printf("\n\r\n\r-----lwIP TCP echo server ------\n\r");
    xil_printf("TCP packets sent to port 6001 will be echoed back\n\r");
}

err_t recv_callback(void *arg, struct tcp_pcb *tpcb,
                               struct pbuf *p, err_t err)
{
    /* do not read the packet if we are not in ESTABLISHED state */
    if (!p) {
        tcp_close(tpcb);
        tcp_recv(tpcb, NULL);
        return ERR_OK;
    }

    /* indicate that the packet has been received */
    tcp_recved(tpcb, p->len);

    /* echo back the payload */
    /* in this case, we assume that the payload is < TCP_SND_BUF */
    if (tcp_sndbuf(tpcb) > p->len) {
        err = tcp_write(tpcb, p->payload, p->len, 1);
    } else
        print("no space in tcp_sndbuf\n\r");

    /* free the received pbuf */
    pbuf_free(p);

    return ERR_OK;
}

err_t accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err)
{
    static int connection = 1;

    /* set the receive callback for this connection */
    tcp_recv(newpcb, recv_callback);

    /* just use an integer number indicating the connection id as the
       callback argument */
    tcp_arg(newpcb, (void*)connection);

    /* increment for subsequent accepted connections */
    connection++;

    return ERR_OK;
}


int start_application()
{
    struct tcp_pcb *pcb;
    err_t err;
    unsigned port = 7;

    /* create new TCP PCB structure */
    pcb = tcp_new();
    if (!pcb) {
        xil_printf("Error creating PCB. Out of Memory\n\r");
        return -1;
    }

    /* bind to specified @port */
    err = tcp_bind(pcb, IP_ADDR_ANY, port);
    if (err != ERR_OK) {
        xil_printf("Unable to bind to port %d: err = %d\n\r", port, err);
        return -2;
    }

    /* we do not need any arguments to callback functions */
    tcp_arg(pcb, NULL);

    /* listen for connections */
    pcb = tcp_listen(pcb);
    if (!pcb) {
        xil_printf("Out of memory while tcp_listen\n\r");
        return -3;
    }

    /* specify callback to use for incoming connections */
    tcp_accept(pcb, accept_callback);

    xil_printf("TCP echo server started @ port %d\n\r", port);

    return 0;
}
/*
*版权所有(c)2009 Xilinx,Inc.保留所有权利。
*
*锡林克斯公司。
*XILINX以“原样”的形式提供此设计、代码或信息
*谢谢你。通过以下方式提供此设计、代码或信息:
*此功能的一种可能实现,应用程序或
*根据标准,XILINX不表示此实现
*不存在任何侵权索赔,您对此负责
*以获取实施过程中可能需要的任何权利。
*XILINX明确否认与以下内容有关的任何担保:
*实施的充分性,包括但不限于
*本实施是免费的任何保证或声明
*从侵权索赔、默示适销性保证
*以及适合特定用途。
*
*/
#包括
#包括
#包括“lwip/err.h”
#包括“lwip/tcp.h”
int传输_数据(){
返回0;
}
无效打印\应用\标题()
{
xil_printf(“\n\r\n\r-----lwIP TCP回显服务器-------\n\r”);
xil_printf(“发送到端口6001的TCP数据包将被回显\n\r”);
}
err_t recv_回调(void*arg,struct tcp_pcb*tpcb,
结构pbuf*p,err\u t err)
{
/*如果我们未处于已建立状态,请不要读取数据包*/
如果(!p){
tcp_关闭(tpcb);
tcp_recv(tpcb,空);
返回ERR_OK;
}
/*指示已收到数据包*/
接收到的tcp(tpcb,p->len);
/*回显有效载荷*/
/*在这种情况下,我们假设有效负载是p->len){
err=tcp_write(tpcb,p->payload,p->len,1);
}否则
打印(“tcp_sndbuf中没有空格\n\r”);
/*释放收到的pbuf*/
无pbuf_(p);
返回ERR_OK;
}
err\u t accept\u回调(void*arg,struct tcp\u pcb*newpcb,err\u t err)
{
静态int连接=1;
/*设置此连接的接收回调*/
tcp_recv(newpcb,recv_回调);
/*只需使用一个表示连接id的整数作为
回调参数*/
tcp_arg(新PCB,(无效*)连接);
/*后续接受连接的增量*/
连接++;
返回ERR_OK;
}
int start_应用程序()
{
结构tcp_pcb*pcb;
犯错;
无符号端口=7;
/*创建新的TCP PCB结构*/
pcb=tcp_new();
如果(!pcb){
xil_printf(“创建PCB时出错。内存不足\n\r”);
返回-1;
}
/*绑定到指定的@port*/
err=tcp\u绑定(pcb、IP地址、任何端口);
如果(err!=err_OK){
xil_printf(“无法绑定到端口%d:err=%d\n\r”,端口,err);
返回-2;
}
/*回调函数不需要任何参数*/
tcp_arg(pcb,空);
/*倾听联系*/
pcb=tcp_侦听(pcb);
如果(!pcb){
xil_printf(“tcp_侦听时内存不足\n\r”);
返回-3;
}
/*指定用于传入连接的回调*/
tcp_接受(pcb,接受_回调);
xil_printf(“TCP回送服务器已在端口%d启动\n\r”,端口);
返回0;
}
下面是主要内容。c:

/*
 * Copyright (c) 2009-2010 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <stdio.h>

#include "xparameters.h"

#include "netif/xadapter.h"

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

/* defined by each RAW mode application */
void print_app_header();
int start_application();
int transfer_data();

/* missing declaration in lwIP */
void lwip_init();

void
print_ip(char *msg, struct ip_addr *ip) 
{
    print(msg);
    xil_printf("%d.%d.%d.%d\n\r", ip4_addr1(ip), ip4_addr2(ip), 
            ip4_addr3(ip), ip4_addr4(ip));
}

void
print_ip_settings(struct ip_addr *ip, struct ip_addr *mask, struct ip_addr *gw)
{

    print_ip("Board IP: ", ip);
    print_ip("Netmask : ", mask);
    print_ip("Gateway : ", gw);
}

int main()
{
    struct netif *netif, server_netif;
    struct ip_addr ipaddr, netmask, gw;

    /* the mac address of the board. this should be unique per board */
    unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };

    netif = &server_netif;

    init_platform();

    /* initliaze IP addresses to be used */
    IP4_ADDR(&ipaddr,  192, 168,   1, 10);
    IP4_ADDR(&netmask, 255, 255, 255,  0);
    IP4_ADDR(&gw,      192, 168,   1,  1);

    print_app_header();
    print_ip_settings(&ipaddr, &netmask, &gw);

    lwip_init();

    /* Add network interface to the netif_list, and set it as default */
    if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, PLATFORM_EMAC_BASEADDR)) {
        xil_printf("Error adding N/W interface\n\r");
        return -1;
    }
    netif_set_default(netif);

    /* Create a new DHCP client for this interface.
     * Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at
     * the predefined regular intervals after starting the client.
     */
    /* dhcp_start(netif); */

    /* now enable interrupts */
    platform_enable_interrupts();

    /* specify that the network if is up */
    netif_set_up(netif);

    /* start the application (web server, rxtest, txtest, etc..) */
    start_application();

    /* receive and process packets */
    while (1) {
        xemacif_input(netif);
        tcp_write()
        //transfer_data();
    }

    /* never reached */
    cleanup_platform();

    return 0;
}
/*
*版权所有(c)2009-2010 Xilinx,Inc.保留所有权利。
*
*锡林克斯公司。
*XILINX以“原样”的形式提供此设计、代码或信息
*谢谢你。通过以下方式提供此设计、代码或信息:
*此功能的一种可能实现,应用程序或
*根据标准,XILINX不表示此实现
*不存在任何侵权索赔,您对此负责
*以获取实施过程中可能需要的任何权利。
*XILINX明确否认与以下内容有关的任何担保:
*实施的充分性,包括但不限于
*本实施是免费的任何保证或声明
*从侵权索赔、默示适销性保证
*以及适合特定用途。
*
*/
#包括
#包括“xparameters.h”
#包括“netif/xadapter.h”
#包括“platform.h”
#包括“platform_config.h”
/*由每个原始模式应用程序定义*/
无效打印应用程序标题();
int start_应用程序();
int传输_数据();
/*lwIP中缺少声明*/
void lwip_init();
无效的
打印ip(字符*消息,结构ip地址*ip)
{
印刷品(味精);
xil_printf(“%d.%d.%d.%d\n\r”、ip4_地址1(ip)、ip4_地址2(ip),
ip4_地址3(ip),ip4_地址4(ip));
}
无效的
打印ip设置(结构ip地址*ip,结构ip地址*掩码,结构ip地址*gw)
{
打印ip(“板ip:,ip”);
打印ip(“网络掩码:”,掩码);
打印ip(“网关:”,gw);
}
int main()
{
结构netif*netif,服务器netif;
结构ip地址ipaddr,网络掩码,gw;
/*电路板的mac地址。每个电路板的mac地址应该是唯一的*/
无符号字符mac_以太网_地址[]={0x00,0x0a,0x35,0x00,0x01,0x02};
netif=&server\u netif;
init_平台();
/*初始化要使用的IP地址*/
IP4地址(ipaddr,192、168、1、10);
IP4_地址(网络掩码,255、255、255、0);
IP4地址(&gw,192,168,1,1);
打印应用程序标题();
打印ip设置(&ipaddr、&netmask、&gw);
lwip_init();
/*将网络接口添加到netif_列表,并将其设置为默认值*/
如果(!xemac\u add(netif、&ipaddr、&netmask、&gw、mac\u以太网地址、平台\u EMAC\u BASEADDR)){
xil_printf(“添加N/W接口时出错\N\r”);
返回-1;
}
netif\u设置默认值(netif);
/*为此接口创建新的DHCP客户端。
*注意:必须在以下位置调用dhcp\u fine\u tmr()和dhcp\u rough\u tmr()
*启动客户端后预定义的定期间隔。
*/
/*dhcp_启动(netif)*/
/*现在启用中断*/
平台启用中断();
/*指定网络是否已启动*/
netif设置(netif);
/*sta
 err = tcp_write(tpcb, p->payload, p->len, 1);
err = tcp_write(tpcb, "hey", 3, 1);