ESPHome  2024.10.2
ethernet_component.cpp
Go to the documentation of this file.
1 #include "ethernet_component.h"
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 
17 namespace esphome {
18 namespace ethernet {
19 
20 static const char *const TAG = "ethernet";
21 
22 EthernetComponent *global_eth_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
23 
24 #define ESPHL_ERROR_CHECK(err, message) \
25  if ((err) != ESP_OK) { \
26  ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \
27  this->mark_failed(); \
28  return; \
29  }
30 
31 #define ESPHL_ERROR_CHECK_RET(err, message, ret) \
32  if ((err) != ESP_OK) { \
33  ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \
34  this->mark_failed(); \
35  return ret; \
36  }
37 
38 EthernetComponent::EthernetComponent() { global_eth_component = this; }
39 
41  ESP_LOGCONFIG(TAG, "Setting up Ethernet...");
42  if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
43  // Delay here to allow power to stabilise before Ethernet is initialized.
44  delay(300); // NOLINT
45  }
46 
47  esp_err_t err;
48 
49 #ifdef USE_ETHERNET_SPI
50  // Install GPIO ISR handler to be able to service SPI Eth modules interrupts
51  gpio_install_isr_service(0);
52 
53  spi_bus_config_t buscfg = {
54  .mosi_io_num = this->mosi_pin_,
55  .miso_io_num = this->miso_pin_,
56  .sclk_io_num = this->clk_pin_,
57  .quadwp_io_num = -1,
58  .quadhd_io_num = -1,
59  .data4_io_num = -1,
60  .data5_io_num = -1,
61  .data6_io_num = -1,
62  .data7_io_num = -1,
63  .max_transfer_sz = 0,
64  .flags = 0,
65  .intr_flags = 0,
66  };
67 
68 #if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
69  defined(USE_ESP32_VARIANT_ESP32C6)
70  auto host = SPI2_HOST;
71 #else
72  auto host = SPI3_HOST;
73 #endif
74 
75  err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
76  ESPHL_ERROR_CHECK(err, "SPI bus initialize error");
77 #endif
78 
79  err = esp_netif_init();
80  ESPHL_ERROR_CHECK(err, "ETH netif init error");
81  err = esp_event_loop_create_default();
82  ESPHL_ERROR_CHECK(err, "ETH event loop error");
83 
84  esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
85  this->eth_netif_ = esp_netif_new(&cfg);
86 
87  // Init MAC and PHY configs to default
88  eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
89  eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
90 
91 #ifdef USE_ETHERNET_SPI // Configure SPI interface and Ethernet driver for specific SPI module
92  spi_device_interface_config_t devcfg = {
93  .command_bits = 16, // Actually it's the address phase in W5500 SPI frame
94  .address_bits = 8, // Actually it's the control phase in W5500 SPI frame
95  .dummy_bits = 0,
96  .mode = 0,
97  .duty_cycle_pos = 0,
98  .cs_ena_pretrans = 0,
99  .cs_ena_posttrans = 0,
100  .clock_speed_hz = this->clock_speed_,
101  .input_delay_ns = 0,
102  .spics_io_num = this->cs_pin_,
103  .flags = 0,
104  .queue_size = 20,
105  .pre_cb = nullptr,
106  .post_cb = nullptr,
107  };
108 
109 #if USE_ESP_IDF && (ESP_IDF_VERSION_MAJOR >= 5)
110  eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
111 #else
112  spi_device_handle_t spi_handle = nullptr;
113  err = spi_bus_add_device(host, &devcfg, &spi_handle);
114  ESPHL_ERROR_CHECK(err, "SPI bus add device error");
115 
116  eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
117 #endif
118  w5500_config.int_gpio_num = this->interrupt_pin_;
119  phy_config.phy_addr = this->phy_addr_spi_;
120  phy_config.reset_gpio_num = this->reset_pin_;
121 
122  esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
123 #elif defined(USE_ETHERNET_OPENETH)
124  esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
125 #else
126  phy_config.phy_addr = this->phy_addr_;
127  phy_config.reset_gpio_num = this->power_pin_;
128 
129 #if ESP_IDF_VERSION_MAJOR >= 5
130  eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
131  esp32_emac_config.smi_mdc_gpio_num = this->mdc_pin_;
132  esp32_emac_config.smi_mdio_gpio_num = this->mdio_pin_;
133  esp32_emac_config.clock_config.rmii.clock_mode = this->clk_mode_;
134  esp32_emac_config.clock_config.rmii.clock_gpio = this->clk_gpio_;
135 
136  esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
137 #else
138  mac_config.smi_mdc_gpio_num = this->mdc_pin_;
139  mac_config.smi_mdio_gpio_num = this->mdio_pin_;
140  mac_config.clock_config.rmii.clock_mode = this->clk_mode_;
141  mac_config.clock_config.rmii.clock_gpio = this->clk_gpio_;
142 
143  esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
144 #endif
145 #endif
146 
147  switch (this->type_) {
148 #ifdef USE_ETHERNET_OPENETH
149  case ETHERNET_TYPE_OPENETH: {
150  phy_config.autonego_timeout_ms = 1000;
151  this->phy_ = esp_eth_phy_new_dp83848(&phy_config);
152  break;
153  }
154 #endif
155 #if CONFIG_ETH_USE_ESP32_EMAC
156  case ETHERNET_TYPE_LAN8720: {
157  this->phy_ = esp_eth_phy_new_lan87xx(&phy_config);
158  break;
159  }
160  case ETHERNET_TYPE_RTL8201: {
161  this->phy_ = esp_eth_phy_new_rtl8201(&phy_config);
162  break;
163  }
164  case ETHERNET_TYPE_DP83848: {
165  this->phy_ = esp_eth_phy_new_dp83848(&phy_config);
166  break;
167  }
168  case ETHERNET_TYPE_IP101: {
169  this->phy_ = esp_eth_phy_new_ip101(&phy_config);
170  break;
171  }
172  case ETHERNET_TYPE_JL1101: {
173  this->phy_ = esp_eth_phy_new_jl1101(&phy_config);
174  break;
175  }
178 #if ESP_IDF_VERSION_MAJOR >= 5
179  this->phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
180 #else
181  this->phy_ = esp_eth_phy_new_ksz8081(&phy_config);
182 #endif
183  break;
184  }
185 #endif
186 #ifdef USE_ETHERNET_SPI
187  case ETHERNET_TYPE_W5500: {
188  this->phy_ = esp_eth_phy_new_w5500(&phy_config);
189  break;
190  }
191 #endif
192  default: {
193  this->mark_failed();
194  return;
195  }
196  }
197 
198  esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->phy_);
199  this->eth_handle_ = nullptr;
200  err = esp_eth_driver_install(&eth_config, &this->eth_handle_);
201  ESPHL_ERROR_CHECK(err, "ETH driver install error");
202 
203 #ifndef USE_ETHERNET_SPI
204  if (this->type_ == ETHERNET_TYPE_KSZ8081RNA && this->clk_mode_ == EMAC_CLK_OUT) {
205  // KSZ8081RNA default is incorrect. It expects a 25MHz clock instead of the 50MHz we provide.
206  this->ksz8081_set_clock_reference_(mac);
207  }
208 
209  for (const auto &phy_register : this->phy_registers_) {
210  this->write_phy_register_(mac, phy_register);
211  }
212 #endif
213 
214  // use ESP internal eth mac
215  uint8_t mac_addr[6];
216  esp_read_mac(mac_addr, ESP_MAC_ETH);
217  err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
218  ESPHL_ERROR_CHECK(err, "set mac address error");
219 
220  /* attach Ethernet driver to TCP/IP stack */
221  err = esp_netif_attach(this->eth_netif_, esp_eth_new_netif_glue(this->eth_handle_));
222  ESPHL_ERROR_CHECK(err, "ETH netif attach error");
223 
224  // Register user defined event handers
225  err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &EthernetComponent::eth_event_handler, nullptr);
226  ESPHL_ERROR_CHECK(err, "ETH event handler register error");
227  err = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &EthernetComponent::got_ip_event_handler, nullptr);
228  ESPHL_ERROR_CHECK(err, "GOT IP event handler register error");
229 #if USE_NETWORK_IPV6
230  err = esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &EthernetComponent::got_ip6_event_handler, nullptr);
231  ESPHL_ERROR_CHECK(err, "GOT IPv6 event handler register error");
232 #endif /* USE_NETWORK_IPV6 */
233 
234  /* start Ethernet driver state machine */
235  err = esp_eth_start(this->eth_handle_);
236  ESPHL_ERROR_CHECK(err, "ETH start error");
237 }
238 
240  const uint32_t now = millis();
241 
242  switch (this->state_) {
244  if (this->started_) {
245  ESP_LOGI(TAG, "Starting ethernet connection");
247  this->start_connect_();
248  }
249  break;
251  if (!this->started_) {
252  ESP_LOGI(TAG, "Stopped ethernet connection");
254  } else if (this->connected_) {
255  // connection established
256  ESP_LOGI(TAG, "Connected via Ethernet!");
258 
259  this->dump_connect_params_();
260  this->status_clear_warning();
261  } else if (now - this->connect_begin_ > 15000) {
262  ESP_LOGW(TAG, "Connecting via ethernet failed! Re-connecting...");
263  this->start_connect_();
264  }
265  break;
267  if (!this->started_) {
268  ESP_LOGI(TAG, "Stopped ethernet connection");
270  } else if (!this->connected_) {
271  ESP_LOGW(TAG, "Connection via Ethernet lost! Re-connecting...");
273  this->start_connect_();
274  }
275  break;
276  }
277 }
278 
280  const char *eth_type;
281  switch (this->type_) {
283  eth_type = "LAN8720";
284  break;
285 
287  eth_type = "RTL8201";
288  break;
289 
291  eth_type = "DP83848";
292  break;
293 
294  case ETHERNET_TYPE_IP101:
295  eth_type = "IP101";
296  break;
297 
299  eth_type = "JL1101";
300  break;
301 
303  eth_type = "KSZ8081";
304  break;
305 
307  eth_type = "KSZ8081RNA";
308  break;
309 
310  case ETHERNET_TYPE_W5500:
311  eth_type = "W5500";
312  break;
313 
315  eth_type = "OPENETH";
316  break;
317 
318  default:
319  eth_type = "Unknown";
320  break;
321  }
322 
323  ESP_LOGCONFIG(TAG, "Ethernet:");
324  this->dump_connect_params_();
325 #ifdef USE_ETHERNET_SPI
326  ESP_LOGCONFIG(TAG, " CLK Pin: %u", this->clk_pin_);
327  ESP_LOGCONFIG(TAG, " MISO Pin: %u", this->miso_pin_);
328  ESP_LOGCONFIG(TAG, " MOSI Pin: %u", this->mosi_pin_);
329  ESP_LOGCONFIG(TAG, " CS Pin: %u", this->cs_pin_);
330  ESP_LOGCONFIG(TAG, " IRQ Pin: %u", this->interrupt_pin_);
331  ESP_LOGCONFIG(TAG, " Reset Pin: %d", this->reset_pin_);
332  ESP_LOGCONFIG(TAG, " Clock Speed: %d MHz", this->clock_speed_ / 1000000);
333 #else
334  if (this->power_pin_ != -1) {
335  ESP_LOGCONFIG(TAG, " Power Pin: %u", this->power_pin_);
336  }
337  ESP_LOGCONFIG(TAG, " MDC Pin: %u", this->mdc_pin_);
338  ESP_LOGCONFIG(TAG, " MDIO Pin: %u", this->mdio_pin_);
339  ESP_LOGCONFIG(TAG, " PHY addr: %u", this->phy_addr_);
340 #endif
341  ESP_LOGCONFIG(TAG, " Type: %s", eth_type);
342 }
343 
345 
347 
349  network::IPAddresses addresses;
350  esp_netif_ip_info_t ip;
351  esp_err_t err = esp_netif_get_ip_info(this->eth_netif_, &ip);
352  if (err != ESP_OK) {
353  ESP_LOGV(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
354  // TODO: do something smarter
355  // return false;
356  } else {
357  addresses[0] = network::IPAddress(&ip.ip);
358  }
359 #if USE_NETWORK_IPV6
360  struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
361  uint8_t count = 0;
362  count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
363  assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
364  for (int i = 0; i < count; i++) {
365  addresses[i + 1] = network::IPAddress(&if_ip6s[i]);
366  }
367 #endif /* USE_NETWORK_IPV6 */
368 
369  return addresses;
370 }
371 
373  const ip_addr_t *dns_ip = dns_getserver(num);
374  return dns_ip;
375 }
376 
377 void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event, void *event_data) {
378  const char *event_name;
379 
380  switch (event) {
381  case ETHERNET_EVENT_START:
382  event_name = "ETH started";
383  global_eth_component->started_ = true;
384  break;
385  case ETHERNET_EVENT_STOP:
386  event_name = "ETH stopped";
387  global_eth_component->started_ = false;
388  global_eth_component->connected_ = false;
389  break;
390  case ETHERNET_EVENT_CONNECTED:
391  event_name = "ETH connected";
392  break;
393  case ETHERNET_EVENT_DISCONNECTED:
394  event_name = "ETH disconnected";
395  global_eth_component->connected_ = false;
396  break;
397  default:
398  return;
399  }
400 
401  ESP_LOGV(TAG, "[Ethernet event] %s (num=%" PRId32 ")", event_name, event);
402 }
403 
404 void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
405  void *event_data) {
406  ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
407  const esp_netif_ip_info_t *ip_info = &event->ip_info;
408  ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
409  global_eth_component->got_ipv4_address_ = true;
410 #if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
411  global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
412 #else
413  global_eth_component->connected_ = true;
414 #endif /* USE_NETWORK_IPV6 */
415 }
416 
417 #if USE_NETWORK_IPV6
418 void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
419  void *event_data) {
420  ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
421  ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
422  global_eth_component->ipv6_count_ += 1;
423 #if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
424  global_eth_component->connected_ =
425  global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
426 #else
427  global_eth_component->connected_ = global_eth_component->got_ipv4_address_;
428 #endif
429 }
430 #endif /* USE_NETWORK_IPV6 */
431 
433  global_eth_component->got_ipv4_address_ = false;
434 #if USE_NETWORK_IPV6
435  global_eth_component->ipv6_count_ = 0;
436 #endif /* USE_NETWORK_IPV6 */
437  this->connect_begin_ = millis();
438  this->status_set_warning("waiting for IP configuration");
439 
440  esp_err_t err;
441  err = esp_netif_set_hostname(this->eth_netif_, App.get_name().c_str());
442  if (err != ERR_OK) {
443  ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
444  }
445 
446  esp_netif_ip_info_t info;
447  if (this->manual_ip_.has_value()) {
448  info.ip = this->manual_ip_->static_ip;
449  info.gw = this->manual_ip_->gateway;
450  info.netmask = this->manual_ip_->subnet;
451  } else {
452  info.ip.addr = 0;
453  info.gw.addr = 0;
454  info.netmask.addr = 0;
455  }
456 
457  esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
458 
459  err = esp_netif_dhcpc_get_status(this->eth_netif_, &status);
460  ESPHL_ERROR_CHECK(err, "DHCPC Get Status Failed!");
461 
462  ESP_LOGV(TAG, "DHCP Client Status: %d", status);
463 
464  err = esp_netif_dhcpc_stop(this->eth_netif_);
465  if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
466  ESPHL_ERROR_CHECK(err, "DHCPC stop error");
467  }
468 
469  err = esp_netif_set_ip_info(this->eth_netif_, &info);
470  ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");
471 
472  if (this->manual_ip_.has_value()) {
473  if (this->manual_ip_->dns1.is_set()) {
474  ip_addr_t d;
475  d = this->manual_ip_->dns1;
476  dns_setserver(0, &d);
477  }
478  if (this->manual_ip_->dns2.is_set()) {
479  ip_addr_t d;
480  d = this->manual_ip_->dns2;
481  dns_setserver(1, &d);
482  }
483  } else {
484  err = esp_netif_dhcpc_start(this->eth_netif_);
485  if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
486  ESPHL_ERROR_CHECK(err, "DHCPC start error");
487  }
488  }
489 #if USE_NETWORK_IPV6
490  err = esp_netif_create_ip6_linklocal(this->eth_netif_);
491  if (err != ESP_OK) {
492  ESPHL_ERROR_CHECK(err, "Enable IPv6 link local failed");
493  }
494 #endif /* USE_NETWORK_IPV6 */
495 
496  this->connect_begin_ = millis();
497  this->status_set_warning();
498 }
499 
501 
503  esp_netif_ip_info_t ip;
504  esp_netif_get_ip_info(this->eth_netif_, &ip);
505  ESP_LOGCONFIG(TAG, " IP Address: %s", network::IPAddress(&ip.ip).str().c_str());
506  ESP_LOGCONFIG(TAG, " Hostname: '%s'", App.get_name().c_str());
507  ESP_LOGCONFIG(TAG, " Subnet: %s", network::IPAddress(&ip.netmask).str().c_str());
508  ESP_LOGCONFIG(TAG, " Gateway: %s", network::IPAddress(&ip.gw).str().c_str());
509 
510  const ip_addr_t *dns_ip1 = dns_getserver(0);
511  const ip_addr_t *dns_ip2 = dns_getserver(1);
512 
513  ESP_LOGCONFIG(TAG, " DNS1: %s", network::IPAddress(dns_ip1).str().c_str());
514  ESP_LOGCONFIG(TAG, " DNS2: %s", network::IPAddress(dns_ip2).str().c_str());
515 
516 #if USE_NETWORK_IPV6
517  struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
518  uint8_t count = 0;
519  count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
520  assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
521  for (int i = 0; i < count; i++) {
522  ESP_LOGCONFIG(TAG, " IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
523  }
524 #endif /* USE_NETWORK_IPV6 */
525 
526  ESP_LOGCONFIG(TAG, " MAC Address: %s", this->get_eth_mac_address_pretty().c_str());
527  ESP_LOGCONFIG(TAG, " Is Full Duplex: %s", YESNO(this->get_duplex_mode() == ETH_DUPLEX_FULL));
528  ESP_LOGCONFIG(TAG, " Link Speed: %u", this->get_link_speed() == ETH_SPEED_100M ? 100 : 10);
529 }
530 
531 #ifdef USE_ETHERNET_SPI
532 void EthernetComponent::set_clk_pin(uint8_t clk_pin) { this->clk_pin_ = clk_pin; }
533 void EthernetComponent::set_miso_pin(uint8_t miso_pin) { this->miso_pin_ = miso_pin; }
534 void EthernetComponent::set_mosi_pin(uint8_t mosi_pin) { this->mosi_pin_ = mosi_pin; }
535 void EthernetComponent::set_cs_pin(uint8_t cs_pin) { this->cs_pin_ = cs_pin; }
536 void EthernetComponent::set_interrupt_pin(uint8_t interrupt_pin) { this->interrupt_pin_ = interrupt_pin; }
537 void EthernetComponent::set_reset_pin(uint8_t reset_pin) { this->reset_pin_ = reset_pin; }
538 void EthernetComponent::set_clock_speed(int clock_speed) { this->clock_speed_ = clock_speed; }
539 #else
540 void EthernetComponent::set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; }
541 void EthernetComponent::set_power_pin(int power_pin) { this->power_pin_ = power_pin; }
542 void EthernetComponent::set_mdc_pin(uint8_t mdc_pin) { this->mdc_pin_ = mdc_pin; }
543 void EthernetComponent::set_mdio_pin(uint8_t mdio_pin) { this->mdio_pin_ = mdio_pin; }
544 void EthernetComponent::set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio) {
545  this->clk_mode_ = clk_mode;
546  this->clk_gpio_ = clk_gpio;
547 }
548 void EthernetComponent::add_phy_register(PHYRegister register_value) { this->phy_registers_.push_back(register_value); }
549 #endif
551 void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; }
552 
554  if (this->use_address_.empty()) {
555  return App.get_name() + ".local";
556  }
557  return this->use_address_;
558 }
559 
560 void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
561 
563  esp_err_t err;
564  err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
565  ESPHL_ERROR_CHECK(err, "ETH_CMD_G_MAC error");
566 }
567 
569  uint8_t mac[6];
570  get_mac_address_raw(mac);
571  return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
572 }
573 
575  esp_err_t err;
576  eth_duplex_t duplex_mode;
577  err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
578  ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_DUPLEX_MODE error", ETH_DUPLEX_HALF);
579  return duplex_mode;
580 }
581 
583  esp_err_t err;
584  eth_speed_t speed;
585  err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_SPEED, &speed);
586  ESPHL_ERROR_CHECK_RET(err, "ETH_CMD_G_SPEED error", ETH_SPEED_10M);
587  return speed;
588 }
589 
591  ESP_LOGI(TAG, "Powering down ethernet PHY");
592  if (this->phy_ == nullptr) {
593  ESP_LOGE(TAG, "Ethernet PHY not assigned");
594  return false;
595  }
596  this->connected_ = false;
597  this->started_ = false;
598  if (this->phy_->pwrctl(this->phy_, false) != ESP_OK) {
599  ESP_LOGE(TAG, "Error powering down ethernet PHY");
600  return false;
601  }
602  return true;
603 }
604 
605 #ifndef USE_ETHERNET_SPI
606 
607 constexpr uint8_t KSZ80XX_PC2R_REG_ADDR = 0x1F;
608 
610  esp_err_t err;
611 
612  uint32_t phy_control_2;
613  err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
614  ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
615  ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
616 
617  /*
618  * Bit 7 is `RMII Reference Clock Select`. Default is `0`.
619  * KSZ8081RNA:
620  * 0 - clock input to XI (Pin 8) is 25 MHz for RMII - 25 MHz clock mode.
621  * 1 - clock input to XI (Pin 8) is 50 MHz for RMII - 50 MHz clock mode.
622  * KSZ8081RND:
623  * 0 - clock input to XI (Pin 8) is 50 MHz for RMII - 50 MHz clock mode.
624  * 1 - clock input to XI (Pin 8) is 25 MHz (driven clock only, not a crystal) for RMII - 25 MHz clock mode.
625  */
626  if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
627  phy_control_2 |= 1 << 7;
628  err = mac->write_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
629  ESPHL_ERROR_CHECK(err, "Write PHY Control 2 failed");
630  err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
631  ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
632  ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
633  }
634 }
635 
636 void EthernetComponent::write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data) {
637  esp_err_t err;
638  constexpr uint8_t eth_phy_psr_reg_addr = 0x1F;
639 
640  if (this->type_ == ETHERNET_TYPE_RTL8201 && register_data.page) {
641  ESP_LOGD(TAG, "Select PHY Register Page: 0x%02" PRIX32, register_data.page);
642  err = mac->write_phy_reg(mac, this->phy_addr_, eth_phy_psr_reg_addr, register_data.page);
643  ESPHL_ERROR_CHECK(err, "Select PHY Register page failed");
644  }
645 
646  ESP_LOGD(TAG, "Writing to PHY Register Address: 0x%02" PRIX32, register_data.address);
647  ESP_LOGD(TAG, "Writing to PHY Register Value: 0x%04" PRIX32, register_data.value);
648  err = mac->write_phy_reg(mac, this->phy_addr_, register_data.address, register_data.value);
649  ESPHL_ERROR_CHECK(err, "Writing PHY Register failed");
650 
651  if (this->type_ == ETHERNET_TYPE_RTL8201 && register_data.page) {
652  ESP_LOGD(TAG, "Select PHY Register Page 0x00");
653  err = mac->write_phy_reg(mac, this->phy_addr_, eth_phy_psr_reg_addr, 0x0);
654  ESPHL_ERROR_CHECK(err, "Select PHY Register Page 0 failed");
655  }
656 }
657 
658 #endif
659 
660 } // namespace ethernet
661 } // namespace esphome
662 
663 #endif // USE_ESP32
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
Definition: helpers.cpp:359
void set_manual_ip(const ManualIP &manual_ip)
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
void ksz8081_set_clock_reference_(esp_eth_mac_t *mac)
Set RMII Reference Clock Select bit for KSZ8081.
std::string str() const
Definition: ip_address.h:122
void set_interrupt_pin(uint8_t interrupt_pin)
int speed
Definition: fan.h:35
network::IPAddress get_dns_address(uint8_t num)
static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
constexpr uint8_t KSZ80XX_PC2R_REG_ADDR
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
EthernetComponent * global_eth_component
void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio)
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void status_clear_warning()
Definition: component.cpp:166
uint8_t type
Application App
Global storage of Application pointer - only one Application can exist.
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:202
std::array< IPAddress, 5 > IPAddresses
Definition: ip_address.h:141
void set_use_address(const std::string &use_address)
uint8_t status
Definition: bl0942.h:74
void add_phy_register(PHYRegister register_value)
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
in_addr ip_addr_t
Definition: ip_address.h:22
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t event_id
Definition: tt21100.cpp:15
void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data)
Set arbitratry PHY registers from config.
std::vector< PHYRegister > phy_registers_
std::string str_snprintf(const char *fmt, size_t len,...)
Definition: helpers.cpp:296
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Definition: helpers.cpp:660