Skip to content

Commit aca449f

Browse files
authored
ETHERNET - Peripheral manager implementation (#8297)
* Peripheral manager implemented * remove unused variable * Add all RMII pins * fix typo
1 parent 07bf7be commit aca449f

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

cores/esp32/esp32-hal-periman.h

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ typedef enum {
5757
#endif
5858
#if SOC_USB_SERIAL_JTAG_SUPPORTED || SOC_USB_OTG_SUPPORTED
5959
ESP32_BUS_TYPE_USB, // IO is used as USB pin
60+
#endif
61+
#if CONFIG_IDF_TARGET_ESP32
62+
ESP32_BUS_TYPE_ETHERNET, // IO is used as ETHERNET-RMII pin
6063
#endif
6164
ESP32_BUS_TYPE_MAX
6265
} peripheral_bus_type_t;

libraries/Ethernet/src/ETH.cpp

+87-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
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
@@ -32,6 +31,7 @@
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

232232
ETHClass::~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+
235243
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)
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, &eth_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

418469
bool 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+
618701
ETHClass ETH;

libraries/Ethernet/src/ETH.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
#include "esp_eth.h"
2727
#include "esp_netif.h"
2828

29+
//Dedicated GPIOs for RMII
30+
#define ETH_RMII_TX_EN 21
31+
#define ETH_RMII_TX0 19
32+
#define ETH_RMII_TX1 22
33+
#define ETH_RMII_RX0 25
34+
#define ETH_RMII_RX1_EN 26
35+
#define ETH_RMII_CRS_DV 27
36+
2937
#ifndef ETH_PHY_ADDR
3038
#define ETH_PHY_ADDR 0
3139
#endif
@@ -66,10 +74,14 @@ class ETHClass {
6674
esp_netif_t *esp_netif;
6775

6876
protected:
69-
bool started;
77+
bool _started;
78+
int8_t _pin_mcd = -1;
79+
int8_t _pin_mdio = -1;
80+
int8_t _pin_power = -1;
81+
int8_t _pin_rmii_clock = -1;
7082
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
7183
#else
72-
bool started;
84+
bool _started;
7385
eth_config_t eth_config;
7486
#endif
7587
public:
@@ -102,8 +114,13 @@ class ETHClass {
102114
uint8_t * macAddress(uint8_t* mac);
103115
String macAddress();
104116

117+
void end();
118+
105119
friend class WiFiClient;
106120
friend class WiFiServer;
121+
122+
private:
123+
static bool ethDetachBus(void * bus_pointer);
107124
};
108125

109126
extern ETHClass ETH;

0 commit comments

Comments
 (0)