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 (
OREis set inSR), clear it and returnUART_OVERRUN. On this UART,OREis cleared by readingSRand then readingDR; the byte read fromDRis discarded. - Otherwise, if a byte has been received (
RXNE), return it (0 to 255). ReadingDRtakes the byte and clearsRXNE. - 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:
helloToo busy to read, so bytes are lost
init();
stall(500);
collect(1, 50);
printf("%d\n", uart_getc(UART1));Expected output:
[-2]
-1Learn it first
- UART · Baud rate, framing, errors, flow control and DMA reception.
More UART problems
- Assemble command lines · Collect received characters into lines, with backspace and overflow handled.
- Parse a modem signal report · Validate a +CSQ reply and convert it to dBm.
- Detect the baud rate · Measure a 'U' character's bit times and snap to the nearest standard baud rate.
- Interrupt-driven transmit queue · Queue bytes for the UART and send them from the data-register-empty interrupt.
- Spot OK and ERROR in a modem stream · Feed received characters one at a time and report complete OK or ERROR lines.
- Driver: a polled UART transmitter · Configure a UART from its register map and send bytes without losing any.
- Fix: lost bytes are never reported · A receive routine checks the data flag before the overrun flag, so overruns disappear.
- Command console · Split a command line into words, with quoted arguments, and dispatch it through a command table.
- Greenhouse: parse a console command · Parse a technician's command line, and refuse anything malformed or out of range.