Communication Protocols · Interview question

How do you write a UART receive path that doesn't drop bytes?

A strong answer

Use an interrupt-driven receive feeding a ring buffer, not a polling loop. Configure the RX-not-empty interrupt so the ISR fires the instant a byte arrives; the ISR does the minimum, read the data register (which clears the flag), and enqueue the byte into a ring buffer, then returns, and the main loop dequeues and processes bytes when convenient. This decouples the fast, hard-deadline arrival of bytes from the slower, variable processing in the main loop, which is exactly the single-producer/single-consumer ring-buffer pattern. If you instead poll the RX register in a slow main loop, a byte can arrive and be overwritten by the next one before you read it, the hardware sets an overrun-error flag and you lose data. For very high baud rates where even a per-byte interrupt is too much overhead, you escalate to DMA, which transfers received bytes into a memory buffer with no CPU involvement until a half/full-transfer interrupt. The ring buffer's indices shared with the ISR must be volatile, and you handle the buffer-full case (drop or overwrite) deliberately.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

UART

Two wires, no clock, and a pre-agreed baud: the UART frame (start/data/parity/stop), how the receiver recovers timing by oversampling, and an interrupt-driven RX ring buffer.

More UART questions

Browse all 472 interview questions
How do you write a UART receive path that doesn't drop bytes? | EmbeddedPrep.io