diff --git a/include/Settings.h b/include/Settings.h index d707881..2a1484b 100644 --- a/include/Settings.h +++ b/include/Settings.h @@ -32,7 +32,7 @@ struct NetworkSettings { } wifi; struct { - EthernetType type = EthernetType::W5500; + EthernetType type = DEFAULT_ETHERNET_TYPE; struct { uint8_t clk = DEFAULT_ETH_CLK_GPIO; @@ -42,6 +42,14 @@ struct NetworkSettings { uint8_t irq = DEFAULT_ETH_IRQ_GPIO; uint8_t rst = DEFAULT_ETH_RST_GPIO; } w5500; + + struct { + uint8_t mdc = DEFAULT_ETH_MDC_GPIO; + uint8_t mdio = DEFAULT_ETH_MDIO_GPIO; + uint8_t power = DEFAULT_ETH_POWER_GPIO; + uint8_t phyAddr = DEFAULT_ETH_PHY_ADDR; + uint8_t clockMode = 0; // External REF_CLK on GPIO0. + } lan8720; } ethernet; NetworkMode type = NetworkMode::STA; diff --git a/include/app.h b/include/app.h index c203574..cafb1a3 100644 --- a/include/app.h +++ b/include/app.h @@ -153,7 +153,7 @@ IPAddress getNetworkSubnet(); IPAddress getNetworkGateway(); IPAddress getNetworkDns(); String getNetworkMac(); -bool isEthernetConfigured(const NetworkSettings& config = networkSettings); +bool isEthernetConfigValid(const NetworkSettings& config = networkSettings); bool isNetworkInterfaceStarted(NetworkMode mode); // Last state published by the system worker, including during event callbacks. bool isNetworkConnected(); diff --git a/include/macros.h b/include/macros.h index 8a345c1..3df1ce4 100644 --- a/include/macros.h +++ b/include/macros.h @@ -87,6 +87,10 @@ #define DEFAULT_STATUS_LED_GPIO GPIO_IS_NOT_CONFIGURED #endif +#ifndef DEFAULT_ETHERNET_TYPE + #define DEFAULT_ETHERNET_TYPE EthernetType::W5500 +#endif + #ifndef DEFAULT_ETH_CLK_GPIO #define DEFAULT_ETH_CLK_GPIO GPIO_IS_NOT_CONFIGURED #endif @@ -111,6 +115,22 @@ #define DEFAULT_ETH_RST_GPIO GPIO_IS_NOT_CONFIGURED #endif +#ifndef DEFAULT_ETH_MDC_GPIO + #define DEFAULT_ETH_MDC_GPIO GPIO_IS_NOT_CONFIGURED +#endif + +#ifndef DEFAULT_ETH_MDIO_GPIO + #define DEFAULT_ETH_MDIO_GPIO GPIO_IS_NOT_CONFIGURED +#endif + +#ifndef DEFAULT_ETH_POWER_GPIO + #define DEFAULT_ETH_POWER_GPIO GPIO_IS_NOT_CONFIGURED +#endif + +#ifndef DEFAULT_ETH_PHY_ADDR + #define DEFAULT_ETH_PHY_ADDR 1 +#endif + #ifndef DEFAULT_PORTAL_LOGIN #define DEFAULT_PORTAL_LOGIN "" #endif diff --git a/include/strings_shared.h b/include/strings_shared.h index 9fa75c0..0bf6c2c 100644 --- a/include/strings_shared.h +++ b/include/strings_shared.h @@ -52,9 +52,9 @@ inline const char S_CH2_ALWAYS_ENABLED[] = "ch2_always_enabled inline const char S_CH2_DISABLING_ACTION[] = "ch2_disabling_action"; inline const char S_CHIP[] = "chip"; inline const char S_CLK[] = "clk"; +inline const char S_CLOCK_MODE[] = "clock_mode"; inline const char S_CODE[] = "code"; inline const char S_COMMIT[] = "commit"; -inline const char S_CONFIGURED[] = "configured"; inline const char S_CONNECTED[] = "connected"; inline const char S_CONNECTION_TYPE[] = "connection_type"; inline const char S_COOLING[] = "cooling"; @@ -125,6 +125,7 @@ inline const char S_IP[] = "ip"; inline const char S_IRQ[] = "irq"; inline const char S_I_FACTOR[] = "i_factor"; inline const char S_I_MULTIPLIER[] = "i_multiplier"; +inline const char S_LAN8720[] = "lan8720"; inline const char S_LOGIN[] = "login"; inline const char S_LOG_LEVEL[] = "loglevel"; inline const char S_LOW_TEMP[] = "low_temp"; @@ -136,6 +137,8 @@ inline const char S_MAX_MODULATION[] = "max_modulation"; inline const char S_MAX_POWER[] = "max_power"; inline const char S_MAX_TEMP[] = "max_temp"; inline const char S_MAX_TEMP_SYNC_WITH_TARGET_TEMP[] = "max_temp_sync_with_target_temp"; +inline const char S_MDC[] = "mdc"; +inline const char S_MDIO[] = "mdio"; inline const char S_MDNS[] = "mdns"; inline const char S_MEMBER_ID[] = "member_id"; inline const char S_MIN[] = "min"; @@ -166,6 +169,7 @@ inline const char S_OUTPUT[] = "output"; inline const char S_OVERHEAT[] = "overheat"; inline const char S_OVERHEAT_PROTECTION[] = "overheat_protection"; inline const char S_PASSWORD[] = "password"; +inline const char S_PHY_ADDR[] = "phy_addr"; inline const char S_PID[] = "pid"; inline const char S_PORT[] = "port"; inline const char S_PORTAL[] = "portal"; @@ -228,5 +232,6 @@ inline const char S_USE_DHCP[] = "use_dhcp"; inline const char S_USER[] = "user"; inline const char S_VALUE[] = "value"; inline const char S_VERSION[] = "version"; +inline const char S_W5500[] = "w5500"; inline const char S_WEBSERIAL[] = "web_serial"; inline const char S_WIFI[] = "wifi"; diff --git a/include/types.h b/include/types.h index 7729cf0..4e3eec7 100644 --- a/include/types.h +++ b/include/types.h @@ -21,10 +21,13 @@ enum class PumpStartReason { }; enum class EthernetType : uint8_t { - NONE = 0, - W5500 = 1 + NONE = 0, + W5500 = 1, + LAN8720 = 2 }; +typedef std::function GpioValidator; + struct SettingResult { bool changed = false; bool accepted = false; diff --git a/platformio.ini b/platformio.ini index 9b20d25..eac4307 100644 --- a/platformio.ini +++ b/platformio.ini @@ -173,6 +173,8 @@ board = lolin_s2_mini extra_scripts = ${esp32_defaults.extra_scripts} pre:tools/tinyusb_s2.py custom_sdkconfig = ${esp32_defaults.custom_sdkconfig} + CONFIG_ETH_USE_SPI_ETHERNET=y + CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=y '# CONFIG_BT_ENABLED is not set' CONFIG_TINYUSB_ENABLED=y CONFIG_TINYUSB_CDC_ENABLED=y @@ -188,6 +190,7 @@ custom_sdkconfig = ${esp32_defaults.custom_sdkconfig} '# CONFIG_TINYUSB_VENDOR_ENABLED is not set' '# CONFIG_TINYUSB_NCM_ENABLED is not set' build_flags = ${esp32_defaults.build_flags} + -D USE_ETHERNET=1 ${this.custom_board_flags} custom_board_flags = -D DEFAULT_OT_IN_GPIO=33 -D DEFAULT_OT_OUT_GPIO=35 @@ -251,15 +254,28 @@ custom_board_flags = -D DEFAULT_OT_IN_GPIO=8 custom_sdkconfig = ${esp32_defaults.custom_sdkconfig} CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS=y -[env:nodemcu_32] -custom_title = NodeMCU (ESP32) +[esp32_classic] extends = esp32_defaults -board = nodemcu-32s +custom_board_flags = lib_deps = ${esp32_defaults.lib_deps} ${esp32_defaults.nimble_lib} +custom_sdkconfig = ${esp32_defaults.custom_sdkconfig} + CONFIG_ETH_USE_ESP32_EMAC=y + CONFIG_ETH_USE_SPI_ETHERNET=y + CONFIG_ESP_NETIF_SET_DNS_PER_DEFAULT_NETIF=y + ; Keep assertion checks and omit their messages to fit BLE and Ethernet in OTA. + '# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE is not set' + '# CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE is not set' + CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y build_flags = ${esp32_defaults.build_flags} + -D USE_ETHERNET=1 -D USE_BLE=1 ${this.custom_board_flags} + +[env:nodemcu_32] +custom_title = NodeMCU (ESP32) +extends = esp32_classic +board = nodemcu-32s custom_board_flags = -D DEFAULT_OT_IN_GPIO=16 -D DEFAULT_OT_OUT_GPIO=4 -D DEFAULT_SENSOR_OUTDOOR_GPIO=15 @@ -272,15 +288,23 @@ custom_title = NodeMCU (ESP32, 160 mhz) extends = env:nodemcu_32 board_build.f_cpu = 160000000L ; set frequency to 160MHz +[env:wt32_eth01] +custom_title = WT32-ETH01 (ESP32) +extends = esp32_classic +board = wt32-eth01 +custom_board_flags = -D DEFAULT_OT_IN_GPIO=32 + -D DEFAULT_OT_OUT_GPIO=33 + -D DEFAULT_STATUS_LED_GPIO=15 + -D DEFAULT_ETHERNET_TYPE=EthernetType::LAN8720 + -D DEFAULT_ETH_MDC_GPIO=ETH_PHY_MDC + -D DEFAULT_ETH_MDIO_GPIO=ETH_PHY_MDIO + -D DEFAULT_ETH_POWER_GPIO=ETH_PHY_POWER + -D DEFAULT_ETH_PHY_ADDR=ETH_PHY_ADDR + [env:d1_mini32] custom_title = Wemos D1 mini32 (ESP32) -extends = esp32_defaults +extends = esp32_classic board = wemos_d1_mini32 -lib_deps = ${esp32_defaults.lib_deps} - ${esp32_defaults.nimble_lib} -build_flags = ${esp32_defaults.build_flags} - -D USE_BLE=1 - ${this.custom_board_flags} custom_board_flags = -D DEFAULT_OT_IN_GPIO=21 -D DEFAULT_OT_OUT_GPIO=22 -D DEFAULT_SENSOR_OUTDOOR_GPIO=12 diff --git a/src/config.cpp b/src/config.cpp index e17c73b..42c1284 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -2,6 +2,9 @@ #include +#if USE_ETHERNET + #include +#endif #include #include #include @@ -292,6 +295,37 @@ static bool isSettingInteger(float value, float minValue, float maxValue) { return std::isfinite(value) && value >= minValue && value <= maxValue && std::trunc(value) == value; } +static const GpioValidator isValidOutputGpio = [](uint8_t gpio) { + return GPIO_IS_VALID_OUTPUT(gpio); +}; + +static const GpioValidator isValidInputGpio = [](uint8_t gpio) { + return GPIO_IS_VALID_INPUT(gpio); +}; + +static void parseJsonGpio(JsonVariantConst gpio, uint8_t& target, SettingResult& result, + const GpioValidator& isValid = nullptr) { + + if (gpio.is() && gpio.as().size() == 0) { + result.accepted = true; + if (target != GPIO_IS_NOT_CONFIGURED) { + target = GPIO_IS_NOT_CONFIGURED; + result.changed = true; + } + + } else if (gpio.is()) { + const auto value = gpio.as(); + + if (value == GPIO_IS_NOT_CONFIGURED || (GPIO_IS_VALID(value) && (!isValid || isValid(value)))) { + result.accepted = true; + if (value != target) { + target = value; + result.changed = true; + } + } + } +} + // Network settings static bool isHostnameValid(const char* value) { @@ -353,13 +387,27 @@ JsonVariant toJson(const NetworkSettings& src, JsonVariant dst) { wifi[S_STA][S_CHANNEL] = src.wifi.sta.channel; #if USE_ETHERNET - auto gpio = dst[S_ETHERNET][S_GPIO].to(); - gpio[S_CLK] = src.ethernet.w5500.clk; - gpio[S_MOSI] = src.ethernet.w5500.mosi; - gpio[S_MISO] = src.ethernet.w5500.miso; - gpio[S_CS] = src.ethernet.w5500.cs; - gpio[S_IRQ] = src.ethernet.w5500.irq; - gpio[S_RST] = src.ethernet.w5500.rst; + auto ethernet = dst[S_ETHERNET].to(); + ethernet[S_TYPE] = static_cast(src.ethernet.type); + + #if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500 + auto w5500 = ethernet[S_W5500].to(); + w5500[S_CLK] = src.ethernet.w5500.clk; + w5500[S_MOSI] = src.ethernet.w5500.mosi; + w5500[S_MISO] = src.ethernet.w5500.miso; + w5500[S_CS] = src.ethernet.w5500.cs; + w5500[S_IRQ] = src.ethernet.w5500.irq; + w5500[S_RST] = src.ethernet.w5500.rst; + #endif + + #if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32 + auto lan8720 = ethernet[S_LAN8720].to(); + lan8720[S_MDC] = src.ethernet.lan8720.mdc; + lan8720[S_MDIO] = src.ethernet.lan8720.mdio; + lan8720[S_POWER] = src.ethernet.lan8720.power; + lan8720[S_PHY_ADDR] = src.ethernet.lan8720.phyAddr; + lan8720[S_CLOCK_MODE] = src.ethernet.lan8720.clockMode; + #endif #endif dst[S_TYPE] = static_cast(src.type); @@ -545,47 +593,79 @@ SettingResult fromJson(JsonVariantConst src, NetworkSettings& dst) { } #if USE_ETHERNET - // Ethernet GPIOs use the same strict numeric contract as the other GPIO settings. const auto ethernet = src[S_ETHERNET]; - const auto parseGpio = [&result](JsonVariantConst gpio, uint8_t& target, bool output) { - uint8_t value; - if (gpio.is() && gpio.as().size() == 0) { - value = GPIO_IS_NOT_CONFIGURED; + if (ethernet[S_TYPE].is()) { + const auto value = static_cast(ethernet[S_TYPE].as()); - } else if (gpio.is()) { - value = gpio.as(); + bool valid = value == EthernetType::NONE; + #if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500 + valid |= value == EthernetType::W5500; + #endif - } else { - return; + #if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32 + valid |= value == EthernetType::LAN8720; + #endif + + if (valid) { + result.accepted = true; + if (value != dst.ethernet.type) { + dst.ethernet.type = value; + result.changed = true; + } + } + } + + #if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500 + const auto w5500 = ethernet[S_W5500]; + parseJsonGpio(w5500[S_CLK], dst.ethernet.w5500.clk, result, isValidOutputGpio); + parseJsonGpio(w5500[S_MOSI], dst.ethernet.w5500.mosi, result, isValidOutputGpio); + parseJsonGpio(w5500[S_MISO], dst.ethernet.w5500.miso, result); + parseJsonGpio(w5500[S_CS], dst.ethernet.w5500.cs, result, isValidOutputGpio); + parseJsonGpio(w5500[S_IRQ], dst.ethernet.w5500.irq, result); + parseJsonGpio(w5500[S_RST], dst.ethernet.w5500.rst, result, isValidOutputGpio); + #endif + + #if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32 + const auto lan8720 = ethernet[S_LAN8720]; + parseJsonGpio(lan8720[S_MDC], dst.ethernet.lan8720.mdc, result, isValidOutputGpio); + parseJsonGpio(lan8720[S_MDIO], dst.ethernet.lan8720.mdio, result, isValidOutputGpio); + parseJsonGpio(lan8720[S_POWER], dst.ethernet.lan8720.power, result, isValidOutputGpio); + + if (lan8720[S_PHY_ADDR].is()) { + const auto value = lan8720[S_PHY_ADDR].as(); + + if (value <= 31) { + result.accepted = true; + if (value != dst.ethernet.lan8720.phyAddr) { + dst.ethernet.lan8720.phyAddr = value; + result.changed = true; + } + } } - if (value != GPIO_IS_NOT_CONFIGURED && !(output ? GPIO_IS_VALID_OUTPUT(value) : GPIO_IS_VALID_INPUT(value))) { - return; - } + if (lan8720[S_CLOCK_MODE].is()) { + const auto value = lan8720[S_CLOCK_MODE].as(); - result.accepted = true; - if (value != target) { - target = value; - result.changed = true; + if (value <= 3) { + result.accepted = true; + if (value != dst.ethernet.lan8720.clockMode) { + dst.ethernet.lan8720.clockMode = value; + result.changed = true; + } + } } - }; - - const auto gpio = ethernet[S_GPIO]; - parseGpio(gpio[S_CLK], dst.ethernet.w5500.clk, true); - parseGpio(gpio[S_MOSI], dst.ethernet.w5500.mosi, true); - parseGpio(gpio[S_MISO], dst.ethernet.w5500.miso, false); - parseGpio(gpio[S_CS], dst.ethernet.w5500.cs, true); - parseGpio(gpio[S_IRQ], dst.ethernet.w5500.irq, false); - parseGpio(gpio[S_RST], dst.ethernet.w5500.rst, true); + #endif #endif if (src[S_TYPE].is()) { const auto value = static_cast(src[S_TYPE].as()); - if (value == NetworkMode::STA - #if USE_ETHERNET - || value == NetworkMode::ETH - #endif - ) { + + bool valid = value == NetworkMode::STA; + #if USE_ETHERNET + valid |= value == NetworkMode::ETH; + #endif + + if (valid) { result.accepted = true; if (value != dst.type) { dst.type = value; diff --git a/src/network.cpp b/src/network.cpp index 0c833f8..6306eaa 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -26,6 +26,7 @@ static std::atomic staState{NetworkState::NETWORK_DISABLED}; static std::atomic apState{NetworkState::NETWORK_DISABLED}; #if USE_ETHERNET static std::atomic ethStarted{false}; +static eth_phy_type_t ethType = ETH_PHY_MAX; static std::atomic ethState{NetworkState::NETWORK_DISABLED}; #endif @@ -183,21 +184,48 @@ String getNetworkMac() { return getNetworkInterface().macAddress(); } -bool isEthernetConfigured(const NetworkSettings& config) { +bool isEthernetConfigValid(const NetworkSettings& src) { #if USE_ETHERNET - const auto& gpios = config.ethernet.w5500; + #if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500 + if (src.ethernet.type == EthernetType::W5500) { + const auto& config = src.ethernet.w5500; - return config.ethernet.type == EthernetType::W5500 && - GPIO_IS_VALID_OUTPUT(gpios.clk) && - GPIO_IS_VALID_OUTPUT(gpios.mosi) && - GPIO_IS_VALID_INPUT(gpios.miso) && - GPIO_IS_VALID_OUTPUT(gpios.cs) && - (gpios.irq == GPIO_IS_NOT_CONFIGURED || GPIO_IS_VALID_INPUT(gpios.irq)) && - (gpios.rst == GPIO_IS_NOT_CONFIGURED || GPIO_IS_VALID_OUTPUT(gpios.rst)); + return GPIO_IS_VALID_OUTPUT(config.clk) && + GPIO_IS_VALID_OUTPUT(config.mosi) && + GPIO_IS_VALID_INPUT(config.miso) && + GPIO_IS_VALID_OUTPUT(config.cs) && + (config.irq == GPIO_IS_NOT_CONFIGURED || GPIO_IS_VALID_INPUT(config.irq)) && + (config.rst == GPIO_IS_NOT_CONFIGURED || GPIO_IS_VALID_OUTPUT(config.rst)); + } + #endif + + #if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32 + if (src.ethernet.type == EthernetType::LAN8720) { + const auto& config = src.ethernet.lan8720; + static const uint8_t clockGpios[] = {0, 0, 16, 17}; // eth_clock_mode_t order + + if (config.phyAddr > 31 || config.clockMode >= sizeof(clockGpios)) { + return false; + } + + const auto clockGpio = clockGpios[config.clockMode]; + const auto isControlGpio = [clockGpio](uint8_t gpio) { + // The ESP32 reserves the RMII data lines and the selected reference clock. + return GPIO_IS_VALID_OUTPUT(gpio) && gpio != clockGpio && + gpio != ETH_RMII_TX_EN && gpio != ETH_RMII_TX0 && gpio != ETH_RMII_TX1 && + gpio != ETH_RMII_CRS_DV && gpio != ETH_RMII_RX0 && gpio != ETH_RMII_RX1_EN; + }; + + return isControlGpio(config.mdc) && isControlGpio(config.mdio) && + (config.power == GPIO_IS_NOT_CONFIGURED || isControlGpio(config.power)) && + config.mdc != config.mdio && config.power != config.mdc && config.power != config.mdio; + } + #endif #else - (void)config; - return false; + (void)src; #endif + + return false; } bool isNetworkInterfaceStarted(NetworkMode mode) { @@ -557,13 +585,18 @@ static bool stopEthernet() { return false; } - // end() removes the device, but leaves the SPI bus with its previous GPIOs. - const esp_err_t result = spi_bus_free(SPI2_HOST); - if (result != ESP_OK && result != ESP_ERR_INVALID_STATE) { - ESP_LOGE(L_NETWORK, "Failed release Ethernet SPI bus: %s", esp_err_to_name(result)); - return false; + #if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500 + // SPI bus free + if (ethType == ETH_PHY_W5500) { + const esp_err_t result = spi_bus_free(SPI2_HOST); + if (result != ESP_OK && result != ESP_ERR_INVALID_STATE) { + ESP_LOGE(L_NETWORK, "Failed release Ethernet SPI bus: %s", esp_err_to_name(result)); + return false; + } } + #endif + ethType = ETH_PHY_MAX; postInterfaceStopped(ARDUINO_EVENT_ETH_STOP); return true; } @@ -583,27 +616,47 @@ static bool configureEthernet() { } static bool startEthernet() { - if ((ETH.handle() != nullptr || ETH.netif() != nullptr) && !stopEthernet()) { - return false; + // A previous begin() can leave driver resources allocated. + if (ethType != ETH_PHY_MAX || ETH.handle() != nullptr || ETH.netif() != nullptr) { + // Release them before starting the next driver. + if (!stopEthernet()) { + return false; + } } - const auto& gpios = networkSettings.ethernet.w5500; - ESP_LOGI(L_NETWORK, "Initialize W5500 Ethernet"); - const bool initialized = ETH.begin( - ETH_PHY_W5500, - 1, - gpios.cs, - gpios.irq == GPIO_IS_NOT_CONFIGURED ? -1 : gpios.irq, // optionally - gpios.rst == GPIO_IS_NOT_CONFIGURED ? -1 : gpios.rst, // optionally - SPI2_HOST, - gpios.clk, - gpios.miso, - gpios.mosi, - 12 - ); + bool initialized = false; + const auto type = networkSettings.ethernet.type; + #if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500 + if (type == EthernetType::W5500) { + const auto& config = networkSettings.ethernet.w5500; + ESP_LOGI(L_NETWORK, "Initializing W5500 driver..."); + + ethType = ETH_PHY_W5500; + initialized = ETH.begin( + ETH_PHY_W5500, 1, config.cs, + config.irq == GPIO_IS_NOT_CONFIGURED ? -1 : config.irq, + config.rst == GPIO_IS_NOT_CONFIGURED ? -1 : config.rst, + SPI2_HOST, config.clk, config.miso, config.mosi, 12 + ); + } + #endif + + #if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32 + if (type == EthernetType::LAN8720) { + const auto& config = networkSettings.ethernet.lan8720; + ESP_LOGI(L_NETWORK, "Initializing LAN8720 driver..."); + + ethType = ETH_PHY_LAN8720; + initialized = ETH.begin( + ETH_PHY_LAN8720, config.phyAddr, config.mdc, config.mdio, + config.power == GPIO_IS_NOT_CONFIGURED ? -1 : config.power, + static_cast(config.clockMode) + ); + } + #endif if (!initialized) { - ESP_LOGE(L_NETWORK, "W5500 initialization failed, check GPIOs and wiring"); + ESP_LOGE(L_NETWORK, "Ethernet initialization failed!"); stopEthernet(); return false; } @@ -1120,7 +1173,7 @@ static Task networkTask("Network", [](void*) { return; } - if (networkSettings.type == NetworkMode::ETH && !isEthernetConfigured()) { + if (networkSettings.type == NetworkMode::ETH && !isEthernetConfigValid()) { #if USE_ETHERNET if (ethStarted.load()) { stopEthernet(); diff --git a/src/portal.cpp b/src/portal.cpp index 988219c..360acbe 100644 --- a/src/portal.cpp +++ b/src/portal.cpp @@ -400,11 +400,7 @@ static void restApi() { webServer->on("/api/network/settings", HTTP_GET, [](AsyncWebServerRequest *request) { // Prepare response auto *response = new AsyncJsonResponse(); - auto& doc = response->getRoot(); - toJson(networkSettings, doc); - #if USE_ETHERNET - doc[S_ETHERNET][S_CONFIGURED] = isEthernetConfigured(); - #endif + toJson(networkSettings, response->getRoot()); // Send response response->setLength(); @@ -437,13 +433,9 @@ static void restApi() { } else { // Prepare response auto *response = new AsyncJsonResponse(); - auto& responseDoc = response->getRoot(); - toJson(networkSettings, responseDoc); - #if USE_ETHERNET - responseDoc[S_ETHERNET][S_CONFIGURED] = isEthernetConfigured(); - #endif // Send response + toJson(networkSettings, response->getRoot()); response->setLength(); response->setCode(result.changed ? 201 : 200); request->send(response); diff --git a/src_data/locales/cn.json b/src_data/locales/cn.json index 1b28434..8420c19 100644 --- a/src_data/locales/cn.json +++ b/src_data/locales/cn.json @@ -170,14 +170,18 @@ "ethernet": { "unavailable": "此固件版本不支持以太网。", - "notConfigured": "配置全部六个 GPIO 引脚以使用以太网。", + "controller": "以太网控制器", "clk": "SPI clock GPIO", "mosi": "SPI MOSI GPIO", "miso": "SPI MISO GPIO", "cs": "SPI CS GPIO", "irq": "Interrupt GPIO", "rst": "Reset GPIO", - "note": "W5500 SPI 接线" + "phyAddr": "PHY 地址", + "clockMode": "RMII 时钟", + "mdc": "MDC GPIO", + "mdio": "MDIO GPIO", + "power": "电源/复位 GPIO(可选)" }, "wifi": { "ssid": "SSID", diff --git a/src_data/locales/en.json b/src_data/locales/en.json index 49e9a14..42e0c10 100644 --- a/src_data/locales/en.json +++ b/src_data/locales/en.json @@ -170,14 +170,18 @@ "ethernet": { "unavailable": "This firmware build does not support Ethernet.", - "notConfigured": "Configure all six GPIOs to use Ethernet.", + "controller": "Ethernet controller", "clk": "SPI CLK GPIO", "mosi": "SPI MOSI GPIO", "miso": "SPI MISO GPIO", "cs": "SPI CS GPIO", "irq": "IRQ GPIO", "rst": "RST GPIO", - "note": "W5500 SPI wiring" + "phyAddr": "PHY address", + "clockMode": "RMII clock", + "mdc": "MDC GPIO", + "mdio": "MDIO GPIO", + "power": "Power/reset GPIO (optional)" }, "wifi": { "ssid": "SSID", diff --git a/src_data/locales/it.json b/src_data/locales/it.json index c6e7924..2f4711a 100644 --- a/src_data/locales/it.json +++ b/src_data/locales/it.json @@ -170,14 +170,18 @@ "ethernet": { "unavailable": "Questa versione del firmware non supporta Ethernet.", - "notConfigured": "Configura tutti e sei i GPIO per utilizzare Ethernet.", + "controller": "Controller Ethernet", "clk": "SPI clock GPIO", "mosi": "SPI MOSI GPIO", "miso": "SPI MISO GPIO", "cs": "SPI CS GPIO", "irq": "Interrupt GPIO", "rst": "Reset GPIO", - "note": "Cablaggio SPI del W5500" + "phyAddr": "Indirizzo PHY", + "clockMode": "Clock RMII", + "mdc": "MDC GPIO", + "mdio": "MDIO GPIO", + "power": "GPIO alimentazione/reset (opzionale)" }, "wifi": { "ssid": "SSID", diff --git a/src_data/locales/nl.json b/src_data/locales/nl.json index 005059b..945ed70 100644 --- a/src_data/locales/nl.json +++ b/src_data/locales/nl.json @@ -170,14 +170,18 @@ "ethernet": { "unavailable": "Deze firmwarebuild ondersteunt geen Ethernet.", - "notConfigured": "Configureer alle zes GPIO's om Ethernet te gebruiken.", + "controller": "Ethernetcontroller", "clk": "SPI clock GPIO", "mosi": "SPI MOSI GPIO", "miso": "SPI MISO GPIO", "cs": "SPI CS GPIO", "irq": "Interrupt GPIO", "rst": "Reset GPIO", - "note": "W5500 SPI-bedrading" + "phyAddr": "PHY-adres", + "clockMode": "RMII-klok", + "mdc": "MDC GPIO", + "mdio": "MDIO GPIO", + "power": "Voedings-/reset-GPIO (optioneel)" }, "wifi": { "ssid": "SSID", diff --git a/src_data/locales/ru.json b/src_data/locales/ru.json index 7247a63..ebc3fa5 100644 --- a/src_data/locales/ru.json +++ b/src_data/locales/ru.json @@ -170,14 +170,18 @@ "ethernet": { "unavailable": "Эта сборка прошивки не поддерживает Ethernet.", - "notConfigured": "Для подключения через Ethernet настройте все шесть GPIO.", + "controller": "Контроллер Ethernet", "clk": "SPI clock GPIO", "mosi": "SPI MOSI GPIO", "miso": "SPI MISO GPIO", "cs": "SPI CS GPIO", "irq": "Interrupt GPIO", "rst": "Reset GPIO", - "note": "Подключение W5500 по SPI" + "phyAddr": "Адрес PHY", + "clockMode": "Тактирование RMII", + "mdc": "MDC GPIO", + "mdio": "MDIO GPIO", + "power": "GPIO питания/сброса (необязательно)" }, "wifi": { "ssid": "Имя сети", diff --git a/src_data/pages/network.html b/src_data/pages/network.html index fb5741c..0602aaa 100644 --- a/src_data/pages/network.html +++ b/src_data/pages/network.html @@ -101,37 +101,84 @@
+ +
+ +
+ W5500
+
+
+ +
+ LAN8720 +
+ + + +
+ +
+ + + + +
@@ -418,18 +465,35 @@ const lang = new Lang(document.getElementById('lang')); await lang.build(); + const ethernetType = document.querySelector("[name='ethernet[type]']"); + const fillNetworkSettings = (settings) => { - setSelectValue("[name='type']", settings.type); + setSelectValue("[name='type']", ethernetType.disabled ? 2 : settings.type); setInputValue("[name='hostname']", settings.hostname); setCheckboxValue("[name='use_dhcp']", settings.use_dhcp); for (const field of ['ip', 'gateway', 'subnet', 'dns']) { setInputValue(`[name='static_config[${field}]']`, settings.static_config[field]); } - if (settings.ethernet?.gpio) { - for (const pin of ['clk', 'mosi', 'miso', 'cs', 'irq', 'rst']) { - const value = settings.ethernet.gpio[pin]; - setInputValue(`[name='ethernet[gpio][${pin}]']`, value < 255 ? value : ''); + const ethernet = settings.ethernet; + if (ethernet) { + ethernetType.value = ethernet.type; + + if (ethernet.w5500) { + for (const gpio of ['clk', 'mosi', 'miso', 'cs', 'irq', 'rst']) { + const value = ethernet.w5500[gpio]; + setInputValue(`[name='ethernet[w5500][${gpio}]']`, value < 255 ? value : ''); + } + } + + if (ethernet.lan8720) { + for (const gpio of ['mdc', 'mdio', 'power']) { + const value = ethernet.lan8720[gpio]; + setInputValue(`[name='ethernet[lan8720][${gpio}]']`, value < 255 ? value : ''); + } + + setInputValue("[name='ethernet[lan8720][phy_addr]']", ethernet.lan8720.phy_addr); + setSelectValue("[name='ethernet[lan8720][clock_mode]']", ethernet.lan8720.clock_mode); } } @@ -454,11 +518,29 @@ } const settings = await response.json(); - if (!settings.ethernet) { + const ethernet = settings.ethernet; + if (ethernet?.w5500) { + ethernetType.add(new Option('W5500', '1')); + + } else { + document.getElementById('ethernet-w5500').remove(); + } + + if (ethernet?.lan8720) { + ethernetType.add(new Option('LAN8720', '2')); + + } else { + document.getElementById('ethernet-lan8720').remove(); + } + + // The API includes settings only for Ethernet drivers built into this firmware. + if (!ethernet?.w5500 && !ethernet?.lan8720) { show('.ethernet .unsupported'); - const gpioFields = document.querySelector('.ethernet .supported'); - gpioFields.classList.add('hidden'); - gpioFields.disabled = true; + hide('.ethernet .supported'); + ethernetType.disabled = true; + + // Value 3 in the network mode selector is Ethernet (NetworkMode::ETH). + document.querySelector("[name='type'] [value='3']").disabled = true; } fillNetworkSettings(settings); diff --git a/src_data/scripts/settings-converter.js b/src_data/scripts/settings-converter.js index 7e733f9..b0192b8 100644 --- a/src_data/scripts/settings-converter.js +++ b/src_data/scripts/settings-converter.js @@ -1,4 +1,5 @@ const convertLegacyBackup = (data) => { + // 1.x to 2.0.0: rename legacy settings keys throughout the backup. const aliases = { alwaysSendIndoorTemp: 'always_send_indoor_temp', antiStuckInterval: 'antistuck_interval', @@ -61,8 +62,8 @@ const convertLegacyBackup = (data) => { useDhcp: 'use_dhcp', webSerial: 'web_serial' }; - const logLevels = [0, 1, 1, 2, 3, 3, 4, 5]; + // 1.x to 2.0.0: apply the key aliases recursively, including objects inside arrays. const convert = (value) => { if (Array.isArray(value)) { return value.map(convert); @@ -79,6 +80,8 @@ const convertLegacyBackup = (data) => { return result; }; + // 1.x to 2.0.0: map TinyLogger levels to ESP-IDF levels before renaming logLevel. + const logLevels = [0, 1, 1, 2, 3, 3, 4, 5]; const legacySystem = data?.settings?.system; if (legacySystem !== null && typeof legacySystem === 'object' && Object.prototype.hasOwnProperty.call(legacySystem, 'logLevel')) { @@ -98,8 +101,15 @@ const convertLegacyBackup = (data) => { const result = convert(data); + // 1.x to 2.0.0: settings.system.unit_system became the sole unit setting. + const opentherm = result?.settings?.opentherm; + if (opentherm !== null && typeof opentherm === 'object') { + delete opentherm.unit_system; + } + const network = result?.network; if (network !== null && typeof network === 'object' && !Array.isArray(network)) { + // 1.x to 2.0.0: move network.ap and network.sta under network.wifi. const hasLegacyAp = Object.prototype.hasOwnProperty.call(network, 'ap'); const hasLegacySta = Object.prototype.hasOwnProperty.call(network, 'sta'); if (hasLegacyAp || hasLegacySta) { @@ -117,28 +127,40 @@ const convertLegacyBackup = (data) => { delete network.sta; } + // 1.x to 2.0.0: derive network.type from the settings present in the backup. if (!Object.prototype.hasOwnProperty.call(network, 'type')) { if (typeof network.ethernet?.enabled === 'boolean') { + // Legacy builds with W5500 after the 1.6.0 tag (#286) stored ethernet.enabled. network.type = network.ethernet.enabled ? 3 : 2; } else if (hasLegacyAp || hasLegacySta) { + // Tagged 1.x releases only supported Wi-Fi. network.type = 2; } } - if (network.ethernet !== null && typeof network.ethernet === 'object' && !Array.isArray(network.ethernet)) { - if (Object.prototype.hasOwnProperty.call(network.ethernet, 'pins') - && !Object.prototype.hasOwnProperty.call(network.ethernet, 'gpio')) { - network.ethernet.gpio = network.ethernet.pins; + const ethernet = network.ethernet; + if (ethernet !== null && typeof ethernet === 'object' && !Array.isArray(ethernet)) { + // Post-1.6.0 legacy (#286) to 2.0.0: rename ethernet.pins to ethernet.gpio. + if (Object.prototype.hasOwnProperty.call(ethernet, 'pins') + && !Object.prototype.hasOwnProperty.call(ethernet, 'gpio')) { + ethernet.gpio = ethernet.pins; } - delete network.ethernet.pins; - delete network.ethernet.enabled; - } - } + delete ethernet.pins; + delete ethernet.enabled; - const opentherm = result?.settings?.opentherm; - if (opentherm !== null && typeof opentherm === 'object') { - delete opentherm.unit_system; + // 2.0.0 and 2.0.1 to the per-controller format: ethernet.gpio belonged to W5500. + const gpio = ethernet.gpio; + if (gpio !== null && typeof gpio === 'object' && !Array.isArray(gpio)) { + if (!Object.prototype.hasOwnProperty.call(ethernet, 'w5500')) { + ethernet.w5500 = gpio; + } + if (!Object.prototype.hasOwnProperty.call(ethernet, 'type')) { + ethernet.type = 1; // W5500 + } + } + delete ethernet.gpio; + } } return result;