Search code examples
puttyuartxilinxbare-metalzynq

why there is a overlapped output at putty while calling a function is written using xuartps.h to send data to uart in normal mode in zynq?


I am trying to send data from zynq soc 7000 board to pc using uart through a function (driver xuartps.h). within the main program, the code works but calling it from function, output at putty gets overlapped. i tried to debug the code but it looks fine and get output as expected.Need help. thanks

#include "xil_printf.h"
#include<stdlib.h>
#include "xuartps.h"


void uart_output(){


        char *p;
        char  tip[100]="hi : uart_text_multiple time \n\r\0";

        u32 transmittedBytes;
        u32  totalTransmittedBytes;
        u32 status;
        u16 byteCnt;


        XUartPs_Config *PiUartConfig;
        XUartPs PiUart;
        byteCnt=0;


        PiUartConfig=XUartPs_LookupConfig(XPAR_PS7_UART_0_DEVICE_ID);

        status = XUartPs_CfgInitialize(&PiUart,PiUartConfig, PiUartConfig->BaseAddress);

        if(status!=XST_SUCCESS)
            print("Uart initialization failed...\n\r");
        status = XUartPs_SetBaudRate(&PiUart, 115200);

        if(status!=XST_SUCCESS)
                print("BaudRATE init failed....\n\r");
        int o=0;

        p =tip;

        while(*p != '\0'){

            byteCnt+=1;
            p++;

            }



        do{
            totalTransmittedBytes=0;
            p =tip;


            while(totalTransmittedBytes < byteCnt){
                 transmittedBytes = XUartPs_Send(&PiUart, (u8*)&p[totalTransmittedBytes], byteCnt-totalTransmittedBytes);
                 totalTransmittedBytes += transmittedBytes;
             }
                 p++;
                 o++;

        }while(o<10);

}

int main()
{


        init_platform();

        uart_output();
        uart_output();

        cleanup_platform();
    return 0;
}

new output at putty


Solution

  • The inner loop which calls XUartPs_Send has a few mistakes.

     while(totalTransmittedBytes<byteCnt+2){
         transmittedBytes = XUartPs_Send(&PiUart, (u8*)&p[totalTransmittedBytes],byteCnt);
         totalTransmittedBytes += transmittedBytes;
     }
    

    byteCnt holds the string's length, so the loop condition should be changed to totalTransmittedBytes<byteCnt. XUartPs_Send is a non-blocking function and its returned value indicates the number of bytes sent successfully. If XUartPs_Send fails to send byteCnt bytes in the first call, it should be called with byteCnt-totalTransmittedBytes as the last argument to send the remaining of the string. Applying these changes to your code, the following snippet would be the result:

    while(totalTransmittedBytes < byteCnt){
         transmittedBytes = XUartPs_Send(&PiUart, (u8*)&p[totalTransmittedBytes], byteCnt-totalTransmittedBytes);
         totalTransmittedBytes += transmittedBytes;
     }
    

    Xilinx usually provides some example projects for most of its cores. xuartps example projects can be found here.