cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Observer
Observer
297 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
0 Replies