From fe7e6064e50938363c35c031d6597a17086f92f3 Mon Sep 17 00:00:00 2001 From: Eagle Claw <301410538+ladyada-eagleclaw@users.noreply.github.com> Date: Fri, 2 Oct 2026 21:40:43 -0400 Subject: [PATCH 1/7] espressif: add ESP32-P4 USB host and Hosted Wi-Fi support Include the ADC mapping, stable UID, ROM USB reset, DSI output and shared SDMMC fixes verified during Metro P4 bring-up. Keep board wiring and configuration in a separate dependent commit. Co-authored-by: Limor Fried --- .gitmodules | 6 ++ ports/espressif/CMakeLists.txt | 6 ++ ports/espressif/Makefile | 28 ++++++++- ports/espressif/background.c | 10 ++++ .../common-hal/microcontroller/Pin.c | 2 +- .../common-hal/microcontroller/Processor.c | 4 +- .../common-hal/microcontroller/__init__.c | 8 ++- ports/espressif/common-hal/mipidsi/Display.c | 3 +- ports/espressif/common-hal/sdioio/SDCard.c | 29 +++++---- ports/espressif/common-hal/usb_host/Port.c | 59 +++++++++++++++++++ ports/espressif/common-hal/usb_host/Port.h | 15 +++++ .../espressif/common-hal/usb_host/__init__.c | 7 +++ ports/espressif/common-hal/wifi/Radio.c | 2 +- .../common-hal/wifi/ScannedNetworks.c | 2 +- ports/espressif/common-hal/wifi/__init__.c | 20 ++++++- ports/espressif/esp-hosted | 1 + ports/espressif/esp-idf | 2 +- ports/espressif/esp-wifi-remote | 1 + ports/espressif/mpconfigport.mk | 9 +-- ports/espressif/peripherals/esp32p4/pins.c | 16 +++-- ports/espressif/peripherals/esp32p4/pins.h | 3 + ports/espressif/supervisor/usb.c | 21 +++++++ supervisor/shared/usb/tusb_config.h | 2 +- supervisor/shared/usb/usb.c | 5 ++ 24 files changed, 225 insertions(+), 36 deletions(-) create mode 100644 ports/espressif/common-hal/usb_host/Port.c create mode 100644 ports/espressif/common-hal/usb_host/Port.h create mode 100644 ports/espressif/common-hal/usb_host/__init__.c create mode 160000 ports/espressif/esp-hosted create mode 160000 ports/espressif/esp-wifi-remote diff --git a/.gitmodules b/.gitmodules index 0fcda5904a6..74d3d01078e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -421,3 +421,9 @@ [submodule "frozen/Adafruit_CircuitPython_TAS2505"] path = frozen/Adafruit_CircuitPython_TAS2505 url = https://github.com/adafruit/Adafruit_CircuitPython_TAS2505.git +[submodule "ports/espressif/esp-hosted"] + path = ports/espressif/esp-hosted + url = https://github.com/espressif/esp-hosted-mcu.git +[submodule "ports/espressif/esp-wifi-remote"] + path = ports/espressif/esp-wifi-remote + url = https://github.com/espressif/esp-wifi-remote.git diff --git a/ports/espressif/CMakeLists.txt b/ports/espressif/CMakeLists.txt index e612e4f31de..c303c282711 100644 --- a/ports/espressif/CMakeLists.txt +++ b/ports/espressif/CMakeLists.txt @@ -8,7 +8,13 @@ set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf) set(COMPONENTS bt driver esp_driver_dac esp_driver_gpio esp_driver_gptimer esp_driver_i2c esp_driver_i2s esp_driver_ledc esp_driver_pcnt esp_driver_rmt esp_driver_sdmmc esp_driver_spi esp_driver_touch_sens esp_driver_tsens esp_driver_uart esp-tls esp_adc esp_event esp_netif esp_psram esp_security esp_wifi esptool_py freertos log lwip main mbedtls mdns pthread soc ulp wpa_supplicant esp_lcd vfs esp_stdio sdmmc esp-camera) set(EXTRA_COMPONENT_DIRS "esp-protocols/components/mdns" "esp-camera") +if(CIRCUITPY_ESP_HOSTED) + list(APPEND EXTRA_COMPONENT_DIRS "esp-hosted" "esp-wifi-remote/components/esp_wifi_remote") + list(APPEND COMPONENTS esp-hosted esp_wifi_remote) +endif() + include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(ENV{ESP_IDF_VERSION} "${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}") project(circuitpython) idf_build_set_property(__OUTPUT_SDKCONFIG 0) diff --git a/ports/espressif/Makefile b/ports/espressif/Makefile index 881bfbf45e6..b37c1f6d53a 100644 --- a/ports/espressif/Makefile +++ b/ports/espressif/Makefile @@ -6,6 +6,9 @@ include ../../py/circuitpy_mkenv.mk +# Keep dependency selection identical during automatic CMake reconfiguration. +export IDF_COMPONENT_MANAGER = 0 + ifeq ($(IDF_TARGET),esp32s3) BT_IDF_TARGET = esp32c3 else ifeq ($(IDF_TARGET),esp32c61) @@ -254,9 +257,11 @@ endif ifneq ($(CIRCUITPY_WIFI),0) ifeq ($(filter $(IDF_TARGET),esp32s2 esp32p4),) +ifneq ($(CIRCUITPY_ESP_HOSTED),1) REGISTRATION_FUNCTIONS += -u esp_coex_init_include_func endif endif +endif #Debugging/Optimization @@ -660,6 +665,10 @@ SRC_C += \ lib/tinyusb/src/portable/synopsys/dwc2/dwc2_common.c endif +ifeq ($(CIRCUITPY_USB_HOST),1) +SRC_C += lib/tinyusb/src/portable/synopsys/dwc2/hcd_dwc2.c +endif + ifneq ($(CIRCUITPY_AUDIOBUSIO),0) CHIP_COMPONENTS += esp_driver_i2s endif @@ -885,7 +894,7 @@ do-sdkconfig: $(BUILD)/esp-idf/config/sdkconfig.h QSTR_GLOBAL_REQUIREMENTS += $(BUILD)/esp-idf/config/sdkconfig.h $(BUILD)/esp-idf/config/sdkconfig.h: boards/$(BOARD)/sdkconfig boards/$(BOARD)/mpconfigboard.mk CMakeLists.txt | $(BUILD)/esp-idf $(STEPECHO) "Create $@" - $(Q)env IDF_PATH=$(IDF_PATH) IDF_COMPONENT_MANAGER=0 cmake -S . -B $(BUILD)/esp-idf -DSDKCONFIG=$(BUILD)/esp-idf/sdkconfig -DSDKCONFIG_DEFAULTS="$(SDKCONFIGS)" -DCMAKE_TOOLCHAIN_FILE=$(IDF_PATH)/tools/cmake/toolchain-$(IDF_TARGET).cmake -DIDF_TARGET=$(IDF_TARGET) -GNinja + $(Q)env IDF_PATH=$(IDF_PATH) IDF_COMPONENT_MANAGER=0 cmake -S . -B $(BUILD)/esp-idf -DSDKCONFIG=$(BUILD)/esp-idf/sdkconfig -DSDKCONFIG_DEFAULTS="$(SDKCONFIGS)" -DCMAKE_TOOLCHAIN_FILE=$(IDF_PATH)/tools/cmake/toolchain-$(IDF_TARGET).cmake -DIDF_TARGET=$(IDF_TARGET) -DCIRCUITPY_ESP_HOSTED=$(CIRCUITPY_ESP_HOSTED) -GNinja $(Q)$(PYTHON) tools/check-sdkconfig.py \ CIRCUITPY_DUALBANK=$(CIRCUITPY_DUALBANK) \ CIRCUITPY_STORAGE_EXTEND=$(CIRCUITPY_STORAGE_EXTEND) \ @@ -923,6 +932,7 @@ BINARY_WIFI_BLOBS = libcore.a libespnow.a libnet80211.a libpp.a libsmartconfig.a ifneq ($(IDF_TARGET),esp32p4) BINARY_BLOBS = esp-idf/components/esp_phy/lib/$(IDF_TARGET)/libphy.a endif +ifneq ($(CIRCUITPY_ESP_HOSTED),1) ifneq ($(CIRCUITPY_WIFI),0) BINARY_BLOBS += $(addprefix esp-idf/components/esp_wifi/lib/$(IDF_TARGET)/, $(BINARY_WIFI_BLOBS)) BINARY_BLOBS += esp-idf/components/esp_coex/lib/$(IDF_TARGET)/libcoexist.a @@ -931,13 +941,27 @@ ifneq ($(CIRCUITPY_WIFI),0) endif endif +endif + ifeq ($(IDF_TARGET),esp32) BINARY_BLOBS += esp-idf/components/esp_phy/lib/$(IDF_TARGET)/librtc.a endif ESP_IDF_COMPONENTS_LINK = $(IDF_TARGET_ARCH) $(CHIP_COMPONENTS) app_update bootloader_support driver esp_driver_dma esp_driver_gpio esp_driver_gptimer esp_driver_i2c esp_driver_ledc esp_driver_spi esp_driver_uart efuse esp_adc esp_app_format esp_common esp_event esp_gdbstub esp_hal_ana_conv esp_hal_clock esp_hal_gpio esp_hal_gpspi esp_hal_i2c esp_hal_ledc esp_hal_mspi esp_hal_security esp_hal_timg esp_hal_uart esp_hal_wdt esp_hw_support esp_mm esp_partition esp_pm esp_ringbuf esp_rom esp_system esp_timer freertos hal heap log esp_libc nvs_flash nvs_sec_provider pthread soc spi_flash vfs esp_stdio ifneq ($(CIRCUITPY_WIFI),0) - ESP_IDF_COMPONENTS_LINK += esp_coex esp_netif esp_security esp-tls esp_wifi lwip mbedtls mdns wpa_supplicant esp_phy + ESP_IDF_COMPONENTS_LINK += esp_netif esp_security esp-tls esp_wifi lwip mbedtls mdns +ifneq ($(CIRCUITPY_ESP_HOSTED),1) +ESP_IDF_COMPONENTS_LINK += esp_coex esp_phy wpa_supplicant +endif +endif +ifeq ($(CIRCUITPY_ESP_HOSTED),1) +export ESP_IDF_VERSION := $(shell sed -nE 's/set\(IDF_VERSION_(MAJOR|MINOR) ([0-9]+)\)/\2/p' esp-idf/tools/cmake/version.cmake | paste -sd.) +INC := -isystem esp-wifi-remote/components/esp_wifi_remote/idf_v$(ESP_IDF_VERSION)/include/injected \ + -isystem esp-wifi-remote/components/esp_wifi_remote/idf_v$(ESP_IDF_VERSION)/include \ + -isystem esp-wifi-remote/components/esp_wifi_remote/include \ + -isystem esp-hosted/host/api/include -isystem esp-hosted/host $(INC) +ESP_IDF_COMPONENTS_LINK += esp_wifi_remote esp-hosted esp_http_client http_parser console +REGISTRATION_FUNCTIONS += -u esp_hosted_init endif ifneq ($(CIRCUITPY_BLEIO_NATIVE),0) BLE_IMPL_esp32 := esp32 diff --git a/ports/espressif/background.c b/ports/espressif/background.c index b1b81ac2c83..d28b1bb3765 100644 --- a/ports/espressif/background.c +++ b/ports/espressif/background.c @@ -8,11 +8,21 @@ #include "supervisor/filesystem.h" #include "supervisor/port.h" #include "supervisor/shared/stack.h" +#if CIRCUITPY_USB_HOST +#include "supervisor/usb.h" +#include "tusb.h" +#endif #include "freertos/FreeRTOS.h" #include "freertos/task.h" void port_background_tick(void) { + #if CIRCUITPY_USB_HOST + // Enumeration delays need polling even when no new USB IRQ arrives. + if (tuh_task_event_ready()) { + usb_background_schedule(); + } + #endif // Yield with zero delay in case FreeRTOS wants to switch to something else. port_task_yield(); } diff --git a/ports/espressif/common-hal/microcontroller/Pin.c b/ports/espressif/common-hal/microcontroller/Pin.c index 0c49318f4bc..36a83c7d900 100644 --- a/ports/espressif/common-hal/microcontroller/Pin.c +++ b/ports/espressif/common-hal/microcontroller/Pin.c @@ -471,7 +471,7 @@ void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) { } bool pin_number_is_free(gpio_num_t pin_number) { - return !(_in_use_pin_mask & PIN_BIT(pin_number)); + return GPIO_IS_VALID_GPIO(pin_number) && !(_in_use_pin_mask & PIN_BIT(pin_number)); } bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) { diff --git a/ports/espressif/common-hal/microcontroller/Processor.c b/ports/espressif/common-hal/microcontroller/Processor.c index 26cec5eb467..0b117ec779a 100644 --- a/ports/espressif/common-hal/microcontroller/Processor.c +++ b/ports/espressif/common-hal/microcontroller/Processor.c @@ -15,6 +15,7 @@ #include "shared-bindings/microcontroller/Processor.h" #include "shared-bindings/microcontroller/ResetReason.h" +#include "esp_mac.h" #include "esp_sleep.h" #include "esp_system.h" #include "esp_pm.h" @@ -161,8 +162,7 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { mac_address_part >>= 8; *ptr-- = swap_nibbles(mac_address_part & 0xff); #else - // TODO: Get UID for ESP32-P4. - return; + CHECK_ESP_RESULT(esp_efuse_mac_get_default(raw_id)); #endif } diff --git a/ports/espressif/common-hal/microcontroller/__init__.c b/ports/espressif/common-hal/microcontroller/__init__.c index 2da2d09f22d..04eb54f8c2c 100644 --- a/ports/espressif/common-hal/microcontroller/__init__.c +++ b/ports/espressif/common-hal/microcontroller/__init__.c @@ -111,7 +111,7 @@ void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { #if defined(CONFIG_IDF_TARGET_ESP32) // No UF2 bootloader. #elif defined(CONFIG_IDF_TARGET_ESP32P4) - REG_WRITE(LP_SYSTEM_REG_SYS_CTRL_REG, 0); + REG_CLR_BIT(LP_SYSTEM_REG_SYS_CTRL_REG, LP_SYSTEM_REG_FORCE_DOWNLOAD_BOOT); #elif defined(SOC_LP_AON_SUPPORTED) REG_WRITE(LP_AON_SYS_CFG_REG, 0); // reset bootloader #else @@ -132,7 +132,7 @@ void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { #endif #if defined(CONFIG_IDF_TARGET_ESP32P4) - REG_WRITE(LP_SYSTEM_REG_SYS_CTRL_REG, LP_SYSTEM_REG_FORCE_DOWNLOAD_BOOT); + REG_SET_BIT(LP_SYSTEM_REG_SYS_CTRL_REG, LP_SYSTEM_REG_FORCE_DOWNLOAD_BOOT); #elif defined(SOC_LP_AON_SUPPORTED) REG_WRITE(LP_AON_SYS_CFG_REG, LP_AON_FORCE_DOWNLOAD_BOOT); #else @@ -347,5 +347,9 @@ static const mp_rom_map_elem_t mcu_pin_global_dict_table[] = { #ifdef GPIO54_EXISTS { MP_ROM_QSTR(MP_QSTR_GPIO54), MP_ROM_PTR(&pin_GPIO54) }, #endif + #if defined(CONFIG_IDF_TARGET_ESP32P4) + { MP_ROM_QSTR(MP_QSTR_USB_HS_DP), MP_ROM_PTR(&pin_USB_HS_DP) }, + { MP_ROM_QSTR(MP_QSTR_USB_HS_DM), MP_ROM_PTR(&pin_USB_HS_DM) }, + #endif }; MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_global_dict_table); diff --git a/ports/espressif/common-hal/mipidsi/Display.c b/ports/espressif/common-hal/mipidsi/Display.c index dc70f901437..2fdbe331311 100644 --- a/ports/espressif/common-hal/mipidsi/Display.c +++ b/ports/espressif/common-hal/mipidsi/Display.c @@ -74,8 +74,9 @@ void common_hal_mipidsi_display_construct(mipidsi_display_obj_t *self, esp_lcd_dpi_panel_config_t dpi_config = { .virtual_channel = virtual_channel, .dpi_clk_src = MIPI_DSI_DPI_CLK_SRC_DEFAULT, - .dpi_clock_freq_mhz = pixel_clock_frequency / 1000000, + .dpi_clock_freq_mhz = pixel_clock_frequency / 1000000.0f, .in_color_format = color_format, + .out_color_format = color_format, .num_fbs = 1, .video_timing = { .h_size = width, diff --git a/ports/espressif/common-hal/sdioio/SDCard.c b/ports/espressif/common-hal/sdioio/SDCard.c index 35850f2f8ae..cef59631351 100644 --- a/ports/espressif/common-hal/sdioio/SDCard.c +++ b/ports/espressif/common-hal/sdioio/SDCard.c @@ -22,7 +22,6 @@ static const char *TAG = "SDCard.c"; static bool slot_in_use[2]; static bool never_reset_sdio[2] = { false, false }; -static bool host_initialized = false; static void common_hal_sdioio_sdcard_check_for_deinit(sdioio_sdcard_obj_t *self) { if (common_hal_sdioio_sdcard_deinited(self)) { @@ -35,9 +34,17 @@ static int check_pins(const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command, // ESP32-S3 and P4 can use any pin for any SDMMC func in either slot // Default to SLOT_1 for SD cards ESP_LOGI(TAG, "Using chip with CONFIG_SOC_SDMMC_USE_GPIO_MATRIX"); - if (!slot_in_use[1]) { + if (!slot_in_use[1] + #ifdef CONFIG_ESP_HOSTED_SDIO_HOST_INTERFACE + && CONFIG_ESP_HOSTED_SDIO_SLOT != 1 + #endif + ) { return SDMMC_HOST_SLOT_1; - } else if (!slot_in_use[0]) { + } else if (!slot_in_use[0] + #ifdef CONFIG_ESP_HOSTED_SDIO_HOST_INTERFACE + && CONFIG_ESP_HOSTED_SDIO_SLOT != 0 + #endif + ) { return SDMMC_HOST_SLOT_0; } #else @@ -85,6 +92,7 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self, esp_err_t err = ESP_OK; sdmmc_host_t host = SDMMC_HOST_DEFAULT(); + host.slot = sd_slot; host.max_freq_khz = frequency / 1000; sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); @@ -113,7 +121,6 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self, if (err != ESP_OK) { mp_raise_OSError_msg_varg(MP_ERROR_TEXT("SDIO Init Error 0x%02x"), err); } - host_initialized = true; } err = sdmmc_host_init_slot(sd_slot, &slot_config); @@ -145,7 +152,7 @@ void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self, ESP_LOGI(TAG, "Number of sectors: %d with sector_size: %d", self->card.csd.capacity, self->card.csd.sector_size); - self->frequency = self->card.real_freq_khz; + self->frequency = self->card.real_freq_khz * 1000; ESP_LOGI(TAG, "Real frequency is %lu", self->frequency); self->capacity = self->card.csd.capacity; // Reported number of sectors ESP_LOGI(TAG, "Reported capacity is %lu", self->capacity); @@ -251,13 +258,10 @@ void common_hal_sdioio_sdcard_deinit(sdioio_sdcard_obj_t *self) { return; } + sdmmc_host_deinit_slot(self->slot); never_reset_sdio[get_slot_index(self)] = false; slot_in_use[get_slot_index(self)] = false; - if (!slot_in_use[0] && !slot_in_use[1] && host_initialized) { - sdmmc_host_deinit(); - host_initialized = false; - } reset_pin_number(self->command); self->command = COMMON_HAL_MCU_NO_PIN; @@ -293,13 +297,12 @@ void common_hal_sdioio_sdcard_never_reset(sdioio_sdcard_obj_t *self) { void sdioio_reset(void) { for (size_t i = 0; i < MP_ARRAY_SIZE(slot_in_use); i++) { if (!never_reset_sdio[i]) { + if (slot_in_use[i]) { + sdmmc_host_deinit_slot(i); + } slot_in_use[i] = false; } } - if (!slot_in_use[0] && !slot_in_use[1] && host_initialized) { - sdmmc_host_deinit(); - host_initialized = false; - } return; } diff --git a/ports/espressif/common-hal/usb_host/Port.c b/ports/espressif/common-hal/usb_host/Port.c new file mode 100644 index 00000000000..510906289a1 --- /dev/null +++ b/ports/espressif/common-hal/usb_host/Port.c @@ -0,0 +1,59 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Limor Fried for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "bindings/espidf/__init__.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/usb_host/Port.h" +#include "supervisor/usb.h" +#include "supervisor/shared/tick.h" +#include "esp_private/usb_phy.h" +#include "tusb.h" + +#if !defined(CONFIG_IDF_TARGET_ESP32P4) || CIRCUITPY_USB_HOST_INSTANCE != 1 +#error "USB host requires the ESP32-P4 high-speed controller" +#endif +#if CIRCUITPY_USB_DEVICE && CIRCUITPY_USB_DEVICE_INSTANCE == CIRCUITPY_USB_HOST_INSTANCE +#error "USB host and device must use different controllers" +#endif + +static usb_host_port_obj_t usb_host_instance; +static usb_phy_handle_t host_phy; + +usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) { + // The high-speed PHY has dedicated pads, not GPIOs. + if (dp != &pin_USB_HS_DP || dm != &pin_USB_HS_DM) { + raise_ValueError_invalid_pins(); + } + usb_host_port_obj_t *self = &usb_host_instance; + if (self->dp != NULL) { + return self; + } + + const usb_phy_config_t config = { + .controller = USB_PHY_CTRL_OTG, + .target = USB_PHY_TARGET_UTMI, + .otg_mode = USB_OTG_MODE_HOST, + .otg_speed = USB_PHY_SPEED_HIGH, + }; + CHECK_ESP_RESULT(usb_new_phy(&config, &host_phy)); + if (!tuh_init(TUH_OPT_RHPORT)) { + usb_del_phy(host_phy); + host_phy = NULL; + CHECK_ESP_RESULT(ESP_FAIL); + } + self->base.type = &usb_host_port_type; + self->dp = dp; + self->dm = dm; + // Keep polling enumeration timers even without an active display. + supervisor_enable_tick(); + return self; +} + +// TinyUSB installs the controller IRQ. Process its events on the VM task, +// where the Python USB device and keyboard callbacks are safe to run. +void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr) { + usb_background_schedule(); +} diff --git a/ports/espressif/common-hal/usb_host/Port.h b/ports/espressif/common-hal/usb_host/Port.h new file mode 100644 index 00000000000..e7b44c9524c --- /dev/null +++ b/ports/espressif/common-hal/usb_host/Port.h @@ -0,0 +1,15 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2022 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + const mcu_pin_obj_t *dp; + const mcu_pin_obj_t *dm; +} usb_host_port_obj_t; diff --git a/ports/espressif/common-hal/usb_host/__init__.c b/ports/espressif/common-hal/usb_host/__init__.c new file mode 100644 index 00000000000..c3cdbae38c4 --- /dev/null +++ b/ports/espressif/common-hal/usb_host/__init__.c @@ -0,0 +1,7 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2022 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// Nothing diff --git a/ports/espressif/common-hal/wifi/Radio.c b/ports/espressif/common-hal/wifi/Radio.c index 78ce251def4..ceeb01afe61 100644 --- a/ports/espressif/common-hal/wifi/Radio.c +++ b/ports/espressif/common-hal/wifi/Radio.c @@ -24,7 +24,7 @@ #include "common-hal/socketpool/__init__.h" #include "components/esp_netif/include/esp_netif_net_stack.h" -#include "components/esp_wifi/include/esp_wifi.h" +#include "esp_wifi.h" #include "soc/soc_caps.h" #include "components/lwip/include/apps/ping/ping_sock.h" #include "lwip/sockets.h" diff --git a/ports/espressif/common-hal/wifi/ScannedNetworks.c b/ports/espressif/common-hal/wifi/ScannedNetworks.c index 0a87d3ab342..2beb6b754a3 100644 --- a/ports/espressif/common-hal/wifi/ScannedNetworks.c +++ b/ports/espressif/common-hal/wifi/ScannedNetworks.c @@ -17,7 +17,7 @@ #include "shared-bindings/wifi/Radio.h" #include "shared-bindings/wifi/ScannedNetworks.h" -#include "components/esp_wifi/include/esp_wifi.h" +#include "esp_wifi.h" #include "soc/soc_caps.h" static void wifi_scannednetworks_done(wifi_scannednetworks_obj_t *self) { diff --git a/ports/espressif/common-hal/wifi/__init__.c b/ports/espressif/common-hal/wifi/__init__.c index 7a9aea3b5e0..3dcbf1d7ed0 100644 --- a/ports/espressif/common-hal/wifi/__init__.c +++ b/ports/espressif/common-hal/wifi/__init__.c @@ -17,7 +17,7 @@ #include "py/mpstate.h" #include "py/runtime.h" -#include "components/esp_wifi/include/esp_wifi.h" +#include "esp_wifi.h" #include "components/heap/include/esp_heap_caps.h" @@ -40,6 +40,11 @@ wifi_radio_obj_t common_hal_wifi_radio_obj; #include "esp_ipc.h" +#ifdef CONFIG_ESP_HOSTED_ENABLED +#include "esp_hosted.h" +#include "common-hal/microcontroller/Pin.h" +#endif + #ifdef CONFIG_IDF_TARGET_ESP32 #include "nvs_flash.h" #endif @@ -457,6 +462,19 @@ void common_hal_wifi_init(bool user_initiated) { if (!wifi_ever_inited) { ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_netif_init()); + #ifdef CONFIG_ESP_HOSTED_ENABLED + const uint8_t pins[] = { + CONFIG_ESP_HOSTED_SDIO_PIN_CLK, CONFIG_ESP_HOSTED_SDIO_PIN_CMD, + CONFIG_ESP_HOSTED_SDIO_PIN_D0, CONFIG_ESP_HOSTED_SDIO_PIN_D1, + CONFIG_ESP_HOSTED_SDIO_PIN_D2, CONFIG_ESP_HOSTED_SDIO_PIN_D3, + CONFIG_ESP_HOSTED_SDIO_GPIO_RESET_SLAVE, + }; + for (size_t i = 0; i < MP_ARRAY_SIZE(pins); i++) { + claim_pin_number(pins[i]); + never_reset_pin_number(pins[i]); + } + ESP_ERROR_CHECK(esp_hosted_init()); + #endif wifi_ever_inited = true; } diff --git a/ports/espressif/esp-hosted b/ports/espressif/esp-hosted new file mode 160000 index 00000000000..dd0176e5fc9 --- /dev/null +++ b/ports/espressif/esp-hosted @@ -0,0 +1 @@ +Subproject commit dd0176e5fc959d79b306f6677a2f878123c58e9a diff --git a/ports/espressif/esp-idf b/ports/espressif/esp-idf index 2b900d1222c..577a17f7e4d 160000 --- a/ports/espressif/esp-idf +++ b/ports/espressif/esp-idf @@ -1 +1 @@ -Subproject commit 2b900d1222cfce5dfa340ed99375af69ec1ee192 +Subproject commit 577a17f7e4d40c70ba2dde2fa7209e7a77855f86 diff --git a/ports/espressif/esp-wifi-remote b/ports/espressif/esp-wifi-remote new file mode 160000 index 00000000000..896c12ed915 --- /dev/null +++ b/ports/espressif/esp-wifi-remote @@ -0,0 +1 @@ +Subproject commit 896c12ed915adcb2f9a8297fe65814af6d1b7bf6 diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 48ab8d04580..0c6a9e650f5 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -347,11 +347,12 @@ else ifeq ($(IDF_TARGET),esp32p4) # No DAC CIRCUITPY_AUDIOIO = 0 -# No wifi -# TODO: Support ESP32-C6 coprocessor on some boards. +# Optional ESP-Hosted Wi-Fi coprocessor. +CIRCUITPY_ESP_HOSTED ?= 0 +CIRCUITPY_ESPNOW = 0 CIRCUITPY_BLEIO_NATIVE = 0 -CIRCUITPY_WIFI = 0 -CIRCUITPY_SSL = 0 +CIRCUITPY_WIFI = $(CIRCUITPY_ESP_HOSTED) +CIRCUITPY_SSL = $(CIRCUITPY_ESP_HOSTED) # Second stage bootloader doesn't work when the factory partition is empty due to # UF2 missing. diff --git a/ports/espressif/peripherals/esp32p4/pins.c b/ports/espressif/peripherals/esp32p4/pins.c index a4e085e6971..05a6bc33bfa 100644 --- a/ports/espressif/peripherals/esp32p4/pins.c +++ b/ports/espressif/peripherals/esp32p4/pins.c @@ -55,9 +55,13 @@ const mcu_pin_obj_t pin_GPIO45 = PIN(45, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNE const mcu_pin_obj_t pin_GPIO46 = PIN(46, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); const mcu_pin_obj_t pin_GPIO47 = PIN(47, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); const mcu_pin_obj_t pin_GPIO48 = PIN(48, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); -const mcu_pin_obj_t pin_GPIO49 = PIN(49, ADC_UNIT_2, ADC_CHANNEL_2, NO_TOUCH_CHANNEL); -const mcu_pin_obj_t pin_GPIO50 = PIN(50, ADC_UNIT_2, ADC_CHANNEL_3, NO_TOUCH_CHANNEL); -const mcu_pin_obj_t pin_GPIO51 = PIN(51, ADC_UNIT_2, ADC_CHANNEL_4, NO_TOUCH_CHANNEL); -const mcu_pin_obj_t pin_GPIO52 = PIN(52, ADC_UNIT_2, ADC_CHANNEL_5, NO_TOUCH_CHANNEL); -const mcu_pin_obj_t pin_GPIO53 = PIN(53, ADC_UNIT_2, ADC_CHANNEL_6, NO_TOUCH_CHANNEL); -const mcu_pin_obj_t pin_GPIO54 = PIN(54, ADC_UNIT_2, ADC_CHANNEL_7, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO49 = PIN(49, ADC_UNIT_2, ADC_CHANNEL_0, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO50 = PIN(50, ADC_UNIT_2, ADC_CHANNEL_1, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO51 = PIN(51, ADC_UNIT_2, ADC_CHANNEL_2, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO52 = PIN(52, ADC_UNIT_2, ADC_CHANNEL_3, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO53 = PIN(53, ADC_UNIT_2, ADC_CHANNEL_4, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_GPIO54 = PIN(54, ADC_UNIT_2, ADC_CHANNEL_5, NO_TOUCH_CHANNEL); + +// The USB high-speed PHY pads cannot be used as GPIOs. +const mcu_pin_obj_t pin_USB_HS_DP = PIN(NO_PIN, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); +const mcu_pin_obj_t pin_USB_HS_DM = PIN(NO_PIN, NO_ADC, NO_ADC_CHANNEL, NO_TOUCH_CHANNEL); diff --git a/ports/espressif/peripherals/esp32p4/pins.h b/ports/espressif/peripherals/esp32p4/pins.h index 59cf4258a40..7f9f7717c88 100644 --- a/ports/espressif/peripherals/esp32p4/pins.h +++ b/ports/espressif/peripherals/esp32p4/pins.h @@ -120,3 +120,6 @@ extern const mcu_pin_obj_t pin_GPIO52; extern const mcu_pin_obj_t pin_GPIO53; #define GPIO54_EXISTS 1 extern const mcu_pin_obj_t pin_GPIO54; + +extern const mcu_pin_obj_t pin_USB_HS_DP; +extern const mcu_pin_obj_t pin_USB_HS_DM; diff --git a/ports/espressif/supervisor/usb.c b/ports/espressif/supervisor/usb.c index 7c3b1561a44..df133fcb69c 100644 --- a/ports/espressif/supervisor/usb.c +++ b/ports/espressif/supervisor/usb.c @@ -27,7 +27,10 @@ #include "tusb.h" #ifdef CONFIG_IDF_TARGET_ESP32P4 +#include "esp_rom_sys.h" +#include "esp_system.h" #include "hal/usb_serial_jtag_ll.h" +#include "soc/lp_system_reg.h" #endif #if CIRCUITPY_USB_DEVICE @@ -42,6 +45,20 @@ StaticTask_t usb_device_taskdef; static usb_phy_handle_t device_phy_hdl; +#ifdef CONFIG_IDF_TARGET_ESP32P4 +static void usb_shutdown(void) { + if (!(REG_READ(LP_SYSTEM_REG_SYS_CTRL_REG) & LP_SYSTEM_REG_FORCE_DOWNLOAD_BOOT)) { + return; + } + tud_disconnect(); + vTaskDelay(pdMS_TO_TICKS(20)); + // The PHY selection survives a CPU reset. Restore the ROM downloader's pins. + usb_serial_jtag_ll_phy_select(0); + // Reset the USB peripheral too so the ROM can initialize its downloader. + esp_rom_software_reset_system(); +} +#endif + // USB Device Driver task // This top level thread process all usb events and invoke callbacks static void usb_device_task(void *param) { @@ -105,6 +122,10 @@ void init_usb_hardware(void) { usb_serial_jtag_ll_phy_disable_pull_override(); #endif + #ifdef CONFIG_IDF_TARGET_ESP32P4 + ESP_ERROR_CHECK(esp_register_shutdown_handler(usb_shutdown)); + #endif + // Pin the USB task to the same core as CircuitPython. This way we leave // the other core for networking. (void)xTaskCreateStaticPinnedToCore(usb_device_task, diff --git a/supervisor/shared/usb/tusb_config.h b/supervisor/shared/usb/tusb_config.h index 8bd920c996f..e66b5f71e90 100644 --- a/supervisor/shared/usb/tusb_config.h +++ b/supervisor/shared/usb/tusb_config.h @@ -180,7 +180,7 @@ extern "C" { #define CFG_TUH_RPI_PIO_USB 1 #endif -#if CIRCUITPY_USB_DEVICE_HIGH_SPEED == 1 +#if CIRCUITPY_USB_DEVICE_HIGH_SPEED == 1 || (defined(CONFIG_IDF_TARGET_ESP32P4) && CIRCUITPY_USB_HOST_INSTANCE == 1) #define _HOST_SPEED OPT_MODE_HIGH_SPEED #else #define _HOST_SPEED 0 diff --git a/supervisor/shared/usb/usb.c b/supervisor/shared/usb/usb.c index 51222be0e59..fbb00541cbc 100644 --- a/supervisor/shared/usb/usb.c +++ b/supervisor/shared/usb/usb.c @@ -167,6 +167,11 @@ void usb_background(void) { #elif CFG_TUSB_OS == OPT_OS_FREERTOS // TinyUSB may run in a separate task, at the same priority as CircuitPython. port_task_yield(); + #if CIRCUITPY_USB_HOST + if (tuh_inited()) { + tuh_task_ext(0, false); + } + #endif #endif // No need to flush if there's no REPL. #if CIRCUITPY_USB_DEVICE && CIRCUITPY_USB_CDC From a62beb436721c699fdfbb143f75c4c2febee898e Mon Sep 17 00:00:00 2001 From: Eagle Claw <301410538+ladyada-eagleclaw@users.noreply.github.com> Date: Fri, 2 Oct 2026 21:40:44 -0400 Subject: [PATCH 2/7] Add Adafruit Metro ESP32-P4 board Configure USB-C device, USB-A host, SDIO storage and the C6 Hosted coprocessor with the allocated Adafruit USB ID. Co-authored-by: Limor Fried --- .../boards/adafruit_metro_esp32p4/board.c | 19 +++++ .../adafruit_metro_esp32p4/mpconfigboard.h | 30 +++++++ .../adafruit_metro_esp32p4/mpconfigboard.mk | 17 ++++ .../boards/adafruit_metro_esp32p4/pins.c | 81 +++++++++++++++++++ .../boards/adafruit_metro_esp32p4/sdkconfig | 25 ++++++ 5 files changed, 172 insertions(+) create mode 100644 ports/espressif/boards/adafruit_metro_esp32p4/board.c create mode 100644 ports/espressif/boards/adafruit_metro_esp32p4/mpconfigboard.h create mode 100644 ports/espressif/boards/adafruit_metro_esp32p4/mpconfigboard.mk create mode 100644 ports/espressif/boards/adafruit_metro_esp32p4/pins.c create mode 100644 ports/espressif/boards/adafruit_metro_esp32p4/sdkconfig diff --git a/ports/espressif/boards/adafruit_metro_esp32p4/board.c b/ports/espressif/boards/adafruit_metro_esp32p4/board.c new file mode 100644 index 00000000000..53af421609b --- /dev/null +++ b/ports/espressif/boards/adafruit_metro_esp32p4/board.c @@ -0,0 +1,19 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Limor Fried for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "supervisor/board.h" +#include "esp_ldo_regulator.h" + +static esp_ldo_channel_handle_t io_ldo; + +void board_init(void) { + // The SD card and C6 control pins (GPIO39-48) require the VO4 3.3 V rail. + const esp_ldo_channel_config_t config = { + .chan_id = 4, + .voltage_mv = 3300, + }; + ESP_ERROR_CHECK(esp_ldo_acquire_channel(&config, &io_ldo)); +} diff --git a/ports/espressif/boards/adafruit_metro_esp32p4/mpconfigboard.h b/ports/espressif/boards/adafruit_metro_esp32p4/mpconfigboard.h new file mode 100644 index 00000000000..b6675893027 --- /dev/null +++ b/ports/espressif/boards/adafruit_metro_esp32p4/mpconfigboard.h @@ -0,0 +1,30 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Limor Fried for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#define MICROPY_HW_BOARD_NAME "Adafruit Metro ESP32-P4" +#define MICROPY_HW_MCU_NAME "ESP32P4" + +#define MICROPY_HW_NEOPIXEL (&pin_GPIO23) +#define CIRCUITPY_BOOT_BUTTON (&pin_GPIO35) + +#define DEFAULT_I2C_BUS_SCL (&pin_GPIO32) +#define DEFAULT_I2C_BUS_SDA (&pin_GPIO33) + +#define DEFAULT_SPI_BUS_SCK (&pin_GPIO15) +#define DEFAULT_SPI_BUS_MOSI (&pin_GPIO16) +#define DEFAULT_SPI_BUS_MISO (&pin_GPIO17) + +#define DEFAULT_UART_BUS_RX (&pin_GPIO0) +#define DEFAULT_UART_BUS_TX (&pin_GPIO1) + +// USB-C shares GPIO24/25 with the ROM USB Serial/JTAG downloader. +#define CIRCUITPY_USB_DEVICE_INSTANCE 0 +#define CIRCUITPY_ESP32P4_SWAP_LSFS (1) + +// USB-A is connected to the dedicated high-speed PHY. +#define CIRCUITPY_USB_HOST_INSTANCE 1 diff --git a/ports/espressif/boards/adafruit_metro_esp32p4/mpconfigboard.mk b/ports/espressif/boards/adafruit_metro_esp32p4/mpconfigboard.mk new file mode 100644 index 00000000000..4dca7e3879a --- /dev/null +++ b/ports/espressif/boards/adafruit_metro_esp32p4/mpconfigboard.mk @@ -0,0 +1,17 @@ +USB_VID = 0x239A +USB_PID = 0x817C +USB_PRODUCT = "Adafruit Metro ESP32-P4" +USB_MANUFACTURER = "Adafruit" + +IDF_TARGET = esp32p4 + +CIRCUITPY_USB_HOST = 1 +CIRCUITPY_ESP_HOSTED = 1 + +CIRCUITPY_ESP_FLASH_SIZE = 16MB +CIRCUITPY_ESP_FLASH_MODE = dio +CIRCUITPY_ESP_FLASH_FREQ = 80m + +CIRCUITPY_ESP_PSRAM_SIZE = 16MB +CIRCUITPY_ESP_PSRAM_MODE = hpi +CIRCUITPY_ESP_PSRAM_FREQ = 200m diff --git a/ports/espressif/boards/adafruit_metro_esp32p4/pins.c b/ports/espressif/boards/adafruit_metro_esp32p4/pins.c new file mode 100644 index 00000000000..ee2679ae1f0 --- /dev/null +++ b/ports/espressif/boards/adafruit_metro_esp32p4/pins.c @@ -0,0 +1,81 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Limor Fried for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "shared-bindings/board/__init__.h" + +static const mp_rom_map_elem_t board_module_globals_table[] = { + CIRCUITPYTHON_BOARD_DICT_STANDARD_ITEMS + + { MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_GPIO49) }, + { MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_GPIO50) }, + { MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_GPIO51) }, + { MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_GPIO52) }, + { MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_GPIO53) }, + { MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_GPIO54) }, + + { MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_GPIO0) }, + { MP_ROM_QSTR(MP_QSTR_RX), MP_ROM_PTR(&pin_GPIO0) }, + { MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_GPIO1) }, + { MP_ROM_QSTR(MP_QSTR_TX), MP_ROM_PTR(&pin_GPIO1) }, + { MP_ROM_QSTR(MP_QSTR_D2), MP_ROM_PTR(&pin_GPIO2) }, + { MP_ROM_QSTR(MP_QSTR_D3), MP_ROM_PTR(&pin_GPIO3) }, + { MP_ROM_QSTR(MP_QSTR_D4), MP_ROM_PTR(&pin_GPIO4) }, + { MP_ROM_QSTR(MP_QSTR_D5), MP_ROM_PTR(&pin_GPIO5) }, + { MP_ROM_QSTR(MP_QSTR_D6), MP_ROM_PTR(&pin_GPIO6) }, + { MP_ROM_QSTR(MP_QSTR_D7), MP_ROM_PTR(&pin_GPIO7) }, + { MP_ROM_QSTR(MP_QSTR_D8), MP_ROM_PTR(&pin_GPIO8) }, + { MP_ROM_QSTR(MP_QSTR_D9), MP_ROM_PTR(&pin_GPIO9) }, + { MP_ROM_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_GPIO10) }, + { MP_ROM_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_GPIO11) }, + { MP_ROM_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_GPIO12) }, + { MP_ROM_QSTR(MP_QSTR_D13), MP_ROM_PTR(&pin_GPIO13) }, + { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO13) }, + + { MP_ROM_QSTR(MP_QSTR_SCK), MP_ROM_PTR(&pin_GPIO15) }, + { MP_ROM_QSTR(MP_QSTR_MOSI), MP_ROM_PTR(&pin_GPIO16) }, + { MP_ROM_QSTR(MP_QSTR_MISO), MP_ROM_PTR(&pin_GPIO17) }, + { MP_ROM_QSTR(MP_QSTR_SCL), MP_ROM_PTR(&pin_GPIO32) }, + { MP_ROM_QSTR(MP_QSTR_SDA), MP_ROM_PTR(&pin_GPIO33) }, + + { MP_ROM_QSTR(MP_QSTR_BUTTON), MP_ROM_PTR(&pin_GPIO35) }, + { MP_ROM_QSTR(MP_QSTR_NEOPIXEL), MP_ROM_PTR(&pin_GPIO23) }, + { MP_ROM_QSTR(MP_QSTR_VOLTAGE_MONITOR), MP_ROM_PTR(&pin_GPIO18) }, + { MP_ROM_QSTR(MP_QSTR_USB_HOST_DP), MP_ROM_PTR(&pin_USB_HS_DP) }, + { MP_ROM_QSTR(MP_QSTR_USB_HOST_DM), MP_ROM_PTR(&pin_USB_HS_DM) }, + { MP_ROM_QSTR(MP_QSTR_USB_HOST_POWER), MP_ROM_PTR(&pin_GPIO14) }, + { MP_ROM_QSTR(MP_QSTR_DEBUG_TX), MP_ROM_PTR(&pin_GPIO37) }, + { MP_ROM_QSTR(MP_QSTR_DEBUG_RX), MP_ROM_PTR(&pin_GPIO38) }, + + { MP_ROM_QSTR(MP_QSTR_SD_CLK), MP_ROM_PTR(&pin_GPIO43) }, + { MP_ROM_QSTR(MP_QSTR_SD_CMD), MP_ROM_PTR(&pin_GPIO44) }, + { MP_ROM_QSTR(MP_QSTR_SD_DATA0), MP_ROM_PTR(&pin_GPIO39) }, + { MP_ROM_QSTR(MP_QSTR_SD_DATA1), MP_ROM_PTR(&pin_GPIO40) }, + { MP_ROM_QSTR(MP_QSTR_SD_DATA2), MP_ROM_PTR(&pin_GPIO41) }, + { MP_ROM_QSTR(MP_QSTR_SD_DATA3), MP_ROM_PTR(&pin_GPIO42) }, + { MP_ROM_QSTR(MP_QSTR_SD_CD), MP_ROM_PTR(&pin_GPIO45) }, + + { MP_ROM_QSTR(MP_QSTR_C6_RX), MP_ROM_PTR(&pin_GPIO26) }, + { MP_ROM_QSTR(MP_QSTR_C6_TX), MP_ROM_PTR(&pin_GPIO27) }, + { MP_ROM_QSTR(MP_QSTR_C6_BOOT), MP_ROM_PTR(&pin_GPIO46) }, + { MP_ROM_QSTR(MP_QSTR_C6_RESET), MP_ROM_PTR(&pin_GPIO47) }, + { MP_ROM_QSTR(MP_QSTR_C6_WAKE), MP_ROM_PTR(&pin_GPIO48) }, + { MP_ROM_QSTR(MP_QSTR_C6_SDIO_CLK), MP_ROM_PTR(&pin_GPIO34) }, + { MP_ROM_QSTR(MP_QSTR_C6_SDIO_CMD), MP_ROM_PTR(&pin_GPIO36) }, + { MP_ROM_QSTR(MP_QSTR_C6_SDIO_DATA0), MP_ROM_PTR(&pin_GPIO31) }, + { MP_ROM_QSTR(MP_QSTR_C6_SDIO_DATA1), MP_ROM_PTR(&pin_GPIO30) }, + { MP_ROM_QSTR(MP_QSTR_C6_SDIO_DATA2), MP_ROM_PTR(&pin_GPIO29) }, + { MP_ROM_QSTR(MP_QSTR_C6_SDIO_DATA3), MP_ROM_PTR(&pin_GPIO28) }, + + { MP_ROM_QSTR(MP_QSTR_CAMERA_RESET), MP_ROM_PTR(&pin_GPIO21) }, + { MP_ROM_QSTR(MP_QSTR_LCD_BACKLIGHT), MP_ROM_PTR(&pin_GPIO19) }, + { MP_ROM_QSTR(MP_QSTR_LCD_RESET), MP_ROM_PTR(&pin_GPIO20) }, + + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_STEMMA_I2C), MP_ROM_PTR(&board_i2c_obj) }, + { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, + { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, +}; +MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/espressif/boards/adafruit_metro_esp32p4/sdkconfig b/ports/espressif/boards/adafruit_metro_esp32p4/sdkconfig new file mode 100644 index 00000000000..a30634b1e0d --- /dev/null +++ b/ports/espressif/boards/adafruit_metro_esp32p4/sdkconfig @@ -0,0 +1,25 @@ +CONFIG_ESP32P4_SELECTS_REV_LESS_V3=y +CONFIG_ESP32P4_REV_MIN_1=y +CONFIG_ESP_WIFI_REMOTE_ENABLED=y +CONFIG_ESP_WIFI_REMOTE_LIBRARY_HOSTED=y +CONFIG_SLAVE_IDF_TARGET_ESP32C6=y +CONFIG_ESP_HOSTED_ENABLED=y +CONFIG_ESP_HOSTED_CP_TARGET_ESP32C6=y +CONFIG_ESP_HOSTED_P4_DEV_BOARD_NONE=y +CONFIG_ESP_HOSTED_SDIO_HOST_INTERFACE=y +CONFIG_ESP_HOSTED_SDIO_SLOT_1=y +CONFIG_ESP_HOSTED_SDIO_4_BIT_BUS=y +CONFIG_ESP_HOSTED_PRIV_SDIO_PIN_CLK_SLOT_1=34 +CONFIG_ESP_HOSTED_PRIV_SDIO_PIN_CMD_SLOT_1=36 +CONFIG_ESP_HOSTED_PRIV_SDIO_PIN_D0_SLOT_1=31 +CONFIG_ESP_HOSTED_PRIV_SDIO_PIN_D1_4BIT_BUS_SLOT_1=30 +CONFIG_ESP_HOSTED_PRIV_SDIO_PIN_D2_4BIT_BUS_SLOT_1=29 +CONFIG_ESP_HOSTED_PRIV_SDIO_PIN_D3_4BIT_BUS_SLOT_1=28 +CONFIG_ESP_HOSTED_SDIO_GPIO_RESET_SLAVE=47 +CONFIG_ESP_HOSTED_SDIO_CLOCK_FREQ_KHZ=40000 + +# CONFIG_WIFI_RMT_NVS_ENABLED is not set + +# CONFIG_ESP_WIFI_NVS_ENABLED is not set + +# CONFIG_ESP32_WIFI_NVS_ENABLED is not set From edeb01cc204773fc5f5366ac933d37c26fda805c Mon Sep 17 00:00:00 2001 From: Eagle Claw <301410538+ladyada-eagleclaw@users.noreply.github.com> Date: Mon, 5 Oct 2026 10:31:03 -0400 Subject: [PATCH 3/7] ci: fetch ESP-Hosted protobuf-c submodule Initialize the required nested runtime after fetching ESP-Hosted, using the same filter or shallow-clone options as mbedtls. Co-authored-by: Limor Fried --- tools/ci_fetch_deps.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tools/ci_fetch_deps.py b/tools/ci_fetch_deps.py index 8ef4df9fc6f..fd870582414 100644 --- a/tools/ci_fetch_deps.py +++ b/tools/ci_fetch_deps.py @@ -157,6 +157,16 @@ def fetch(where): cwd=mbedtls, ) + # ESP-Hosted keeps the protobuf-c runtime in a nested submodule. + esp_hosted = TOP / "ports" / "espressif" / "esp-hosted" + if (esp_hosted / ".gitmodules").exists(): + depth_maybe = "" if clone_supports_filter else "--depth 1" + run( + "Init esp-hosted nested submodules", + f"git submodule update --init {filter_maybe} {depth_maybe} common/protobuf-c", + cwd=esp_hosted, + ) + def set_output(name, value): if "GITHUB_OUTPUT" in os.environ: From c04d6043c814ad64e5b2c0a8034db61d836cca8b Mon Sep 17 00:00:00 2001 From: Eagle Claw <301410538+ladyada-eagleclaw@users.noreply.github.com> Date: Mon, 5 Oct 2026 10:31:03 -0400 Subject: [PATCH 4/7] boards: acquire Metro P4 VO4 rail only once Retain the existing LDO handle across simulated deep-sleep wakes instead of adding another reference each time board_init runs. Co-authored-by: Limor Fried --- ports/espressif/boards/adafruit_metro_esp32p4/board.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/espressif/boards/adafruit_metro_esp32p4/board.c b/ports/espressif/boards/adafruit_metro_esp32p4/board.c index 53af421609b..85e13ed334d 100644 --- a/ports/espressif/boards/adafruit_metro_esp32p4/board.c +++ b/ports/espressif/boards/adafruit_metro_esp32p4/board.c @@ -10,6 +10,11 @@ static esp_ldo_channel_handle_t io_ldo; void board_init(void) { + // Keep the rail acquired across simulated deep-sleep wakes. + if (io_ldo != NULL) { + return; + } + // The SD card and C6 control pins (GPIO39-48) require the VO4 3.3 V rail. const esp_ldo_channel_config_t config = { .chan_id = 4, From 2b424a11fd26a79bf44b733026b5064154025152 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 20 Oct 2026 15:26:39 -0700 Subject: [PATCH 5/7] espressif: drop esp-wifi-remote ESP-IDF v6.1 integrates Wi-Fi remote support into esp_wifi itself --- .gitmodules | 3 --- ports/espressif/CMakeLists.txt | 4 ++-- ports/espressif/Makefile | 10 ++++------ ports/espressif/esp-wifi-remote | 1 - 4 files changed, 6 insertions(+), 12 deletions(-) delete mode 160000 ports/espressif/esp-wifi-remote diff --git a/.gitmodules b/.gitmodules index b8ea36cec02..f02565e6915 100644 --- a/.gitmodules +++ b/.gitmodules @@ -424,6 +424,3 @@ [submodule "ports/espressif/esp-hosted"] path = ports/espressif/esp-hosted url = https://github.com/espressif/esp-hosted-mcu.git -[submodule "ports/espressif/esp-wifi-remote"] - path = ports/espressif/esp-wifi-remote - url = https://github.com/espressif/esp-wifi-remote.git diff --git a/ports/espressif/CMakeLists.txt b/ports/espressif/CMakeLists.txt index 9d7d44e1455..e0901d649a4 100644 --- a/ports/espressif/CMakeLists.txt +++ b/ports/espressif/CMakeLists.txt @@ -9,8 +9,8 @@ set(COMPONENTS bt driver esp_driver_dac esp_driver_gpio esp_driver_gptimer esp_d set(EXTRA_COMPONENT_DIRS "esp-protocols/components/mdns" "esp-camera") if(CIRCUITPY_ESP_HOSTED) - list(APPEND EXTRA_COMPONENT_DIRS "esp-hosted" "esp-wifi-remote/components/esp_wifi_remote") - list(APPEND COMPONENTS esp-hosted esp_wifi_remote) + list(APPEND EXTRA_COMPONENT_DIRS "esp-hosted") + list(APPEND COMPONENTS esp-hosted) endif() include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/ports/espressif/Makefile b/ports/espressif/Makefile index c8e59a8a0e5..978a3b0e7c0 100644 --- a/ports/espressif/Makefile +++ b/ports/espressif/Makefile @@ -1051,12 +1051,10 @@ ESP_IDF_COMPONENTS_LINK += esp_coex esp_phy wpa_supplicant endif endif ifeq ($(CIRCUITPY_ESP_HOSTED),1) -export ESP_IDF_VERSION := $(shell sed -nE 's/set\(IDF_VERSION_(MAJOR|MINOR) ([0-9]+)\)/\2/p' esp-idf/tools/cmake/version.cmake | paste -sd.) -INC := -isystem esp-wifi-remote/components/esp_wifi_remote/idf_v$(ESP_IDF_VERSION)/include/injected \ - -isystem esp-wifi-remote/components/esp_wifi_remote/idf_v$(ESP_IDF_VERSION)/include \ - -isystem esp-wifi-remote/components/esp_wifi_remote/include \ - -isystem esp-hosted/host/api/include -isystem esp-hosted/host $(INC) -ESP_IDF_COMPONENTS_LINK += esp_wifi_remote esp-hosted esp_http_client http_parser console +# ESP-IDF v6.1+ has remote Wi-Fi support built into esp_wifi, so no separate +# esp_wifi_remote component is needed. +INC := -isystem esp-hosted/host/api/include -isystem esp-hosted/host $(INC) +ESP_IDF_COMPONENTS_LINK += esp-hosted esp_http_client http_parser console REGISTRATION_FUNCTIONS += -u esp_hosted_init endif # mdns registers handlers on ETH_EVENT, which is defined by esp_eth. On EMAC-capable diff --git a/ports/espressif/esp-wifi-remote b/ports/espressif/esp-wifi-remote deleted file mode 160000 index 896c12ed915..00000000000 --- a/ports/espressif/esp-wifi-remote +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 896c12ed915adcb2f9a8297fe65814af6d1b7bf6 From 9fc280783e627188c6d024cc5868eb7ad856588f Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Tue, 20 Oct 2026 17:46:08 -0700 Subject: [PATCH 6/7] espressif: point esp-idf at pushed circuitpython-v6.1 66cb264d was never pushed, so every espressif build failed at checkout. 29d36039 is the tip of adafruit/esp-idf circuitpython-v6.1 and carries the same SDMMC slot removal guard (adafruit/esp-idf#21). Co-Authored-By: Claude Opus 5.5 (1M context) --- ports/espressif/esp-idf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/espressif/esp-idf b/ports/espressif/esp-idf index 66cb264daf9..29d36039e56 160000 --- a/ports/espressif/esp-idf +++ b/ports/espressif/esp-idf @@ -1 +1 @@ -Subproject commit 66cb264daf95b6b3d40e42e5722a38362d28c739 +Subproject commit 29d36039e564db6df505d0582b94b230a17e63f3 From 746c9ed32068568f24032e8fc159db0f8477611a Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Tue, 20 Oct 2026 18:11:23 -0700 Subject: [PATCH 7/7] ci: keep ESP_IDF_VERSION from export.sh for espressif builds esp-hosted's Kconfig uses $(ESP_IDF_VERSION). export.sh sets it, but the "Set environment" step only carried IDF_PYTHON_ENV_PATH and PATH forward, so ldgen failed on the Metro ESP32-P4 with "macro expanded to blank string". Co-Authored-By: Claude Opus 5.5 (1M context) --- .github/actions/deps/external/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/deps/external/action.yml b/.github/actions/deps/external/action.yml index 9f6dbf222a4..0624042b45b 100644 --- a/.github/actions/deps/external/action.yml +++ b/.github/actions/deps/external/action.yml @@ -40,6 +40,7 @@ runs: run: | source $IDF_PATH/export.sh echo >> $GITHUB_ENV "IDF_PYTHON_ENV_PATH=$IDF_PYTHON_ENV_PATH" + echo >> $GITHUB_ENV "ESP_IDF_VERSION=$ESP_IDF_VERSION" echo >> $GITHUB_PATH "$PATH" shell: bash