Simvik

Driver: receive bytes and detect an overrun

Read the UART's receive register without blocking, and report lost bytes. A hard embedded C problem (UART), run and graded in the browser.

hard · UART · driver · registers · uart · overrun

Write the receive side of the UART driver. The UART is already initialised and receiving; the test's input arrives on its receiver one byte at a time, whether or not your code is ready for it.

uart_getc(u) must not block:

  • If an overrun happened (ORE is set in SR), clear it and return UART_OVERRUN. On this UART, ORE is cleared by reading SR and then reading DR; the byte read from DR is discarded.
  • Otherwise, if a byte has been received (RXNE), return it (0 to 255). Reading DR takes the byte and clears RXNE.
  • Otherwise return UART_EMPTY.

Starter code

int uart_getc(uart_t *u) {
  return UART_EMPTY;
}

Example tests

Receive a word while polling

init();
collect(5, 5000);

Expected output:

hello

Too busy to read, so bytes are lost

init();
stall(500);
collect(1, 50);
printf("%d\n", uart_getc(UART1));

Expected output:

[-2]
-1

Learn it first

  • UART · Baud rate, framing, errors, flow control and DMA reception.

More UART problems