ESPHome 2025.10.3
Loading...
Searching...
No Matches
ethernet_component.cpp
Go to the documentation of this file.
3#include "esphome/core/log.h"
4#include "esphome/core/util.h"
5
6#ifdef USE_ESP32
7
8#include <lwip/dns.h>
9#include <cinttypes>
10#include "esp_event.h"
11
12#ifdef USE_ETHERNET_LAN8670
13#include "esp_eth_phy_lan867x.h"
14#endif
15
16#ifdef USE_ETHERNET_SPI
17#include <driver/gpio.h>
18#include <driver/spi_master.h>
19#endif
20
21namespace esphome {
22namespace ethernet {
23
24#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
25// work around IDF compile issue on P4 https://github.com/espressif/esp-idf/pull/15637
26#ifdef USE_ESP32_VARIANT_ESP32P4
27#undef ETH_ESP32_EMAC_DEFAULT_CONFIG
28#define ETH_ESP32_EMAC_DEFAULT_CONFIG() \
29 { \
30 .smi_gpio = {.mdc_num = 31, .mdio_num = 52}, .interface = EMAC_DATA_INTERFACE_RMII, \
31 .clock_config = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) 50}}, \
32 .dma_burst_len = ETH_DMA_BURST_LEN_32, .intr_priority = 0, \
33 .emac_dataif_gpio = \
34 {.rmii = {.tx_en_num = 49, .txd0_num = 34, .txd1_num = 35, .crs_dv_num = 28, .rxd0_num = 29, .rxd1_num = 30}}, \
35 .clock_config_out_in = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) -1}}, \
36 }
37#endif
38#endif
39
40static const char *const TAG = "ethernet";
41
42EthernetComponent *global_eth_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
43
45 ESP_LOGE(TAG, "%s: (%d) %s", message, err, esp_err_to_name(err));
46 this->mark_failed();
47}
48
49#define ESPHL_ERROR_CHECK(err, message) \
50 if ((err) != ESP_OK) { \
51 this->log_error_and_mark_failed_(err, message); \
52 return; \
53 }
54
55#define ESPHL_ERROR_CHECK_RET(err, message, ret) \
56 if ((err) != ESP_OK) { \
57 this->log_error_and_mark_failed_(err, message); \
58 return ret; \
59 }
60
61EthernetComponent::EthernetComponent() { global_eth_component = this; }
62
64 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
65 // Delay here to allow power to stabilise before Ethernet is initialized.
66 delay(300); // NOLINT
67 }
68
69 esp_err_t err;
70
71#ifdef USE_ETHERNET_SPI
72 // Install GPIO ISR handler to be able to service SPI Eth modules interrupts
73 gpio_install_isr_service(0);
74
75 spi_bus_config_t buscfg = {
76 .mosi_io_num = this->mosi_pin_,
77 .miso_io_num = this->miso_pin_,
78 .sclk_io_num = this->clk_pin_,
79 .quadwp_io_num = -1,
80 .quadhd_io_num = -1,
81 .data4_io_num = -1,
82 .data5_io_num = -1,
83 .data6_io_num = -1,
84 .data7_io_num = -1,
85 .max_transfer_sz = 0,
86 .flags = 0,
87 .intr_flags = 0,
88 };
89
90#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
91 defined(USE_ESP32_VARIANT_ESP32C6)
92 auto host = SPI2_HOST;
93#else
94 auto host = SPI3_HOST;
95#endif
96
97 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
98 ESPHL_ERROR_CHECK(err, "SPI bus initialize error");
99#endif
100
101 err = esp_netif_init();
102 ESPHL_ERROR_CHECK(err, "ETH netif init error");
103 err = esp_event_loop_create_default();
104 ESPHL_ERROR_CHECK(err, "ETH event loop error");
105
106 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
107 this->eth_netif_ = esp_netif_new(&cfg);
108
109 // Init MAC and PHY configs to default
110 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
111 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
112
113#ifdef USE_ETHERNET_SPI // Configure SPI interface and Ethernet driver for specific SPI module
114 spi_device_interface_config_t devcfg = {
115 .command_bits = 0,
116 .address_bits = 0,
117 .dummy_bits = 0,
118 .mode = 0,
119 .duty_cycle_pos = 0,
120 .cs_ena_pretrans = 0,
121 .cs_ena_posttrans = 0,
122 .clock_speed_hz = this->clock_speed_,
123 .input_delay_ns = 0,
124 .spics_io_num = this->cs_pin_,
125 .flags = 0,
126 .queue_size = 20,
127 .pre_cb = nullptr,
128 .post_cb = nullptr,
129 };
130
131#if CONFIG_ETH_SPI_ETHERNET_W5500
132 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
133#endif
134#if CONFIG_ETH_SPI_ETHERNET_DM9051
135 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
136#endif
137
138#if CONFIG_ETH_SPI_ETHERNET_W5500
139 w5500_config.int_gpio_num = this->interrupt_pin_;
140#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
141 w5500_config.poll_period_ms = this->polling_interval_;
142#endif
143#endif
144
145#if CONFIG_ETH_SPI_ETHERNET_DM9051
146 dm9051_config.int_gpio_num = this->interrupt_pin_;
147#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
148 dm9051_config.poll_period_ms = this->polling_interval_;
149#endif
150#endif
151
152 phy_config.phy_addr = this->phy_addr_spi_;
153 phy_config.reset_gpio_num = this->reset_pin_;
154
155 esp_eth_mac_t *mac = nullptr;
156#elif defined(USE_ETHERNET_OPENETH)
157 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
158#else
159 phy_config.phy_addr = this->phy_addr_;
160 phy_config.reset_gpio_num = this->power_pin_;
161
162 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
163#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
164 esp32_emac_config.smi_gpio.mdc_num = this->mdc_pin_;
165 esp32_emac_config.smi_gpio.mdio_num = this->mdio_pin_;
166#else
167 esp32_emac_config.smi_mdc_gpio_num = this->mdc_pin_;
168 esp32_emac_config.smi_mdio_gpio_num = this->mdio_pin_;
169#endif
170 esp32_emac_config.clock_config.rmii.clock_mode = this->clk_mode_;
171 esp32_emac_config.clock_config.rmii.clock_gpio = (emac_rmii_clock_gpio_t) this->clk_pin_;
172
173 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
174#endif
175
176 switch (this->type_) {
177#ifdef USE_ETHERNET_OPENETH
179 phy_config.autonego_timeout_ms = 1000;
180 this->phy_ = esp_eth_phy_new_dp83848(&phy_config);
181 break;
182 }
183#endif
184#if CONFIG_ETH_USE_ESP32_EMAC
186 this->phy_ = esp_eth_phy_new_lan87xx(&phy_config);
187 break;
188 }
190 this->phy_ = esp_eth_phy_new_rtl8201(&phy_config);
191 break;
192 }
194 this->phy_ = esp_eth_phy_new_dp83848(&phy_config);
195 break;
196 }
197 case ETHERNET_TYPE_IP101: {
198 this->phy_ = esp_eth_phy_new_ip101(&phy_config);
199 break;
200 }
202 this->phy_ = esp_eth_phy_new_jl1101(&phy_config);
203 break;
204 }
207 this->phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
208 break;
209 }
210#ifdef USE_ETHERNET_LAN8670
212 this->phy_ = esp_eth_phy_new_lan867x(&phy_config);
213 break;
214 }
215#endif
216#endif
217#ifdef USE_ETHERNET_SPI
218#if CONFIG_ETH_SPI_ETHERNET_W5500
219 case ETHERNET_TYPE_W5500: {
220 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
221 this->phy_ = esp_eth_phy_new_w5500(&phy_config);
222 break;
223 }
224#endif
225#if CONFIG_ETH_SPI_ETHERNET_DM9051
227 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
228 this->phy_ = esp_eth_phy_new_dm9051(&phy_config);
229 break;
230 }
231#endif
232#endif
233 default: {
234 this->mark_failed();
235 return;
236 }
237 }
238
239 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->phy_);
240 this->eth_handle_ = nullptr;
241 err = esp_eth_driver_install(&eth_config, &this->eth_handle_);
242 ESPHL_ERROR_CHECK(err, "ETH driver install error");
243
244#ifndef USE_ETHERNET_SPI
245#ifdef USE_ETHERNET_KSZ8081
246 if (this->type_ == ETHERNET_TYPE_KSZ8081RNA && this->clk_mode_ == EMAC_CLK_OUT) {
247 // KSZ8081RNA default is incorrect. It expects a 25MHz clock instead of the 50MHz we provide.
249 }
250#endif // USE_ETHERNET_KSZ8081
251
252 for (const auto &phy_register : this->phy_registers_) {
253 this->write_phy_register_(mac, phy_register);
254 }
255#endif
256
257 // use ESP internal eth mac
258 uint8_t mac_addr[6];
259 if (this->fixed_mac_.has_value()) {
260 memcpy(mac_addr, this->fixed_mac_->data(), 6);
261 } else {
262 esp_read_mac(mac_addr, ESP_MAC_ETH);
263 }
264 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
265 ESPHL_ERROR_CHECK(err, "set mac address error");
266
267 /* attach Ethernet driver to TCP/IP stack */
268 err = esp_netif_attach(this->eth_netif_, esp_eth_new_netif_glue(this->eth_handle_));
269 ESPHL_ERROR_CHECK(err, "ETH netif attach error");
270
271 // Register user defined event handers
272 err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &EthernetComponent::eth_event_handler, nullptr);
273 ESPHL_ERROR_CHECK(err, "ETH event handler register error");
274 err = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &EthernetComponent::got_ip_event_handler, nullptr);
275 ESPHL_ERROR_CHECK(err, "GOT IP event handler register error");
276#if USE_NETWORK_IPV6
277 err = esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &EthernetComponent::got_ip6_event_handler, nullptr);
278 ESPHL_ERROR_CHECK(err, "GOT IPv6 event handler register error");
279#endif /* USE_NETWORK_IPV6 */
280
281 /* start Ethernet driver state machine */
282 err = esp_eth_start(this->eth_handle_);
283 ESPHL_ERROR_CHECK(err, "ETH start error");
284}
285
287 const uint32_t now = App.get_loop_component_start_time();
288
289 switch (this->state_) {
291 if (this->started_) {
292 ESP_LOGI(TAG, "Starting connection");
294 this->start_connect_();
295 }
296 break;
298 if (!this->started_) {
299 ESP_LOGI(TAG, "Stopped connection");
301 } else if (this->connected_) {
302 // connection established
303 ESP_LOGI(TAG, "Connected");
305
306 this->dump_connect_params_();
307 this->status_clear_warning();
308 } else if (now - this->connect_begin_ > 15000) {
309 ESP_LOGW(TAG, "Connecting failed; reconnecting");
310 this->start_connect_();
311 }
312 break;
314 if (!this->started_) {
315 ESP_LOGI(TAG, "Stopped connection");
317 } else if (!this->connected_) {
318 ESP_LOGW(TAG, "Connection lost; reconnecting");
320 this->start_connect_();
321 } else {
322 this->finish_connect_();
323 // When connected and stable, disable the loop to save CPU cycles
324 this->disable_loop();
325 }
326 break;
327 }
328}
329
331 const char *eth_type;
332 switch (this->type_) {
334 eth_type = "LAN8720";
335 break;
336
338 eth_type = "RTL8201";
339 break;
340
342 eth_type = "DP83848";
343 break;
344
346 eth_type = "IP101";
347 break;
348
350 eth_type = "JL1101";
351 break;
352
354 eth_type = "KSZ8081";
355 break;
356
358 eth_type = "KSZ8081RNA";
359 break;
360
362 eth_type = "W5500";
363 break;
364
366 eth_type = "OPENETH";
367 break;
368
370 eth_type = "DM9051";
371 break;
372
373#ifdef USE_ETHERNET_LAN8670
375 eth_type = "LAN8670";
376 break;
377#endif
378
379 default:
380 eth_type = "Unknown";
381 break;
382 }
383
384 ESP_LOGCONFIG(TAG, "Ethernet:");
385 this->dump_connect_params_();
386#ifdef USE_ETHERNET_SPI
387 ESP_LOGCONFIG(TAG,
388 " CLK Pin: %u\n"
389 " MISO Pin: %u\n"
390 " MOSI Pin: %u\n"
391 " CS Pin: %u",
392 this->clk_pin_, this->miso_pin_, this->mosi_pin_, this->cs_pin_);
393#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
394 if (this->polling_interval_ != 0) {
395 ESP_LOGCONFIG(TAG, " Polling Interval: %lu ms", this->polling_interval_);
396 } else
397#endif
398 {
399 ESP_LOGCONFIG(TAG, " IRQ Pin: %d", this->interrupt_pin_);
400 }
401 ESP_LOGCONFIG(TAG,
402 " Reset Pin: %d\n"
403 " Clock Speed: %d MHz",
404 this->reset_pin_, this->clock_speed_ / 1000000);
405#else
406 if (this->power_pin_ != -1) {
407 ESP_LOGCONFIG(TAG, " Power Pin: %u", this->power_pin_);
408 }
409 ESP_LOGCONFIG(TAG,
410 " CLK Pin: %u\n"
411 " MDC Pin: %u\n"
412 " MDIO Pin: %u\n"
413 " PHY addr: %u",
414 this->clk_pin_, this->mdc_pin_, this->mdio_pin_, this->phy_addr_);
415#endif
416 ESP_LOGCONFIG(TAG, " Type: %s", eth_type);
417}
418
420
422
424 network::IPAddresses addresses;
425 esp_netif_ip_info_t ip;
426 esp_err_t err = esp_netif_get_ip_info(this->eth_netif_, &ip);
427 if (err != ESP_OK) {
428 ESP_LOGV(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
429 // TODO: do something smarter
430 // return false;
431 } else {
432 addresses[0] = network::IPAddress(&ip.ip);
433 }
434#if USE_NETWORK_IPV6
435 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
436 uint8_t count = 0;
437 count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
438 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
439 for (int i = 0; i < count; i++) {
440 addresses[i + 1] = network::IPAddress(&if_ip6s[i]);
441 }
442#endif /* USE_NETWORK_IPV6 */
443
444 return addresses;
445}
446
448 LwIPLock lock;
449 const ip_addr_t *dns_ip = dns_getserver(num);
450 return dns_ip;
451}
452
453void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event, void *event_data) {
454 const char *event_name;
455
456 switch (event) {
457 case ETHERNET_EVENT_START:
458 event_name = "ETH started";
461 break;
462 case ETHERNET_EVENT_STOP:
463 event_name = "ETH stopped";
466 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
467 break;
468 case ETHERNET_EVENT_CONNECTED:
469 event_name = "ETH connected";
470 break;
471 case ETHERNET_EVENT_DISCONNECTED:
472 event_name = "ETH disconnected";
474 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
475 break;
476 default:
477 return;
478 }
479
480 ESP_LOGV(TAG, "[Ethernet event] %s (num=%" PRId32 ")", event_name, event);
481}
482
483void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
484 void *event_data) {
485 ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
486 const esp_netif_ip_info_t *ip_info = &event->ip_info;
487 ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
489#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
490 global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
491 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
492#else
494 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
495#endif /* USE_NETWORK_IPV6 */
496}
497
498#if USE_NETWORK_IPV6
499void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
500 void *event_data) {
501 ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
502 ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
504#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
506 global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
507 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
508#else
510 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
511#endif
512}
513#endif /* USE_NETWORK_IPV6 */
514
516#if USE_NETWORK_IPV6
517 // Retry IPv6 link-local setup if it failed during initial connect
518 // This handles the case where min_ipv6_addr_count is NOT set (or is 0),
519 // allowing us to reach CONNECTED state with just IPv4.
520 // If IPv6 setup failed in start_connect_() because the interface wasn't ready:
521 // - Bootup timing issues (#10281)
522 // - Cable unplugged/network interruption (#10705)
523 // We can now retry since we're in CONNECTED state and the interface is definitely up.
524 if (!this->ipv6_setup_done_) {
525 esp_err_t err = esp_netif_create_ip6_linklocal(this->eth_netif_);
526 if (err == ESP_OK) {
527 ESP_LOGD(TAG, "IPv6 link-local address created (retry succeeded)");
528 }
529 // Always set the flag to prevent continuous retries
530 // If IPv6 setup fails here with the interface up and stable, it's
531 // likely a persistent issue (IPv6 disabled at router, hardware
532 // limitation, etc.) that won't be resolved by further retries.
533 // The device continues to work with IPv4.
534 this->ipv6_setup_done_ = true;
535 }
536#endif /* USE_NETWORK_IPV6 */
537}
538
540 global_eth_component->got_ipv4_address_ = false;
541#if USE_NETWORK_IPV6
543 this->ipv6_setup_done_ = false;
544#endif /* USE_NETWORK_IPV6 */
545 this->connect_begin_ = millis();
546 this->status_set_warning(LOG_STR("waiting for IP configuration"));
547
548 esp_err_t err;
549 err = esp_netif_set_hostname(this->eth_netif_, App.get_name().c_str());
550 if (err != ERR_OK) {
551 ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
552 }
553
554 esp_netif_ip_info_t info;
555 if (this->manual_ip_.has_value()) {
556 info.ip = this->manual_ip_->static_ip;
557 info.gw = this->manual_ip_->gateway;
558 info.netmask = this->manual_ip_->subnet;
559 } else {
560 info.ip.addr = 0;
561 info.gw.addr = 0;
562 info.netmask.addr = 0;
563 }
564
565 esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
566
567 err = esp_netif_dhcpc_get_status(this->eth_netif_, &status);
568 ESPHL_ERROR_CHECK(err, "DHCPC Get Status Failed!");
569
570 ESP_LOGV(TAG, "DHCP Client Status: %d", status);
571
572 err = esp_netif_dhcpc_stop(this->eth_netif_);
573 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
574 ESPHL_ERROR_CHECK(err, "DHCPC stop error");
575 }
576
577 err = esp_netif_set_ip_info(this->eth_netif_, &info);
578 ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");
579
580 if (this->manual_ip_.has_value()) {
581 LwIPLock lock;
582 if (this->manual_ip_->dns1.is_set()) {
583 ip_addr_t d;
584 d = this->manual_ip_->dns1;
585 dns_setserver(0, &d);
586 }
587 if (this->manual_ip_->dns2.is_set()) {
588 ip_addr_t d;
589 d = this->manual_ip_->dns2;
590 dns_setserver(1, &d);
591 }
592 } else {
593 err = esp_netif_dhcpc_start(this->eth_netif_);
594 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
595 ESPHL_ERROR_CHECK(err, "DHCPC start error");
596 }
597 }
598#if USE_NETWORK_IPV6
599 // Attempt to create IPv6 link-local address
600 // We MUST attempt this here, not just in finish_connect_(), because with
601 // min_ipv6_addr_count set, the component won't reach CONNECTED state without IPv6.
602 // However, this may fail with ESP_FAIL if the interface is not up yet:
603 // - At bootup when link isn't ready (#10281)
604 // - After disconnection/cable unplugged (#10705)
605 // We'll retry in finish_connect_() if it fails here.
606 err = esp_netif_create_ip6_linklocal(this->eth_netif_);
607 if (err != ESP_OK) {
608 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
609 // This is a programming error, not a transient failure
610 ESPHL_ERROR_CHECK(err, "esp_netif_create_ip6_linklocal invalid parameters");
611 } else {
612 // ESP_FAIL means the interface isn't up yet
613 // This is expected and non-fatal, happens in multiple scenarios:
614 // - During reconnection after network interruptions (#10705)
615 // - At bootup when the link isn't ready yet (#10281)
616 // We'll retry once we reach CONNECTED state and the interface is up
617 ESP_LOGW(TAG, "esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
618 // Don't mark component as failed - this is a transient error
619 }
620 }
621#endif /* USE_NETWORK_IPV6 */
622
623 this->connect_begin_ = millis();
624 this->status_set_warning();
625}
626
628
630 esp_netif_ip_info_t ip;
631 esp_netif_get_ip_info(this->eth_netif_, &ip);
632 const ip_addr_t *dns_ip1;
633 const ip_addr_t *dns_ip2;
634 {
635 LwIPLock lock;
636 dns_ip1 = dns_getserver(0);
637 dns_ip2 = dns_getserver(1);
638 }
639
640 ESP_LOGCONFIG(TAG,
641 " IP Address: %s\n"
642 " Hostname: '%s'\n"
643 " Subnet: %s\n"
644 " Gateway: %s\n"
645 " DNS1: %s\n"
646 " DNS2: %s",
647 network::IPAddress(&ip.ip).str().c_str(), App.get_name().c_str(),
648 network::IPAddress(&ip.netmask).str().c_str(), network::IPAddress(&ip.gw).str().c_str(),
649 network::IPAddress(dns_ip1).str().c_str(), network::IPAddress(dns_ip2).str().c_str());
650
651#if USE_NETWORK_IPV6
652 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
653 uint8_t count = 0;
654 count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
655 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
656 for (int i = 0; i < count; i++) {
657 ESP_LOGCONFIG(TAG, " IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
658 }
659#endif /* USE_NETWORK_IPV6 */
660
661 ESP_LOGCONFIG(TAG,
662 " MAC Address: %s\n"
663 " Is Full Duplex: %s\n"
664 " Link Speed: %u",
665 this->get_eth_mac_address_pretty().c_str(), YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL),
666 this->get_link_speed() == ETH_SPEED_100M ? 100 : 10);
667}
668
669#ifdef USE_ETHERNET_SPI
670void EthernetComponent::set_clk_pin(uint8_t clk_pin) { this->clk_pin_ = clk_pin; }
671void EthernetComponent::set_miso_pin(uint8_t miso_pin) { this->miso_pin_ = miso_pin; }
672void EthernetComponent::set_mosi_pin(uint8_t mosi_pin) { this->mosi_pin_ = mosi_pin; }
673void EthernetComponent::set_cs_pin(uint8_t cs_pin) { this->cs_pin_ = cs_pin; }
674void EthernetComponent::set_interrupt_pin(uint8_t interrupt_pin) { this->interrupt_pin_ = interrupt_pin; }
675void EthernetComponent::set_reset_pin(uint8_t reset_pin) { this->reset_pin_ = reset_pin; }
676void EthernetComponent::set_clock_speed(int clock_speed) { this->clock_speed_ = clock_speed; }
677#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
678void EthernetComponent::set_polling_interval(uint32_t polling_interval) { this->polling_interval_ = polling_interval; }
679#endif
680#else
681void EthernetComponent::set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; }
682void EthernetComponent::set_power_pin(int power_pin) { this->power_pin_ = power_pin; }
683void EthernetComponent::set_mdc_pin(uint8_t mdc_pin) { this->mdc_pin_ = mdc_pin; }
684void EthernetComponent::set_mdio_pin(uint8_t mdio_pin) { this->mdio_pin_ = mdio_pin; }
685void EthernetComponent::set_clk_pin(uint8_t clk_pin) { this->clk_pin_ = clk_pin; }
686void EthernetComponent::set_clk_mode(emac_rmii_clock_mode_t clk_mode) { this->clk_mode_ = clk_mode; }
687void EthernetComponent::add_phy_register(PHYRegister register_value) { this->phy_registers_.push_back(register_value); }
688#endif
690void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; }
691
693 if (this->use_address_.empty()) {
694 return App.get_name() + ".local";
695 }
696 return this->use_address_;
697}
698
699void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
700
702 esp_err_t err;
703 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
704 ESPHL_ERROR_CHECK(err, "ETH_CMD_G_MAC error");
705}
706
708 uint8_t mac[6];
710 char buf[18];
711 format_mac_addr_upper(mac, buf);
712 return std::string(buf);
713}
714
716 esp_err_t err;
717 eth_duplex_t duplex_mode;
718 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
719 ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_DUPLEX_MODE error", ETH_DUPLEX_HALF);
720 return duplex_mode;
721}
722
724 esp_err_t err;
725 eth_speed_t speed;
726 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_SPEED, &speed);
727 ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_SPEED error", ETH_SPEED_10M);
728 return speed;
729}
730
732 ESP_LOGI(TAG, "Powering down ethernet PHY");
733 if (this->phy_ == nullptr) {
734 ESP_LOGE(TAG, "Ethernet PHY not assigned");
735 return false;
736 }
737 this->connected_ = false;
738 this->started_ = false;
739 // No need to enable_loop() here as this is only called during shutdown/reboot
740 if (this->phy_->pwrctl(this->phy_, false) != ESP_OK) {
741 ESP_LOGE(TAG, "Error powering down ethernet PHY");
742 return false;
743 }
744 return true;
745}
746
747#ifndef USE_ETHERNET_SPI
748
749#ifdef USE_ETHERNET_KSZ8081
750constexpr uint8_t KSZ80XX_PC2R_REG_ADDR = 0x1F;
751
753 esp_err_t err;
754
755 uint32_t phy_control_2;
756 err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
757 ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
758 ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
759
760 /*
761 * Bit 7 is `RMII Reference Clock Select`. Default is `0`.
762 * KSZ8081RNA:
763 * 0 - clock input to XI (Pin 8) is 25 MHz for RMII - 25 MHz clock mode.
764 * 1 - clock input to XI (Pin 8) is 50 MHz for RMII - 50 MHz clock mode.
765 * KSZ8081RND:
766 * 0 - clock input to XI (Pin 8) is 50 MHz for RMII - 50 MHz clock mode.
767 * 1 - clock input to XI (Pin 8) is 25 MHz (driven clock only, not a crystal) for RMII - 25 MHz clock mode.
768 */
769 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
770 phy_control_2 |= 1 << 7;
771 err = mac->write_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
772 ESPHL_ERROR_CHECK(err, "Write PHY Control 2 failed");
773 err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
774 ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
775 ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
776 }
777}
778#endif // USE_ETHERNET_KSZ8081
779
780void EthernetComponent::write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data) {
781 esp_err_t err;
782 constexpr uint8_t eth_phy_psr_reg_addr = 0x1F;
783
784 if (this->type_ == ETHERNET_TYPE_RTL8201 && register_data.page) {
785 ESP_LOGD(TAG, "Select PHY Register Page: 0x%02" PRIX32, register_data.page);
786 err = mac->write_phy_reg(mac, this->phy_addr_, eth_phy_psr_reg_addr, register_data.page);
787 ESPHL_ERROR_CHECK(err, "Select PHY Register page failed");
788 }
789
790 ESP_LOGD(TAG, "Writing to PHY Register Address: 0x%02" PRIX32, register_data.address);
791 ESP_LOGD(TAG, "Writing to PHY Register Value: 0x%04" PRIX32, register_data.value);
792 err = mac->write_phy_reg(mac, this->phy_addr_, register_data.address, register_data.value);
793 ESPHL_ERROR_CHECK(err, "Writing PHY Register failed");
794
795 if (this->type_ == ETHERNET_TYPE_RTL8201 && register_data.page) {
796 ESP_LOGD(TAG, "Select PHY Register Page 0x00");
797 err = mac->write_phy_reg(mac, this->phy_addr_, eth_phy_psr_reg_addr, 0x0);
798 ESPHL_ERROR_CHECK(err, "Select PHY Register Page 0 failed");
799 }
800}
801
802#endif
803
804} // namespace ethernet
805} // namespace esphome
806
807#endif // USE_ESP32
uint8_t status
Definition bl0942.h:8
const std::string & 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.
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:806
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 set_manual_ip(const ManualIP &manual_ip)
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.
void set_use_address(const std::string &use_address)
void set_interrupt_pin(uint8_t interrupt_pin)
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
optional< std::array< uint8_t, 6 > > fixed_mac_
void set_clk_mode(emac_rmii_clock_mode_t clk_mode)
bool has_value() const
Definition optional.h:92
const char * message
Definition component.cpp:38
uint16_t type
int speed
Definition fan.h:1
in_addr ip_addr_t
Definition ip_address.h:22
constexpr uint8_t KSZ80XX_PC2R_REG_ADDR
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:144
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase)
Definition helpers.h:415
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Definition helpers.cpp:293
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:30
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:29
Application App
Global storage of Application pointer - only one Application can exist.
std::string str() const
Definition ip_address.h:52
uint8_t event_id
Definition tt21100.cpp:3