diff --git a/libraries/Ethernet/src/ETH.cpp b/libraries/Ethernet/src/ETH.cpp index 59dd102cced..e1223ea446f 100644 --- a/libraries/Ethernet/src/ETH.cpp +++ b/libraries/Ethernet/src/ETH.cpp @@ -305,6 +305,7 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i } if (_phy == NULL) { log_e("esp_eth_phy_new failed"); + _delMacAndPhy(); return false; } @@ -738,15 +739,29 @@ bool ETHClass::beginSPI( return false; } + if (_mac == NULL) { + log_e("esp_eth_mac_new failed"); + _delMacAndPhy(); + return false; + } + + if (_phy == NULL) { + log_e("esp_eth_phy_new failed"); + _delMacAndPhy(); + return false; + } + // Init Ethernet driver to default and install it esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(_mac, _phy); ret = esp_eth_driver_install(ð_config, &_eth_handle); if (ret != ESP_OK) { log_e("SPI Ethernet driver install failed: %d", ret); + _delMacAndPhy(); return false; } if (_eth_handle == NULL) { log_e("esp_eth_driver_install failed! eth_handle is NULL"); + _delMacAndPhy(); return false; } @@ -923,6 +938,18 @@ static bool empty_ethDetachBus(void *bus_pointer) { return true; } +void ETHClass::_delMacAndPhy() { + if (_mac != NULL) { + _mac->del(_mac); + _mac = NULL; + } + + if (_phy != NULL) { + _phy->del(_phy); + _phy = NULL; + } +} + void ETHClass::end(void) { Network.removeEvent(_eth_connected_event_handle); @@ -954,18 +981,10 @@ void ETHClass::end(void) { return; } _eth_handle = NULL; - //delete mac - if (_mac != NULL) { - _mac->del(_mac); - _mac = NULL; - } - //delete phy - if (_phy != NULL) { - _phy->del(_phy); - _phy = NULL; - } } + _delMacAndPhy(); + if (_eth_ev_instance != NULL) { bool do_not_unreg_ev_handler = false; for (int i = 0; i < NUM_SUPPORTED_ETH_PORTS; ++i) { diff --git a/libraries/Ethernet/src/ETH.h b/libraries/Ethernet/src/ETH.h index f609c651102..ceeaf789848 100644 --- a/libraries/Ethernet/src/ETH.h +++ b/libraries/Ethernet/src/ETH.h @@ -271,6 +271,8 @@ class ETHClass : public NetworkInterface { bool _setLinkSpeed(uint16_t speed); bool _setAutoNegotiation(bool on); + void _delMacAndPhy(); + friend class EthernetClass; // to access beginSPI };