diff options
author | Damien George <damien@micropython.org> | 2021-02-03 16:18:35 +1100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2021-02-14 18:30:49 +1100 |
commit | d2a34c62e73f63a7822c823b5523e5924c28831d (patch) | |
tree | f480533d65bc7cdc02c4b010cb752c01b23ae71a /ports/stm32/uart.c | |
parent | 7ed99544e4cc1c09bd5abf9f54869c3122fa033b (diff) |
stm32/uart: Add uart_set_baudrate function.
This allows changing the baudrate of the UART without reinitialising it
(reinitialising can lead to spurious characters sent on the TX line).
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'ports/stm32/uart.c')
-rw-r--r-- | ports/stm32/uart.c | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index b14a18c6d..8091ba05f 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -603,7 +603,7 @@ void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached) { self->attached_to_repl = attached; } -uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { +uint32_t uart_get_source_freq(pyb_uart_obj_t *self) { uint32_t uart_clk = 0; #if defined(STM32F0) @@ -672,10 +672,20 @@ uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { } #endif + return uart_clk; +} + +uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { // This formula assumes UART_OVERSAMPLING_16 - uint32_t baudrate = uart_clk / self->uartx->BRR; + return uart_get_source_freq(self) / self->uartx->BRR; +} - return baudrate; +void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate) { + LL_USART_SetBaudRate(self->uartx, uart_get_source_freq(self), + #if defined(STM32H7) || defined(STM32WB) + LL_USART_PRESCALER_DIV1, + #endif + LL_USART_OVERSAMPLING_16, baudrate); } mp_uint_t uart_rx_any(pyb_uart_obj_t *self) { |