Simvik

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 divisor BRR to UART_CLOCK / baud, rounded to the nearest whole number, and enables the UART with both its transmitter and receiver (UE, TE, RE in CR1).
  • uart_putc(u, c) sends one byte. DR holds only one byte at a time: writing it before TXE is 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 0x0A

Learn it first

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

More UART problems