21
21
#include " ETH.h"
22
22
#include " esp_system.h"
23
23
#ifdef ESP_IDF_VERSION_MAJOR
24
+ #include " esp_event.h"
24
25
#include " esp_eth.h"
25
26
#include " esp_eth_phy.h"
27
+ #include " esp_eth_mac.h"
28
+ #include " esp_eth_com.h"
26
29
#else
27
30
#include " eth_phy/phy.h"
28
31
#include " eth_phy/phy_tlk110.h"
33
36
34
37
extern void tcpipInit ();
35
38
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
37
95
static int _eth_phy_mdc_pin = -1 ;
38
96
static int _eth_phy_mdio_pin = -1 ;
39
97
static int _eth_phy_power_pin = -1 ;
@@ -57,7 +115,11 @@ static void _eth_phy_power_enable(bool enable)
57
115
}
58
116
#endif
59
117
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)
61
123
{
62
124
}
63
125
@@ -66,7 +128,79 @@ ETHClass::~ETHClass()
66
128
67
129
#ifdef ESP_IDF_VERSION_MAJOR
68
130
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 (ð_config, ð_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 ;
70
204
}
71
205
#else
72
206
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)
260
394
bool ETHClass::fullDuplex ()
261
395
{
262
396
#ifdef ESP_IDF_VERSION_MAJOR
263
- return true ;// todo
397
+ return true ;// todo: do not see an API for this
264
398
#else
265
399
return eth_config.phy_get_duplex_mode ();
266
400
#endif
@@ -269,7 +403,7 @@ bool ETHClass::fullDuplex()
269
403
bool ETHClass::linkUp ()
270
404
{
271
405
#ifdef ESP_IDF_VERSION_MAJOR
272
- return true ; // todo
406
+ return eth_link == ETH_LINK_UP;
273
407
#else
274
408
return eth_config.phy_check_link ();
275
409
#endif
@@ -278,7 +412,9 @@ bool ETHClass::linkUp()
278
412
uint8_t ETHClass::linkSpeed ()
279
413
{
280
414
#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 ;
282
418
#else
283
419
return eth_config.phy_get_speed_mode ()?100 :10 ;
284
420
#endif
@@ -298,13 +434,13 @@ IPv6Address ETHClass::localIPv6()
298
434
return IPv6Address (addr.addr );
299
435
}
300
436
301
- uint8_t * macAddress (uint8_t * mac)
437
+ uint8_t * ETHClass:: macAddress (uint8_t * mac)
302
438
{
303
439
if (!mac){
304
440
return NULL ;
305
441
}
306
442
#ifdef ESP_IDF_VERSION_MAJOR
307
- // ToDo
443
+ esp_eth_ioctl (eth_handle, ETH_CMD_G_MAC_ADDR, mac);
308
444
#else
309
445
esp_eth_get_mac (mac);
310
446
#endif
@@ -313,13 +449,9 @@ uint8_t * macAddress(uint8_t* mac)
313
449
314
450
String ETHClass::macAddress (void )
315
451
{
316
- uint8_t mac[6 ] = {0 ,0 ,0 ,0 ,0 ,0 };// ToDo
452
+ uint8_t mac[6 ] = {0 ,0 ,0 ,0 ,0 ,0 };
317
453
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);
323
455
sprintf (macStr, " %02X:%02X:%02X:%02X:%02X:%02X" , mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]);
324
456
return String (macStr);
325
457
}
0 commit comments