From 826f1a158e5382f893b2e5bab0fa710a03edc82a Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 21 Aug 2024 17:35:25 +0200 Subject: [PATCH] nrf/modules/machine/uart.c: Allow changing the UART baud rate w/o reset. This commit fixes a bug in the existing driver, that the UART baud rate could not be changed without reset or power cycle. It adds as well functionality to UART.deinit(). Signed-off-by: robert-hh --- ports/nrf/modules/machine/uart.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index 0f4e3c67c520a..42553a17db9f9 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -62,6 +62,7 @@ typedef struct _machine_uart_buf_t { #define nrfx_uart_tx nrfx_uarte_tx #define nrfx_uart_tx_in_progress nrfx_uarte_tx_in_progress #define nrfx_uart_init nrfx_uarte_init +#define nrfx_uart_uninit nrfx_uarte_uninit #define nrfx_uart_event_t nrfx_uarte_event_t #define NRFX_UART_INSTANCE NRFX_UARTE_INSTANCE @@ -299,7 +300,11 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg #endif // Enable event callback and start asynchronous receive + if (self->initialized) { + nrfx_uart_uninit(self->p_uart); + } nrfx_uart_init(self->p_uart, &config, uart_event_handler); + self->initialized = true; nrfx_uart_rx(self->p_uart, &self->buf.rx_buf[0], 1); #if NRFX_UART_ENABLED @@ -310,7 +315,10 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg } static void mp_machine_uart_deinit(machine_uart_obj_t *self) { - (void)self; + if (self->initialized) { + nrfx_uart_uninit(self->p_uart); + } + self->initialized = false; } // Write a single character on the bus. `data` is an integer to write.