From c28e6b11ee6f29755995985440b7e88bab862808 Mon Sep 17 00:00:00 2001 From: Ross Tyler Date: Mon, 1 Sep 2025 07:16:50 -0700 Subject: [PATCH] moved ethernet interface choice entirely to config verified that default choices that were made in C++ are still made now, in config. that is, c3, c6, s2 and s3 variants choose spi2 and everything else (h2) chooses spi3. these default choices can be overridden by explicit config grep -H board idf.* idf.c3.yaml: board: esp32-c3-devkitm-1 idf.c6.yaml: board: esp32-c6-devkitc-1 idf.h2.yaml: board: esp32-h2-devkitm-1 idf.s2.yaml: board: esp32-s2-saola-1 idf.s3.yaml: board: esp32-s3-devkitc-1 for y in idf.*; do esphome config $y 2>/dev/null | grep ' interface'; echo $y; done interface: spi2 idf.c3.yaml interface: spi2 idf.c6.yaml interface: spi3 idf.h2.yaml interface: spi2 idf.s2.yaml interface: spi2 idf.s3.yaml --- esphome/components/ethernet/__init__.py | 13 ++++++++----- esphome/components/ethernet/ethernet_component.cpp | 12 +----------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/esphome/components/ethernet/__init__.py b/esphome/components/ethernet/__init__.py index 302a7c0e7c..5fde2cf02e 100644 --- a/esphome/components/ethernet/__init__.py +++ b/esphome/components/ethernet/__init__.py @@ -13,6 +13,7 @@ from esphome.components.esp32.const import ( VARIANT_ESP32P4, VARIANT_ESP32S2, VARIANT_ESP32S3, + VARIANT_ESP32C6, ) from esphome.components.network import IPAddress from esphome.components.spi import CONF_INTERFACE_INDEX, get_spi_interface @@ -174,6 +175,12 @@ def _validate(config): f"({CORE.target_framework} {CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]}), " f"'{CONF_INTERRUPT_PIN}' is a required option for [ethernet]." ) + if CORE.using_esp_idf and CONF_INTERFACE not in config: + variant = get_esp32_variant() + if variant in (VARIANT_ESP32C3, VARIANT_ESP32S2, VARIANT_ESP32S3, VARIANT_ESP32C6): + config[CONF_INTERFACE] = "spi2" + else: + config[CONF_INTERFACE] = "spi3" elif config[CONF_TYPE] != "OPENETH": if CONF_CLK_MODE in config: LOGGER.warning( @@ -357,11 +364,7 @@ async def to_code(config): "spi2": spi_host_device_t.SPI2_HOST, "spi3": spi_host_device_t.SPI3_HOST, } - cg.add( - var.set_interface( - map[config[CONF_INTERFACE]] - ) - ) + cg.add(var.set_interface(map[config[CONF_INTERFACE]])) add_idf_sdkconfig_option("CONFIG_ETH_USE_SPI_ETHERNET", True) add_idf_sdkconfig_option( f"CONFIG_ETH_SPI_ETHERNET_{config[CONF_TYPE]}", True diff --git a/esphome/components/ethernet/ethernet_component.cpp b/esphome/components/ethernet/ethernet_component.cpp index f2a91bfdf9..7861c5683a 100644 --- a/esphome/components/ethernet/ethernet_component.cpp +++ b/esphome/components/ethernet/ethernet_component.cpp @@ -87,17 +87,7 @@ void EthernetComponent::setup() { .intr_flags = 0, }; -#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \ - defined(USE_ESP32_VARIANT_ESP32C6) - auto host = SPI2_HOST; -#else - auto host = SPI3_HOST; -#endif -#ifdef USE_ESP_IDF - if (this->interface_ != SPI_HOST_MAX) { - host = this->interface_; - } -#endif + auto host = this->interface_; err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO); ESPHL_ERROR_CHECK(err, "SPI bus initialize error");