From 0183d2ca615c8297e398111c5cfb072d44f9ab6f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 15 Mar 2026 17:09:59 -1000 Subject: [PATCH] [ethernet] Only compile JL1101 driver when needed - Exclude esp_eth_phy_jl1101.c when JL1101 is not configured - Exclude on IDF 5.4.2-5.x where pioarduino has it builtin - Keep custom driver for IDF < 5.4.2 and IDF 6.0+ (pioarduino may not have patched JL1101 into their IDF 6.0 fork yet) - Always use esp_eth_phy_new_jl1101() in C++ (no generic fallback) - Namespace ethernet type in CORE.data[KEY_ETHERNET] --- esphome/components/ethernet/__init__.py | 23 ++++++++++++------- .../components/ethernet/esp_eth_phy_jl1101.c | 3 +-- .../ethernet/ethernet_component_esp32.cpp | 9 ++------ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/esphome/components/ethernet/__init__.py b/esphome/components/ethernet/__init__.py index 3cc39c6e9e..b534df8985 100644 --- a/esphome/components/ethernet/__init__.py +++ b/esphome/components/ethernet/__init__.py @@ -54,6 +54,9 @@ LOGGER = logging.getLogger(__name__) # Key for tracking IP state listener count in CORE.data ETHERNET_IP_STATE_LISTENERS_KEY = "ethernet_ip_state_listeners" +# Key for tracking configured ethernet type +ETHERNET_TYPE_KEY = "ethernet_type" +KEY_ETHERNET = "ethernet" def request_ethernet_ip_state_listener() -> None: @@ -430,6 +433,7 @@ async def to_code(config): cg.add(var.set_type(ETHERNET_TYPES[config[CONF_TYPE]])) cg.add(var.set_use_address(config[CONF_USE_ADDRESS])) + CORE.data.setdefault(KEY_ETHERNET, {})[ETHERNET_TYPE_KEY] = config[CONF_TYPE] if CONF_MANUAL_IP in config: cg.add_define("USE_ETHERNET_MANUAL_IP") @@ -616,15 +620,18 @@ _platform_filter = filter_source_files_from_platform( def _filter_source_files() -> list[str]: excluded = _platform_filter() - if not CORE.is_esp32: - return excluded - from esphome.components.esp32 import idf_version - - # Custom JL1101 driver not needed on IDF >= 6.0 (uses generic PHY) - # On IDF 5.4.2+ with PlatformIO, the .c file compiles to empty via - # its own preprocessor guard (PLATFORMIO is a C-level define) - if idf_version() >= cv.Version(6, 0, 0): + eth_data = CORE.data.get(KEY_ETHERNET, {}) + eth_type = eth_data.get(ETHERNET_TYPE_KEY) + # Only compile the custom JL1101 driver when JL1101 is configured + # and pioarduino doesn't have it builtin (IDF 5.4.2 to 5.x) + if eth_type != "JL1101": excluded.append("esp_eth_phy_jl1101.c") + elif CORE.is_esp32: + from esphome.components.esp32 import idf_version + + ver = idf_version() + if cv.Version(5, 4, 2) <= ver < cv.Version(6, 0, 0): + excluded.append("esp_eth_phy_jl1101.c") return excluded diff --git a/esphome/components/ethernet/esp_eth_phy_jl1101.c b/esphome/components/ethernet/esp_eth_phy_jl1101.c index 692f759eed..b81d8227d4 100644 --- a/esphome/components/ethernet/esp_eth_phy_jl1101.c +++ b/esphome/components/ethernet/esp_eth_phy_jl1101.c @@ -29,8 +29,7 @@ #include "esp_rom_sys.h" #include "esp_idf_version.h" -#if defined(USE_ETHERNET_JL1101) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0) && \ - (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO)) +#if defined(USE_ETHERNET_JL1101) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO)) static const char *TAG = "jl1101"; #define PHY_CHECK(a, str, goto_tag, ...) \ diff --git a/esphome/components/ethernet/ethernet_component_esp32.cpp b/esphome/components/ethernet/ethernet_component_esp32.cpp index 212a60356e..9afa46f310 100644 --- a/esphome/components/ethernet/ethernet_component_esp32.cpp +++ b/esphome/components/ethernet/ethernet_component_esp32.cpp @@ -272,14 +272,9 @@ void EthernetComponent::setup() { #endif #ifdef USE_ETHERNET_JL1101 case ETHERNET_TYPE_JL1101: { -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0) - // No registry component exists; generic PHY driver works for JL1101 - this->phy_ = esp_eth_phy_new_generic(&phy_config); -#else - // IDF < 5.4.2 or non-PlatformIO: uses custom ESPHome driver (esp_eth_phy_jl1101.c) - // IDF 5.4.2+ with PlatformIO: uses builtin esp_eth_phy_new_jl1101() + // PlatformIO (pioarduino): builtin esp_eth_phy_new_jl1101() on all IDF versions + // Non-PlatformIO: custom ESPHome driver (esp_eth_phy_jl1101.c) this->phy_ = esp_eth_phy_new_jl1101(&phy_config); -#endif break; } #endif