42
42
#include " esp_netif_defaults.h"
43
43
#include " esp_eth_phy.h"
44
44
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 };
46
47
static esp_event_handler_instance_t _eth_ev_instance = NULL ;
47
48
48
49
static void _eth_event_cb (void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
49
50
50
51
if (event_base == ETH_EVENT) {
51
52
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) {
53
54
if (_ethernets[i] != NULL && _ethernets[i]->handle () == eth_handle) {
54
55
_ethernets[i]->_onEthEvent (event_id, event_data);
55
56
}
@@ -60,14 +61,14 @@ static void _eth_event_cb(void *arg, esp_event_base_t event_base, int32_t event_
60
61
// This callback needs to be aware of which interface it should match against
61
62
static void onEthConnected (arduino_event_id_t event, arduino_event_info_t info) {
62
63
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) {
65
66
if (_ethernets[i] != NULL && _ethernets[i]->handle () == info.eth_connected ) {
66
67
index = i;
67
68
break ;
68
69
}
69
70
}
70
- if (index == 3 ) {
71
+ if (index == NUM_SUPPORTED_ETH_PORTS ) {
71
72
log_e (" Could not find ETH interface with that handle!" );
72
73
return ;
73
74
}
@@ -118,7 +119,7 @@ void ETHClass::_onEthEvent(int32_t event_id, void *event_data) {
118
119
}
119
120
120
121
ETHClass::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 )
122
123
#if ETH_SPI_SUPPORTS_CUSTOM
123
124
,
124
125
_spi (NULL )
@@ -534,7 +535,11 @@ bool ETHClass::beginSPI(
534
535
if (_spi != NULL ) {
535
536
pinMode (_pin_cs, OUTPUT);
536
537
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);
538
543
}
539
544
#endif
540
545
@@ -586,8 +591,6 @@ bool ETHClass::beginSPI(
586
591
spi_devcfg.spics_io_num = _pin_cs;
587
592
spi_devcfg.queue_size = 20 ;
588
593
589
- esp_eth_mac_t *mac = NULL ;
590
- esp_eth_phy_t *phy = NULL ;
591
594
#if CONFIG_ETH_SPI_ETHERNET_W5500
592
595
if (type == ETH_PHY_W5500) {
593
596
eth_w5500_config_t mac_config = ETH_W5500_DEFAULT_CONFIG (spi_host, &spi_devcfg);
@@ -606,8 +609,8 @@ bool ETHClass::beginSPI(
606
609
mac_config.custom_spi_driver .write = _eth_spi_write;
607
610
}
608
611
#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);
611
614
} else
612
615
#endif
613
616
#if CONFIG_ETH_SPI_ETHERNET_DM9051
@@ -623,8 +626,8 @@ bool ETHClass::beginSPI(
623
626
mac_config.custom_spi_driver .write = _eth_spi_write;
624
627
}
625
628
#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);
628
631
} else
629
632
#endif
630
633
#if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL
@@ -640,8 +643,8 @@ bool ETHClass::beginSPI(
640
643
mac_config.custom_spi_driver .write = _eth_spi_write;
641
644
}
642
645
#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);
645
648
} else
646
649
#endif
647
650
{
@@ -650,7 +653,7 @@ bool ETHClass::beginSPI(
650
653
}
651
654
652
655
// 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 );
654
657
ret = esp_eth_driver_install (ð_config, &_eth_handle);
655
658
if (ret != ESP_OK) {
656
659
log_e (" SPI Ethernet driver install failed: %d" , ret);
@@ -743,40 +746,46 @@ bool ETHClass::beginSPI(
743
746
#if ETH_SPI_SUPPORTS_CUSTOM
744
747
if (_spi == NULL ) {
745
748
#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 )) {
747
750
goto err;
748
751
}
752
+ perimanSetPinBusExtraType (_pin_cs, " ETH_SPI_CS" );
749
753
#if ETH_SPI_SUPPORTS_CUSTOM
750
754
}
751
755
#endif
752
756
#if ETH_SPI_SUPPORTS_NO_IRQ
753
757
if (_pin_irq != -1 ) {
754
758
#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 )) {
756
760
goto err;
757
761
}
762
+ perimanSetPinBusExtraType (_pin_irq, " ETH_IRQ" );
758
763
#if ETH_SPI_SUPPORTS_NO_IRQ
759
764
}
760
765
#endif
761
766
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 )) {
763
768
goto err;
764
769
}
770
+ perimanSetPinBusExtraType (_pin_sck, " ETH_SPI_SCK" );
765
771
}
766
772
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 )) {
768
774
goto err;
769
775
}
776
+ perimanSetPinBusExtraType (_pin_miso, " ETH_SPI_MISO" );
770
777
}
771
778
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 )) {
773
780
goto err;
774
781
}
782
+ perimanSetPinBusExtraType (_pin_mosi, " ETH_SPI_MOSI" );
775
783
}
776
784
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 )) {
778
786
goto err;
779
787
}
788
+ perimanSetPinBusExtraType (_pin_rst, " ETH_RST" );
780
789
}
781
790
782
791
Network.onSysEvent (onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
@@ -840,11 +849,33 @@ void ETHClass::end(void) {
840
849
return ;
841
850
}
842
851
_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
+ }
843
862
}
844
863
845
864
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
+ }
848
879
}
849
880
}
850
881
0 commit comments