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