10000 ports/esp32: Use shared/tinyusb integration. by andrewleech · Pull Request #15108 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ports/esp32: Use shared/tinyusb integration. #15108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions ports/esp32/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ BUILD ?= build-$(BOARD)
endif

# Device serial settings.
PORT ?= /dev/ttyUSB0
BAUD ?= 460800
ifneq ($(PORT),)
PORT_ARG := -p $(PORT)
endif
ifneq ($(BAUD),)
BAUD_ARG := -b $(BAUD)
endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this now leaves the selection of port and baud to idf.py? Does that give a better user experience?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a default, yes. Esptool does have some auto detection logic that has worked very well for me during development.


PYTHON ?= python3

Expand Down Expand Up @@ -59,7 +63,7 @@ endif
HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m"

define RUN_IDF_PY
idf.py $(IDFPY_FLAGS) -B $(BUILD) -p $(PORT) -b $(BAUD) $(1)
idf.py $(IDFPY_FLAGS) -B $(BUILD) $(PORT_ARG) $(BAUD_ARG) $(1)
endef

all:
Expand Down
8 changes: 0 additions & 8 deletions ports/esp32/boards/ARDUINO_NANO_ESP32/board_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
#include "double_tap.h"
#include "usb.h"

#include "tinyusb.h"
#include "tusb_cdc_acm.h"

#define LED_RED GPIO_NUM_46
#define LED_GREEN GPIO_NUM_0
#define LED_BLUE GPIO_NUM_45
Expand Down Expand Up @@ -87,11 +84,6 @@ void NANO_ESP32_enter_bootloader(void) {
esp_restart();
}

void NANO_ESP32_usb_callback_line_state_changed(int itf, void *event_in) {
cdcacm_event_t *event = event_in;
mp_usbd_line_state_cb(itf, event->line_state_changed_data.dtr, event->line_state_changed_data.rts);
}

void NANO_ESP32_board_startup(void) {
boardctrl_startup();

Expand Down
5 changes: 0 additions & 5 deletions ports/esp32/boards/ARDUINO_NANO_ESP32/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@
#define MICROPY_HW_SPI2_SCK (18)

#define MICROPY_HW_ENABLE_USBDEV (1)
#define MICROPY_EXCLUDE_SHARED_TINYUSB_USBD_CDC (1)
#define MICROPY_HW_USB_EXTERNAL_TINYUSB (1)
#define MICROPY_HW_USB_CDC_1200BPS_TOUCH (1)
#define MICROPY_SCHEDULER_STATIC_NODES (1)

#define MICROPY_HW_USB_CUSTOM_LINE_STATE_CB NANO_ESP32_usb_callback_line_state_changed
void NANO_ESP32_usb_callback_line_state_changed(int itf, void *event);

#define MICROPY_BOARD_STARTUP NANO_ESP32_board_startup
void NANO_ESP32_board_startup(void);

Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/ESP32_GENERIC_S3/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/UM_FEATHERS2/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

CONFIG_SPIRAM_MEMTEST=

Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

# LWIP
CONFIG_LWIP_LOCAL_HOSTNAME="UMFeatherS2Neo"
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/UM_FEATHERS3/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/UM_NANOS3/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/UM_PROS3/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/UM_TINYS2/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

# LWIP
CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyS2"
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/UM_TINYS3/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
Expand Down
1 change: 0 additions & 1 deletion ports/esp32/boards/UM_TINYWATCHS3/sdkconfig.board
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
CONFIG_ESPTOOLPY_AFTER_NORESET=y

CONFIG_ESPTOOLPY_FLASHSIZE_4MB=
CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y
Expand Down
40 changes: 40 additions & 0 deletions ports/esp32/esp32_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,37 @@ list(APPEND MICROPY_SOURCE_DRIVERS
${MICROPY_DIR}/drivers/dht/dht.c
)

string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/tinyusb)
if(MICROPY_PY_TINYUSB)
set(TINYUSB_SRC "${MICROPY_DIR}/lib/tinyusb/src")
string(TOUPPER OPT_MCU_${IDF_TARGET} tusb_mcu)

