ESPHome 2025.9.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_SPI
13#include <driver/gpio.h>
14#include <driver/spi_master.h>
15#endif
16
17namespace esphome {
18namespace ethernet {
19
20#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
21// work around IDF compile issue on P4 https://github.com/espressif/esp-idf/pull/15637
22#ifdef USE_ESP32_VARIANT_ESP32P4
23#undef ETH_ESP32_EMAC_DEFAULT_CONFIG
24#define ETH_ESP32_EMAC_DEFAULT_CONFIG() \
25 { \
26 .smi_gpio = {.mdc_num = 31, .mdio_num = 52}, .interface = EMAC_DATA_INTERFACE_RMII, \
27 .clock_config = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) 50}}, \
28 .dma_burst_len = ETH_DMA_BURST_LEN_32, .intr_priority = 0, \
29 .emac_dataif_gpio = \
30 {.rmii = {.tx_en_num = 49, .txd0_num = 34, .txd1_num = 35, .crs_dv_num = 28, .rxd0_num = 29, .rxd1_num = 30}}, \
31 .clock_config_out_in = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) -1}}, \
32 }
33#endif
34#endif
35
36static const char *const TAG = "ethernet";
37
38EthernetComponent *global_eth_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
39
40#define ESPHL_ERROR_CHECK(err, message) \
41 if ((err) != ESP_OK) { \
42 ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \
43 this->mark_failed(); \
44 return; \
45 }
46
47#define ESPHL_ERROR_CHECK_RET(err, message, ret) \
48 if ((err) != ESP_OK) { \
49 ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \
50 this->mark_failed(); \
51 return ret; \
52 }
53
55
57 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
58 // Delay here to allow power to stabilise before Ethernet is initialized.
59 delay(300); // NOLINT
60 }
61
62 esp_err_t err;
63
64#ifdef USE_ETHERNET_SPI
65 // Install GPIO ISR handler to be able to service SPI Eth modules interrupts
66 gpio_install_isr_service(0);
67
68 spi_bus_config_t buscfg = {
69 .mosi_io_num = this->mosi_pin_,
70 .miso_io_num = this->miso_pin_,
71 .sclk_io_num = this->clk_pin_,
72 .quadwp_io_num = -1,
73 .quadhd_io_num = -1,
74 .data4_io_num = -1,
75 .data5_io_num = -1,
76 .data6_io_num = -1,
77 .data7_io_num = -1,
78 .max_transfer_sz = 0,
79 .flags = 0,
80 .intr_flags = 0,
81 };
82
83#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
84 defined(USE_ESP32_VARIANT_ESP32C6)
85 auto host = SPI2_HOST;
86#else
87 auto host = SPI3_HOST;
88#endif
89
90 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
91 ESPHL_ERROR_CHECK(err, "SPI bus initialize error");
92#endif
93
94 err = esp_netif_init();
95 ESPHL_ERROR_CHECK(err, "ETH netif init error");
96 err = esp_event_loop_create_default();
97 ESPHL_ERROR_CHECK(err, "ETH event loop error");
98
99 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
100 this->eth_netif_ = esp_netif_new(&cfg);
101
102 // Init MAC and PHY configs to default
103 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
104 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
105
106#ifdef USE_ETHERNET_SPI // Configure SPI interface and Ethernet driver for specific SPI module
107 spi_device_interface_config_t devcfg = {
108 .command_bits = 0,
109 .address_bits = 0,
110 .dummy_bits = 0,
111 .mode = 0,
112 .duty_cycle_pos = 0,
113 .cs_ena_pretrans = 0,
114 .cs_ena_posttrans = 0,
115 .clock_speed_hz = this->clock_speed_,
116 .input_delay_ns = 0,
117 .spics_io_num = this->cs_pin_,
118 .flags = 0,
119 .queue_size = 20,
120 .pre_cb = nullptr,
121 .post_cb = nullptr,
122 };
123
124#if CONFIG_ETH_SPI_ETHERNET_W5500
125 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
126#endif
127#if CONFIG_ETH_SPI_ETHERNET_DM9051
128 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
129#endif
130
131#if CONFIG_ETH_SPI_ETHERNET_W5500
132 w5500_config.int_gpio_num = this->interrupt_pin_;
133#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
134 w5500_config.poll_period_ms = this->polling_interval_;
135#endif
136#endif
137
138#if CONFIG_ETH_SPI_ETHERNET_DM9051
139 dm9051_config.int_gpio_num = this->interrupt_pin_;
140#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
141 dm9051_config.poll_period_ms = this->polling_interval_;
142#endif
143#endif
144
145 phy_config.phy_addr = this->phy_addr_spi_;
146 phy_config.reset_gpio_num = this->reset_pin_;
147
148 esp_eth_mac_t *mac = nullptr;
149#elif defined(USE_ETHERNET_OPENETH)
150 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
151#else
152 phy_config.phy_addr = this->phy_addr_;
153 phy_config.reset_gpio_num = this->power_pin_;
154
155 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
156#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
157 esp32_emac_config.smi_gpio.mdc_num = this->mdc_pin_;
158 esp32_emac_config.smi_gpio.mdio_num = this->mdio_pin_;
159#else
160 esp32_emac_config.smi_mdc_gpio_num = this->mdc_pin_;
161 esp32_emac_config.smi_mdio_gpio_num = this->mdio_pin_;
162#endif
163 esp32_emac_config.clock_config.rmii.clock_mode = this->clk_mode_;
164 esp32_emac_config.clock_config.rmii.clock_gpio = (emac_rmii_clock_gpio_t) this->clk_pin_;
165
166 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
167#endif
168
169 switch (this->type_) {
170#ifdef USE_ETHERNET_OPENETH
172 phy_config.autonego_timeout_ms = 1000;
173 this->phy_ = esp_eth_phy_new_dp83848(&phy_config);
174 break;
175 }
176#endif
177#if CONFIG_ETH_USE_ESP32_EMAC
179 this->phy_ = esp_eth_phy_new_lan87xx(&phy_config);
180 break;
181 }
183 this->phy_ = esp_eth_phy_new_rtl8201(&phy_config);
184 break;
185 }
187 this->phy_ = esp_eth_phy_new_dp83848(&phy_config);
188 break;
189 }
190 case ETHERNET_TYPE_IP101: {
191 this->phy_ = esp_eth_phy_new_ip101(&phy_config);
192 break;
193 }
195 this->phy_ = esp_eth_phy_new_jl1101(&phy_config);
196 break;
197 }
200 this->phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
201 break;
202 }
203#endif
204#ifdef USE_ETHERNET_SPI
205#if CONFIG_ETH_SPI_ETHERNET_W5500
206 case ETHERNET_TYPE_W5500: {
207 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
208 this->phy_ = esp_eth_phy_new_w5500(&phy_config);
209 break;
210 }
211#endif
212#if CONFIG_ETH_SPI_ETHERNET_DM9051
214 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
215 this->phy_ = esp_eth_phy_new_dm9051(&phy_config);
216 break;
217 }
218#endif
219#endif
220 default: {
221 this->mark_failed();
222 return;
223 }
224 }
225
226 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->phy_);
227 this->eth_handle_ = nullptr;
228 err = esp_eth_driver_install(&eth_config, &this->eth_handle_);
229 ESPHL_ERROR_CHECK(err, "ETH driver install error");
230
231#ifndef USE_ETHERNET_SPI
232 if (this->type_ == ETHERNET_TYPE_KSZ8081RNA && this->clk_mode_ == EMAC_CLK_OUT) {
233 // KSZ8081RNA default is incorrect. It expects a 25MHz clock instead of the 50MHz we provide.
235 }
236
237 for (const auto &phy_register : this->phy_registers_) {
238 this->write_phy_register_(mac, phy_register);
239 }
240#endif
241
242 // use ESP internal eth mac
243 uint8_t mac_addr[6];
244 esp_read_mac(mac_addr, ESP_MAC_ETH);
245 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
246 ESPHL_ERROR_CHECK(err, "set mac address error");
247
248 /* attach Ethernet driver to TCP/IP stack */
249 err = esp_netif_attach(this->eth_netif_, esp_eth_new_netif_glue(this->eth_handle_));
250 ESPHL_ERROR_CHECK(err, "ETH netif attach error");
251
252 // Register user defined event handers
253 err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &EthernetComponent::eth_event_handler, nullptr);
254 ESPHL_ERROR_CHECK(err, "ETH event handler register error");
255 err = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &EthernetComponent::got_ip_event_handler, nullptr);
256 ESPHL_ERROR_CHECK(err, "GOT IP event handler register error");
257#if USE_NETWORK_IPV6
258 err = esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &EthernetComponent::got_ip6_event_handler, nullptr);
259 ESPHL_ERROR_CHECK(err, "GOT IPv6 event handler register error");
260#endif /* USE_NETWORK_IPV6 */
261
262 /* start Ethernet driver state machine */
263 err = esp_eth_start(this->eth_handle_);
264 ESPHL_ERROR_CHECK(err, "ETH start error");
265}
266
268 const uint32_t now = App.get_loop_component_start_time();
269
270 switch (this->state_) {
272 if (this->started_) {
273 ESP_LOGI(TAG, "Starting connection");
275 this->start_connect_();
276 }
277 break;
279 if (!this->started_) {
280 ESP_LOGI(TAG, "Stopped connection");
282 } else if (this->connected_) {
283 // connection established
284 ESP_LOGI(TAG, "Connected");
286
287 this->dump_connect_params_();
288 this->status_clear_warning();
289 } else if (now - this->connect_begin_ > 15000) {
290 ESP_LOGW(TAG, "Connecting failed; reconnecting");
291 this->start_connect_();
292 }
293 break;
295 if (!this->started_) {
296 ESP_LOGI(TAG, "Stopped connection");
298 } else if (!this->connected_) {
299 ESP_LOGW(TAG, "Connection lost; reconnecting");
301 this->start_connect_();
302 } else {
303 // When connected and stable, disable the loop to save CPU cycles
304 this->disable_loop();
305 }
306 break;
307 }
308}
309
311 const char *eth_type;
312 switch (this->type_) {
314 eth_type = "LAN8720";
315 break;
316
318 eth_type = "RTL8201";
319 break;
320
322 eth_type = "DP83848";
323 break;
324
326 eth_type = "IP101";
327 break;
328
330 eth_type = "JL1101";
331 break;
332
334 eth_type = "KSZ8081";
335 break;
336
338 eth_type = "KSZ8081RNA";
339 break;
340
342 eth_type = "W5500";
343 break;
344
346 eth_type = "OPENETH";
347 break;
348
350 eth_type = "DM9051";
351 break;
352
353 default:
354 eth_type = "Unknown";
355 break;
356 }
357
358 ESP_LOGCONFIG(TAG, "Ethernet:");
359 this->dump_connect_params_();
360#ifdef USE_ETHERNET_SPI
361 ESP_LOGCONFIG(TAG,
362 " CLK Pin: %u\n"
363 " MISO Pin: %u\n"
364 " MOSI Pin: %u\n"
365 " CS Pin: %u",
366 this->clk_pin_, this->miso_pin_, this->mosi_pin_, this->cs_pin_);
367#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
368 if (this->polling_interval_ != 0) {
369 ESP_LOGCONFIG(TAG, " Polling Interval: %lu ms", this->polling_interval_);
370 } else
371#endif
372 {
373 ESP_LOGCONFIG(TAG, " IRQ Pin: %d", this->interrupt_pin_);
374 }
375 ESP_LOGCONFIG(TAG,
376 " Reset Pin: %d\n"
377 " Clock Speed: %d MHz",
378 this->reset_pin_, this->clock_speed_ / 1000000);
379#else
380 if (this->power_pin_ != -1) {
381 ESP_LOGCONFIG(TAG, " Power Pin: %u", this->power_pin_);
382 }
383 ESP_LOGCONFIG(TAG,
384 " CLK Pin: %u\n"
385 " MDC Pin: %u\n"
386 " MDIO Pin: %u\n"
387 " PHY addr: %u",
388 this->clk_pin_, this->mdc_pin_, this->mdio_pin_, this->phy_addr_);
389#endif
390 ESP_LOGCONFIG(TAG, " Type: %s", eth_type);
391}
392
394
396
398 network::IPAddresses addresses;
399 esp_netif_ip_info_t ip;
400 esp_err_t err = esp_netif_get_ip_info(this->eth_netif_, &ip);
401 if (err != ESP_OK) {
402 ESP_LOGV(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
403 // TODO: do something smarter
404 // return false;
405 } else {
406 addresses[0] = network::IPAddress(&ip.ip);
407 }
408#if USE_NETWORK_IPV6
409 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
410 uint8_t count = 0;
411 count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
412 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
413 for (int i = 0; i < count; i++) {
414 addresses[i + 1] = network::IPAddress(&if_ip6s[i]);
415 }
416#endif /* USE_NETWORK_IPV6 */
417
418 return addresses;
419}
420
422 LwIPLock lock;
423 const ip_addr_t *dns_ip = dns_getserver(num);
424 return dns_ip;
425}
426
427void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event, void *event_data) {
428 const char *event_name;
429
430 switch (event) {
431 case ETHERNET_EVENT_START:
432 event_name = "ETH started";
435 break;
436 case ETHERNET_EVENT_STOP:
437 event_name = "ETH stopped";
440 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
441 break;
442 case ETHERNET_EVENT_CONNECTED:
443 event_name = "ETH connected";
444 break;
445 case ETHERNET_EVENT_DISCONNECTED:
446 event_name = "ETH disconnected";
448 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
449 break;
450 default:
451 return;
452 }
453
454 ESP_LOGV(TAG, "[Ethernet event] %s (num=%" PRId32 ")", event_name, event);
455}
456
457void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
458 void *event_data) {
459 ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
460 const esp_netif_ip_info_t *ip_info = &event->ip_info;
461 ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
463#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
464 global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
465 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
466#else
468 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
469#endif /* USE_NETWORK_IPV6 */
470}
471
472#if USE_NETWORK_IPV6
473void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
474 void *event_data) {
475 ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
476 ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
478#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
480 global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
481 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
482#else
484 global_eth_component->enable_loop_soon_any_context(); // Enable loop when connection state changes
485#endif
486}
487#endif /* USE_NETWORK_IPV6 */
488
491#if USE_NETWORK_IPV6
493#endif /* USE_NETWORK_IPV6 */
494 this->connect_begin_ = millis();
495 this->status_set_warning("waiting for IP configuration");
496
497 esp_err_t err;
498 err = esp_netif_set_hostname(this->eth_netif_, App.get_name().c_str());
499 if (err != ERR_OK) {
500 ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
501 }
502
503 esp_netif_ip_info_t info;
504 if (this->manual_ip_.has_value()) {
505 info.ip = this->manual_ip_->static_ip;
506 info.gw = this->manual_ip_->gateway;
507 info.netmask = this->manual_ip_->subnet;
508 } else {
509 info.ip.addr = 0;
510 info.gw.addr = 0;
511 info.netmask.addr = 0;
512 }
513
514 esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
515
516 err = esp_netif_dhcpc_get_status(this->eth_netif_, &status);
517 ESPHL_ERROR_CHECK(err, "DHCPC Get Status Failed!");
518
519 ESP_LOGV(TAG, "DHCP Client Status: %d", status);
520
521 err = esp_netif_dhcpc_stop(this->eth_netif_);
522 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
523 ESPHL_ERROR_CHECK(err, "DHCPC stop error");
524 }
525
526 err = esp_netif_set_ip_info(this->eth_netif_, &info);
527 ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");
528
529 if (this->manual_ip_.has_value()) {
530 LwIPLock lock;
531 if (this->manual_ip_->dns1.is_set()) {
532 ip_addr_t d;
533 d = this->manual_ip_->dns1;
534 dns_setserver(0, &d);
535 }
536 if (this->manual_ip_->dns2.is_set()) {
537 ip_addr_t d;
538 d = this->manual_ip_->dns2;
539 dns_setserver(1, &d);
540 }
541 } else {
542 err = esp_netif_dhcpc_start(this->eth_netif_);
543 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
544 ESPHL_ERROR_CHECK(err, "DHCPC start error");
545 }
546 }
547#if USE_NETWORK_IPV6
548 err = esp_netif_create_ip6_linklocal(this->eth_netif_);
549 if (err != ESP_OK) {
550 ESPHL_ERROR_CHECK(err, "Enable IPv6 link local failed");
551 }
552#endif /* USE_NETWORK_IPV6 */
553
554 this->connect_begin_ = millis();
555 this->status_set_warning();
556}
557
559
561 esp_netif_ip_info_t ip;
562 esp_netif_get_ip_info(this->eth_netif_, &ip);
563 const ip_addr_t *dns_ip1;
564 const ip_addr_t *dns_ip2;
565 {
566 LwIPLock lock;
567 dns_ip1 = dns_getserver(0);
568 dns_ip2 = dns_getserver(1);
569 }
570
571 ESP_LOGCONFIG(TAG,
572 " IP Address: %s\n"
573 " Hostname: '%s'\n"
574 " Subnet: %s\n"
575 " Gateway: %s\n"
576 " DNS1: %s\n"
577 " DNS2: %s",
578 network::IPAddress(&ip.ip).str().c_str(), App.get_name().c_str(),
579 network::IPAddress(&ip.netmask).str().c_str(), network::IPAddress(&ip.gw).str().c_str(),
580 network::IPAddress(dns_ip1).str().c_str(), network::IPAddress(dns_ip2).str().c_str());
581
582#if USE_NETWORK_IPV6
583 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
584 uint8_t count = 0;
585 count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
586 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
587 for (int i = 0; i < count; i++) {
588 ESP_LOGCONFIG(TAG, " IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
589 }
590#endif /* USE_NETWORK_IPV6 */
591
592 ESP_LOGCONFIG(TAG,
593 " MAC Address: %s\n"
594 " Is Full Duplex: %s\n"
595 " Link Speed: %u",
596 this->get_eth_mac_address_pretty().c_str(), YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL),
597 this->get_link_speed() == ETH_SPEED_100M ? 100 : 10);
598}
599
600#ifdef USE_ETHERNET_SPI
601void EthernetComponent::set_clk_pin(uint8_t clk_pin) { this->clk_pin_ = clk_pin; }
602void EthernetComponent::set_miso_pin(uint8_t miso_pin) { this->miso_pin_ = miso_pin; }
603void EthernetComponent::set_mosi_pin(uint8_t mosi_pin) { this->mosi_pin_ = mosi_pin; }
604void EthernetComponent::set_cs_pin(uint8_t cs_pin) { this->cs_pin_ = cs_pin; }
605void EthernetComponent::set_interrupt_pin(uint8_t interrupt_pin) { this->interrupt_pin_ = interrupt_pin; }
606void EthernetComponent::set_reset_pin(uint8_t reset_pin) { this->reset_pin_ = reset_pin; }
607void EthernetComponent::set_clock_speed(int clock_speed) { this->clock_speed_ = clock_speed; }
608#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
609void EthernetComponent::set_polling_interval(uint32_t polling_interval) { this->polling_interval_ = polling_interval; }
610#endif
611#else
612void EthernetComponent::set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; }
613void EthernetComponent::set_power_pin(int power_pin) { this->power_pin_ = power_pin; }
614void EthernetComponent::set_mdc_pin(uint8_t mdc_pin) { this->mdc_pin_ = mdc_pin; }
615void EthernetComponent::set_mdio_pin(uint8_t mdio_pin) { this->mdio_pin_ = mdio_pin; }
616void EthernetComponent::set_clk_pin(uint8_t clk_pin) { this->clk_pin_ = clk_pin; }
617void EthernetComponent::set_clk_mode(emac_rmii_clock_mode_t clk_mode) { this->clk_mode_ = clk_mode; }
618void EthernetComponent::add_phy_register(PHYRegister register_value) { this->phy_registers_.push_back(register_value); }
619#endif
621void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; }
622
624 if (this->use_address_.empty()) {
625 return App.get_name() + ".local";
626 }
627 return this->use_address_;
628}
629
630void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
631
633 esp_err_t err;
634 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
635 ESPHL_ERROR_CHECK(err, "ETH_CMD_G_MAC error");
636}
637
639 uint8_t mac[6];
641 return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
642}
643
645 esp_err_t err;
646 eth_duplex_t duplex_mode;
647 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
648 ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_DUPLEX_MODE error", ETH_DUPLEX_HALF);
649 return duplex_mode;
650}
651
653 esp_err_t err;
654 eth_speed_t speed;
655 err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_SPEED, &speed);
656 ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_SPEED error", ETH_SPEED_10M);
657 return speed;
658}
659
661 ESP_LOGI(TAG, "Powering down ethernet PHY");
662 if (this->phy_ == nullptr) {
663 ESP_LOGE(TAG, "Ethernet PHY not assigned");
664 return false;
665 }
666 this->connected_ = false;
667 this->started_ = false;
668 // No need to enable_loop() here as this is only called during shutdown/reboot
669 if (this->phy_->pwrctl(this->phy_, false) != ESP_OK) {
670 ESP_LOGE(TAG, "Error powering down ethernet PHY");
671 return false;
672 }
673 return true;
674}
675
676#ifndef USE_ETHERNET_SPI
677
678constexpr uint8_t KSZ80XX_PC2R_REG_ADDR = 0x1F;
679
681 esp_err_t err;
682
683 uint32_t phy_control_2;
684 err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
685 ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
686 ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
687
688 /*
689 * Bit 7 is `RMII Reference Clock Select`. Default is `0`.
690 * KSZ8081RNA:
691 * 0 - clock input to XI (Pin 8) is 25 MHz for RMII - 25 MHz clock mode.
692 * 1 - clock input to XI (Pin 8) is 50 MHz for RMII - 50 MHz clock mode.
693 * KSZ8081RND:
694 * 0 - clock input to XI (Pin 8) is 50 MHz for RMII - 50 MHz clock mode.
695 * 1 - clock input to XI (Pin 8) is 25 MHz (driven clock only, not a crystal) for RMII - 25 MHz clock mode.
696 */
697 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
698 phy_control_2 |= 1 << 7;
699 err = mac->write_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
700 ESPHL_ERROR_CHECK(err, "Write PHY Control 2 failed");
701 err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
702 ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
703 ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
704 }
705}
706
707void EthernetComponent::write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data) {
708 esp_err_t err;
709 constexpr uint8_t eth_phy_psr_reg_addr = 0x1F;
710
711 if (this->type_ == ETHERNET_TYPE_RTL8201 && register_data.page) {
712 ESP_LOGD(TAG, "Select PHY Register Page: 0x%02" PRIX32, register_data.page);
713 err = mac->write_phy_reg(mac, this->phy_addr_, eth_phy_psr_reg_addr, register_data.page);
714 ESPHL_ERROR_CHECK(err, "Select PHY Register page failed");
715 }
716
717 ESP_LOGD(TAG, "Writing to PHY Register Address: 0x%02" PRIX32, register_data.address);
718 ESP_LOGD(TAG, "Writing to PHY Register Value: 0x%04" PRIX32, register_data.value);
719 err = mac->write_phy_reg(mac, this->phy_addr_, register_data.address, register_data.value);
720 ESPHL_ERROR_CHECK(err, "Writing PHY Register failed");
721
722 if (this->type_ == ETHERNET_TYPE_RTL8201 && register_data.page) {
723 ESP_LOGD(TAG, "Select PHY Register Page 0x00");
724 err = mac->write_phy_reg(mac, this->phy_addr_, eth_phy_psr_reg_addr, 0x0);
725 ESPHL_ERROR_CHECK(err, "Select PHY Register Page 0 failed");
726 }
727}
728
729#endif
730
731} // namespace ethernet
732} // namespace esphome
733
734#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:750
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)
uint8_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
std::string str_snprintf(const char *fmt, size_t len,...)
Definition helpers.cpp:194
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:280
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