2323#if ESP_IDF_VERSION_MAJOR > 3
2424 #include " esp_event.h"
2525 #include " esp_eth.h"
26- #include " esp_eth_phy.h"
2726 #include " esp_eth_mac.h"
2827 #include " esp_eth_com.h"
2928#if CONFIG_IDF_TARGET_ESP32
3231 // #include "soc/io_mux_reg.h"
3332 // #include "hal/gpio_hal.h"
3433#endif
34+ #include " esp32-hal-periman.h"
3535#else
3636 #include " eth_phy/phy.h"
3737 #include " eth_phy/phy_tlk110.h"
@@ -225,19 +225,28 @@ ETHClass::ETHClass()
225225#if ESP_IDF_VERSION_MAJOR > 3
226226 ,eth_handle(NULL )
227227#endif
228- ,started (false )
228+ ,_started (false )
229229{
230230}
231231
232232ETHClass::~ETHClass ()
233233{}
234234
235+ bool ETHClass::ethDetachBus (void * bus_pointer){
236+ ETHClass *bus = (ETHClass *) bus_pointer;
237+ if (bus->_started ) {
238+ bus->end ();
239+ }
240+ return true ;
241+ }
242+
235243bool ETHClass::begin (uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type, eth_clock_mode_t clock_mode, bool use_mac_from_efuse)
236244{
237245#if ESP_IDF_VERSION_MAJOR > 3
238246 if (esp_netif != NULL ){
239247 return true ;
240248 }
249+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET, ETHClass::ethDetachBus);
241250
242251 eth_clock_mode = clock_mode;
243252 tcpipInit ();
@@ -267,10 +276,25 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
267276 mac_config.smi_mdc_gpio_num = mdc;
268277 mac_config.smi_mdio_gpio_num = mdio;
269278
279+ _pin_mcd = mdc;
280+ _pin_mdio = mdio;
281+ _pin_rmii_clock = mac_config.clock_config .rmii .clock_gpio ;
282+
283+ if (!perimanSetPinBus (_pin_rmii_clock, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
284+ if (!perimanSetPinBus (_pin_mcd, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
285+ if (!perimanSetPinBus (_pin_mdio, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
286+
270287 eth_mac_config_t eth_mac_config = ETH_MAC_DEFAULT_CONFIG ();
271288 eth_mac_config.sw_reset_timeout_ms = 1000 ;
272289
273290 eth_mac = esp_eth_mac_new_esp32 (&mac_config, ð_mac_config);
291+
292+ if (!perimanSetPinBus (ETH_RMII_TX_EN, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
293+ if (!perimanSetPinBus (ETH_RMII_TX0, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
294+ if (!perimanSetPinBus (ETH_RMII_TX1, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
295+ if (!perimanSetPinBus (ETH_RMII_RX0, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
296+ if (!perimanSetPinBus (ETH_RMII_RX1_EN, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
297+ if (!perimanSetPinBus (ETH_RMII_CRS_DV, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
274298#endif
275299#if CONFIG_ETH_SPI_ETHERNET_DM9051
276300 }
@@ -281,9 +305,15 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
281305 return false ;
282306 }
283307
308+ _pin_power = power;
309+ if (_pin_power != -1 ){
310+ if (!perimanSetPinBus (_pin_power, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
311+ }
312+
284313 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG ();
285314 phy_config.phy_addr = phy_addr;
286315 phy_config.reset_gpio_num = power;
316+
287317 esp_eth_phy_t *eth_phy = NULL ;
288318 switch (type){
289319 case ETH_PHY_LAN8720:
@@ -347,6 +377,22 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
347377 log_e (" esp_eth_start failed" );
348378 return false ;
349379 }
380+ _started = true ;
381+
382+ if (!perimanSetPinBus (_pin_rmii_clock, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
383+ if (!perimanSetPinBus (_pin_mcd, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
384+ if (!perimanSetPinBus (_pin_mdio, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
385+
386+ if (!perimanSetPinBus (ETH_RMII_TX_EN, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
387+ if (!perimanSetPinBus (ETH_RMII_TX0, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
388+ if (!perimanSetPinBus (ETH_RMII_TX1, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
389+ if (!perimanSetPinBus (ETH_RMII_RX0, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
390+ if (!perimanSetPinBus (ETH_RMII_RX1_EN, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
391+ if (!perimanSetPinBus (ETH_RMII_CRS_DV, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
392+
393+ if (_pin_power != -1 ){
394+ if (!perimanSetPinBus (_pin_power, ESP32_BUS_TYPE_ETHERNET, (void *)(this ))){ goto err; }
395+ }
350396#else
351397 esp_err_t err;
352398 if (initialized){
@@ -355,7 +401,7 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
355401 log_e (" esp_eth_enable error: %d" , err);
356402 return false ;
357403 }
358- started = true ;
404+ _started = true ;
359405 return true ;
360406 }
361407 _eth_phy_mdc_pin = mdc;
@@ -401,7 +447,7 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
401447 if (err){
402448 log_e (" esp_eth_enable error: %d" , err);
403449 } else {
404- started = true ;
450+ _started = true ;
405451 return true ;
406452 }
407453 } else {
@@ -413,6 +459,11 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
413459 delay (50 );
414460
415461 return true ;
462+
463+ err:
464+ log_e (" Failed to set all pins bus to ETHERNET" );
465+ ETHClass::ethDetachBus ((void *)(this ));
466+ return false ;
416467}
417468
418469bool ETHClass::config (IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
@@ -615,4 +666,36 @@ String ETHClass::macAddress(void)
615666 return String (macStr);
616667}
617668
669+ #if ESP_IDF_VERSION_MAJOR > 3
670+
671+ void ETHClass::end (void )
672+ {
673+ if (esp_eth_stop (eth_handle) != ESP_OK) {
674+ log_e (" Failed to stop Ehternet" );
675+ return ;
676+ }
677+ if (esp_eth_driver_uninstall (eth_handle) != ESP_OK) {
678+ log_e (" Failed to stop Ethernet" );
679+ return ;
680+ }
681+ _started = false ;
682+
683+ perimanSetPinBus (_pin_rmii_clock, ESP32_BUS_TYPE_INIT, NULL );
684+ perimanSetPinBus (_pin_mcd, ESP32_BUS_TYPE_INIT, NULL );
685+ perimanSetPinBus (_pin_mdio, ESP32_BUS_TYPE_INIT, NULL );
686+
687+ perimanSetPinBus (ETH_RMII_TX_EN, ESP32_BUS_TYPE_INIT, NULL );
688+ perimanSetPinBus (ETH_RMII_TX0, ESP32_BUS_TYPE_INIT, NULL );
689+ perimanSetPinBus (ETH_RMII_TX1, ESP32_BUS_TYPE_INIT, NULL );
690+ perimanSetPinBus (ETH_RMII_RX0, ESP32_BUS_TYPE_INIT, NULL );
691+ perimanSetPinBus (ETH_RMII_RX1_EN, ESP32_BUS_TYPE_INIT, NULL );
692+ perimanSetPinBus (ETH_RMII_CRS_DV, ESP32_BUS_TYPE_INIT, NULL );
693+
694+ if (_pin_power != -1 ){
695+ perimanSetPinBus (_pin_power, ESP32_BUS_TYPE_INIT, NULL );
696+ }
697+ }
698+
699+ #endif
700+
618701ETHClass ETH;
0 commit comments