Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions libraries/Ethernet/src/ETH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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(&eth_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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/Ethernet/src/ETH.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
Loading