@@ -118,7 +118,7 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
118118}
119119
120120ETHClass::ETHClass (uint8_t eth_index)
121- : _eth_handle(NULL ), _eth_index(eth_index), _phy_type(ETH_PHY_MAX)
121+ : _eth_handle(NULL ), _eth_index(eth_index), _phy_type(ETH_PHY_MAX), _glue_handle( NULL )
122122#if ETH_SPI_SUPPORTS_CUSTOM
123123 ,
124124 _spi (NULL )
@@ -274,9 +274,19 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
274274 cfg.base = &esp_netif_config;
275275
276276 _esp_netif = esp_netif_new (&cfg);
277+ if (_esp_netif == NULL ) {
278+ log_e (" esp_netif_new failed" );
279+ return false ;
280+ }
281+
282+ _glue_handle = esp_eth_new_netif_glue (_eth_handle);
283+ if (_glue_handle == NULL ) {
284+ log_e (" esp_eth_new_netif_glue failed" );
285+ return false ;
286+ }
277287
278288 /* attach Ethernet driver to TCP/IP stack */
279- ret = esp_netif_attach (_esp_netif, esp_eth_new_netif_glue (_eth_handle) );
289+ ret = esp_netif_attach (_esp_netif, _glue_handle );
280290 if (ret != ESP_OK) {
281291 log_e (" esp_netif_attach failed: %d" , ret);
282292 return false ;
@@ -702,13 +712,13 @@ bool ETHClass::beginSPI(
702712 return false ;
703713 }
704714 // Attach Ethernet driver to TCP/IP stack
705- esp_eth_netif_glue_handle_t new_netif_glue = esp_eth_new_netif_glue (_eth_handle);
706- if (new_netif_glue == NULL ) {
715+ _glue_handle = esp_eth_new_netif_glue (_eth_handle);
716+ if (_glue_handle == NULL ) {
707717 log_e (" esp_eth_new_netif_glue failed" );
708718 return false ;
709719 }
710720
711- ret = esp_netif_attach (_esp_netif, new_netif_glue );
721+ ret = esp_netif_attach (_esp_netif, _glue_handle );
712722 if (ret != ESP_OK) {
713723 log_e (" esp_netif_attach failed: %d" , ret);
714724 return false ;
@@ -799,14 +809,11 @@ bool ETHClass::begin(
799809 );
800810}
801811
802- void ETHClass::end (void ) {
803- destroyNetif ();
812+ static bool empty_ethDetachBus (void *bus_pointer) {
813+ return true ;
814+ }
804815
805- if (_eth_ev_instance != NULL ) {
806- if (esp_event_handler_unregister (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
807- _eth_ev_instance = NULL ;
808- }
809- }
816+ void ETHClass::end (void ) {
810817
811818 Network.removeEvent (onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
812819
@@ -815,18 +822,43 @@ void ETHClass::end(void) {
815822 log_e (" Failed to stop Ethernet" );
816823 return ;
817824 }
825+ // wait for stop
826+ while (getStatusBits () & ESP_NETIF_STARTED_BIT) {
827+ delay (10 );
828+ }
829+ // delete glue first
830+ if (_glue_handle != NULL ) {
831+ if (esp_eth_del_netif_glue (_glue_handle) != ESP_OK) {
832+ log_e (" Failed to del_netif_glue Ethernet" );
833+ return ;
834+ }
835+ _glue_handle = NULL ;
836+ }
837+ // uninstall driver
818838 if (esp_eth_driver_uninstall (_eth_handle) != ESP_OK) {
819- log_e (" Failed to stop Ethernet" );
839+ log_e (" Failed to uninstall Ethernet" );
820840 return ;
821841 }
822842 _eth_handle = NULL ;
823843 }
824844
845+ if (_eth_ev_instance != NULL ) {
846+ if (esp_event_handler_unregister (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
847+ _eth_ev_instance = NULL ;
848+ }
849+ }
850+
851+ destroyNetif ();
852+
825853#if ETH_SPI_SUPPORTS_CUSTOM
826854 _spi = NULL ;
827855#endif
828-
829856#if CONFIG_ETH_USE_ESP32_EMAC
857+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_RMII, empty_ethDetachBus);
858+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_CLK, empty_ethDetachBus);
859+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_MCD, empty_ethDetachBus);
860+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_MDIO, empty_ethDetachBus);
861+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_PWR, empty_ethDetachBus);
830862 if (_pin_rmii_clock != -1 && _pin_mcd != -1 && _pin_mdio != -1 ) {
831863 perimanClearPinBus (_pin_rmii_clock);
832864 perimanClearPinBus (_pin_mcd);
@@ -849,6 +881,7 @@ void ETHClass::end(void) {
849881 _pin_power = -1 ;
850882 }
851883#endif /* CONFIG_ETH_USE_ESP32_EMAC */
884+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_SPI, empty_ethDetachBus);
852885 if (_pin_cs != -1 ) {
853886 perimanClearPinBus (_pin_cs);
854887 _pin_cs = -1 ;
0 commit comments