2013-04-01 29 views
3

,所以我不能在互聯網上找到任何簡單的例子比MicroBlaze和LWIP回聲程序,它工作正常,我這裏有thier文件: echo.c:簡單的數據傳輸和LWIP

/* 
* 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; 
} 

這裏是main.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; 
} 

現在我無法理解這一切的代碼(一些,但不是全部的話)你們可以告訴我,一開始如何改變這種代碼,以便代替呼應返回相同的數據,它發回給主機一個字符串conatining「嘿「。 謝謝。

回答

2

此線

err = tcp_write(tpcb, p->payload, p->len, 1); 

從rescive數據p(P->有效載荷 - 傳入消息)發送回數據 改變它,像

err = tcp_write(tpcb, "hey", 3, 1);