Skip to content

Commit

Permalink
esp32: Add MICROPY_HW_USB_CDC macro for native USB-CDC serial.
Browse files Browse the repository at this point in the history
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 <angus@redyak.com.au>
  • Loading branch information
projectgus committed Sep 3, 2024
1 parent 6ad6297 commit 5e692d0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion ports/esp32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ports/esp32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ports/esp32/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ports/esp32/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 5e692d0

Please sign in to comment.