This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Faulty implementation of UARTCharGetNonBlocking()

Genius 5820 points

There is an ugly bug (which looks like a mistake done by a beginner in software development) in uart_irda_cir.c / UARTCharGetNonBlocking(): it returns a "signed char" which is either a return code "-1" or the data read from the current UARTs RX FIFO. So in case there is a "-1" read from the RX FIFO, the function returns the same value that is equal to the error return code.

This means when somebody uses this function, it is not possible to differentiate between "error" and "data received". I'd suggest something like this to fix this problem:

signed char UARTCharGetNonBlocking(unsigned int baseAdd,unsigned char *value)
{
    unsigned int lcrRegValue = 0;
    signed char retVal = -1;

    /* Switching to Register Operational Mode of operation. */
    lcrRegValue = UARTRegConfigModeEnable(baseAdd, UART_REG_OPERATIONAL_MODE);

    /* Checking if the RX FIFO(or RHR) has atleast one byte of data. */
    if(HWREG(baseAdd + UART_LSR) & UART_LSR_RX_FIFO_E)
    {
       *value=(signed char)HWREG(baseAdd + UART_RHR);
       retVal=1;
    }

    /* Restoring the value of LCR. */
    HWREG(baseAdd + UART_LCR) = lcrRegValue;

    return retVal;
}