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..f5e0c2fd168 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 @@ -885,7 +890,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 +928,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 +937,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/common-hal/sdioio/SDCard.c b/ports/espressif/common-hal/sdioio/SDCard.c index 35850f2f8ae..403722d11f1 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); @@ -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/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/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: