Skip to content

Commit

Permalink
cpu/stm32: Fix uart_init()
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
maribu committed Jul 15, 2020
1 parent 73c9161 commit aec9eb7
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cpu/stm32/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand All @@ -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);
Expand Down

0 comments on commit aec9eb7

Please sign in to comment.