Skip to content

Commit

Permalink
stm32/uart: Support low baudrates on LPUART1.
Browse files Browse the repository at this point in the history
By selecting a larger prescaler when needed.

Signed-off-by: Damien George <damien@micropython.org>
  • Loading branch information
dpgeorge committed Jul 26, 2021
1 parent fef2114 commit aa0cf87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
22 changes: 21 additions & 1 deletion ports/stm32/uart.c
Expand Up @@ -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;
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions ports/stm32/uart.h
Expand Up @@ -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);

Expand Down

0 comments on commit aa0cf87

Please sign in to comment.