UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Observer wpmoorenasa
Observer
161 Views
Registered: ‎05-08-2019

Freecode: inbyte() with millisecond timeout

int  platform_uart_inbyte(uint16_t timeout /*msec*/) {
    XTime tstart;
    XTime tnow;
    uint64_t elapsed_ticks;
    double   elapsed_msec; //milliseconds
    double   elapsed_sec;
    double   ftimeout = (double)timeout;

    // Poll for Received Data until Timeout Exceeded
    XTime_GetTime(&tstart);
	while(1) {
		if (XUartPs_IsReceiveData(STDIN_BASEADDRESS)) {
			return (int)XUartPs_ReadReg(STDIN_BASEADDRESS, XUARTPS_FIFO_OFFSET);
		}
	    XTime_GetTime(&tnow);
        //sleep(1);
        elapsed_ticks = (tnow - tstart);
        elapsed_sec   = (elapsed_ticks / (double)(COUNTS_PER_SECOND));
        elapsed_msec  = elapsed_sec * 1000.0;
        if (elapsed_msec > ftimeout) {
           return -1; //TIMEOUT
        }
	}

	//Shouldn't get here
    return -1;
}



 

0 Kudos