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 <robert@hammelrath.com>
pull/12352/head
robert-hh 2023-09-01 09:01:23 +02:00 zatwierdzone przez Damien George
rodzic 8bd2494c95
commit 297892c4f4
3 zmienionych plików z 28 dodań i 13 usunięć

Wyświetl plik

@ -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);

Wyświetl plik

@ -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

Wyświetl plik

@ -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;
}