From 5e692d0460a2af8225534bf7c70f56007641e96e Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 27 Aug 2024 16:11:32 +1000 Subject: [PATCH] esp32: Add MICROPY_HW_USB_CDC macro for native USB-CDC serial. This fixes issue of ESP32-S3 switching its config over to USB serial/JTAG instead of native USB. The the existing logic was hard to follow, adding this config macro makes it easier to see which USB is in use and to have board definitions that enable/disable different USB levels. This commit also drops (nominal) support for manually setting CONFIG_ESP_CONSOLE_USB_CDC in sdkconfig. No included board configs use this and it didn't seem to work (if secondary console was set to the default USB Serial/JTAG then there is no serial output on any port, and if secondary console was set to None then linking fails.) Can be re-added if there's a use case for it. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- ports/esp32/main.c | 2 +- ports/esp32/mpconfigport.h | 11 ++++++++++- ports/esp32/mphalport.c | 2 +- ports/esp32/uart.h | 2 +- ports/esp32/usb.c | 4 ++-- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index dedc5421e34e..ca5a0e3c2097 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -101,7 +101,7 @@ void mp_task(void *pvParameter) { #endif #if MICROPY_HW_ESP_USB_SERIAL_JTAG usb_serial_jtag_init(); - #elif CONFIG_USB_OTG_SUPPORTED + #elif MICROPY_HW_USB_CDC usb_init(); #endif #if MICROPY_HW_ENABLE_UART_REPL diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 25fa4706214c..747c55e92dcb 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -260,9 +260,18 @@ typedef long mp_off_t; // board specifics #define MICROPY_PY_SYS_PLATFORM "esp32" +// Enable stdio over native USB peripheral CDC via TinyUSB +#ifndef MICROPY_HW_USB_CDC +#define MICROPY_HW_USB_CDC (SOC_USB_OTG_SUPPORTED) +#endif + // Enable stdio over USB Serial/JTAG peripheral #ifndef MICROPY_HW_ESP_USB_SERIAL_JTAG -#define MICROPY_HW_ESP_USB_SERIAL_JTAG (SOC_USB_SERIAL_JTAG_SUPPORTED) +#define MICROPY_HW_ESP_USB_SERIAL_JTAG (SOC_USB_SERIAL_JTAG_SUPPORTED && !MICROPY_HW_USB_CDC) +#endif + +#if MICROPY_HW_USB_CDC && MICROPY_HW_ESP_USB_SERIAL_JTAG +#error "Invalid build config: Can't enable both native USB and USB Serial/JTAG peripheral" #endif // ESP32-S3 extended IO for 47 & 48 diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index e85aa6260c06..7d0154cc0531 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -146,7 +146,7 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) { #if MICROPY_HW_ESP_USB_SERIAL_JTAG usb_serial_jtag_tx_strn(str, len); did_write = true; - #elif CONFIG_USB_OTG_SUPPORTED + #elif MICROPY_HW_USB_CDC usb_tx_strn(str, len); did_write = true; #endif diff --git a/ports/esp32/uart.h b/ports/esp32/uart.h index 0a0985d1b4e2..fe08e26899aa 100644 --- a/ports/esp32/uart.h +++ b/ports/esp32/uart.h @@ -30,7 +30,7 @@ // 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_CDC && !MICROPY_HW_ESP_USB_SERIAL_JTAG) +#define MICROPY_HW_ENABLE_UART_REPL (!MICROPY_HW_USB_CDC && !MICROPY_HW_ESP_USB_SERIAL_JTAG) #endif #if MICROPY_HW_ENABLE_UART_REPL diff --git a/ports/esp32/usb.c b/ports/esp32/usb.c index 471f2c2e0e46..4207e77df210 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 && !CONFIG_ESP_CONSOLE_USB_CDC && !MICROPY_HW_ESP_USB_SERIAL_JTAG +#if MICROPY_HW_USB_CDC #include "esp_timer.h" #ifndef NO_QSTR @@ -100,4 +100,4 @@ void usb_tx_strn(const char *str, size_t len) { } } -#endif // CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_CDC && !MICROPY_HW_ESP_USB_SERIAL_JTAG +#endif // MICROPY_HW_USB_CDC