Skip to content

Commit 1c49665

Browse files
committed
make ETH work
1 parent 4bb60f6 commit 1c49665

File tree

3 files changed

+161
-20
lines changed

3 files changed

+161
-20
lines changed

libraries/WiFi/src/ETH.cpp

+146-14
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
#include "ETH.h"
2222
#include "esp_system.h"
2323
#ifdef ESP_IDF_VERSION_MAJOR
24+
#include "esp_event.h"
2425
#include "esp_eth.h"
2526
#include "esp_eth_phy.h"
27+
#include "esp_eth_mac.h"
28+
#include "esp_eth_com.h"
2629
#else
2730
#include "eth_phy/phy.h"
2831
#include "eth_phy/phy_tlk110.h"
@@ -33,7 +36,62 @@
3336

3437
extern void tcpipInit();
3538

36-
#ifndef ESP_IDF_VERSION_MAJOR
39+
#ifdef ESP_IDF_VERSION_MAJOR
40+
41+
/**
42+
* @brief Callback function invoked when lowlevel initialization is finished
43+
*
44+
* @param[in] eth_handle: handle of Ethernet driver
45+
*
46+
* @return
47+
* - ESP_OK: process extra lowlevel initialization successfully
48+
* - ESP_FAIL: error occurred when processing extra lowlevel initialization
49+
*/
50+
//static esp_err_t on_lowlevel_init_done(esp_eth_handle_t eth_handle){
51+
//#define PIN_PHY_POWER 2
52+
// pinMode(PIN_PHY_POWER, OUTPUT);
53+
// digitalWrite(PIN_PHY_POWER, HIGH);
54+
// delay(100);
55+
// return ESP_OK;
56+
//}
57+
58+
/**
59+
* @brief Callback function invoked when lowlevel deinitialization is finished
60+
*
61+
* @param[in] eth_handle: handle of Ethernet driver
62+
*
63+
* @return
64+
* - ESP_OK: process extra lowlevel deinitialization successfully
65+
* - ESP_FAIL: error occurred when processing extra lowlevel deinitialization
66+
*/
67+
//static esp_err_t on_lowlevel_deinit_done(esp_eth_handle_t eth_handle){
68+
// return ESP_OK;
69+
//}
70+
71+
72+
// Event handler for Ethernet
73+
void ETHClass::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
74+
{
75+
switch (event_id) {
76+
case ETHERNET_EVENT_CONNECTED:
77+
((ETHClass*)(arg))->eth_link = ETH_LINK_UP;
78+
break;
79+
case ETHERNET_EVENT_DISCONNECTED:
80+
((ETHClass*)(arg))->eth_link = ETH_LINK_DOWN;
81+
break;
82+
case ETHERNET_EVENT_START:
83+
((ETHClass*)(arg))->started = true;
84+
break;
85+
case ETHERNET_EVENT_STOP:
86+
((ETHClass*)(arg))->started = false;
87+
break;
88+
default:
89+
break;
90+
}
91+
}
92+
93+
94+
#else
3795
static int _eth_phy_mdc_pin = -1;
3896
static int _eth_phy_mdio_pin = -1;
3997
static int _eth_phy_power_pin = -1;
@@ -57,7 +115,11 @@ static void _eth_phy_power_enable(bool enable)
57115
}
58116
#endif
59117

60-
ETHClass::ETHClass():initialized(false),started(false),staticIP(false)
118+
ETHClass::ETHClass()
119+
:initialized(false)
120+
,staticIP(false)
121+
,started(false)
122+
,eth_link(ETH_LINK_DOWN)
61123
{
62124
}
63125

@@ -66,7 +128,79 @@ ETHClass::~ETHClass()
66128

