ESPHome  2024.5.3
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 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  if (this->type_ == ETHERNET_TYPE_RTL8201 && this->clk_mode_ == EMAC_CLK_EXT_IN) {
188  // Change in default behavior of RTL8201FI may require register setting to enable external clock
189  this->rtl8201_set_rmii_mode_(mac);
190  }
191 #endif
192 
193  // use ESP internal eth mac
194  uint8_t mac_addr[6];
195  esp_read_mac(mac_addr, ESP_MAC_ETH);
196  err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
197  ESPHL_ERROR_CHECK(err, "set mac address error");
198 
199  /* attach Ethernet driver to TCP/IP stack */
200  err = esp_netif_attach(this->eth_netif_, esp_eth_new_netif_glue(this->eth_handle_));
201  ESPHL_ERROR_CHECK(err, "ETH netif attach error");
202 
203  // Register user defined event handers
204  err = esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &EthernetComponent::eth_event_handler, nullptr);
205  ESPHL_ERROR_CHECK(err, "ETH event handler register error");
206  err = esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &EthernetComponent::got_ip_event_handler, nullptr);
207  ESPHL_ERROR_CHECK(err, "GOT IP event handler register error");
208 #if USE_NETWORK_IPV6
209  err = esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &EthernetComponent::got_ip6_event_handler, nullptr);
210  ESPHL_ERROR_CHECK(err, "GOT IPv6 event handler register error");
211 #endif /* USE_NETWORK_IPV6 */
212 
213  /* start Ethernet driver state machine */
214  err = esp_eth_start(this->eth_handle_);
215  ESPHL_ERROR_CHECK(err, "ETH start error");
216 }
217 
219  const uint32_t now = millis();
220 
221  switch (this->state_) {
223  if (this->started_) {
224  ESP_LOGI(TAG, "Starting ethernet connection");
226  this->start_connect_();
227  }
228  break;
230  if (!this->started_) {
231  ESP_LOGI(TAG, "Stopped ethernet connection");
233  } else if (this->connected_) {
234  // connection established
235  ESP_LOGI(TAG, "Connected via Ethernet!");
237 
238  this->dump_connect_params_();
239  this->status_clear_warning();
240  } else if (now - this->connect_begin_ > 15000) {
241  ESP_LOGW(TAG, "Connecting via ethernet failed! Re-connecting...");
242  this->start_connect_();
243  }
244  break;
246  if (!this->started_) {
247  ESP_LOGI(TAG, "Stopped ethernet connection");
249  } else if (!this->connected_) {
250  ESP_LOGW(TAG, "Connection via Ethernet lost! Re-connecting...");
252  this->start_connect_();
253  }
254  break;
255  }
256 }
257 
259  const char *eth_type;
260  switch (this->type_) {
262  eth_type = "LAN8720";
263  break;
264 
266  eth_type = "RTL8201";
267  break;
268 
270  eth_type = "DP83848";
271  break;
272 
273  case ETHERNET_TYPE_IP101:
274  eth_type = "IP101";
275  break;
276 
278  eth_type = "JL1101";
279  break;
280 
282  eth_type = "KSZ8081";
283  break;
284 
286  eth_type = "KSZ8081RNA";
287  break;
288 
289  case ETHERNET_TYPE_W5500:
290  eth_type = "W5500";
291  break;
292 
293  default:
294  eth_type = "Unknown";
295  break;
296  }
297 
298  ESP_LOGCONFIG(TAG, "Ethernet:");
299  this->dump_connect_params_();
300 #ifdef USE_ETHERNET_SPI
301  ESP_LOGCONFIG(TAG, " CLK Pin: %u", this->clk_pin_);
302  ESP_LOGCONFIG(TAG, " MISO Pin: %u", this->miso_pin_);
303  ESP_LOGCONFIG(TAG, " MOSI Pin: %u", this->mosi_pin_);
304  ESP_LOGCONFIG(TAG, " CS Pin: %u", this->cs_pin_);
305  ESP_LOGCONFIG(TAG, " IRQ Pin: %u", this->interrupt_pin_);
306  ESP_LOGCONFIG(TAG, " Reset Pin: %d", this->reset_pin_);
307  ESP_LOGCONFIG(TAG, " Clock Speed: %d MHz", this->clock_speed_ / 1000000);
308 #else
309  if (this->power_pin_ != -1) {
310  ESP_LOGCONFIG(TAG, " Power Pin: %u", this->power_pin_);
311  }
312  ESP_LOGCONFIG(TAG, " MDC Pin: %u", this->mdc_pin_);
313  ESP_LOGCONFIG(TAG, " MDIO Pin: %u", this->mdio_pin_);
314  ESP_LOGCONFIG(TAG, " PHY addr: %u", this->phy_addr_);
315 #endif
316  ESP_LOGCONFIG(TAG, " Type: %s", eth_type);
317 }
318 
320 
322 
324  network::IPAddresses addresses;
325  esp_netif_ip_info_t ip;
326  esp_err_t err = esp_netif_get_ip_info(this->eth_netif_, &ip);
327  if (err != ESP_OK) {
328  ESP_LOGV(TAG, "esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
329  // TODO: do something smarter
330  // return false;
331  } else {
332  addresses[0] = network::IPAddress(&ip.ip);
333  }
334 #if USE_NETWORK_IPV6
335  struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
336  uint8_t count = 0;
337  count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
338  assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
339  for (int i = 0; i < count; i++) {
340  addresses[i + 1] = network::IPAddress(&if_ip6s[i]);
341  }
342 #endif /* USE_NETWORK_IPV6 */
343 
344  return addresses;
345 }
346 
348  const ip_addr_t *dns_ip = dns_getserver(num);
349  return dns_ip;
350 }
351 
352 void EthernetComponent::eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event, void *event_data) {
353  const char *event_name;
354 
355  switch (event) {
356  case ETHERNET_EVENT_START:
357  event_name = "ETH started";
358  global_eth_component->started_ = true;
359  break;
360  case ETHERNET_EVENT_STOP:
361  event_name = "ETH stopped";
362  global_eth_component->started_ = false;
363  global_eth_component->connected_ = false;
364  break;
365  case ETHERNET_EVENT_CONNECTED:
366  event_name = "ETH connected";
367  break;
368  case ETHERNET_EVENT_DISCONNECTED:
369  event_name = "ETH disconnected";
370  global_eth_component->connected_ = false;
371  break;
372  default:
373  return;
374  }
375 
376  ESP_LOGV(TAG, "[Ethernet event] %s (num=%" PRId32 ")", event_name, event);
377 }
378 
379 void EthernetComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
380  void *event_data) {
381  ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data;
382  const esp_netif_ip_info_t *ip_info = &event->ip_info;
383  ESP_LOGV(TAG, "[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
384  global_eth_component->got_ipv4_address_ = true;
385 #if USE_NETWORK_IPV6
386  global_eth_component->connected_ = global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
387 #else
388  global_eth_component->connected_ = true;
389 #endif /* USE_NETWORK_IPV6 */
390 }
391 
392 #if USE_NETWORK_IPV6
393 void EthernetComponent::got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id,
394  void *event_data) {
395  ip_event_got_ip6_t *event = (ip_event_got_ip6_t *) event_data;
396  ESP_LOGV(TAG, "[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
397  global_eth_component->ipv6_count_ += 1;
398  global_eth_component->connected_ =
399  global_eth_component->got_ipv4_address_ && (global_eth_component->ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT);
400 }
401 #endif /* USE_NETWORK_IPV6 */
402 
404  global_eth_component->got_ipv4_address_ = false;
405 #if USE_NETWORK_IPV6
406  global_eth_component->ipv6_count_ = 0;
407 #endif /* USE_NETWORK_IPV6 */
408  this->connect_begin_ = millis();
409  this->status_set_warning();
410 
411  esp_err_t err;
412  err = esp_netif_set_hostname(this->eth_netif_, App.get_name().c_str());
413  if (err != ERR_OK) {
414  ESP_LOGW(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err));
415  }
416 
417  esp_netif_ip_info_t info;
418  if (this->manual_ip_.has_value()) {
419  info.ip = this->manual_ip_->static_ip;
420  info.gw = this->manual_ip_->gateway;
421  info.netmask = this->manual_ip_->subnet;
422  } else {
423  info.ip.addr = 0;
424  info.gw.addr = 0;
425  info.netmask.addr = 0;
426  }
427 
428  esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT;
429 
430  err = esp_netif_dhcpc_get_status(this->eth_netif_, &status);
431  ESPHL_ERROR_CHECK(err, "DHCPC Get Status Failed!");
432 
433  ESP_LOGV(TAG, "DHCP Client Status: %d", status);
434 
435  err = esp_netif_dhcpc_stop(this->eth_netif_);
436  if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
437  ESPHL_ERROR_CHECK(err, "DHCPC stop error");
438  }
439 
440  err = esp_netif_set_ip_info(this->eth_netif_, &info);
441  ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");
442 
443  if (this->manual_ip_.has_value()) {
444  if (this->manual_ip_->dns1.is_set()) {
445  ip_addr_t d;
446  d = this->manual_ip_->dns1;
447  dns_setserver(0, &d);
448  }
449  if (this->manual_ip_->dns2.is_set()) {
450  ip_addr_t d;
451  d = this->manual_ip_->dns2;
452  dns_setserver(1, &d);
453  }
454  } else {
455  err = esp_netif_dhcpc_start(this->eth_netif_);
456  if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
457  ESPHL_ERROR_CHECK(err, "DHCPC start error");
458  }
459 #if USE_NETWORK_IPV6
460  err = esp_netif_create_ip6_linklocal(this->eth_netif_);
461  if (err != ESP_OK) {
462  ESPHL_ERROR_CHECK(err, "Enable IPv6 link local failed");
463  }
464 #endif /* USE_NETWORK_IPV6 */
465  }
466 
467  this->connect_begin_ = millis();
468  this->status_set_warning();
469 }
470 
472 
474  esp_netif_ip_info_t ip;
475  esp_netif_get_ip_info(this->eth_netif_, &ip);
476  ESP_LOGCONFIG(TAG, " IP Address: %s", network::IPAddress(&ip.ip).str().c_str());
477  ESP_LOGCONFIG(TAG, " Hostname: '%s'", App.get_name().c_str());
478  ESP_LOGCONFIG(TAG, " Subnet: %s", network::IPAddress(&ip.netmask).str().c_str());
479  ESP_LOGCONFIG(TAG, " Gateway: %s", network::IPAddress(&ip.gw).str().c_str());
480 
481  const ip_addr_t *dns_ip1 = dns_getserver(0);
482  const ip_addr_t *dns_ip2 = dns_getserver(1);
483 
484  ESP_LOGCONFIG(TAG, " DNS1: %s", network::IPAddress(dns_ip1).str().c_str());
485  ESP_LOGCONFIG(TAG, " DNS2: %s", network::IPAddress(dns_ip2).str().c_str());
486 
487 #if USE_NETWORK_IPV6
488  struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
489  uint8_t count = 0;
490  count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
491  assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
492  for (int i = 0; i < count; i++) {
493  ESP_LOGCONFIG(TAG, " IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
494  }
495 #endif /* USE_NETWORK_IPV6 */
496 
497  esp_err_t err;
498 
499  uint8_t mac[6];
500  err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_MAC_ADDR, &mac);
501  ESPHL_ERROR_CHECK(err, "ETH_CMD_G_MAC error");
502  ESP_LOGCONFIG(TAG, " MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
503 
504  eth_duplex_t duplex_mode;
505  err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
506  ESPHL_ERROR_CHECK(err, "ETH_CMD_G_DUPLEX_MODE error");
507  ESP_LOGCONFIG(TAG, " Is Full Duplex: %s", YESNO(duplex_mode == ETH_DUPLEX_FULL));
508 
509  eth_speed_t speed;
510  err = esp_eth_ioctl(this->eth_handle_, ETH_CMD_G_SPEED, &speed);
511  ESPHL_ERROR_CHECK(err, "ETH_CMD_G_SPEED error");
512  ESP_LOGCONFIG(TAG, " Link Speed: %u", speed == ETH_SPEED_100M ? 100 : 10);
513 }
514 
515 #ifdef USE_ETHERNET_SPI
516 void EthernetComponent::set_clk_pin(uint8_t clk_pin) { this->clk_pin_ = clk_pin; }
517 void EthernetComponent::set_miso_pin(uint8_t miso_pin) { this->miso_pin_ = miso_pin; }
518 void EthernetComponent::set_mosi_pin(uint8_t mosi_pin) { this->mosi_pin_ = mosi_pin; }
519 void EthernetComponent::set_cs_pin(uint8_t cs_pin) { this->cs_pin_ = cs_pin; }
520 void EthernetComponent::set_interrupt_pin(uint8_t interrupt_pin) { this->interrupt_pin_ = interrupt_pin; }
521 void EthernetComponent::set_reset_pin(uint8_t reset_pin) { this->reset_pin_ = reset_pin; }
522 void EthernetComponent::set_clock_speed(int clock_speed) { this->clock_speed_ = clock_speed; }
523 #else
524 void EthernetComponent::set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; }
525 void EthernetComponent::set_power_pin(int power_pin) { this->power_pin_ = power_pin; }
526 void EthernetComponent::set_mdc_pin(uint8_t mdc_pin) { this->mdc_pin_ = mdc_pin; }
527 void EthernetComponent::set_mdio_pin(uint8_t mdio_pin) { this->mdio_pin_ = mdio_pin; }
528 void EthernetComponent::set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio) {
529  this->clk_mode_ = clk_mode;
530  this->clk_gpio_ = clk_gpio;
531 }
532 #endif
534 void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; }
535 
537  if (this->use_address_.empty()) {
538  return App.get_name() + ".local";
539  }
540  return this->use_address_;
541 }
542 
543 void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
544 
546  ESP_LOGI(TAG, "Powering down ethernet PHY");
547  if (this->phy_ == nullptr) {
548  ESP_LOGE(TAG, "Ethernet PHY not assigned");
549  return false;
550  }
551  this->connected_ = false;
552  this->started_ = false;
553  if (this->phy_->pwrctl(this->phy_, false) != ESP_OK) {
554  ESP_LOGE(TAG, "Error powering down ethernet PHY");
555  return false;
556  }
557  return true;
558 }
559 
560 #ifndef USE_ETHERNET_SPI
561 
562 constexpr uint8_t KSZ80XX_PC2R_REG_ADDR = 0x1F;
563 
565  esp_err_t err;
566 
567  uint32_t phy_control_2;
568  err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
569  ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
570  ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
571 
572  /*
573  * Bit 7 is `RMII Reference Clock Select`. Default is `0`.
574  * KSZ8081RNA:
575  * 0 - clock input to XI (Pin 8) is 25 MHz for RMII – 25 MHz clock mode.
576  * 1 - clock input to XI (Pin 8) is 50 MHz for RMII – 50 MHz clock mode.
577  * KSZ8081RND:
578  * 0 - clock input to XI (Pin 8) is 50 MHz for RMII – 50 MHz clock mode.
579  * 1 - clock input to XI (Pin 8) is 25 MHz (driven clock only, not a crystal) for RMII – 25 MHz clock mode.
580  */
581  if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
582  phy_control_2 |= 1 << 7;
583  err = mac->write_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
584  ESPHL_ERROR_CHECK(err, "Write PHY Control 2 failed");
585  err = mac->read_phy_reg(mac, this->phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
586  ESPHL_ERROR_CHECK(err, "Read PHY Control 2 failed");
587  ESP_LOGVV(TAG, "KSZ8081 PHY Control 2: %s", format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
588  }
589 }
590 constexpr uint8_t RTL8201_RMSR_REG_ADDR = 0x10;
592  esp_err_t err;
593  uint32_t phy_rmii_mode;
594  err = mac->write_phy_reg(mac, this->phy_addr_, 0x1f, 0x07);
595  ESPHL_ERROR_CHECK(err, "Setting Page 7 failed");
596 
597  /*
598  * RTL8201 RMII Mode Setting Register (RMSR)
599  * Page 7 Register 16
600  *
601  * bit 0 Reserved 0
602  * bit 1 Rg_rmii_rxdsel 1 (default)
603  * bit 2 Rg_rmii_rxdv_sel: 0 (default)
604  * bit 3 RMII Mode: 1 (RMII Mode)
605  * bit 4~7 Rg_rmii_rx_offset: 1111 (default)
606  * bit 8~11 Rg_rmii_tx_offset: 1111 (default)
607  * bit 12 Rg_rmii_clkdir: 1 (Input)
608  * bit 13~15 Reserved 000
609  *
610  * Binary: 0001 1111 1111 1010
611  * Hex: 0x1FFA
612  *
613  */
614 
615  err = mac->read_phy_reg(mac, this->phy_addr_, RTL8201_RMSR_REG_ADDR, &(phy_rmii_mode));
616  ESPHL_ERROR_CHECK(err, "Read PHY RMSR Register failed");
617  ESP_LOGV(TAG, "Hardware default RTL8201 RMII Mode Register is: 0x%04X", phy_rmii_mode);
618 
619  err = mac->write_phy_reg(mac, this->phy_addr_, RTL8201_RMSR_REG_ADDR, 0x1FFA);
620  ESPHL_ERROR_CHECK(err, "Setting Register 16 RMII Mode Setting failed");
621 
622  err = mac->read_phy_reg(mac, this->phy_addr_, RTL8201_RMSR_REG_ADDR, &(phy_rmii_mode));
623  ESPHL_ERROR_CHECK(err, "Read PHY RMSR Register failed");
624  ESP_LOGV(TAG, "Setting RTL8201 RMII Mode Register to: 0x%04X", phy_rmii_mode);
625 
626  err = mac->write_phy_reg(mac, this->phy_addr_, 0x1f, 0x0);
627  ESPHL_ERROR_CHECK(err, "Setting Page 0 failed");
628 }
629 
630 #endif
631 
632 } // namespace ethernet
633 } // namespace esphome
634 
635 #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: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:120
void set_interrupt_pin(uint8_t interrupt_pin)
int speed
Definition: fan.h:35
constexpr uint8_t RTL8201_RMSR_REG_ADDR
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
void rtl8201_set_rmii_mode_(esp_eth_mac_t *mac)
Set RMII Mode Setting Register for RTL8201.
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:195
std::array< IPAddress, 5 > IPAddresses
Definition: ip_address.h:139
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:118
in_addr ip_addr_t
Definition: ip_address.h:20
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