@@ -118,7 +118,7 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
118
118
}
119
119
120
120
ETHClass::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 )
122
122
#if ETH_SPI_SUPPORTS_CUSTOM
123
123
,
124
124
_spi (NULL )
@@ -274,9 +274,19 @@ bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, i
274
274
cfg.base = &esp_netif_config;
275
275
276
276
_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
+ }
277
287
278
288
/* 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 );
280
290
if (ret != ESP_OK) {
281
291
log_e (" esp_netif_attach failed: %d" , ret);
282
292
return false ;
@@ -702,13 +712,13 @@ bool ETHClass::beginSPI(
702
712
return false ;
703
713
}
704
714
// 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 ) {
707
717
log_e (" esp_eth_new_netif_glue failed" );
708
718
return false ;
709
719
}
710
720
711
- ret = esp_netif_attach (_esp_netif, new_netif_glue );
721
+ ret = esp_netif_attach (_esp_netif, _glue_handle );
712
722
if (ret != ESP_OK) {
713
723
log_e (" esp_netif_attach failed: %d" , ret);
714
724
return false ;
@@ -799,14 +809,11 @@ bool ETHClass::begin(
799
809
);
800
810
}
801
811
802
- void ETHClass::end (void ) {
803
- destroyNetif ();
812
+ static bool empty_ethDetachBus (void *bus_pointer) {
813
+ return true ;
814
+ }
804
815
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 ) {
810
817
811
818
Network.removeEvent (onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
812
819
@@ -815,18 +822,43 @@ void ETHClass::end(void) {
815
822
log_e (" Failed to stop Ethernet" );
816
823
return ;
817
824
}
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
818
838
if (esp_eth_driver_uninstall (_eth_handle) != ESP_OK) {
819
- log_e (" Failed to stop Ethernet" );
839
+ log_e (" Failed to uninstall Ethernet" );
820
840
return ;
821
841
}
822
842
_eth_handle = NULL ;
823
843
}
824
844
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
+
825
853
#if ETH_SPI_SUPPORTS_CUSTOM
826
854
_spi = NULL ;
827
855
#endif
828
-
829
856
#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);
830
862
if (_pin_rmii_clock != -1 && _pin_mcd != -1 && _pin_mdio != -1 ) {
831
863
perimanClearPinBus (_pin_rmii_clock);
832
864
perimanClearPinBus (_pin_mcd);
@@ -849,6 +881,7 @@ void ETHClass::end(void) {
849
881
_pin_power = -1 ;
850
882
}
851
883
#endif /* CONFIG_ETH_USE_ESP32_EMAC */
884
+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET_SPI, empty_ethDetachBus);
852
885
if (_pin_cs != -1 ) {
853
886
perimanClearPinBus (_pin_cs);
854
887
_pin_cs = -1 ;
0 commit comments