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