From 0f9c956c04fedb8bdec62187686489893ae0a57d Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 23 Nov 2022 09:43:43 +1300 Subject: [PATCH] Remove manual handling of ethernet power pin (#4062) --- esphome/components/ethernet/__init__.py | 5 ++--- .../ethernet/ethernet_component.cpp | 22 +++++-------------- .../components/ethernet/ethernet_component.h | 8 ++----- 3 files changed, 9 insertions(+), 26 deletions(-) diff --git a/esphome/components/ethernet/__init__.py b/esphome/components/ethernet/__init__.py index 79ee48f32b..9b7a086d8d 100644 --- a/esphome/components/ethernet/__init__.py +++ b/esphome/components/ethernet/__init__.py @@ -79,7 +79,7 @@ CONFIG_SCHEMA = cv.All( CLK_MODES, upper=True, space="_" ), cv.Optional(CONF_PHY_ADDR, default=0): cv.int_range(min=0, max=31), - cv.Optional(CONF_POWER_PIN): pins.gpio_output_pin_schema, + cv.Optional(CONF_POWER_PIN): pins.internal_gpio_output_pin_number, cv.Optional(CONF_MANUAL_IP): MANUAL_IP_SCHEMA, cv.Optional(CONF_DOMAIN, default=".local"): cv.domain_name, cv.Optional(CONF_USE_ADDRESS): cv.string_strict, @@ -118,8 +118,7 @@ async def to_code(config): cg.add(var.set_use_address(config[CONF_USE_ADDRESS])) if CONF_POWER_PIN in config: - pin = await cg.gpio_pin_expression(config[CONF_POWER_PIN]) - cg.add(var.set_power_pin(pin)) + cg.add(var.set_power_pin(config[CONF_POWER_PIN])) if CONF_MANUAL_IP in config: cg.add(var.set_manual_ip(manual_ip(config[CONF_MANUAL_IP]))) diff --git a/esphome/components/ethernet/ethernet_component.cpp b/esphome/components/ethernet/ethernet_component.cpp index e3baa417df..c125b45ec1 100644 --- a/esphome/components/ethernet/ethernet_component.cpp +++ b/esphome/components/ethernet/ethernet_component.cpp @@ -29,10 +29,6 @@ void EthernetComponent::setup() { // Delay here to allow power to stabilise before Ethernet is initialised. delay(300); // NOLINT - if (this->power_pin_ != nullptr) { - this->power_pin_->setup(); - } - esp_err_t err; err = esp_netif_init(); ESPHL_ERROR_CHECK(err, "ETH netif init error"); @@ -47,6 +43,8 @@ void EthernetComponent::setup() { eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG(); phy_config.phy_addr = this->phy_addr_; + if (this->power_pin_ != -1) + phy_config.reset_gpio_num = this->power_pin_; mac_config.smi_mdc_gpio_num = this->mdc_pin_; mac_config.smi_mdio_gpio_num = this->mdio_pin_; @@ -96,11 +94,6 @@ void EthernetComponent::setup() { /* start Ethernet driver state machine */ err = esp_eth_start(this->eth_handle_); ESPHL_ERROR_CHECK(err, "ETH start error"); - - if (this->power_pin_ != nullptr) { - this->orig_power_control_fun_ = phy->pwrctl; - phy->pwrctl = EthernetComponent::eth_phy_power_control; - } } void EthernetComponent::loop() { @@ -169,7 +162,8 @@ void EthernetComponent::dump_config() { ESP_LOGCONFIG(TAG, "Ethernet:"); this->dump_connect_params_(); - LOG_PIN(" Power Pin: ", this->power_pin_); + if (this->power_pin_ != -1) + ESP_LOGCONFIG(TAG, " Power Pin: %u", this->power_pin_); ESP_LOGCONFIG(TAG, " MDC Pin: %u", this->mdc_pin_); ESP_LOGCONFIG(TAG, " MDIO Pin: %u", this->mdio_pin_); ESP_LOGCONFIG(TAG, " Type: %s", eth_type.c_str()); @@ -277,12 +271,6 @@ void EthernetComponent::start_connect_() { this->connect_begin_ = millis(); this->status_set_warning(); } -esp_err_t EthernetComponent::eth_phy_power_control(esp_eth_phy_t *phy, bool enable) { - global_eth_component->power_pin_->digital_write(enable); - // power up takes some time, datasheet says max 300µs - delay(1); - return global_eth_component->orig_power_control_fun_(phy, enable); -} bool EthernetComponent::is_connected() { return this->state_ == EthernetComponentState::CONNECTED; } @@ -319,7 +307,7 @@ void EthernetComponent::dump_connect_params_() { } void EthernetComponent::set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; } -void EthernetComponent::set_power_pin(GPIOPin *power_pin) { this->power_pin_ = power_pin; } +void EthernetComponent::set_power_pin(int power_pin) { this->power_pin_ = power_pin; } void EthernetComponent::set_mdc_pin(uint8_t mdc_pin) { this->mdc_pin_ = mdc_pin; } void EthernetComponent::set_mdio_pin(uint8_t mdio_pin) { this->mdio_pin_ = mdio_pin; } void EthernetComponent::set_type(EthernetType type) { this->type_ = type; } diff --git a/esphome/components/ethernet/ethernet_component.h b/esphome/components/ethernet/ethernet_component.h index abf8b7791f..409dbd8ca5 100644 --- a/esphome/components/ethernet/ethernet_component.h +++ b/esphome/components/ethernet/ethernet_component.h @@ -45,7 +45,7 @@ class EthernetComponent : public Component { bool is_connected(); void set_phy_addr(uint8_t phy_addr); - void set_power_pin(GPIOPin *power_pin); + void set_power_pin(int power_pin); void set_mdc_pin(uint8_t mdc_pin); void set_mdio_pin(uint8_t mdio_pin); void set_type(EthernetType type); @@ -63,11 +63,9 @@ class EthernetComponent : public Component { void start_connect_(); void dump_connect_params_(); - static esp_err_t eth_phy_power_control(esp_eth_phy_t *phy, bool enable); - std::string use_address_; uint8_t phy_addr_{0}; - GPIOPin *power_pin_{nullptr}; + int power_pin_{-1}; uint8_t mdc_pin_{23}; uint8_t mdio_pin_{18}; EthernetType type_{ETHERNET_TYPE_LAN8720}; @@ -80,8 +78,6 @@ class EthernetComponent : public Component { uint32_t connect_begin_; esp_netif_t *eth_netif_{nullptr}; esp_eth_handle_t eth_handle_; - - std::function orig_power_control_fun_; }; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)