/*******************************************************************************
* Function Name: UART_1_GetChar
********************************************************************************
*
* Summary:
* Reads UART RX buffer immediately, if data is not available or an error
* condition exists, zero is returned; otherwise, character is read and
* returned.
*
* Parameters:
* None.
*
* Return:
* Character read from UART RX buffer. ASCII characters from 1 to 255 are valid.
* A returned zero signifies an error condition or no data available.
*
* Global Variables:
* UART_1_rxBuffer - RAM buffer pointer for save received data.
* UART_1_rxBufferWrite - cyclic index for write to rxBuffer,
* checked to identify new data.
* UART_1_rxBufferRead - cyclic index for read from rxBuffer,
* incremented after each byte has been read from buffer.
* UART_1_rxBufferLoopDetect - creared if loop condition was detected
* in RX ISR.
*
* Reentrant:
* No.
*
*******************************************************************************/
uint8 UART_1_GetChar(void)
{
uint8 rxData = 0u;
uint8 rxStatus;
#if(UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH)
uint8 loc_rxBufferRead;
uint8 loc_rxBufferWrite;
/* Protect variables that could change on interrupt. */
/* Disable Rx interrupt. */
#if(UART_1_RX_INTERRUPT_ENABLED)
UART_1_DisableRxInt();
#endif /* UART_1_RX_INTERRUPT_ENABLED */
loc_rxBufferRead = UART_1_rxBufferRead;
loc_rxBufferWrite = UART_1_rxBufferWrite;
if( (UART_1_rxBufferLoopDetect != 0u) || (loc_rxBufferRead != loc_rxBufferWrite) )
{
rxData = UART_1_rxBuffer[loc_rxBufferRead];
loc_rxBufferRead++;
if(loc_rxBufferRead >= UART_1_RXBUFFERSIZE)
{
loc_rxBufferRead = 0u;
}
/* Update the real pointer */
UART_1_rxBufferRead = loc_rxBufferRead;
if(UART_1_rxBufferLoopDetect > 0u )
{
UART_1_rxBufferLoopDetect = 0u;
#if( (UART_1_RX_INTERRUPT_ENABLED) && (UART_1_FLOW_CONTROL != 0u) )
/* When Hardware Flow Control selected - return RX mask */
#if( UART_1_HD_ENABLED )
if((UART_1_CONTROL_REG & UART_1_CTRL_HD_SEND) == 0u)
{ /* In Half duplex mode return RX mask only if
* RX configuration set, otherwise
* mask will be returned in LoadRxConfig() API.
*/
UART_1_RXSTATUS_MASK_REG |= UART_1_RX_STS_FIFO_NOTEMPTY;
}
#else
UART_1_RXSTATUS_MASK_REG |= UART_1_RX_STS_FIFO_NOTEMPTY;
#endif /* end UART_1_HD_ENABLED */
#endif /* UART_1_RX_INTERRUPT_ENABLED and Hardware flow control*/
}
}
else
{ rxStatus = UART_1_RXSTATUS_REG;
if((rxStatus & UART_1_RX_STS_FIFO_NOTEMPTY) != 0u)
{ /* Read received data from FIFO*/
rxData = UART_1_RXDATA_REG;
/*Check status on error*/
if((rxStatus & (UART_1_RX_STS_BREAK | UART_1_RX_STS_PAR_ERROR |
UART_1_RX_STS_STOP_ERROR | UART_1_RX_STS_OVERRUN)) != 0u)
{
rxData = 0u;
}
}
}
/* Enable Rx interrupt. */
#if(UART_1_RX_INTERRUPT_ENABLED)
UART_1_EnableRxInt();
#endif /* UART_1_RX_INTERRUPT_ENABLED */
#else /* UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */
rxStatus =UART_1_RXSTATUS_REG;
if((rxStatus & UART_1_RX_STS_FIFO_NOTEMPTY) != 0u)
{ /* Read received data from FIFO*/
rxData = UART_1_RXDATA_REG;
/*Check status on error*/
if((rxStatus & (UART_1_RX_STS_BREAK | UART_1_RX_STS_PAR_ERROR |
UART_1_RX_STS_STOP_ERROR | UART_1_RX_STS_OVERRUN)) != 0u)
{
rxData = 0u;
}
}
#endif /* UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */
return(rxData);
}
|