diff options
| author | Damien George <damien@micropython.org> | 2021-07-26 13:21:30 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2021-07-26 13:53:50 +1000 |
| commit | aa0cf873bf2c475cf2798461a5e9927e31a53012 (patch) | |
| tree | 75b1f9c36ab689c31cb9298084463a8916b4050d /ports/stm32/uart.c | |
| parent | fef21144047201d639b2692e3a55083c7180cb29 (diff) | |
stm32/uart: Support low baudrates on LPUART1.
By selecting a larger prescaler when needed.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'ports/stm32/uart.c')
| -rw-r--r-- | ports/stm32/uart.c | 22 |
1 files changed, 21 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 |
