199#ifdef USE_ETHERNET_SPI
201 gpio_install_isr_service(0);
203 spi_host_device_t host;
211 spi_bus_config_t buscfg = {
221 .max_transfer_sz = 0,
228 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
229 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
234 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
238 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
239 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
241#ifdef USE_ETHERNET_SPI
242 spi_device_interface_config_t devcfg = {
248 .cs_ena_pretrans = 0,
249 .cs_ena_posttrans = 0,
259#if defined(USE_ETHERNET_W5500)
260 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
261#elif defined(USE_ETHERNET_DM9051)
262 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
263#elif defined(USE_ETHERNET_ENC28J60)
264 eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(host, &devcfg);
265#elif defined(USE_ETHERNET_CH390)
266 eth_ch390_config_t ch390_config = ETH_CH390_DEFAULT_CONFIG(host, &devcfg);
269#if defined(USE_ETHERNET_W5500)
271#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
278#elif defined(USE_ETHERNET_DM9051)
280#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
283#elif defined(USE_ETHERNET_ENC28J60)
286 enc28j60_config.spi_devcfg->cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time((this->
clock_speed_ + 999999) / 1000000);
288#elif defined(USE_ETHERNET_CH390)
290#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
298 esp_eth_mac_t *mac =
nullptr;
299#elif defined(USE_ETHERNET_OPENETH)
300 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
306#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
307 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
308 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
310 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
311 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
317 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
318 esp32_emac_config.clock_config.rmii.clock_gpio =
319 static_cast<decltype(esp32_emac_config.clock_config.rmii.clock_gpio)
>(this->
clk_pin_);
322 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
325 switch (this->
type_) {
326#ifdef USE_ETHERNET_OPENETH
328 phy_config.autonego_timeout_ms = 1000;
329#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
330 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
332 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
337#if CONFIG_ETH_USE_ESP32_EMAC
338#ifdef USE_ETHERNET_LAN8720
340 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
344#ifdef USE_ETHERNET_RTL8201
346 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
350#ifdef USE_ETHERNET_DP83848
352 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
356#ifdef USE_ETHERNET_IP101
358 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
362#ifdef USE_ETHERNET_JL1101
370#ifdef USE_ETHERNET_KSZ8081
373 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
377#ifdef USE_ETHERNET_LAN8670
379 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
383#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
386#ifdef USE_ETHERNET_GENERIC
389#ifdef USE_ETHERNET_YT8531
392#if defined(USE_ETHERNET_GENERIC) || defined(USE_ETHERNET_YT8531)
393 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
398#ifdef USE_ETHERNET_SPI
399#if defined(USE_ETHERNET_W5500)
401 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
402 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
405#elif defined(USE_ETHERNET_DM9051)
407 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
408 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
411#elif defined(USE_ETHERNET_ENC28J60)
413 mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);
414 this->
phy_ = esp_eth_phy_new_enc28j60(&phy_config);
417#elif defined(USE_ETHERNET_CH390)
419 mac = esp_eth_mac_new_ch390(&ch390_config, &mac_config);
420 this->
phy_ = esp_eth_phy_new_ch390(&phy_config);
431 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
433 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
434 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
436#ifndef USE_ETHERNET_SPI
437#ifdef USE_ETHERNET_KSZ8081
448#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
449#ifdef USE_ETHERNET_GENERIC
457 bool autoneg_enable =
true;
458 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
459 ESPHL_ERROR_CHECK(err,
"Enable auto-negotiation failed");
462#ifdef USE_ETHERNET_YT8531
473 uint8_t mac_addr[MAC_ADDRESS_SIZE];
475 memcpy(mac_addr, this->
fixed_mac_->data(), MAC_ADDRESS_SIZE);
477 esp_read_mac(mac_addr, ESP_MAC_ETH);
479 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
480 ESPHL_ERROR_CHECK(err,
"set mac address error");
484 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
485#ifdef USE_ETHERNET_RX_PSRAM
487 if (esp_psram_is_initialized()) {
489 rx_psram_installed = err == ESP_OK;
490 if (!rx_psram_installed) {
491 ESP_LOGW(TAG,
"PSRAM RX path not installed: %s", esp_err_to_name(err));
498 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
500 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
503 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
824 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
827 esp_netif_ip_info_t info;
828#ifdef USE_ETHERNET_MANUAL_IP
838 info.netmask.addr = 0;
841 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
843 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
844 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
846 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
849 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
850 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
853 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
854 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
856#ifdef USE_ETHERNET_MANUAL_IP
863 esp_netif_dns_info_t dns{};
866 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_MAIN, &dns);
868 ESP_LOGE(TAG,
"Set main DNS failed: %s", esp_err_to_name(err));
873 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_BACKUP, &dns);
875 ESP_LOGE(TAG,
"Set backup DNS failed: %s", esp_err_to_name(err));
881 err = esp_netif_dhcpc_start(this->
eth_netif_);
882 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
883 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
894 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
896 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
898 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
905 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
917 ESP_LOGCONFIG(TAG,
" uninitialized/disabled");
920 esp_netif_ip_info_t ip;
926 dns_ip1 = dns_getserver(0);
927 dns_ip2 = dns_getserver(1);
931 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
932 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
933 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
934 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
935 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
936 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
937 uint16_t link_speed = 10;
942#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 1, 0)
943 case ETH_SPEED_1000M:
958 " Is Full Duplex: %s\n"
963 this->get_eth_mac_address_pretty_into_buffer(mac_buf),
967 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
969 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
970 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
971 for (
int i = 0; i < count; i++) {
972 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
1050 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
1051 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
1052#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
1055 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty_to(hex_buf, (uint8_t *) &phy_control_2, PHY_REG_SIZE));
1066 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
1067 phy_control_2 |= 1 << 7;
1068 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
1069 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
1070 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
1071 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
1072 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
1110 bool autoneg_enable =
true;
1111 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
1112 ESPHL_ERROR_CHECK(err,
"YT8531 enable auto-negotiation failed");
1117 esp_eth_phy_reg_rw_data_t phy_reg;
1119 phy_reg.reg_value_p = ®_val;
1123 phy_reg.reg_addr = 0x1E;
1124 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1125 ESPHL_ERROR_CHECK(err,
"YT8531 select Chip_Config failed");
1126 phy_reg.reg_addr = 0x1F;
1127 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1128 ESPHL_ERROR_CHECK(err,
"YT8531 read Chip_Config failed");
1129 reg_val |= (1U << 8);
1130 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1131 ESPHL_ERROR_CHECK(err,
"YT8531 write Chip_Config failed");
1135 phy_reg.reg_addr = 0x1E;
1136 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1137 ESPHL_ERROR_CHECK(err,
"YT8531 select RGMII_Config1 failed");
1138 phy_reg.reg_addr = 0x1F;
1139 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1140 ESPHL_ERROR_CHECK(err,
"YT8531 read RGMII_Config1 failed");
1141 reg_val = (reg_val & ~0x00FFU) | (13U << 4) | (13U << 0);
1142 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1143 ESPHL_ERROR_CHECK(err,
"YT8531 write RGMII_Config1 failed");