295#ifdef USE_ETHERNET_SPI
297 gpio_install_isr_service(0);
299 spi_host_device_t host;
307 spi_bus_config_t buscfg = {
317 .max_transfer_sz = 0,
324 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
325 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
330 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
334 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
335 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
337#ifdef USE_ETHERNET_SPI
338 spi_device_interface_config_t devcfg = {
344 .cs_ena_pretrans = 0,
345 .cs_ena_posttrans = 0,
355#if defined(USE_ETHERNET_W5500)
356 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
357#elif defined(USE_ETHERNET_DM9051)
358 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
359#elif defined(USE_ETHERNET_ENC28J60)
360 eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(host, &devcfg);
361#elif defined(USE_ETHERNET_CH390)
362 eth_ch390_config_t ch390_config = ETH_CH390_DEFAULT_CONFIG(host, &devcfg);
363#elif defined(USE_ETHERNET_KSZ8851SNL)
364 eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(host, &devcfg);
367#if defined(USE_ETHERNET_W5500)
374#elif defined(USE_ETHERNET_DM9051)
377#elif defined(USE_ETHERNET_ENC28J60)
380 enc28j60_config.spi_devcfg->cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time((this->
clock_speed_ + 999999) / 1000000);
382#elif defined(USE_ETHERNET_CH390)
385#elif defined(USE_ETHERNET_KSZ8851SNL)
393 esp_eth_mac_t *mac =
nullptr;
394#elif defined(USE_ETHERNET_OPENETH)
395 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
401 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
402 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
407 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
408 esp32_emac_config.clock_config.rmii.clock_gpio =
409 static_cast<decltype(esp32_emac_config.clock_config.rmii.clock_gpio)
>(this->
clk_pin_);
412 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
415 switch (this->
type_) {
416#ifdef USE_ETHERNET_OPENETH
418 phy_config.autonego_timeout_ms = 1000;
419#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
420 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
422 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
427#if CONFIG_ETH_USE_ESP32_EMAC
428#ifdef USE_ETHERNET_LAN8720
430 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
434#ifdef USE_ETHERNET_RTL8201
436 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
440#ifdef USE_ETHERNET_DP83848
442 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
446#ifdef USE_ETHERNET_IP101
448 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
452#ifdef USE_ETHERNET_JL1101
460#ifdef USE_ETHERNET_KSZ8081
463 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
467#ifdef USE_ETHERNET_LAN8670
469 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
473#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
476#ifdef USE_ETHERNET_GENERIC
479#ifdef USE_ETHERNET_YT8531
482#if defined(USE_ETHERNET_GENERIC) || defined(USE_ETHERNET_YT8531)
483 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
488#ifdef USE_ETHERNET_SPI
489#if defined(USE_ETHERNET_W5500)
491 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
492 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
495#elif defined(USE_ETHERNET_DM9051)
497 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
498 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
501#elif defined(USE_ETHERNET_ENC28J60)
503 mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);
504 this->
phy_ = esp_eth_phy_new_enc28j60(&phy_config);
507#elif defined(USE_ETHERNET_CH390)
509 mac = esp_eth_mac_new_ch390(&ch390_config, &mac_config);
510 this->
phy_ = esp_eth_phy_new_ch390(&phy_config);
513#elif defined(USE_ETHERNET_KSZ8851SNL)
515 mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
516 this->
phy_ = esp_eth_phy_new_ksz8851snl(&phy_config);
527 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
529 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
530 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
532#ifndef USE_ETHERNET_SPI
533#ifdef USE_ETHERNET_KSZ8081
540#ifdef ESPHOME_ETHERNET_PHY_REGISTER_COUNT
546#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
547#ifdef USE_ETHERNET_GENERIC
555 bool autoneg_enable =
true;
556 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
557 ESPHL_ERROR_CHECK(err,
"Enable auto-negotiation failed");
560#ifdef USE_ETHERNET_YT8531
571 uint8_t mac_addr[MAC_ADDRESS_SIZE];
573 memcpy(mac_addr, this->
fixed_mac_->data(), MAC_ADDRESS_SIZE);
575 esp_read_mac(mac_addr, ESP_MAC_ETH);
577 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
578 ESPHL_ERROR_CHECK(err,
"set mac address error");
582 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
583#ifdef USE_ETHERNET_RX_PSRAM
585 if (esp_psram_is_initialized()) {
587 rx_psram_installed = err == ESP_OK;
588 if (!rx_psram_installed) {
589 ESP_LOGW(TAG,
"PSRAM RX path not installed: %s", esp_err_to_name(err));
593#if USE_NETWORK_IPV6 && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
598 uint8_t all_nodes[6] = {0x33, 0x33, 0x00, 0x00, 0x00, 0x01};
599 if (esp_err_t filter_err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_ADD_MAC_FILTER, all_nodes);
600 filter_err != ESP_OK) {
601 ESP_LOGD(TAG,
"IPv6 all-nodes multicast filter not added: %s", esp_err_to_name(filter_err));
608 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
610 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
613 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
942 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
952 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
956 esp_netif_ip_info_t info{};
957#ifdef USE_ETHERNET_MANUAL_IP
961 info.netmask = manual_ip.
subnet;
964 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
966 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
967 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
969 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
972 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
973 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
976 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
977 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
979#ifdef USE_ETHERNET_MANUAL_IP
985 esp_netif_dns_info_t dns{};
987 dns.ip = manual_ip.
dns1;
988 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_MAIN, &dns);
990 ESP_LOGE(TAG,
"Set main DNS failed: %s", esp_err_to_name(err));
994 dns.ip = manual_ip.
dns2;
995 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_BACKUP, &dns);
997 ESP_LOGE(TAG,
"Set backup DNS failed: %s", esp_err_to_name(err));
1001 err = esp_netif_dhcpc_start(this->
eth_netif_);
1002 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
1003 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
1016 if (err != ESP_OK) {
1017 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
1019 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
1026 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
1038 ESP_LOGCONFIG(TAG,
" uninitialized/disabled");
1042 esp_netif_ip_info_t ip;
1043 esp_netif_get_ip_info(this->
eth_netif_, &ip);
1052 dns_ip1 = dns_getserver(0);
1053 dns_ip2 = dns_getserver(1);
1057 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
1058 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
1059 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
1060 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
1061 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
1062 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
1063 uint16_t link_speed = 10;
1068#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 1, 0)
1069 case ETH_SPEED_1000M:
1083 " MAC Address: %s\n"
1084 " Is Full Duplex: %s\n"
1088 this->get_eth_mac_address_pretty_into_buffer(mac_buf),
1092 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
1095 assert(
count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
1096 for (
int i = 0; i <
count; i++) {
1097 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
1238 bool autoneg_enable =
true;
1239 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
1240 ESPHL_ERROR_CHECK(err,
"YT8531 enable auto-negotiation failed");
1245 esp_eth_phy_reg_rw_data_t phy_reg;
1247 phy_reg.reg_value_p = ®_val;
1251 phy_reg.reg_addr = 0x1E;
1252 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1253 ESPHL_ERROR_CHECK(err,
"YT8531 select Chip_Config failed");
1254 phy_reg.reg_addr = 0x1F;
1255 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1256 ESPHL_ERROR_CHECK(err,
"YT8531 read Chip_Config failed");
1257 reg_val |= (1U << 8);
1258 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1259 ESPHL_ERROR_CHECK(err,
"YT8531 write Chip_Config failed");
1263 phy_reg.reg_addr = 0x1E;
1264 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1265 ESPHL_ERROR_CHECK(err,
"YT8531 select RGMII_Config1 failed");
1266 phy_reg.reg_addr = 0x1F;
1267 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1268 ESPHL_ERROR_CHECK(err,
"YT8531 read RGMII_Config1 failed");
1269 reg_val = (reg_val & ~0x00FFU) | (13U << 4) | (13U << 0);
1270 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1271 ESPHL_ERROR_CHECK(err,
"YT8531 write RGMII_Config1 failed");