From f8bd6778c85322b33008d749ae4021cafa067044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Giessen?= Date: Thu, 29 Jun 2023 14:55:34 +0200 Subject: [PATCH] esp32: Support JTAG console, free up UART. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CONFIG_USB_OTG_SUPPORTED is automatically set by the ESP-IDF when the chip supports USB-OTG, which is the case for the ESP32-S2 and ESP32-S3. When trying to use the JTAG console with these chips, it would not work because our USB implementation will take over control over the USB port, breaking the JTAG console in the process. Thus, when the board is configured to use the JTAG console, we should not enable our USB console support. Additionally, this change also frees up UART0 when an USB-based console is configured, since there's no reason to prevent (re)configuration of UART0 for other uses in that case. Signed-off-by: Daniƫl van de Giessen --- ports/esp32/machine_uart.c | 7 ++++++- ports/esp32/main.c | 6 +++--- ports/esp32/mphalport.c | 6 +++--- ports/esp32/uart.c | 10 +++++++--- ports/esp32/uart.h | 6 +++++- ports/esp32/usb.c | 4 ++-- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index e7b2b8376b..541d7ccc55 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -153,9 +153,11 @@ 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 MICROPY_HW_ENABLE_UART_REPL if (self->uart_num == MICROPY_HW_UART_REPL) { mp_raise_ValueError(MP_ERROR_TEXT("UART buffer size is fixed")); } + #endif if (args[ARG_txbuf].u_int >= 0) { self->txbuf = args[ARG_txbuf].u_int; @@ -353,8 +355,11 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, #endif } + #if MICROPY_HW_ENABLE_UART_REPL // Only reset the driver if it's not the REPL UART. - if (uart_num != MICROPY_HW_UART_REPL) { + if (uart_num != MICROPY_HW_UART_REPL) + #endif + { // Remove any existing configuration uart_driver_delete(self->uart_num); diff --git a/ports/esp32/main.c b/ports/esp32/main.c index a6346b027a..1420dd579c 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -89,10 +89,10 @@ void mp_task(void *pvParameter) { #if MICROPY_PY_THREAD mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t)); #endif - #if CONFIG_USB_OTG_SUPPORTED - usb_init(); - #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG + #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG usb_serial_jtag_init(); + #elif CONFIG_USB_OTG_SUPPORTED + usb_init(); #endif #if MICROPY_HW_ENABLE_UART_REPL uart_stdout_init(); diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 63a674c24b..d7003a1437 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -111,10 +111,10 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { if (release_gil) { MP_THREAD_GIL_EXIT(); } - #if CONFIG_USB_OTG_SUPPORTED - usb_tx_strn(str, len); - #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG + #if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG usb_serial_jtag_tx_strn(str, len); + #elif CONFIG_USB_OTG_SUPPORTED + usb_tx_strn(str, len); #endif #if MICROPY_HW_ENABLE_UART_REPL uart_stdout_tx_strn(str, len); diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index 358d434709..fc69e279e3 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -26,14 +26,16 @@ * THE SOFTWARE. */ -#include - -#include "hal/uart_hal.h" #include "py/runtime.h" #include "py/mphal.h" #include "uart.h" +#if MICROPY_HW_ENABLE_UART_REPL + +#include +#include "hal/uart_hal.h" + // Backwards compatibility for when MICROPY_HW_UART_REPL was a ESP-IDF UART // driver enum. Only UART_NUM_0 was supported with that version of the driver. #define UART_NUM_0 0 @@ -118,3 +120,5 @@ STATIC void IRAM_ATTR uart_irq_handler(void *arg) { } } } + +#endif // MICROPY_HW_ENABLE_UART_REPL diff --git a/ports/esp32/uart.h b/ports/esp32/uart.h index 6410db24c9..3d88eed825 100644 --- a/ports/esp32/uart.h +++ b/ports/esp32/uart.h @@ -30,9 +30,11 @@ // Whether to enable the REPL on a UART. #ifndef MICROPY_HW_ENABLE_UART_REPL -#define MICROPY_HW_ENABLE_UART_REPL (!CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG) +#define MICROPY_HW_ENABLE_UART_REPL (!CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_CDC && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG) #endif +#if MICROPY_HW_ENABLE_UART_REPL + #ifndef MICROPY_HW_UART_REPL #define MICROPY_HW_UART_REPL (0) #endif @@ -44,4 +46,6 @@ void uart_stdout_init(void); int uart_stdout_tx_strn(const char *str, size_t len); +#endif // MICROPY_HW_ENABLE_UART_REPL + #endif // MICROPY_INCLUDED_ESP32_UART_H diff --git a/ports/esp32/usb.c b/ports/esp32/usb.c index b9d99676d3..2e417755c9 100644 --- a/ports/esp32/usb.c +++ b/ports/esp32/usb.c @@ -28,7 +28,7 @@ #include "py/mphal.h" #include "usb.h" -#if CONFIG_USB_OTG_SUPPORTED +#if CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_CDC && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG #include "esp_timer.h" #ifndef NO_QSTR @@ -97,4 +97,4 @@ void usb_tx_strn(const char *str, size_t len) { } } -#endif // CONFIG_USB_OTG_SUPPORTED +#endif // CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_CDC && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG