Skip to content

Commit

Permalink
mimxrt/mpbthciport.c: Change the method of changing the baud rate.
Browse files Browse the repository at this point in the history
By calling a helper function machine_uart_set_baudrate() instead of
setting the baud rate by a call to the NXP lib.

Fixes machine_uart.c to work with a baud rate of 921600.

Signed-off-by: robert-hh <robert@hammelrath.com>
  • Loading branch information
robert-hh committed Sep 4, 2023
1 parent 8bd2494 commit 0a83f80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
29 changes: 22 additions & 7 deletions ports/mimxrt/machine_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,32 @@ 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);
}
}

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

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, "
Expand Down Expand Up @@ -292,12 +312,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);
Expand Down
9 changes: 3 additions & 6 deletions ports/mimxrt/mpbthciport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -47,6 +44,8 @@ uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
STATIC mp_sched_node_t mp_bluetooth_hci_sched_node;
STATIC soft_timer_entry_t mp_bluetooth_hci_soft_timer;

extern void machine_uart_set_baudrate(mp_obj_t uart, uint32_t baudrate);

STATIC void mp_bluetooth_hci_soft_timer_callback(soft_timer_entry_t *self) {
mp_bluetooth_hci_poll_now();
}
Expand Down Expand Up @@ -111,9 +110,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;
}
Expand Down

0 comments on commit 0a83f80

Please sign in to comment.