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