From 919e586e46af67e7eaa819b0f187b600a3165526 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Thu, 24 Feb 2022 14:30:50 +1100 Subject: [PATCH] esp32/machine_uart: Allow limited configuration of REPL UART. Some applications may want to adjust the hard coded 115200 REPL buadrate, and this commit allows it to be changed dynamically via machine.UART(0). --- ports/esp32/machine_uart.c | 26 ++++++++++++++------------ ports/esp32/uart.c | 8 -------- ports/esp32/uart.h | 8 ++++++++ 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 3c90a72150..aa8d494ec8 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -35,6 +35,7 @@ #include "py/stream.h" #include "py/mperrno.h" #include "modmachine.h" +#include "uart.h" #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) #define UART_INV_TX UART_INVERSE_TXD @@ -151,6 +152,10 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co if (args[ARG_txbuf].u_int >= 0 || args[ARG_rxbuf].u_int >= 0) { // must reinitialise driver to change the tx/rx buffer size + if (self->uart_num == MICROPY_HW_UART_REPL) { + mp_raise_ValueError(MP_ERROR_TEXT("UART buffer size is fixed")); + } + if (args[ARG_txbuf].u_int >= 0) { self->txbuf = args[ARG_txbuf].u_int; } @@ -291,12 +296,6 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) does not exist"), uart_num); } - // Attempts to use UART0 from Python has resulted in all sorts of fun errors. - // FIXME: UART0 is disabled for now. - if (uart_num == UART_NUM_0) { - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) is disabled (dedicated to REPL)"), uart_num); - } - // Defaults uart_config_t uartcfg = { .baud_rate = 115200, @@ -338,14 +337,17 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, #endif } - // Remove any existing configuration - uart_driver_delete(self->uart_num); + // Only reset the driver if it's not the REPL UART. + if (uart_num != MICROPY_HW_UART_REPL) { + // Remove any existing configuration + uart_driver_delete(self->uart_num); - // init the peripheral - // Setup - uart_param_config(self->uart_num, &uartcfg); + // init the peripheral + // Setup + uart_param_config(self->uart_num, &uartcfg); - uart_driver_install(uart_num, self->rxbuf, self->txbuf, 0, NULL, 0); + uart_driver_install(uart_num, self->rxbuf, self->txbuf, 0, NULL, 0); + } mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index ca4ac67cde..f6493dc796 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -35,14 +35,6 @@ #include "py/mphal.h" #include "uart.h" -#ifndef MICROPY_HW_UART_REPL -#define MICROPY_HW_UART_REPL (UART_NUM_0) -#endif - -#ifndef MICROPY_HW_UART_REPL_BAUD -#define MICROPY_HW_UART_REPL_BAUD (115200) -#endif - STATIC void uart_irq_handler(void *arg); void uart_stdout_init(void) { diff --git a/ports/esp32/uart.h b/ports/esp32/uart.h index 439be521c4..f32d5cf342 100644 --- a/ports/esp32/uart.h +++ b/ports/esp32/uart.h @@ -28,6 +28,14 @@ #ifndef MICROPY_INCLUDED_ESP32_UART_H #define MICROPY_INCLUDED_ESP32_UART_H +#ifndef MICROPY_HW_UART_REPL +#define MICROPY_HW_UART_REPL (UART_NUM_0) +#endif + +#ifndef MICROPY_HW_UART_REPL_BAUD +#define MICROPY_HW_UART_REPL_BAUD (115200) +#endif + void uart_stdout_init(void); int uart_stdout_tx_strn(const char *str, size_t len);