ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ethernet_component_esp32.cpp
Go to the documentation of this file.
2
3#if defined(USE_ETHERNET) && defined(USE_ESP32)
4
8#include "esphome/core/log.h"
9#include "w5500_custom_spi.h"
10
11#include <lwip/dns.h>
12#include <cinttypes>
13#include "esp_event.h"
14#if USE_NETWORK_IPV6
15#include <esp_netif_net_stack.h>
16#include <lwip/netif.h>
17#endif
18#ifdef USE_PSRAM
19#include <esp_psram.h>
20#endif
21
22// IDF 6.0 moved per-chip PHY/MAC drivers to the Espressif Component Registry;
23// they are no longer included via esp_eth.h and need explicit includes.
24// On IDF 5.x these headers don't exist as standalone files.
25#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
26#ifdef USE_ETHERNET_LAN8720
27#include "esp_eth_phy_lan87xx.h"
28#endif
29#ifdef USE_ETHERNET_RTL8201
30#include "esp_eth_phy_rtl8201.h"
31#endif
32#ifdef USE_ETHERNET_DP83848
33#include "esp_eth_phy_dp83848.h"
34#endif
35#ifdef USE_ETHERNET_IP101
36#include "esp_eth_phy_ip101.h"
37#endif
38#ifdef USE_ETHERNET_KSZ8081
39#include "esp_eth_phy_ksz80xx.h"
40#endif
41#ifdef USE_ETHERNET_W5500
42#include "esp_eth_mac_w5500.h"
43#include "esp_eth_phy_w5500.h"
44#endif
45#ifdef USE_ETHERNET_DM9051
46#include "esp_eth_mac_dm9051.h"
47#include "esp_eth_phy_dm9051.h"
48#endif
49#ifdef USE_ETHERNET_KSZ8851SNL
50#include "esp_eth_mac_ksz8851snl.h"
51#include "esp_eth_phy_ksz8851snl.h"
52#endif
53#endif // ESP_IDF_VERSION >= 6.0.0
54
55// LAN867x header exists on all IDF versions (external component since IDF 5.3)
56#ifdef USE_ETHERNET_LAN8670
57#include "esp_eth_phy_lan867x.h"
58#endif
59
60// ENC28J60 header exists on all IDF versions (always an external component)
61#ifdef USE_ETHERNET_ENC28J60
62#include "esp_eth_enc28j60.h"
63#endif
64
65// CH390 headers exist on all IDF versions (always an external component)
66#ifdef USE_ETHERNET_CH390
67#include "esp_eth_mac_ch390.h"
68#include "esp_eth_phy_ch390.h"
69#endif
70
71#ifdef USE_ETHERNET_SPI
72#include <driver/gpio.h>
73#include <driver/spi_master.h>
74#ifdef USE_SPI
76#endif
77#endif
78
79namespace esphome::ethernet {
80
81ESPHOME_LOG_TAG(TAG, "ethernet");
82
83// PHY register size for hex logging
84static constexpr size_t PHY_REG_SIZE = 2;
85
86// Dual wifi + ethernet SPI builds: the one place internal RAM is short and lwip's other buffers are
87// already in PSRAM. Not with L2 TAP, whose filter lives in the glue's input path this replaces.
88#if defined(USE_PSRAM) && defined(USE_ETHERNET_SPI) && defined(USE_WIFI) && !defined(CONFIG_ESP_NETIF_L2_TAP)
89#define USE_ETHERNET_RX_PSRAM
90// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) - reported by dump_config()
91static bool rx_psram_installed = false;
92
93// ESP-IDF ethernet drivers malloc() every received frame in internal RAM, where it stays until lwIP
94// hands it to the application. Move it to PSRAM; if that fails the frame is passed on where it is.
95static esp_err_t eth_input_to_psram(esp_eth_handle_t handle, uint8_t *buffer, uint32_t length, void *priv) {
96 auto *copy = static_cast<uint8_t *>(heap_caps_malloc(length, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
97 if (copy != nullptr) {
98 memcpy(copy, buffer, length);
99 free(buffer); // NOLINT(cppcoreguidelines-no-malloc) - allocated by the driver with malloc()
100 buffer = copy;
101 } else {
102 static bool warned = false; // once, this runs per frame in the driver's task
103 if (!warned) {
104 warned = true;
105 ESP_LOGW(TAG, "PSRAM allocation failed, frame kept in internal RAM (reported once)");
106 }
107 }
108 return esp_netif_receive(static_cast<esp_netif_t *>(priv), buffer, length, nullptr);
109}
110#endif
111
113 ESP_LOGE(TAG, "%s: (%d) %s", message, err, esp_err_to_name(err));
114 this->mark_failed();
115}
116
117#define ESPHL_ERROR_CHECK(err, message) \
118 if ((err) != ESP_OK) { \
119 this->log_error_and_mark_failed_(err, message); \
120 return; \
121 }
122
123#define ESPHL_ERROR_CHECK_RET(err, message, ret) \
124 if ((err) != ESP_OK) { \
125 this->log_error_and_mark_failed_(err, message); \
126 return ret; \
127 }
128
131
132 switch (this->state_) {
134 if (this->started_) {
135 ESP_LOGI(TAG, "Starting connection");
137 this->start_connect_();
138 }
139 break;
141 if (!this->started_) {
142 ESP_LOGI(TAG, "Stopped connection");
144 } else if (this->connected_) {
145 // connection established
146 ESP_LOGI(TAG, "Connected");
148
149 this->dump_connect_params_();
150 this->status_clear_warning();
151#ifdef USE_ETHERNET_CONNECT_TRIGGER
153#endif
154 } else if (now - this->connect_begin_ > 15000) {
155 ESP_LOGW(TAG, "Connecting failed; reconnecting");
156 this->start_connect_();
157 }
158 break;
160 if (!this->started_) {
161 ESP_LOGI(TAG, "Stopped connection");
163#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
165#endif
166 } else if (!this->connected_) {
167 ESP_LOGW(TAG, "Connection lost; reconnecting");
169 this->start_connect_();
170#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
172#endif
173 } else {
174 this->finish_connect_();
175 // When connected and stable, disable the loop to save CPU cycles
176 this->disable_loop();
177 }
178 break;
179 }
180}
181
183 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
184 // Delay here to allow power to stabilise before Ethernet is initialized.
185 delay(300); // NOLINT
186 }
187
188 if (this->enable_on_boot_) {
189 this->ethernet_lazy_init_();
190 if (!this->ethernet_initialized_) {
191 // lazy_init bailed early via ESPHL_ERROR_CHECK or mark_failed; nothing more to do.
192 return;
193 }
194 esp_err_t err = esp_eth_start(this->eth_handle_);
195 ESPHL_ERROR_CHECK(err, "ETH start error");
196 } else {
197 ESP_LOGCONFIG(TAG, "Skipping init (enable_on_boot: false)");
198 this->disabled_ = true;
199 }
200}
201
203 if (this->ethernet_initialized_)
204 return;
205
206 esp_err_t err;
207
208#ifdef USE_ETHERNET_SPI
209 // Install GPIO ISR handler to be able to service SPI Eth modules interrupts
210 gpio_install_isr_service(0);
211
212 spi_host_device_t host;
213#ifdef USE_SPI
214 if (this->spi_parent_ != nullptr) {
215 // The bus is owned and already initialized by the spi component; share its host.
216 host = this->spi_parent_->get_interface();
217 } else
218#endif
219 {
220 spi_bus_config_t buscfg = {
221 .mosi_io_num = this->mosi_pin_,
222 .miso_io_num = this->miso_pin_,
223 .sclk_io_num = this->clk_pin_,
224 .quadwp_io_num = -1,
225 .quadhd_io_num = -1,
226 .data4_io_num = -1,
227 .data5_io_num = -1,
228 .data6_io_num = -1,
229 .data7_io_num = -1,
230 .max_transfer_sz = 0,
231 .flags = 0,
232 .intr_flags = 0,
233 };
234
235 host = this->interface_;
236
237 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
238 ESPHL_ERROR_CHECK(err, "SPI bus initialize error");
239 }
240#endif
241 // Network interface setup handled by network component
242
243 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
244 this->eth_netif_ = esp_netif_new(&cfg);
245
246 // Init MAC and PHY configs to default
247 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
248 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
249
250#ifdef USE_ETHERNET_SPI // Configure SPI interface and Ethernet driver for specific SPI module
251 spi_device_interface_config_t devcfg = {
252 .command_bits = 0,
253 .address_bits = 0,
254 .dummy_bits = 0,
255 .mode = 0,
256 .duty_cycle_pos = 0,
257 .cs_ena_pretrans = 0,
258 .cs_ena_posttrans = 0,
259 .clock_speed_hz = this->clock_speed_,
260 .input_delay_ns = 0,
261 .spics_io_num = this->cs_pin_,
262 .flags = 0,
263 .queue_size = 20,
264 .pre_cb = nullptr,
265 .post_cb = nullptr,
266 };
267
268#if defined(USE_ETHERNET_W5500)
269 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
270#elif defined(USE_ETHERNET_DM9051)
271 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
272#elif defined(USE_ETHERNET_ENC28J60)
273 eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(host, &devcfg);
274#elif defined(USE_ETHERNET_CH390)
275 eth_ch390_config_t ch390_config = ETH_CH390_DEFAULT_CONFIG(host, &devcfg);
276#elif defined(USE_ETHERNET_KSZ8851SNL)
277 eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(host, &devcfg);
278#endif
279
280#if defined(USE_ETHERNET_W5500)
281 w5500_config.int_gpio_num = this->interrupt_pin_;
282#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
283 w5500_config.poll_period_ms = this->polling_interval_;
284#endif
285 // Install the custom SPI driver that offloads the bulk RX/TX frame transfers off the busy-wait
286 // path. w5500_config (and the devcfg it references) outlives esp_eth_mac_new_w5500() below, which
287 // runs the driver's init().
288 install_w5500_async_spi(w5500_config);
289#elif defined(USE_ETHERNET_DM9051)
290 dm9051_config.int_gpio_num = this->interrupt_pin_;
291#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
292 dm9051_config.poll_period_ms = this->polling_interval_;
293#endif
294#elif defined(USE_ETHERNET_ENC28J60)
295 // ENC28J60 does not support poll_period_ms. CS must stay asserted for the chip's CS hold
296 // time (t10, 210 ns) after the last clock or MAC/MII register reads fail ("wrong chip ID")
297 enc28j60_config.spi_devcfg->cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time((this->clock_speed_ + 999999) / 1000000);
298 enc28j60_config.int_gpio_num = this->interrupt_pin_;
299#elif defined(USE_ETHERNET_CH390)
300 ch390_config.int_gpio_num = this->interrupt_pin_;
301#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
302 ch390_config.poll_period_ms = this->polling_interval_;
303#endif
304#elif defined(USE_ETHERNET_KSZ8851SNL)
305 ksz8851snl_config.int_gpio_num = this->interrupt_pin_;
306#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
307 ksz8851snl_config.poll_period_ms = this->polling_interval_;
308#endif
309#endif
310
311 phy_config.phy_addr = this->phy_addr_spi_;
312 phy_config.reset_gpio_num = this->reset_pin_;
313
314 esp_eth_mac_t *mac = nullptr;
315#elif defined(USE_ETHERNET_OPENETH)
316 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
317#else
318 phy_config.phy_addr = this->phy_addr_;
319 phy_config.reset_gpio_num = this->power_pin_;
320
321 eth_esp32_emac_config_t esp32_emac_config = eth_esp32_emac_default_config();
322#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
323 esp32_emac_config.smi_gpio.mdc_num = this->mdc_pin_;
324 esp32_emac_config.smi_gpio.mdio_num = this->mdio_pin_;
325#else
326 esp32_emac_config.smi_mdc_gpio_num = this->mdc_pin_;
327 esp32_emac_config.smi_mdio_gpio_num = this->mdio_pin_;
328#endif
329 // The RGMII types (GENERIC, YT8531) use the RGMII interface and default GPIO map from
330 // eth_esp32_emac_default_config(); writing the RMII clock config would clobber that
331 // union, so skip the RMII clock override for them.
332 if (this->type_ != ETHERNET_TYPE_GENERIC && this->type_ != ETHERNET_TYPE_YT8531) {
333 esp32_emac_config.clock_config.rmii.clock_mode = this->clk_mode_;
334 esp32_emac_config.clock_config.rmii.clock_gpio =
335 static_cast<decltype(esp32_emac_config.clock_config.rmii.clock_gpio)>(this->clk_pin_);
336 }
337
338 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
339#endif
340
341 switch (this->type_) {
342#ifdef USE_ETHERNET_OPENETH
344 phy_config.autonego_timeout_ms = 1000;
345#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
346 this->phy_ = esp_eth_phy_new_generic(&phy_config);
347#else
348 this->phy_ = esp_eth_phy_new_dp83848(&phy_config);
349#endif
350 break;
351 }
352#endif
353#if CONFIG_ETH_USE_ESP32_EMAC
354#ifdef USE_ETHERNET_LAN8720
356 this->phy_ = esp_eth_phy_new_lan87xx(&phy_config);
357 break;
358 }
359#endif
360#ifdef USE_ETHERNET_RTL8201
362 this->phy_ = esp_eth_phy_new_rtl8201(&phy_config);
363 break;
364 }
365#endif
366#ifdef USE_ETHERNET_DP83848
368 this->phy_ = esp_eth_phy_new_dp83848(&phy_config);
369 break;
370 }
371#endif
372#ifdef USE_ETHERNET_IP101
373 case ETHERNET_TYPE_IP101: {
374 this->phy_ = esp_eth_phy_new_ip101(&phy_config);
375 break;
376 }
377#endif
378#ifdef USE_ETHERNET_JL1101
380 // PlatformIO (pioarduino): builtin esp_eth_phy_new_jl1101() on all IDF versions
381 // Non-PlatformIO: custom ESPHome driver (esp_eth_phy_jl1101.c)
382 this->phy_ = esp_eth_phy_new_jl1101(&phy_config);
383 break;
384 }
385#endif
386#ifdef USE_ETHERNET_KSZ8081
389 this->phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
390 break;
391 }
392#endif
393#ifdef USE_ETHERNET_LAN8670
395 this->phy_ = esp_eth_phy_new_lan867x(&phy_config);
396 break;
397 }
398#endif
399#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
400 // GENERIC and YT8531 both use the built-in generic 802.3 PHY driver; YT8531 gets
401 // extra chip-specific tuning applied later in ethernet_lazy_init_().
402#ifdef USE_ETHERNET_GENERIC
404#endif
405#ifdef USE_ETHERNET_YT8531
407#endif
408#if defined(USE_ETHERNET_GENERIC) || defined(USE_ETHERNET_YT8531)
409 this->phy_ = esp_eth_phy_new_generic(&phy_config);
410 break;
411#endif
412#endif
413#endif
414#ifdef USE_ETHERNET_SPI
415#if defined(USE_ETHERNET_W5500)
416 case ETHERNET_TYPE_W5500: {
417 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
418 this->phy_ = esp_eth_phy_new_w5500(&phy_config);
419 break;
420 }
421#elif defined(USE_ETHERNET_DM9051)
423 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
424 this->phy_ = esp_eth_phy_new_dm9051(&phy_config);
425 break;
426 }
427#elif defined(USE_ETHERNET_ENC28J60)
429 mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);
430 this->phy_ = esp_eth_phy_new_enc28j60(&phy_config);
431 break;
432 }
433#elif defined(USE_ETHERNET_CH390)
434 case ETHERNET_TYPE_CH390: {
435 mac = esp_eth_mac_new_ch390(&ch390_config, &mac_config);
436 this->phy_ = esp_eth_phy_new_ch390(&phy_config);
437 break;
438 }
439#elif defined(USE_ETHERNET_KSZ8851SNL)
441 mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
442 this->phy_ = esp_eth_phy_new_ksz8851snl(&phy_config);
443 break;
444 }
445#endif
446#endif
447 default: {
448 this->mark_failed();
449 return;
450 }
451 }
452
453 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->phy_);
454 this->eth_handle_ = nullptr;
455 err = esp_eth_driver_install(&eth_config, &this->eth_handle_);
456 ESPHL_ERROR_CHECK(err, "ETH driver install error");
457
458#ifndef USE_ETHERNET_SPI
459#ifdef USE_ETHERNET_KSZ8081
460 if (this->type_ == ETHERNET_TYPE_KSZ8081RNA && this->clk_mode_ == EMAC_CLK_OUT) {
461 // KSZ8081RNA default is incorrect. It expects a 25MHz clock instead of the 50MHz we provide.
463 }
464#endif // USE_ETHERNET_KSZ8081
465
466#ifdef ESPHOME_ETHERNET_PHY_REGISTER_COUNT
467 for (const auto &phy_register : this->phy_registers_) {
468 this->write_phy_register_(mac, phy_register);
469 }
470#endif
471
472#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
473#ifdef USE_ETHERNET_GENERIC
474 // The generic 802.3 PHY driver only resets the PHY in its init; it never enables
475 // auto-negotiation. A PHY that resets into a forced-speed mode (BMCR auto-nego bit
476 // clear) therefore stays there, and esp_eth_start() skips negotiation because the
477 // driver cached auto_nego_en=false at install time. Force auto-negotiation on here
478 // (which also updates that cached state) so esp_eth_start() restarts a proper
479 // negotiation. (YT8531 does this as part of its own chip-specific init below.)
480 if (this->type_ == ETHERNET_TYPE_GENERIC) {
481 bool autoneg_enable = true;
482 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
483 ESPHL_ERROR_CHECK(err, "Enable auto-negotiation failed");
484 }
485#endif
486#ifdef USE_ETHERNET_YT8531
487 if (this->type_ == ETHERNET_TYPE_YT8531) {
488 this->yt8531_phy_init_();
489 if (this->is_failed())
490 return;
491 }
492#endif
493#endif // ESP_IDF_VERSION >= 6.0.0
494#endif // !USE_ETHERNET_SPI
495
496 // use ESP internal eth mac
497 uint8_t mac_addr[MAC_ADDRESS_SIZE];
498 if (this->fixed_mac_.has_value()) {
499 memcpy(mac_addr, this->fixed_mac_->data(), MAC_ADDRESS_SIZE);
500 } else {
501 esp_read_mac(mac_addr, ESP_MAC_ETH);
502 }
503 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
504 ESPHL_ERROR_CHECK(err, "set mac address error");
505
506 /* attach Ethernet driver to TCP/IP stack */
507 err = esp_netif_attach(this->eth_netif_, esp_eth_new_netif_glue(this->eth_handle_));
508 ESPHL_ERROR_CHECK(err, "ETH netif attach error");
509#ifdef USE_ETHERNET_RX_PSRAM
510 // The glue frees every receive buffer with free(), so the replacement buffer must come from the heap
511 if (esp_psram_is_initialized()) {
512 err = esp_eth_update_input_path(this->eth_handle_, eth_input_to_psram, this->eth_netif_);
513 rx_psram_installed = err == ESP_OK;
514 if (!rx_psram_installed) {
515 ESP_LOGW(TAG, "PSRAM RX path not installed: %s", esp_err_to_name(err));
516 }
517 }
518#endif
519#if USE_NETWORK_IPV6 && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
520 // Since IDF 5.5 the internal EMAC drops multicast groups that were never added (before,
521 // it passed all multicast), and lwIP never adds all-nodes, so router advertisements
522 // were lost and SLAAC never ran.
523 {
524 uint8_t all_nodes[6] = {0x33, 0x33, 0x00, 0x00, 0x00, 0x01};
525 if (esp_err_t filter_err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_ADD_MAC_FILTER, all_nodes);
526 filter_err != ESP_OK) {
527 ESP_LOGD(TAG, "IPv6 all-nodes multicast filter not added: %s", esp_err_to_name(filter_err));
528 }
529 }
530#endif
531
532 // Register user defined event handers
533 err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &EthernetComponent::eth_event_handler, nullptr);
534 ESPHL_ERROR_CHECK(err, "ETH event handler register error");
535 err = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &EthernetComponent::got_ip_event_handler, nullptr);
536 ESPHL_ERROR_CHECK(err, "GOT IP event handler register error");
537#if USE_NETWORK_IPV6
538 err = esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &EthernetComponent::got_ip6_event_handler, nullptr);
539 ESPHL_ERROR_CHECK(err, "GOT IPv6 event handler register error");
540#endif /* USE_NETWORK_IPV6 */
541
542 this->ethernet_initialized_ = true;
543}
544
546 if (!this->disabled_)
547 return;
548
549 ESP_LOGD(TAG, "Enabling");
550 this->ethernet_lazy_init_();
551 if (!this->ethernet_initialized_) {
552 ESP_LOGE(TAG, "Cannot enable - init failed");
553 return;
554 }
555 esp_err_t err = esp_eth_start(this->eth_handle_);
556 if (err != ESP_OK) {
557 ESP_LOGE(TAG, "esp_eth_start failed: %s", esp_err_to_name(err));
558 return;
559 }
560 this->disabled_ = false;
561 // The ETH_EVENT_START handler will set started_=true; the loop state machine
562 // will then drive the STOPPED -> CONNECTING -> CONNECTED transitions.
563 this->enable_loop();
564}
565
567 if (this->disabled_)
568 return;
569
570 ESP_LOGD(TAG, "Disabling");
571 esp_err_t err = esp_eth_stop(this->eth_handle_);
572 if (err != ESP_OK) {
573 ESP_LOGW(TAG, "esp_eth_stop failed: %s — disabling anyway", esp_err_to_name(err));
574 }
575 this->disabled_ = true;
576 // ETH_EVENT_STOP will clear started_; loop() will transition to STOPPED.
577}
578
580 const char *eth_type;
581 switch (this->type_) {
582#ifdef USE_ETHERNET_LAN8720
584 eth_type = "LAN8720";
585 break;
586#endif
587#ifdef USE_ETHERNET_RTL8201
589 eth_type = "RTL8201";
590 break;
591#endif
592#ifdef USE_ETHERNET_DP83848
594 eth_type = "DP83848";
595 break;
596#endif
597#ifdef USE_ETHERNET_IP101
599 eth_type = "IP101";
600 break;
601#endif
602#ifdef USE_ETHERNET_JL1101
604 eth_type = "JL1101";
605 break;
606#endif
607#ifdef USE_ETHERNET_KSZ8081
609 eth_type = "KSZ8081";
610 break;
611
613 eth_type = "KSZ8081RNA";
614 break;
615#endif
616#if defined(USE_ETHERNET_W5500)
618 eth_type = "W5500";
619 break;
620#elif defined(USE_ETHERNET_DM9051)
622 eth_type = "DM9051";
623 break;
624#elif defined(USE_ETHERNET_ENC28J60)
626 eth_type = "ENC28J60";
627 break;
628#elif defined(USE_ETHERNET_CH390)
630 eth_type = "CH390";
631 break;
632#elif defined(USE_ETHERNET_KSZ8851SNL)
634 eth_type = "KSZ8851SNL";
635 break;
636#endif
637#ifdef USE_ETHERNET_OPENETH
639 eth_type = "OPENETH";
640 break;
641#endif
642#ifdef USE_ETHERNET_LAN8670
644 eth_type = "LAN8670";
645 break;
646#endif
647#ifdef USE_ETHERNET_GENERIC
649 eth_type = "Generic (RGMII)";
650 break;
651#endif
652#ifdef USE_ETHERNET_YT8531
654 eth_type = "YT8531 (RGMII)";
655 break;
656#endif
657
658 default:
659 eth_type = "Unknown";
660 break;
661 }
662
663 ESP_LOGCONFIG(TAG,
664 "Ethernet:\n"
665 " Connected: %s",
666 YESNO(this->is_connected()));
667 this->dump_connect_params_();
668#ifdef USE_ETHERNET_SPI
669#ifdef USE_SPI
670 if (this->spi_parent_ != nullptr) {
671 // Pins and interface come from the shared spi bus; only CS is ours.
672 ESP_LOGCONFIG(TAG, " CS Pin: %u", this->cs_pin_);
673 } else
674#endif
675 {
676 ESP_LOGCONFIG(TAG,
677 " CLK Pin: %u\n"
678 " MISO Pin: %u\n"
679 " MOSI Pin: %u\n"
680 " CS Pin: %u",
681 this->clk_pin_, this->miso_pin_, this->mosi_pin_, this->cs_pin_);
682 const char *spi_interface = "spi3";
683 if (this->interface_ == SPI2_HOST) {
684 spi_interface = "spi2";
685 }
686 ESP_LOGCONFIG(TAG, " Interface: %s", spi_interface);
687 }
688#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
689 if (this->polling_interval_ != 0) {
690 ESP_LOGCONFIG(TAG, " Polling Interval: %" PRIu32 " ms", this->polling_interval_);
691 } else
692#endif
693 {
694 ESP_LOGCONFIG(TAG, " IRQ Pin: %d", this->interrupt_pin_);
695 }
696 ESP_LOGCONFIG(TAG,
697 " Reset Pin: %d\n"
698 " Clock Speed: %d MHz",
699 this->reset_pin_, this->clock_speed_ / 1000000);
700#else
701 if (this->power_pin_ != -1) {
702 ESP_LOGCONFIG(TAG, " Power Pin: %u", this->power_pin_);
703 }
704 ESP_LOGCONFIG(TAG,
705 " CLK Pin: %u\n"
706 " MDC Pin: %u\n"
707 " MDIO Pin: %u\n"
708 " PHY addr: %u",
709 this->clk_pin_, this->mdc_pin_, this->mdio_pin_, this->phy_addr_);
710#endif
711 ESP_LOGCONFIG(TAG, " Type: %s", eth_type);
712#ifdef USE_ETHERNET_RX_PSRAM
713 // Only known once the driver is up; with enable_on_boot: false that is after this dump
714 if (this->ethernet_initialized_) {
715 ESP_LOGCONFIG(TAG, " RX frames: %s",
716 rx_psram_installed ? LOG_STR_LITERAL("PSRAM") : LOG_STR_LITERAL("internal RAM"));
717 }
718#endif
719}
720
722 network::IPAddresses addresses;
723 if (!this->ethernet_initialized_)
724 return addresses; // all-zero IPs
725 esp_netif_ip_info_t ip;
726 esp_err_t err = esp_netif_get_ip_info(this->eth_netif_, &ip);
727 if (err != ESP_OK) {
728 ESP_LOGV(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
729 // TODO: do something smarter
730 // return false;
731 } else {
732 addresses[0] = network::IPAddress(&ip.ip);
733 }
734#if USE_NETWORK_IPV6
735 network::fill_ip6_addresses(this->eth_netif_, addresses);
736#endif /* USE_NETWORK_IPV6 */
737
738 return addresses;
739}
740
743 const ip_addr_t *dns_ip = dns_getserver(num);
744 return dns_ip;
745}
746
747void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event, void *event_data) {
748 const char *event_name;
749
750 switch (event) {
751 case ETHERNET_EVENT_START:
752 event_name = "ETH started";
755 break;
756 case ETHERNET_EVENT_STOP:
757 event_name = "ETH stopped";
760 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
761 break;
762 case ETHERNET_EVENT_CONNECTED:
763 event_name = "ETH connected";
764 // For static IP configurations, GOT_IP event may not fire, so notify IP listeners here
765#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
766 if (global_eth_component->manual_ip_.has_value()) {
768 }
769#endif
770#if USE_NETWORK_IPV6
771 // Start SLAAC on link-up, not after the DHCPv4 lease. This also restores the
772 // link-local after a link flap, which clears the IPv6 addresses.
773 if (esp_err_t ll_err = esp_netif_create_ip6_linklocal(global_eth_component->eth_netif_); ll_err != ESP_OK) {
774 ESP_LOGW(TAG, "esp_netif_create_ip6_linklocal failed on link-up: %s", esp_err_to_name(ll_err));
775 }
776#endif /* USE_NETWORK_IPV6 */
777 break;
778 case ETHERNET_EVENT_DISCONNECTED:
779 event_name = "ETH disconnected";
781 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
782 break;
783 default:
784 return;
785 }
786
787 ESP_LOGV(TAG, "[Ethernet event] %s (num=%" PRId32 ")", event_name, event);
788}
789
790void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
791 void *event_data) {
792 ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
793 const esp_netif_ip_info_t *ip_info = &event->ip_info;
794 ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
796#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
797 global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
798 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
799#else
801 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
802#endif /* USE_NETWORK_IPV6 */
803#ifdef USE_ETHERNET_IP_STATE_LISTENERS
805#endif
806}
807
808#if USE_NETWORK_IPV6
809void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
810 void *event_data) {
811 ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
812 ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
813 // Count the addresses on the interface, not the events: recreating the link-local
814 // after a link flap fires another event for the same address.
815 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
816 global_eth_component->ipv6_count_ = esp_netif_get_all_ip6(global_eth_component->eth_netif_, if_ip6s);
817#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
819 global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
820 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
821#else
823 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
824#endif
825#ifdef USE_ETHERNET_IP_STATE_LISTENERS
827#endif
828}
829#endif /* USE_NETWORK_IPV6 */
830
831#if USE_NETWORK_IPV6
832// Create the link-local address unless the interface already has one, including one still in
833// duplicate address detection: recreating it would restart DAD. esp_netif_get_ip6_linklocal()
834// only reports a preferred address, so ask lwIP for the slot state instead.
836 if (auto *netif = static_cast<struct netif *>(esp_netif_get_netif_impl(this->eth_netif_)); netif != nullptr) {
837 u8_t state;
838 {
840 state = netif_ip6_addr_state(netif, 0);
841 }
842 if (ip6_addr_istentative(state) || ip6_addr_isvalid(state)) {
843 return ESP_OK;
844 }
845 }
846 esp_err_t err = esp_netif_create_ip6_linklocal(this->eth_netif_);
847 if (err == ESP_OK) {
848 ESP_LOGD(TAG, "IPv6 link-local address created");
849 }
850 return err;
851}
852#endif /* USE_NETWORK_IPV6 */
853
855#if USE_NETWORK_IPV6
856 // Retry IPv6 link-local setup if it failed during initial connect
857 // This handles the case where min_ipv6_addr_count is NOT set (or is 0),
858 // allowing us to reach CONNECTED state with just IPv4.
859 // If IPv6 setup failed in start_connect_() because the interface wasn't ready:
860 // - Bootup timing issues (#10281)
861 // - Cable unplugged/network interruption (#10705)
862 // We can now retry since we're in CONNECTED state and the interface is definitely up.
863 if (!this->ipv6_setup_done_) {
864 this->ensure_ip6_linklocal_();
865 // Always set the flag to prevent continuous retries
866 // If IPv6 setup fails here with the interface up and stable, it's
867 // likely a persistent issue (IPv6 disabled at router, hardware
868 // limitation, etc.) that won't be resolved by further retries.
869 // The device continues to work with IPv4.
870 this->ipv6_setup_done_ = true;
871 }
872#endif /* USE_NETWORK_IPV6 */
873}
874
877#if USE_NETWORK_IPV6
878 // Recount rather than zero: addresses that survive a reconnect are not announced again.
879 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
880 global_eth_component->ipv6_count_ = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
881 this->ipv6_setup_done_ = false;
882#endif /* USE_NETWORK_IPV6 */
883 this->connect_begin_ = millis();
884 this->status_set_warning(LOG_STR("waiting for IP configuration"));
885
886 esp_err_t err;
887 err = esp_netif_set_hostname(this->eth_netif_, App.get_name().c_str());
888 if (err != ERR_OK) {
889 ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
890 }
891
892 esp_netif_ip_info_t info;
893#ifdef USE_ETHERNET_MANUAL_IP
894 if (this->manual_ip_.has_value()) {
895 info.ip = this->manual_ip_->static_ip;
896 info.gw = this->manual_ip_->gateway;
897 info.netmask = this->manual_ip_->subnet;
898 } else
899#endif
900 {
901 info.ip.addr = 0;
902 info.gw.addr = 0;
903 info.netmask.addr = 0;
904 }
905
906 esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
907
908 err = esp_netif_dhcpc_get_status(this->eth_netif_, &status);
909 ESPHL_ERROR_CHECK(err, "DHCPC Get Status Failed!");
910
911 ESP_LOGV(TAG, "DHCP Client Status: %d", status);
912
913 err = esp_netif_dhcpc_stop(this->eth_netif_);
914 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
915 ESPHL_ERROR_CHECK(err, "DHCPC stop error");
916 }
917
918 err = esp_netif_set_ip_info(this->eth_netif_, &info);
919 ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");
920
921#ifdef USE_ETHERNET_MANUAL_IP
922 if (this->manual_ip_.has_value()) {
923 // Set DNS through esp_netif so the servers are stored in the netif's own
924 // dns[] array; raw dns_setserver() would be lost when the default-route
925 // arbitration re-applies the default netif's DNS.
926 // Log-only on failure: the link still has a working IP/gateway, so degraded
927 // name resolution does not justify marking the whole component failed.
928 esp_netif_dns_info_t dns{};
929 if (this->manual_ip_->dns1.is_set()) {
930 dns.ip = this->manual_ip_->dns1;
931 err = esp_netif_set_dns_info(this->eth_netif_, ESP_NETIF_DNS_MAIN, &dns);
932 if (err != ESP_OK) {
933 ESP_LOGE(TAG, "Set main DNS failed: %s", esp_err_to_name(err));
934 }
935 }
936 if (this->manual_ip_->dns2.is_set()) {
937 dns.ip = this->manual_ip_->dns2;
938 err = esp_netif_set_dns_info(this->eth_netif_, ESP_NETIF_DNS_BACKUP, &dns);
939 if (err != ESP_OK) {
940 ESP_LOGE(TAG, "Set backup DNS failed: %s", esp_err_to_name(err));
941 }
942 }
943 } else
944#endif
945 {
946 err = esp_netif_dhcpc_start(this->eth_netif_);
947 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
948 ESPHL_ERROR_CHECK(err, "DHCPC start error");
949 }
950 }
951#if USE_NETWORK_IPV6
952 // Attempt to create IPv6 link-local address
953 // We MUST attempt this here, not just in finish_connect_(), because with
954 // min_ipv6_addr_count set, the component won't reach CONNECTED state without IPv6.
955 // However, this may fail with ESP_FAIL if the interface is not up yet:
956 // - At bootup when link isn't ready (#10281)
957 // - After disconnection/cable unplugged (#10705)
958 // We'll retry in finish_connect_() if it fails here.
959 err = this->ensure_ip6_linklocal_();
960 if (err != ESP_OK) {
961 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
962 // This is a programming error, not a transient failure
963 ESPHL_ERROR_CHECK(err, "esp_netif_create_ip6_linklocal invalid parameters");
964 } else {
965 // ESP_FAIL means the interface isn't up yet
966 // This is expected and non-fatal, happens in multiple scenarios:
967 // - During reconnection after network interruptions (#10705)
968 // - At bootup when the link isn't ready yet (#10281)
969 // We'll retry once we reach CONNECTED state and the interface is up
970 ESP_LOGW(TAG, "esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
971 // Don't mark component as failed - this is a transient error
972 }
973 }
974#endif /* USE_NETWORK_IPV6 */
975
976 this->connect_begin_ = millis();
977 this->status_set_warning();
978}
979
981 if (!this->ethernet_initialized_) {
982 ESP_LOGCONFIG(TAG, " uninitialized/disabled");
983 return;
984 }
985 esp_netif_ip_info_t ip;
986 esp_netif_get_ip_info(this->eth_netif_, &ip);
987 const ip_addr_t *dns_ip1;
988 const ip_addr_t *dns_ip2;
989 {
991 dns_ip1 = dns_getserver(0);
992 dns_ip2 = dns_getserver(1);
993 }
994
995 // Use stack buffers for IP address formatting to avoid heap allocations
996 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
997 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
998 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
999 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
1000 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
1001 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
1002 uint16_t link_speed = 10;
1003 switch (this->get_link_speed()) {
1004 case ETH_SPEED_100M:
1005 link_speed = 100;
1006 break;
1007#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 1, 0)
1008 case ETH_SPEED_1000M:
1009 link_speed = 1000;
1010 break;
1011#endif
1012 default:
1013 break;
1014 }
1015 ESP_LOGCONFIG(TAG,
1016 " IP Address: %s\n"
1017 " Hostname: '%s'\n"
1018 " Subnet: %s\n"
1019 " Gateway: %s\n"
1020 " DNS1: %s\n"
1021 " DNS2: %s\n"
1022 " MAC Address: %s\n"
1023 " Is Full Duplex: %s\n"
1024 " Link Speed: %u",
1025 network::IPAddress(&ip.ip).str_to(ip_buf), App.get_name().c_str(),
1026 network::IPAddress(&ip.netmask).str_to(subnet_buf), network::IPAddress(&ip.gw).str_to(gateway_buf),
1027 network::IPAddress(dns_ip1).str_to(dns1_buf), network::IPAddress(dns_ip2).str_to(dns2_buf),
1028 this->get_eth_mac_address_pretty_into_buffer(mac_buf),
1029 YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL), link_speed);
1030
1031#if USE_NETWORK_IPV6
1032 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
1033 uint8_t count = 0;
1034 count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
1035 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
1036 for (int i = 0; i < count; i++) {
1037 ESP_LOGCONFIG(TAG, " IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
1038 }
1039#endif /* USE_NETWORK_IPV6 */
1040}
1041
1043 if (!this->ethernet_initialized_) {
1044 // External callers (mdns, ethernet_info, etc.) may ask for the MAC before/regardless
1045 // of whether ethernet is enabled. Use the configured MAC if set, else the system ETH MAC.
1046 if (this->fixed_mac_.has_value()) {
1047 memcpy(mac, this->fixed_mac_->data(), MAC_ADDRESS_SIZE);
1048 } else {
1049 esp_read_mac(mac, ESP_MAC_ETH);
1050 }
1051 return;
1052 }
1053 esp_err_t err;
1054 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
1055 ESPHL_ERROR_CHECK(err, "ETH_CMD_G_MAC error");
1056}
1057
1059 std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) {
1060 uint8_t mac[MAC_ADDRESS_SIZE];
1062 format_mac_addr_upper(mac, buf.data());
1063 return buf.data();
1064}
1065
1067 if (!this->ethernet_initialized_)
1068 return ETH_DUPLEX_HALF;
1069 esp_err_t err;
1070 eth_duplex_t duplex_mode;
1071 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
1072 ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_DUPLEX_MODE error", ETH_DUPLEX_HALF);
1073 return duplex_mode;
1074}
1075
1077 if (!this->ethernet_initialized_)
1078 return ETH_SPEED_10M;
1079 esp_err_t err;
1081 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_SPEED, &speed);
1082 ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_SPEED error", ETH_SPEED_10M);
1083 return speed;
1084}
1085
1087 ESP_LOGI(TAG, "Powering down ethernet PHY");
1088 if (this->phy_ == nullptr) {
1089 ESP_LOGE(TAG, "Ethernet PHY not assigned");
1090 return false;
1091 }
1092 this->connected_ = false;
1093 this->started_ = false;
1094 // No need to enable_loop() here as this is only called during shutdown/reboot
1095 if (this->phy_->pwrctl(this->phy_, false) != ESP_OK) {
1096 ESP_LOGE(TAG, "Error powering down ethernet PHY");
1097 return false;
1098 }
1099 return true;
1100}
1101
1102#ifndef USE_ETHERNET_SPI
1103
1104#ifdef USE_ETHERNET_KSZ8081
1105constexpr uint8_t KSZ80XX_PC2R_REG_ADDR = 0x1F;
1106
1108 esp_err_t err;
1109
1110 uint32_t phy_control_2;
1111 err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
1112 ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
1113#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
1114 char hex_buf[format_hex_pretty_size(PHY_REG_SIZE)];
1115#endif
1116 ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty_to(hex_buf, (uint8_t *) &phy_control_2, PHY_REG_SIZE));
1117
1118 /*
1119 * Bit 7 is `RMII Reference Clock Select`. Default is `0`.
1120 * KSZ8081RNA:
1121 * 0 - clock input to XI (Pin 8) is 25 MHz for RMII - 25 MHz clock mode.
1122 * 1 - clock input to XI (Pin 8) is 50 MHz for RMII - 50 MHz clock mode.
1123 * KSZ8081RND:
1124 * 0 - clock input to XI (Pin 8) is 50 MHz for RMII - 50 MHz clock mode.
1125 * 1 - clock input to XI (Pin 8) is 25 MHz (driven clock only, not a crystal) for RMII - 25 MHz clock mode.
1126 */
1127 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
1128 phy_control_2 |= 1 << 7;
1129 err = mac->write_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
1130 ESPHL_ERROR_CHECK(err, "Write PHY Control 2 failed");
1131 err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
1132 ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
1133 ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s",
1134 format_hex_pretty_to(hex_buf, (uint8_t *) &phy_control_2, PHY_REG_SIZE));
1135 }
1136}
1137#endif // USE_ETHERNET_KSZ8081
1138
1139#ifdef ESPHOME_ETHERNET_PHY_REGISTER_COUNT
1140void EthernetComponent::write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data) {
1141 esp_err_t err;
1142
1143#ifdef USE_ETHERNET_RTL8201
1144 constexpr uint8_t eth_phy_psr_reg_addr = 0x1F;
1145 if (this->type_ == ETHERNET_TYPE_RTL8201 && register_data.page) {
1146 ESP_LOGD(TAG, "Select PHY Register Page: 0x%02" PRIX32, register_data.page);
1147 err = mac->write_phy_reg(mac, this->phy_addr_, eth_phy_psr_reg_addr, register_data.page);
1148 ESPHL_ERROR_CHECK(err, "Select PHY Register page failed");
1149 }
1150#endif
1151
1152 ESP_LOGD(TAG, "Writing PHY reg 0x%02" PRIX32 " = 0x%04" PRIX32, register_data.address, register_data.value);
1153 err = mac->write_phy_reg(mac, this->phy_addr_, register_data.address, register_data.value);
1154 ESPHL_ERROR_CHECK(err, "Writing PHY Register failed");
1155
1156#ifdef USE_ETHERNET_RTL8201
1157 if (this->type_ == ETHERNET_TYPE_RTL8201 && register_data.page) {
1158 ESP_LOGD(TAG, "Select PHY Register Page 0x00");
1159 err = mac->write_phy_reg(mac, this->phy_addr_, eth_phy_psr_reg_addr, 0x0);
1160 ESPHL_ERROR_CHECK(err, "Select PHY Register Page 0 failed");
1161 }
1162#endif
1163}
1164#endif // ESPHOME_ETHERNET_PHY_REGISTER_COUNT
1165
1166#ifdef USE_ETHERNET_YT8531
1168 esp_err_t err;
1169
1170 // The YT8531 disables auto-negotiation on hardware reset (undocumented behavior), and the
1171 // generic 802.3 driver only resets the PHY, so re-enable it (this also updates the driver's
1172 // cached auto-nego state used by esp_eth_start()).
1173 bool autoneg_enable = true;
1174 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
1175 ESPHL_ERROR_CHECK(err, "YT8531 enable auto-negotiation failed");
1176
1177 // RGMII needs ~2 ns Tx and Rx clock delays for reliable data sampling. These are set through
1178 // the YT8531 extended-register interface: write the ext-register address to 0x1E, then
1179 // read/modify/write its value via 0x1F.
1180 esp_eth_phy_reg_rw_data_t phy_reg;
1181 uint32_t reg_val;
1182 phy_reg.reg_value_p = &reg_val;
1183
1184 // RX ~2 ns coarse delay: EXT_CHIP_CONFIG (0xA001), set rxc_dly_en (bit 8).
1185 reg_val = 0xA001;
1186 phy_reg.reg_addr = 0x1E;
1187 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1188 ESPHL_ERROR_CHECK(err, "YT8531 select Chip_Config failed");
1189 phy_reg.reg_addr = 0x1F;
1190 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1191 ESPHL_ERROR_CHECK(err, "YT8531 read Chip_Config failed");
1192 reg_val |= (1U << 8);
1193 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1194 ESPHL_ERROR_CHECK(err, "YT8531 write Chip_Config failed");
1195
1196 // TX ~2 ns delay: EXT_RGMII_CONFIG1 (0xA003), tx_delay_sel[3:0] and tx_delay_sel_fe[7:4] = 13.
1197 reg_val = 0xA003;
1198 phy_reg.reg_addr = 0x1E;
1199 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1200 ESPHL_ERROR_CHECK(err, "YT8531 select RGMII_Config1 failed");
1201 phy_reg.reg_addr = 0x1F;
1202 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1203 ESPHL_ERROR_CHECK(err, "YT8531 read RGMII_Config1 failed");
1204 reg_val = (reg_val & ~0x00FFU) | (13U << 4) | (13U << 0);
1205 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1206 ESPHL_ERROR_CHECK(err, "YT8531 write RGMII_Config1 failed");
1207}
1208#endif
1209
1210#endif
1211
1212} // namespace esphome::ethernet
1213
1214#endif // USE_ETHERNET && USE_ESP32
uint8_t status
Definition bl0942.h:8
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void enable_loop()
Enable this component's loop.
Definition component.h:248
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Definition component.h:291
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:2107
constexpr const char * c_str() const
Definition string_ref.h:78
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:533
StaticVector< PHYRegister, ESPHOME_ETHERNET_PHY_REGISTER_COUNT > phy_registers_
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void yt8531_phy_init_()
Apply YT8531-specific config: re-enable auto-negotiation (disabled on reset) and set the RGMII Tx/Rx ...
void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data)
Set arbitratry PHY registers from config.
static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void log_error_and_mark_failed_(esp_err_t err, const char *message)
network::IPAddress get_dns_address(uint8_t num)
void ksz8081_set_clock_reference_(esp_eth_mac_t *mac)
Set RMII Reference Clock Select bit for KSZ8081.
optional< std::array< uint8_t, MAC_ADDRESS_SIZE > > fixed_mac_
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
const char * get_eth_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
SPIInterface get_interface() const
Definition spi.h:362
const LogString * message
Definition component.cpp:35
eth_esp32_emac_config_t eth_esp32_emac_default_config(void)
int speed
Definition fan.h:3
bool state
Definition fan.h:2
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "ethernet")
constexpr uint8_t KSZ80XX_PC2R_REG_ADDR
void install_w5500_async_spi(eth_w5500_config_t &config)
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
EthernetComponent * global_eth_component
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:292
void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses)
Copies the IPv6 addresses of an esp_netif into addresses[1..], warning once when some do not fit.
Definition util.cpp:13
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:498
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1533
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1600
struct esp_netif_obj esp_netif_t
static void uint32_t
char * str_to(char *buf) const
Definition ip_address.h:101
uint16_t length
Definition tt21100.cpp:0
uint8_t event_id
Definition tt21100.cpp:3
SemaphoreHandle_t lock
spi_device_handle_t handle