Search code examples
cvivadozynq

change in endianness while ethernet transmission


I work with zedboard and vivado v2017.3. I am trying to send data stored in a register(slv_reg0 in PL section of Zynq) pointed by baseaddr_p in the following program.

I use lwip echo server application to read this address and send it to the PC via Ethernet.

This part of the code is executed when a echo request (telnet) is received.In this part of the code I also print the read values in gtkterm so that i can verify the received data.

void process_echo_request(void *p)
{
    int sd = (int)p;
    int RECV_BUF_SIZE = 2048;
    char recv_buf[RECV_BUF_SIZE];
    int n,i,nwrote;
    unsigned long my_buffer[10];
    i=0;
    init_platform();
    while(1){
    while(i<11)
    {
    Xuint32 *baseaddr_p = (Xuint32 *)XPAR_MYIP_0_S00_AXI_BASEADDR;
    my_buffer[i] = *(baseaddr_p);
    xil_printf("0x%08x \n\r", my_buffer[i]);
    i++;}
    //xil_printf("0x%08x \n\r", my_buffer);
     /* handle request */
    if ((nwrote = write(sd, my_buffer, sizeof (my_buffer))) < 0) {
        xil_printf("%s: ERROR responding to client echo request. received = %d, written = %d\r\n",
                __FUNCTION__, n, nwrote);
        xil_printf("Closing socket %d\r\n", sd);
     break;

    }

    while (1) {
        /* read a max of RECV_BUF_SIZE bytes from socket */
        if ((n = read(sd, recv_buf, RECV_BUF_SIZE)) < 0) {
            xil_printf("%s: error reading from socket %d, closing socket\r\n", __FUNCTION__, sd);
            break;
        }

        /* break if the recved message = "quit" */
        if (!strncmp(recv_buf, "quit", 4))
            break;

        /* break if client closed connection */
        if (n <= 0)
            break;


        }
    }

    /* close connection */
    close(sd);
    vTaskDelete(NULL);
    cleanup_platform();
}

gtkterm result: read values 0x00000004 0x00000005 0x00000006 0x00000007 0x00000008 0x00000009 0x0000000A 0x0000000B 0x0000000C 0x0000000D 0x0000000E

While receiving these data there seems to a byte reversal happening.

Received data: 04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00 09 00 00 00 0A 00 00 00 0B 00 00 00 0C 00 00 00 0D 00 00 00

Any suggestion on why its happening and a possible workaround for that?


Solution

  • The issue is due to endianess. When you are sending and receiving data it is always good to do host to network and vice-versa conversions.

    There are two POSIX APIs to do those byte order conversions: htonl and ntohl

    On receiving side try to do network to host byte order conversion, if you have access to sender side do the opposite there.