From aec9eb7f6a634ead6db873aba1803067133b23ef Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Sun, 5 Jul 2020 22:57:11 +0200 Subject: [PATCH] cpu/stm32: Fix uart_init() - Make use of the fact that gpio_init_af() does not need prior call to gpio_init() for all STM32 families anymore and drop call to gpio_init() - Initialize the UART periph first, before initializing the pins - While uninitialized, the UART periph will send signal LOW to TXD. This results in a start bit being picked up by the other side. - Instead, we do not connect the UART periph to the pins until it is initialized, so that the TXD level will already be HIGH when the pins are attached. - This results in no more garbage being send during initialization --- cpu/stm32/periph/uart.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cpu/stm32/periph/uart.c b/cpu/stm32/periph/uart.c index 5cba9470ec9f..0f9791fa7999 100644 --- a/cpu/stm32/periph/uart.c +++ b/cpu/stm32/periph/uart.c @@ -117,9 +117,6 @@ static inline void uart_init_cts_pin(uart_t uart) static inline void uart_init_pins(uart_t uart, uart_rx_cb_t rx_cb) { /* configure TX pin */ - gpio_init(uart_config[uart].tx_pin, GPIO_OUT); - /* set TX pin high to avoid garbage during further initialization */ - gpio_set(uart_config[uart].tx_pin); #ifdef CPU_FAM_STM32F1 gpio_init_af(uart_config[uart].tx_pin, GPIO_AF_OUT_PP); #else @@ -172,8 +169,6 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) tsrb_init(&uart_tx_rb[uart], uart_tx_rb_buf[uart], UART_TXBUF_SIZE); #endif - uart_init_pins(uart, rx_cb); - uart_enable_clock(uart); /* reset UART configuration -> defaults to 8N1 mode */ @@ -199,6 +194,12 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) uart_init_usart(uart, baudrate); #endif + /* Attach pins to enabled UART periph. Note: It is important that the UART + * interface is configured prior to attaching the pins, as otherwise the + * signal level flickers during initialization resulting in garbage being + * sent. */ + uart_init_pins(uart, rx_cb); + /* enable RX interrupt if applicable */ if (rx_cb) { NVIC_EnableIRQ(uart_config[uart].irqn);