Commit d2a34c62 authored by Damien George's avatar Damien George

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: default avatarDamien George <damien@micropython.org>
parent 7ed99544
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
#include "stm32f0xx_hal_wwdg.h" #include "stm32f0xx_hal_wwdg.h"
#include "stm32f0xx_ll_adc.h" #include "stm32f0xx_ll_adc.h"
#include "stm32f0xx_ll_rtc.h" #include "stm32f0xx_ll_rtc.h"
#include "stm32f0xx_ll_usart.h"
// Enable various HAL modules // Enable various HAL modules
#define HAL_MODULE_ENABLED #define HAL_MODULE_ENABLED
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
#include "stm32f4xx_ll_adc.h" #include "stm32f4xx_ll_adc.h"
#include "stm32f4xx_ll_pwr.h" #include "stm32f4xx_ll_pwr.h"
#include "stm32f4xx_ll_rtc.h" #include "stm32f4xx_ll_rtc.h"
#include "stm32f4xx_ll_usart.h"
// Enable various HAL modules // Enable various HAL modules
#define HAL_ADC_MODULE_ENABLED #define HAL_ADC_MODULE_ENABLED
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
#include "stm32f7xx_ll_adc.h" #include "stm32f7xx_ll_adc.h"
#include "stm32f7xx_ll_pwr.h" #include "stm32f7xx_ll_pwr.h"
#include "stm32f7xx_ll_rtc.h" #include "stm32f7xx_ll_rtc.h"
#include "stm32f7xx_ll_usart.h"
// Enable various HAL modules // Enable various HAL modules
#define HAL_ADC_MODULE_ENABLED #define HAL_ADC_MODULE_ENABLED
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
#include "stm32h7xx_ll_adc.h" #include "stm32h7xx_ll_adc.h"
#include "stm32h7xx_ll_pwr.h" #include "stm32h7xx_ll_pwr.h"
#include "stm32h7xx_ll_rtc.h" #include "stm32h7xx_ll_rtc.h"
#include "stm32h7xx_ll_usart.h"
// Enable various HAL modules // Enable various HAL modules
#define HAL_ADC_MODULE_ENABLED #define HAL_ADC_MODULE_ENABLED
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#include "stm32l0xx_hal_wwdg.h" #include "stm32l0xx_hal_wwdg.h"
#include "stm32l0xx_ll_adc.h" #include "stm32l0xx_ll_adc.h"
#include "stm32l0xx_ll_rtc.h" #include "stm32l0xx_ll_rtc.h"
#include "stm32l0xx_ll_usart.h"
// Enable various HAL modules // Enable various HAL modules
#define HAL_MODULE_ENABLED #define HAL_MODULE_ENABLED
......
...@@ -52,6 +52,7 @@ ...@@ -52,6 +52,7 @@
#include "stm32l4xx_hal_wwdg.h" #include "stm32l4xx_hal_wwdg.h"
#include "stm32l4xx_ll_adc.h" #include "stm32l4xx_ll_adc.h"
#include "stm32l4xx_ll_rtc.h" #include "stm32l4xx_ll_rtc.h"
#include "stm32l4xx_ll_usart.h"
// Enable various HAL modules // Enable various HAL modules
#define HAL_MODULE_ENABLED #define HAL_MODULE_ENABLED
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "stm32wbxx_hal_usart.h" #include "stm32wbxx_hal_usart.h"
#include "stm32wbxx_ll_adc.h" #include "stm32wbxx_ll_adc.h"
#include "stm32wbxx_ll_rtc.h" #include "stm32wbxx_ll_rtc.h"
#include "stm32wbxx_ll_usart.h"
// Enable various HAL modules // Enable various HAL modules
#define HAL_MODULE_ENABLED #define HAL_MODULE_ENABLED
......
...@@ -603,7 +603,7 @@ void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached) { ...@@ -603,7 +603,7 @@ void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached) {
self->attached_to_repl = 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; uint32_t uart_clk = 0;
#if defined(STM32F0) #if defined(STM32F0)
...@@ -672,10 +672,20 @@ uint32_t uart_get_baudrate(pyb_uart_obj_t *self) { ...@@ -672,10 +672,20 @@ uint32_t uart_get_baudrate(pyb_uart_obj_t *self) {
} }
#endif #endif
return uart_clk;
}
uint32_t uart_get_baudrate(pyb_uart_obj_t *self) {
// This formula assumes UART_OVERSAMPLING_16 // 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) { mp_uint_t uart_rx_any(pyb_uart_obj_t *self) {
......
...@@ -86,6 +86,8 @@ void uart_irq_handler(mp_uint_t uart_id); ...@@ -86,6 +86,8 @@ void uart_irq_handler(mp_uint_t uart_id);
void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached); void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached);
uint32_t uart_get_baudrate(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);
mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj); mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj);
bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout); bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout);
int uart_rx_char(pyb_uart_obj_t *uart_obj); int uart_rx_char(pyb_uart_obj_t *uart_obj);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment