23
23
#if ESP_IDF_VERSION_MAJOR > 3
24
24
#include " esp_event.h"
25
25
#include " esp_eth.h"
26
- #include " esp_eth_phy.h"
27
26
#include " esp_eth_mac.h"
28
27
#include " esp_eth_com.h"
29
28
#if CONFIG_IDF_TARGET_ESP32
32
31
// #include "soc/io_mux_reg.h"
33
32
// #include "hal/gpio_hal.h"
34
33
#endif
34
+ #include " esp32-hal-periman.h"
35
35
#else
36
36
#include " eth_phy/phy.h"
37
37
#include " eth_phy/phy_tlk110.h"
@@ -225,19 +225,28 @@ ETHClass::ETHClass()
225
225
#if ESP_IDF_VERSION_MAJOR > 3
226
226
,eth_handle(NULL )
227
227
#endif
228
- ,started (false )
228
+ ,_started (false )
229
229
{
230
230
}
231
231
232
232
ETHClass::~ETHClass ()
233
233
{}
234
234
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
+
235
243
bool 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)
236
244
{
237
245
#if ESP_IDF_VERSION_MAJOR > 3
238
246
if (esp_netif != NULL ){
239
247
return true ;
240
248
}
249
+ perimanSetBusDeinit (ESP32_BUS_TYPE_ETHERNET, ETHClass::ethDetachBus);
241
250
242
251
eth_clock_mode = clock_mode;
243
252
tcpipInit ();
@@ -267,10 +276,25 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
267
276
mac_config.smi_mdc_gpio_num = mdc;
268
277
mac_config.smi_mdio_gpio_num = mdio;
269
278
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
+
270
287
eth_mac_config_t eth_mac_config = ETH_MAC_DEFAULT_CONFIG ();
271
288
eth_mac_config.sw_reset_timeout_ms = 1000 ;
272
289
273
290
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 ; }
274
298
#endif
275
299
#if CONFIG_ETH_SPI_ETHERNET_DM9051
276
300
}
@@ -281,9 +305,15 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
281
305
return false ;
282
306
}
283
307
308
+ _pin_power = power;
309
+ if (_pin_power != -1 ){
310
+ if (!perimanSetPinBus (_pin_power, ESP32_BUS_TYPE_INIT, NULL )){ return false ; }
311
+ }
312
+
284
313
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG ();
285
314
phy_config.phy_addr = phy_addr;
286
315
phy_config.reset_gpio_num = power;
316
+
287
317
esp_eth_phy_t *eth_phy = NULL ;
288
318
switch (type){
289
319
case ETH_PHY_LAN8720:
@@ -347,6 +377,22 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
347
377
log_e (" esp_eth_start failed" );
348
378
return false ;
349
379
}
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
+ }
350
396
#else
351
397
esp_err_t err;
352
398
if (initialized){
@@ -355,7 +401,7 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
355
401
log_e (" esp_eth_enable error: %d" , err);
356
402
return false ;
357
403
}
358
- started = true ;
404
+ _started = true ;
359
405
return true ;
360
406
}
361
407
_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
401
447
if (err){
402
448
log_e (" esp_eth_enable error: %d" , err);
403
449
} else {
404
- started = true ;
450
+ _started = true ;
405
451
return true ;
406
452
}
407
453
} else {
@@ -413,6 +459,11 @@ bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_typ
413
459
delay (50 );
414
460
415
461
return true ;
462
+
463
+ err:
464
+ log_e (" Failed to set all pins bus to ETHERNET" );
465
+ ETHClass::ethDetachBus ((void *)(this ));
466
+ return false ;
416
467
}
417
468
418
469
bool ETHClass::config (IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1, IPAddress dns2)
@@ -615,4 +666,36 @@ String ETHClass::macAddress(void)
615
666
return String (macStr);
616
667
}
617
668
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
+
618
701
ETHClass ETH;
0 commit comments