4242#include  " esp_netif_defaults.h" 
4343#include  " esp_eth_phy.h" 
4444
45- static  ETHClass *_ethernets[3 ] = {NULL , NULL , NULL };
45+ #define  NUM_SUPPORTED_ETH_PORTS  3 
46+ static  ETHClass *_ethernets[NUM_SUPPORTED_ETH_PORTS] = {NULL , NULL , NULL };
4647static  esp_event_handler_instance_t  _eth_ev_instance = NULL ;
4748
4849static  void  _eth_event_cb (void  *arg, esp_event_base_t  event_base, int32_t  event_id, void  *event_data) {
4950
5051 if  (event_base == ETH_EVENT) {
5152 esp_eth_handle_t  eth_handle = *((esp_eth_handle_t  *)event_data);
52-  for  (int  i = 0 ; i < 3 ; ++i) {
53+  for  (int  i = 0 ; i < NUM_SUPPORTED_ETH_PORTS ; ++i) {
5354 if  (_ethernets[i] != NULL  && _ethernets[i]->handle () == eth_handle) {
5455 _ethernets[i]->_onEthEvent (event_id, event_data);
5556 }
@@ -60,14 +61,14 @@ static void _eth_event_cb(void *arg, esp_event_base_t event_base, int32_t event_
6061//  This callback needs to be aware of which interface it should match against
6162static  void  onEthConnected (arduino_event_id_t  event, arduino_event_info_t  info) {
6263 if  (event == ARDUINO_EVENT_ETH_CONNECTED) {
63-  uint8_t  index = 3 ;
64-  for  (int  i = 0 ; i < 3 ; ++i) {
64+  uint8_t  index = NUM_SUPPORTED_ETH_PORTS ;
65+  for  (int  i = 0 ; i < NUM_SUPPORTED_ETH_PORTS ; ++i) {
6566 if  (_ethernets[i] != NULL  && _ethernets[i]->handle () == info.eth_connected ) {
6667 index = i;
6768 break ;
6869 }
6970 }
70-  if  (index == 3 ) {
71+  if  (index == NUM_SUPPORTED_ETH_PORTS ) {
7172 log_e (" Could not find ETH interface with that handle!"  );
7273 return ;
7374 }
@@ -118,7 +119,7 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
118119}
119120
120121ETHClass::ETHClass (uint8_t  eth_index)
121-  : _eth_handle(NULL ), _eth_index(eth_index), _phy_type(ETH_PHY_MAX), _glue_handle(NULL )
122+  : _eth_handle(NULL ), _eth_index(eth_index), _phy_type(ETH_PHY_MAX), _glue_handle(NULL ), _mac( NULL ), _phy( NULL ) 
122123#if  ETH_SPI_SUPPORTS_CUSTOM
123124 ,
124125 _spi (NULL )
@@ -534,7 +535,11 @@ bool ETHClass::beginSPI(
534535 if  (_spi != NULL ) {
535536 pinMode (_pin_cs, OUTPUT);
536537 digitalWrite (_pin_cs, HIGH);
537-  perimanSetPinBusExtraType (_pin_cs, " ETH_CS"  );
538+  char  cs_num_str[3 ];
539+  itoa (_eth_index, cs_num_str, 10 );
540+  strcat (strcpy (_cs_str, " ETH_CS["  ), cs_num_str);
541+  strcat (_cs_str, " ]"  );
542+  perimanSetPinBusExtraType (_pin_cs, _cs_str);
538543 }
539544#endif 
540545
@@ -586,8 +591,6 @@ bool ETHClass::beginSPI(
586591 spi_devcfg.spics_io_num  = _pin_cs;
587592 spi_devcfg.queue_size  = 20 ;
588593
589-  esp_eth_mac_t  *mac = NULL ;
590-  esp_eth_phy_t  *phy = NULL ;
591594#if  CONFIG_ETH_SPI_ETHERNET_W5500
592595 if  (type == ETH_PHY_W5500) {
593596 eth_w5500_config_t  mac_config = ETH_W5500_DEFAULT_CONFIG (spi_host, &spi_devcfg);
@@ -606,8 +609,8 @@ bool ETHClass::beginSPI(
606609 mac_config.custom_spi_driver .write  = _eth_spi_write;
607610 }
608611#endif 
609-  mac  = esp_eth_mac_new_w5500 (&mac_config, ð_mac_config);
610-  phy  = esp_eth_phy_new_w5500 (&phy_config);
612+  _mac  = esp_eth_mac_new_w5500 (&mac_config, ð_mac_config);
613+  _phy  = esp_eth_phy_new_w5500 (&phy_config);
611614 } else 
612615#endif 
613616#if  CONFIG_ETH_SPI_ETHERNET_DM9051
@@ -623,8 +626,8 @@ bool ETHClass::beginSPI(
623626 mac_config.custom_spi_driver .write  = _eth_spi_write;
624627 }
625628#endif 
626-  mac  = esp_eth_mac_new_dm9051 (&mac_config, ð_mac_config);
627-  phy  = esp_eth_phy_new_dm9051 (&phy_config);
629+  _mac  = esp_eth_mac_new_dm9051 (&mac_config, ð_mac_config);
630+  _phy  = esp_eth_phy_new_dm9051 (&phy_config);
628631 } else 
629632#endif 
630633#if  CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
@@ -640,8 +643,8 @@ bool ETHClass::beginSPI(
640643 mac_config.custom_spi_driver .write  = _eth_spi_write;
641644 }
642645#endif 
643-  mac  = esp_eth_mac_new_ksz8851snl (&mac_config, ð_mac_config);
644-  phy  = esp_eth_phy_new_ksz8851snl (&phy_config);
646+  _mac  = esp_eth_mac_new_ksz8851snl (&mac_config, ð_mac_config);
647+  _phy  = esp_eth_phy_new_ksz8851snl (&phy_config);
645648 } else 
646649#endif 
647650 {
@@ -650,7 +653,7 @@ bool ETHClass::beginSPI(
650653 }
651654
652655 //  Init Ethernet driver to default and install it
653-  esp_eth_config_t  eth_config = ETH_DEFAULT_CONFIG (mac, phy );
656+  esp_eth_config_t  eth_config = ETH_DEFAULT_CONFIG (_mac, _phy );
654657 ret = esp_eth_driver_install (ð_config, &_eth_handle);
655658 if  (ret != ESP_OK) {
656659 log_e (" SPI Ethernet driver install failed: %d"  , ret);
@@ -743,40 +746,46 @@ bool ETHClass::beginSPI(
743746#if  ETH_SPI_SUPPORTS_CUSTOM
744747 if  (_spi == NULL ) {
745748#endif
746-  if  (!perimanSetPinBus (_pin_cs, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), - 1 , -1 )) {
749+  if  (!perimanSetPinBus (_pin_cs, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), _eth_index , -1 )) {
747750 goto  err;
748751 }
752+  perimanSetPinBusExtraType (_pin_cs, " ETH_SPI_CS"  );
749753#if  ETH_SPI_SUPPORTS_CUSTOM
750754 }
751755#endif 
752756#if  ETH_SPI_SUPPORTS_NO_IRQ
753757 if  (_pin_irq != -1 ) {
754758#endif
755-  if  (!perimanSetPinBus (_pin_irq, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), - 1 , -1 )) {
759+  if  (!perimanSetPinBus (_pin_irq, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), _eth_index , -1 )) {
756760 goto  err;
757761 }
762+  perimanSetPinBusExtraType (_pin_irq, " ETH_IRQ"  );
758763#if  ETH_SPI_SUPPORTS_NO_IRQ
759764 }
760765#endif 
761766 if  (_pin_sck != -1 ) {
762-  if  (!perimanSetPinBus (_pin_sck, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), - 1 , -1 )) {
767+  if  (!perimanSetPinBus (_pin_sck, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), _eth_index , -1 )) {
763768 goto  err;
764769 }
770+  perimanSetPinBusExtraType (_pin_sck, " ETH_SPI_SCK"  );
765771 }
766772 if  (_pin_miso != -1 ) {
767-  if  (!perimanSetPinBus (_pin_miso, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), - 1 , -1 )) {
773+  if  (!perimanSetPinBus (_pin_miso, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), _eth_index , -1 )) {
768774 goto  err;
769775 }
776+  perimanSetPinBusExtraType (_pin_miso, " ETH_SPI_MISO"  );
770777 }
771778 if  (_pin_mosi != -1 ) {
772-  if  (!perimanSetPinBus (_pin_mosi, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), - 1 , -1 )) {
779+  if  (!perimanSetPinBus (_pin_mosi, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), _eth_index , -1 )) {
773780 goto  err;
774781 }
782+  perimanSetPinBusExtraType (_pin_mosi, " ETH_SPI_MOSI"  );
775783 }
776784 if  (_pin_rst != -1 ) {
777-  if  (!perimanSetPinBus (_pin_rst, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), - 1 , -1 )) {
785+  if  (!perimanSetPinBus (_pin_rst, ESP32_BUS_TYPE_ETHERNET_SPI, (void  *)(this ), _eth_index , -1 )) {
778786 goto  err;
779787 }
788+  perimanSetPinBusExtraType (_pin_rst, " ETH_RST"  );
780789 }
781790
782791 Network.onSysEvent (onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
@@ -840,11 +849,33 @@ void ETHClass::end(void) {
840849 return ;
841850 }
842851 _eth_handle = NULL ;
852+  // delete mac
853+  if  (_mac != NULL ) {
854+  _mac->del (_mac);
855+  _mac = NULL ;
856+  }
857+  // delete phy
858+  if  (_phy != NULL ) {
859+  _phy->del (_phy);
860+  _phy = NULL ;
861+  }
843862 }
844863
845864 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 ;
865+  bool  do_not_unreg_ev_handler = false ;
866+  for  (int  i = 0 ; i < NUM_SUPPORTED_ETH_PORTS; ++i) {
867+  if  (_ethernets[i] != NULL  && _ethernets[i]->netif () != NULL  && _ethernets[i]->netif () != _esp_netif) {
868+  do_not_unreg_ev_handler = true ;
869+  break ;
870+  }
871+  }
872+  if  (!do_not_unreg_ev_handler) {
873+  if  (esp_event_handler_unregister (ETH_EVENT, ESP_EVENT_ANY_ID, &_eth_event_cb) == ESP_OK) {
874+  _eth_ev_instance = NULL ;
875+  log_v (" Unregistered event handler"  );
876+  } else  {
877+  log_e (" Failed to unregister event handler"  );
878+  }
848879 }
849880 }
850881
0 commit comments