@@ -278,9 +278,19 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
278
278
cfg.base = &esp_netif_config;
279
279
280
280
_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
+ }
281
291
282
292
/* 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 );
284
294
if (ret != ESP_OK) {
285
295
log_e (" esp_netif_attach failed: %d" , ret);
286
296
return false ;
@@ -705,13 +715,13 @@ bool ETHClass::beginSPI(
705
715
return false ;
706
716
}
707
717
// 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 ) {
710
720
log_e (" esp_eth_new_netif_glue failed" );
711
721
return false ;
712
722
}
713
723
714
- ret = esp_netif_attach (_esp_netif, new_netif_glue );
724
+ ret = esp_netif_attach (_esp_netif, _glue_handle );
715
725
if (ret != ESP_OK) {
716
726
log_e (" esp_netif_attach failed: %d" , ret);
717
727
return false ;
@@ -797,14 +807,11 @@ bool ETHClass::begin(
797
807
);
798
808
}
799
809
800
- void ETHClass::end (void ) {
801
- destroyNetif ();
810
+ static bool empty_ethDetachBus (void *bus_pointer) {
811
+ return true ;
812
+ }
802
813
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 ) {
808
815
809
816
Network.removeEvent (onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
810
817
@@ -813,18 +820,42 @@ void ETHClass::end(void) {
813
820
log_e (" Failed to stop Ethernet" );
814
821
return ;
815
822
}
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
816
835
if (esp_eth_driver_uninstall (_eth_handle) != ESP_OK) {
817
- log_e (" Failed to stop Ethernet" );
836
+ log_e (" Failed to uninstall Ethernet" );
818
837
return ;
819
838
}
820
839
_eth_handle = NULL ;
821
840
}
822
841
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
+
823
850
#if ETH_SPI_SUPPORTS_CUSTOM
824
851
_spi = NULL ;
825
852
#endif
826
-
827
853
#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);
828
859
if (_pin_rmii_clock != -1 && _pin_mcd != -1 && _pin_mdio != -1 ) {
829
860
perimanClearPinBus (_pin_rmii_clock);
830
861
perimanClearPinBus (_pin_mcd);
@@ -847,6 +878,7 @@ void ETHClass::end(void) {
847
878
_pin_power = -1 ;
848
879
}
849
880
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
881
+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_SPI, empty_ethDetachBus);
850
882
if (_pin_cs != -1 ) {
851
883
perimanClearPinBus (_pin_cs);
852
884
_pin_cs = -1 ;
0 commit comments