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