Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 349 of 9365. Communication Protocols and Data TransferFree lesson

Handling UART Receive Status Flags

Detect incoming data availability and identify hardware-level errors like overrun, framing, or parity mismatches through status register monitoring.

Handling UART Receive Status Flags

In this lesson, you will learn how to monitor UART hardware status registers to confirm data arrival and detect potential communication errors.

When your microcontroller receives data via UART, the hardware doesn't just hand you the byte automatically; it sets specific flags in a status register to tell you what happened. Asynchronous communication is prone to timing mismatches and electrical noise, so the hardware provides "Status Flags" that act as a health check for every incoming byte.

Identifying Data Availability

The most common flag you will interact with is the Receive Data Register Not Empty (RXNE) flag. When the UART shift register successfully assembles a full byte, it moves that data into the receive buffer and flips the RXNE flag to logic high. Your software must poll this bit or trigger an interrupt to know that data is waiting to be read.

Recognizing Hardware-Level Errors

Data transmission isn't always perfect. The hardware status register typically contains three critical error bits:

  1. Overrun Error (ORE): This occurs if you fail to read the previous byte from the buffer before a new one arrives. The old data is overwritten, and the incoming byte is lost.
  2. Framing Error (FE): This happens if the receiver fails to detect a "Stop Bit" at the expected time. This usually indicates a baud rate mismatch between the sender and receiver.
  3. Parity Error (PE): If you have enabled parity checking, this flag triggers if the received parity bit does not match the parity of the data, indicating electrical noise corrupted the signal.

Implementation Example

To safely process incoming data, your logic should look like this:

if (UART_Status & FLAG_RXNE) {
    if (UART_Status & (FLAG_ORE | FLAG_FE | FLAG_PE)) {
        // Handle error: flush buffer or reset UART
    } else {
        uint8_t receivedData = UART_DataRegister;
        // Process valid data
    }
}

Always check the error flags before processing the data. If an error is detected, the byte currently in the buffer may be invalid, and you may need to perform a "clear error" sequence defined by your specific hardware manual to resume communication.

Key Takeaway: Always verify the integrity of the status register before consuming data, as RXNE tells you data is ready, but error flags tell you if that data is actually trustworthy.