From 28604a43fdff8f1a946b775447ca5706986be43d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Tue, 9 Jun 2020 00:59:19 +0200 Subject: [PATCH] uart: improve setup, blocking transmission, and flushing --- lib/uart.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/uart.c b/lib/uart.c index 18c58c0..60254ec 100644 --- a/lib/uart.c +++ b/lib/uart.c @@ -52,6 +52,7 @@ void uart_setup(void) /* enable UART I/O peripheral */ rcc_periph_clock_enable(RCC_USART_PORT(UART_ID)); // enable clock for UART port peripheral rcc_periph_clock_enable(RCC_USART(UART_ID)); // enable clock for UART peripheral + rcc_periph_reset_pulse(RST_USART(UART_ID)); // reset peripheral rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (UART) gpio_set_mode(USART_TX_PORT(UART_ID), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_TX_PIN(UART_ID)); // setup GPIO pin UART transmit gpio_set_mode(USART_RX_PORT(UART_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_RX_PIN(UART_ID)); // setup GPIO pin UART receive @@ -76,8 +77,7 @@ void uart_setup(void) void uart_putchar_blocking(char c) { - uart_flush(); // empty buffer first - usart_send_blocking(USART(UART_ID), c); // send character + usart_send_blocking(USART(UART_ID), c); // send character (this already wait for the buffer to be empty) } void uart_flush(void) @@ -86,6 +86,7 @@ void uart_flush(void) __WFI(); // sleep until interrupt } usart_wait_send_ready(USART(UART_ID)); // wait until transmit register is empty (transmission might not be complete) + while(!usart_get_flag(USART(UART_ID), USART_SR_TC)); // wait for transmission to be complete } void uart_putchar_nonblocking(char c)