From 297892c4f465c1c6991e6bed0649771bbe1374f6 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 1 Sep 2023 09:01:23 +0200 Subject: [PATCH] mimxrt/machine_uart: Add a helper function to change the baudrate. And use it in the Bluetooth bindings instead of setting the baudrate by a call to the NXP lib. Also fixes machine_uart.c to work with a baud rate of 921600. Signed-off-by: robert-hh --- ports/mimxrt/machine_uart.c | 32 +++++++++++++++++++++++++------- ports/mimxrt/modmachine.h | 2 ++ ports/mimxrt/mpbthciport.c | 7 +------ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/ports/mimxrt/machine_uart.c b/ports/mimxrt/machine_uart.c index ab5d13e083..fba2b7fe30 100644 --- a/ports/mimxrt/machine_uart.c +++ b/ports/mimxrt/machine_uart.c @@ -33,6 +33,7 @@ #include "fsl_lpuart.h" #include "fsl_iomuxc.h" #include CLOCK_CONFIG_H +#include "modmachine.h" #include "pin.h" #define DEFAULT_UART_BAUDRATE (115200) @@ -146,12 +147,34 @@ void LPUART_UserCallback(LPUART_Type *base, lpuart_handle_t *handle, status_t st } } -static void machine_uart_ensure_active(machine_uart_obj_t *uart) { +static void machine_uart_ensure_active(machine_uart_obj_t *uart) { if (uart->lpuart->CTRL == 0) { mp_raise_OSError(EIO); } } +#if !defined(MIMXRT117x_SERIES) +static inline void uart_set_clock_divider(uint32_t baudrate) { + // For baud rates < 460800 divide the clock by 10, supporting baud rates down to 50 baud. + if (baudrate >= 460800) { + CLOCK_SetDiv(kCLOCK_UartDiv, 0); + } else { + CLOCK_SetDiv(kCLOCK_UartDiv, 9); + } +} +#endif + +void machine_uart_set_baudrate(mp_obj_t uart_in, uint32_t baudrate) { + machine_uart_obj_t *uart = MP_OBJ_TO_PTR(uart_in); + #if defined(MIMXRT117x_SERIES) + // Use the Lpuart1 clock value, which is set for All UART devices. + LPUART_SetBaudRate(uart->lpuart, baudrate, CLOCK_GetRootClockFreq(kCLOCK_Root_Lpuart1)); + #else + uart_set_clock_divider(baudrate); + LPUART_SetBaudRate(uart->lpuart, baudrate, CLOCK_GetClockRootFreq(kCLOCK_UartClkRoot)); + #endif +} + STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, flow=%s, " @@ -292,12 +315,7 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args // Use the Lpuart1 clock value, which is set for All UART devices. LPUART_Init(self->lpuart, &self->config, CLOCK_GetRootClockFreq(kCLOCK_Root_Lpuart1)); #else - // For baud rates < 1000000 divide the clock by 10, supporting baud rates down to 50 baud. - if (self->config.baudRate_Bps > 1000000) { - CLOCK_SetDiv(kCLOCK_UartDiv, 0); - } else { - CLOCK_SetDiv(kCLOCK_UartDiv, 9); - } + uart_set_clock_divider(self->config.baudRate_Bps); LPUART_Init(self->lpuart, &self->config, CLOCK_GetClockRootFreq(kCLOCK_UartClkRoot)); #endif LPUART_TransferCreateHandle(self->lpuart, &self->handle, LPUART_UserCallback, self); diff --git a/ports/mimxrt/modmachine.h b/ports/mimxrt/modmachine.h index 1f669af09e..904419df2c 100644 --- a/ports/mimxrt/modmachine.h +++ b/ports/mimxrt/modmachine.h @@ -51,4 +51,6 @@ void machine_i2s_init0(); void machine_i2s_deinit_all(void); void machine_rtc_start(void); +void machine_uart_set_baudrate(mp_obj_t uart, uint32_t baudrate); + #endif // MICROPY_INCLUDED_MIMXRT_MODMACHINE_H diff --git a/ports/mimxrt/mpbthciport.c b/ports/mimxrt/mpbthciport.c index c83c765871..d8b3841364 100644 --- a/ports/mimxrt/mpbthciport.c +++ b/ports/mimxrt/mpbthciport.c @@ -34,9 +34,6 @@ #include "modmachine.h" #include "mpbthciport.h" -#include "fsl_lpuart.h" -#include CLOCK_CONFIG_H - #if MICROPY_PY_BLUETOOTH #define DEBUG_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) @@ -111,9 +108,7 @@ int mp_bluetooth_hci_uart_deinit(void) { int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { DEBUG_printf("mp_bluetooth_hci_uart_set_baudrate(%lu)\n", baudrate); if (mp_bthci_uart != MP_OBJ_NULL) { - // This struct is not public, so we use the base defined in board config files. - // machine_uart_obj_t uart = (machine_uart_obj_t *) MP_PTR_FROM_OBJ(mp_bthci_uart); - LPUART_SetBaudRate(MICROPY_HW_BLE_UART_BASE, baudrate, BOARD_BOOTCLOCKRUN_UART_CLK_ROOT); + machine_uart_set_baudrate(mp_bthci_uart, baudrate); } return 0; }