Driver: a polled UART transmitter
Configure a UART from its register map and send bytes without losing any. A medium embedded C problem (UART), run and graded in the browser.
medium · UART · driver · registers · uart · polling
Write the transmit half of a UART driver against the register map under Provided. The UART is simulated: its registers behave like an STM32 USART's, and every byte it actually sends shows up in the output as uart1: tx '…'.
uart_init(u, baud)sets the baud-rate divisorBRRtoUART_CLOCK / baud, rounded to the nearest whole number, and enables the UART with both its transmitter and receiver (UE,TE,REinCR1).uart_putc(u, c)sends one byte.DRholds only one byte at a time: writing it beforeTXEis set loses the byte in flight.uart_puts(u, s)sends a NUL-terminated string.
Only u may be used to reach the registers, so the same driver works for any UART instance.
Starter code
void uart_init(uart_t *u, uint32_t baud) {
// Set BRR, then enable the UART with TX and RX.
}
void uart_putc(uart_t *u, char c) {
}
void uart_puts(uart_t *u, const char *s) {
}Example tests
Initialise at 115200 baud and send OK
uart_init(UART1, 115200);
printf("BRR=%u CR1=0x%04X\n", UART1->BRR, UART1->CR1);
uart_puts(UART1, "OK\n");Expected output:
BRR=139 CR1=0x200C
uart1: tx 'O'
uart1: tx 'K'
uart1: tx 0x0ALearn 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: receive bytes and detect an overrun · Read the UART's receive register without blocking, and report lost bytes.
- 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.