summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/stm32/uart.c22
-rw-r--r--ports/stm32/uart.h1
2 files changed, 22 insertions, 1 deletions
diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c
index 002b1f168..d2953b264 100644
--- a/ports/stm32/uart.c
+++ b/ports/stm32/uart.c
@@ -488,7 +488,7 @@ bool uart_init(pyb_uart_obj_t *uart_obj,
uart_obj->uartx = UARTx;
- // init UARTx
+ // Set the initialisation parameters for the UART.
UART_HandleTypeDef huart;
memset(&huart, 0, sizeof(huart));
huart.Instance = UARTx;
@@ -499,6 +499,26 @@ bool uart_init(pyb_uart_obj_t *uart_obj,
huart.Init.Mode = UART_MODE_TX_RX;
huart.Init.HwFlowCtl = flow;
huart.Init.OverSampling = UART_OVERSAMPLING_16;
+ #if !defined(STM32F4)
+ huart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
+ #endif
+
+ #if defined(STM32H7) || defined(STM32WB)
+ // Compute the smallest prescaler that will allow the given baudrate.
+ uint32_t presc = UART_PRESCALER_DIV1;
+ if (uart_obj->uart_id == PYB_LPUART_1) {
+ uint32_t source_clk = uart_get_source_freq(uart_obj);
+ for (; presc < UART_PRESCALER_DIV256; ++presc) {
+ uint32_t brr = UART_DIV_LPUART(source_clk, baudrate, presc);
+ if (brr <= LPUART_BRR_MASK) {
+ break;
+ }
+ }
+ }
+ huart.Init.ClockPrescaler = presc;
+ #endif
+
+ // Initialise the UART hardware.
HAL_UART_Init(&huart);
// Disable all individual UART IRQs, but enable the global handler
diff --git a/ports/stm32/uart.h b/ports/stm32/uart.h
index 286b6cdec..0a268ad46 100644
--- a/ports/stm32/uart.h
+++ b/ports/stm32/uart.h
@@ -88,6 +88,7 @@ void uart_deinit(pyb_uart_obj_t *uart_obj);
void uart_irq_handler(mp_uint_t uart_id);
void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached);
+uint32_t uart_get_source_freq(pyb_uart_obj_t *self);
uint32_t uart_get_baudrate(pyb_uart_obj_t *self);
void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate);