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