list(APPEND MICROPY_DEF_TINYUSB
CFG_TUSB_MCU=${tusb_mcu}
)

list(APPEND MICROPY_SOURCE_TINYUSB
${TINYUSB_SRC}/tusb.c
${TINYUSB_SRC}/common/tusb_fifo.c
${TINYUSB_SRC}/device/usbd.c
${TINYUSB_SRC}/device/usbd_control.c
${TINYUSB_SRC}/class/cdc/cdc_device.c
${TINYUSB_SRC}/portable/synopsys/dwc2/dcd_dwc2.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd_cdc.c
${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
)

list(APPEND MICROPY_INC_TINYUSB
${TINYUSB_SRC}
${MICROPY_DIR}/shared/tinyusb/
)

list(APPEND MICROPY_LINK_TINYUSB
-Wl,--wrap=dcd_event_handler
)
endif()

list(APPEND MICROPY_SOURCE_PORT
panichandler.c
adc.c
Expand Down Expand Up @@ -100,6 +131,7 @@ list(APPEND MICROPY_SOURCE_QSTR
${MICROPY_SOURCE_LIB}
${MICROPY_SOURCE_PORT}
${MICROPY_SOURCE_BOARD}
${MICROPY_SOURCE_TINYUSB}
)

list(APPEND IDF_COMPONENTS
Expand Down Expand Up @@ -134,6 +166,7 @@ list(APPEND IDF_COMPONENTS
soc
spi_flash
ulp
usb
vfs
)

Expand All @@ -147,9 +180,11 @@ idf_component_register(
${MICROPY_SOURCE_DRIVERS}
${MICROPY_SOURCE_PORT}
${MICROPY_SOURCE_BOARD}
${MICROPY_SOURCE_TINYUSB}
INCLUDE_DIRS
${MICROPY_INC_CORE}
${MICROPY_INC_USERMOD}
${MICROPY_INC_TINYUSB}
${MICROPY_PORT_DIR}
${MICROPY_BOARD_DIR}
${CMAKE_BINARY_DIR}
Expand All @@ -171,6 +206,7 @@ endif()
target_compile_definitions(${MICROPY_TARGET} PUBLIC
${MICROPY_DEF_CORE}
${MICROPY_DEF_BOARD}
${MICROPY_DEF_TINYUSB}
MICROPY_ESP_IDF_4=1
MICROPY_VFS_FAT=1
MICROPY_VFS_LFS2=1
Expand All @@ -186,6 +222,10 @@ target_compile_options(${MICROPY_TARGET} PUBLIC
-Wno-missing-field-initializers
)

target_link_options(${MICROPY_TARGET} PUBLIC
${MICROPY_LINK_TINYUSB}
)

# Additional include directories needed for private NimBLE headers.
target_include_directories(${MICROPY_TARGET} PUBLIC
${IDF_PATH}/components/bt/host/nimble/nimble
Expand Down
3 changes: 2 additions & 1 deletion ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "shared/readline/readline.h"
#include "shared/runtime/pyexec.h"
#include "shared/timeutils/timeutils.h"
#include "shared/tinyusb/mp_usbd.h"
#include "mbedtls/platform_time.h"

#include "uart.h"
Expand Down Expand Up @@ -101,7 +102,7 @@ void mp_task(void *pvParameter) {
#endif
#if MICROPY_HW_ESP_USB_SERIAL_JTAG
usb_serial_jtag_init();
#elif MICROPY_HW_USB_CDC
#elif MICROPY_HW_ENABLE_USBDEV
usb_init();
#endif
#if MICROPY_HW_ENABLE_UART_REPL
Expand Down
2 changes: 2 additions & 0 deletions ports/esp32/main_esp32s2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ if(NOT MICROPY_PORT_DIR)
get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE)
endif()

set(MICROPY_PY_TINYUSB ON)

include(${MICROPY_PORT_DIR}/esp32_common.cmake)
2 changes: 2 additions & 0 deletions ports/esp32/main_esp32s3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ if(NOT MICROPY_PORT_DIR)
get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE)
endif()

set(MICROPY_PY_TINYUSB ON)

include(${MICROPY_PORT_DIR}/esp32_common.cmake)
12 changes: 12 additions & 0 deletions ports/esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,19 @@ static mp_int_t mp_machine_reset_cause(void) {

#if MICROPY_ESP32_USE_BOOTLOADER_RTC
#include "soc/rtc_cntl_reg.h"
#include "usb.h"
#if CONFIG_IDF_TARGET_ESP32S3
#include "esp32s3/rom/usb/usb_dc.h"
#include "esp32s3/rom/usb/usb_persist.h"
#include "esp32s3/rom/usb/chip_usb_dw_wrapper.h"
#endif

NORETURN static void machine_bootloader_rtc(void) {
#if CONFIG_IDF_TARGET_ESP32S3
usb_usj_mode();
usb_dc_prepare_persist();
chip_usb_set_persist_flags(USBDC_BOOT_DFU);
#endif
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
esp_restart();
}
Expand Down
78 changes: 64 additions & 14 deletions ports/esp32/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,70 @@

#define MP_STATE_PORT MP_STATE_VM

#ifndef MICROPY_HW_ENABLE_USBDEV
#define MICROPY_HW_ENABLE_USBDEV (SOC_USB_OTG_SUPPORTED)
#endif

#if MICROPY_HW_ENABLE_USBDEV
#define MICROPY_SCHEDULER_STATIC_NODES (1)
#define MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER (1)

#ifndef MICROPY_HW_USB_VID
#define USB_ESPRESSIF_VID 0x303A
#if CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID
#define MICROPY_HW_USB_VID (USB_ESPRESSIF_VID)
#else
#define MICROPY_HW_USB_VID (CONFIG_TINYUSB_DESC_CUSTOM_VID)
#endif
#endif

#ifndef MICROPY_HW_USB_PID
#if CONFIG_TINYUSB_DESC_USE_DEFAULT_PID
#define _PID_MAP(itf, n) ((CFG_TUD_##itf) << (n))
// A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
// Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
// Auto ProductID layout's Bitmap:
// [MSB] HID | MSC | CDC [LSB]
#define USB_TUSB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
_PID_MAP(MIDI, 3)) // | _PID_MAP(AUDIO, 4) | _PID_MAP(VENDOR, 5) )
#define MICROPY_HW_USB_PID (USB_TUSB_PID)
#else
#define MICROPY_HW_USB_PID (CONFIG_TINYUSB_DESC_CUSTOM_PID)
#endif
#endif

#ifndef MICROPY_HW_USB_MANUFACTURER_STRING
#ifdef CONFIG_TINYUSB_DESC_MANUFACTURER_STRING
#define MICROPY_HW_USB_MANUFACTURER_STRING CONFIG_TINYUSB_DESC_MANUFACTURER_STRING
#else
#define MICROPY_HW_USB_MANUFACTURER_STRING "MicroPython"
#endif
#endif

#ifndef MICROPY_HW_USB_PRODUCT_FS_STRING
#ifdef CONFIG_TINYUSB_DESC_PRODUCT_STRING
#define MICROPY_HW_USB_PRODUCT_FS_STRING CONFIG_TINYUSB_DESC_PRODUCT_STRING
#else
#define MICROPY_HW_USB_PRODUCT_FS_STRING "Board in FS mode"
#endif
#endif

#endif // MICROPY_HW_ENABLE_USBDEV

// Enable stdio over native USB peripheral CDC via TinyUSB
#ifndef MICROPY_HW_USB_CDC
#define MICROPY_HW_USB_CDC (MICROPY_HW_ENABLE_USBDEV)
#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 && !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

// type definitions for the specific machine

#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void *)((mp_uint_t)(p)))
Expand Down Expand Up @@ -253,20 +317,6 @@ 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 && !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
#ifndef MICROPY_HW_ESP32S3_EXTENDED_IO
#define MICROPY_HW_ESP32S3_EXTENDED_IO (1)
Expand Down
Loading
Loading
0