@@ -278,9 +278,19 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
278278 cfg.base = &esp_netif_config;
279279
280280 _esp_netif = esp_netif_new (&cfg);
281+ if (_esp_netif == NULL ) {
282+ log_e (" esp_netif_new failed" );
283+ return false ;
284+ }
285+
286+ _glue_handle = esp_eth_new_netif_glue (_eth_handle);
287+ if (_glue_handle == NULL ) {
288+ log_e (" esp_eth_new_netif_glue failed" );
289+ return false ;
290+ }
281291
282292 /* attach Ethernet driver to TCP/IP stack */
283- ret = esp_netif_attach (_esp_netif, esp_eth_new_netif_glue (_eth_handle) );
293+ ret = esp_netif_attach (_esp_netif, _glue_handle );
284294 if (ret != ESP_OK) {
285295 log_e (" esp_netif_attach failed: %d" , ret);
286296 return false ;
@@ -705,13 +715,13 @@ bool ETHClass::beginSPI(
705715 return false ;
706716 }
707717 // Attach Ethernet driver to TCP/IP stack
708- esp_eth_netif_glue_handle_t new_netif_glue = esp_eth_new_netif_glue (_eth_handle);
709- if (new_netif_glue == NULL ) {
718+ _glue_handle = esp_eth_new_netif_glue (_eth_handle);
719+ if (_glue_handle == NULL ) {
710720 log_e (" esp_eth_new_netif_glue failed" );
711721 return false ;
712722 }
713723
714- ret = esp_netif_attach (_esp_netif, new_netif_glue );
724+ ret = esp_netif_attach (_esp_netif, _glue_handle );
715725 if (ret != ESP_OK) {
716726 log_e (" esp_netif_attach failed: %d" , ret);
717727 return false ;
@@ -797,14 +807,11 @@ bool ETHClass::begin(
797807 );
798808}
799809
800- void ETHClass::end (void ) {
801- destroyNetif ();
810+ static bool empty_ethDetachBus (void *bus_pointer) {
811+ return true ;
812+ }
802813
803- if (_eth_ev_instance != NULL ) {
804- if (esp_event_handler_unregister (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
805- _eth_ev_instance = NULL ;
806- }
807- }
814+ void ETHClass::end (void ) {
808815
809816 Network.removeEvent (onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
810817
@@ -813,18 +820,42 @@ void ETHClass::end(void) {
813820 log_e (" Failed to stop Ethernet" );
814821 return ;
815822 }
823+ // wait for stop
824+ while (getStatusBits () & ESP_NETIF_STARTED_BIT) {
825+ delay (10 );
826+ }
827+ // delete glue first
828+ if (_glue_handle != NULL ) {
829+ if (esp_eth_del_netif_glue (_glue_handle) != ESP_OK) {
830+ log_e (" Failed to del_netif_glue Ethernet" );
831+ return ;
832+ }
833+ }
834+ // uninstall driver
816835 if (esp_eth_driver_uninstall (_eth_handle) != ESP_OK) {
817- log_e (" Failed to stop Ethernet" );
836+ log_e (" Failed to uninstall Ethernet" );
818837 return ;
819838 }
820839 _eth_handle = NULL ;
821840 }
822841
842+ if (_eth_ev_instance != NULL ) {
843+ if (esp_event_handler_unregister (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
844+ _eth_ev_instance = NULL ;
845+ }
846+ }
847+
848+ destroyNetif ();
849+
823850#if ETH_SPI_SUPPORTS_CUSTOM
824851 _spi = NULL ;
825852#endif
826-
827853#if CONFIG_ETH_USE_ESP32_EMAC
854+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_RMII, empty_ethDetachBus);
855+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_CLK, empty_ethDetachBus);
856+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_MCD, empty_ethDetachBus);
857+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_MDIO, empty_ethDetachBus);
858+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_PWR, empty_ethDetachBus);
828859 if (_pin_rmii_clock != -1 && _pin_mcd != -1 && _pin_mdio != -1 ) {
829860 perimanClearPinBus (_pin_rmii_clock);
830861 perimanClearPinBus (_pin_mcd);
@@ -847,6 +878,7 @@ void ETHClass::end(void) {
847878 _pin_power = -1 ;
848879 }
849880#endif /* CONFIG_ETH_USE_ESP32_EMAC */
881+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_SPI, empty_ethDetachBus);
850882 if (_pin_cs != -1 ) {
851883 perimanClearPinBus (_pin_cs);
852884 _pin_cs = -1 ;
0 commit comments