67129
#ifdef ESP_IDF_VERSION_MAJOR
68130
bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type){
69-
return false;
131+
132+
tcpipInit();
133+
134+
esp_event_loop_create_default();
135+
tcpip_adapter_set_default_eth_handlers();
136+
esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, eth_event_handler, this);
137+
//ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
138+
139+
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
140+
mac_config.smi_mdc_gpio_num = mdc;
141+
mac_config.smi_mdio_gpio_num = mdio;
142+
//mac_config.sw_reset_timeout_ms = 1000;
143+
esp_eth_mac_t *eth_mac = NULL;
144+
#if CONFIG_ETH_SPI_ETHERNET_DM9051
145+
if(type == ETH_PHY_DM9051){
146+
return false;//todo
147+
} else {
148+
#endif
149+
eth_mac = esp_eth_mac_new_esp32(&mac_config);
150+
#if CONFIG_ETH_SPI_ETHERNET_DM9051
151+
}
152+
#endif
153+
154+
if(eth_mac == NULL){
155+
log_e("esp_eth_mac_new_esp32 failed");
156+
return false;
157+
}
158+
159+
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
160+
phy_config.phy_addr = phy_addr;
161+
phy_config.reset_gpio_num = power;
162+
esp_eth_phy_t *eth_phy = NULL;
163+
switch(type){
164+
case ETH_PHY_LAN8720:
165+
eth_phy = esp_eth_phy_new_lan8720(&phy_config);
166+
break;
167+
case ETH_PHY_TLK110:
168+
eth_phy = esp_eth_phy_new_ip101(&phy_config);
169+
break;
170+
case ETH_PHY_RTL8201:
171+
eth_phy = esp_eth_phy_new_rtl8201(&phy_config);
172+
break;
173+
case ETH_PHY_DP83848:
174+
eth_phy = esp_eth_phy_new_dp83848(&phy_config);
175+
break;
176+
#if CONFIG_ETH_SPI_ETHERNET_DM9051
177+
case ETH_PHY_DM9051:
178+
eth_phy = esp_eth_phy_new_dm9051(&phy_config);
179+
break;
180+
#endif
181+
default:
182+
break;
183+
}
184+
if(eth_phy == NULL){
185+
log_e("esp_eth_phy_new failed");
186+
return false;
187+
}
188+
189+
eth_handle = NULL;
190+
esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(eth_mac, eth_phy);
191+
//eth_config.on_lowlevel_init_done = on_lowlevel_init_done;
192+
//eth_config.on_lowlevel_deinit_done = on_lowlevel_deinit_done;
193+
if(esp_eth_driver_install(&eth_config, &eth_handle) != ESP_OK || eth_handle == NULL){
194+
log_e("esp_eth_driver_install failed");
195+
return false;
196+
}
197+
198+
if(esp_eth_start(eth_handle) != ESP_OK){
199+
log_e("esp_eth_start failed");
200+
return false;
201+
}
202+
203+
return true;
70204
}
71205
#else
72206
bool ETHClass::begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type, eth_clock_mode_t clock_mode)
@@ -260,7 +394,7 @@ bool ETHClass::setHostname(const char * hostname)
260394
bool ETHClass::fullDuplex()
261395
{
262396
#ifdef ESP_IDF_VERSION_MAJOR
263-
return true;//todo
397+
return true;//todo: do not see an API for this
264398
#else
265399
return eth_config.phy_get_duplex_mode();
266400
#endif
@@ -269,7 +403,7 @@ bool ETHClass::fullDuplex()
269403
bool ETHClass::linkUp()
270404
{
271405
#ifdef ESP_IDF_VERSION_MAJOR
272-
return true;//todo
406+
return eth_link == ETH_LINK_UP;
273407
#else
274408
return eth_config.phy_check_link();
275409
#endif
@@ -278,7 +412,9 @@ bool ETHClass::linkUp()
278412
uint8_t ETHClass::linkSpeed()
279413
{
280414
#ifdef ESP_IDF_VERSION_MAJOR
281-
return 100;//ToDo
415+
eth_speed_t link_speed;
416+
esp_eth_ioctl(eth_handle, ETH_CMD_G_SPEED, &link_speed);
417+
return (link_speed == ETH_SPEED_10M)?10:100;
282418
#else
283419
return eth_config.phy_get_speed_mode()?100:10;
284420
#endif
@@ -298,13 +434,13 @@ IPv6Address ETHClass::localIPv6()
298434
return IPv6Address(addr.addr);
299435
}
300436

301-
uint8_t * macAddress(uint8_t* mac)
437+
uint8_t * ETHClass::macAddress(uint8_t* mac)
302438
{
303439
if(!mac){
304440
return NULL;
305441
}
306442
#ifdef ESP_IDF_VERSION_MAJOR
307-
//ToDo
443+
esp_eth_ioctl(eth_handle, ETH_CMD_G_MAC_ADDR, mac);
308444
#else
309445
esp_eth_get_mac(mac);
310446
#endif
@@ -313,13 +449,9 @@ uint8_t * macAddress(uint8_t* mac)
313449

314450
String ETHClass::macAddress(void)
315451
{
316-
uint8_t mac[6] = {0,0,0,0,0,0};//ToDo
452+
uint8_t mac[6] = {0,0,0,0,0,0};
317453
char macStr[18] = { 0 };
318-
#ifdef ESP_IDF_VERSION_MAJOR
319-
//ToDo
320-
#else
321-
esp_eth_get_mac(mac);
322-
#endif
454+
macAddress(mac);
323455
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
324456
return String(macStr);
325457
}

libraries/WiFi/src/ETH.h

+9-6
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,27 @@
4545
#define ETH_PHY_MDIO 18
4646
#endif
4747

48+
#ifndef ESP_IDF_VERSION_MAJOR
4849
#ifndef ETH_CLK_MODE
49-
#ifdef ESP_IDF_VERSION_MAJOR
50-
#define ETH_CLK_MODE ESP_ETH_CLOCK_GPIO0_IN
51-
#else
5250
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
5351
#endif
5452
#endif
5553

56-
typedef enum { ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_MAX } eth_phy_type_t;
54+
typedef enum { ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_RTL8201, ETH_PHY_DP83848, ETH_PHY_DM9051, ETH_PHY_MAX } eth_phy_type_t;
5755

5856
class ETHClass {
5957
private:
6058
bool initialized;
61-
bool started;
6259
bool staticIP;
6360
#ifdef ESP_IDF_VERSION_MAJOR
64-
esp_eth_config_t eth_config;
61+
esp_eth_handle_t eth_handle;
62+
63+
protected:
64+
bool started;
65+
eth_link_t eth_link;
66+
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
6567
#else
68+
bool started;
6669
eth_config_t eth_config;
6770
#endif
6871
public:

libraries/WiFi/src/WiFiGeneric.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ void tcpipInit(){
102102
static bool initialized = false;
103103
if(!initialized && _start_network_event_task()){
104104
initialized = true;
105+
#ifdef ESP_IDF_VERSION_MAJOR
106+
uint8_t mac[8];
107+
if(esp_efuse_mac_get_default(mac) == ESP_OK){
108+
esp_base_mac_addr_set(mac);
109+
}
110+
#endif
105111
tcpip_adapter_init();
106112
}
107113
}

0 commit comments

Comments
 (0)