208#ifdef USE_ETHERNET_SPI
210 gpio_install_isr_service(0);
212 spi_host_device_t host;
220 spi_bus_config_t buscfg = {
230 .max_transfer_sz = 0,
237 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
238 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
243 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
247 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
248 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
250#ifdef USE_ETHERNET_SPI
251 spi_device_interface_config_t devcfg = {
257 .cs_ena_pretrans = 0,
258 .cs_ena_posttrans = 0,
268#if defined(USE_ETHERNET_W5500)
269 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
270#elif defined(USE_ETHERNET_DM9051)
271 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
272#elif defined(USE_ETHERNET_ENC28J60)
273 eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(host, &devcfg);
274#elif defined(USE_ETHERNET_CH390)
275 eth_ch390_config_t ch390_config = ETH_CH390_DEFAULT_CONFIG(host, &devcfg);
276#elif defined(USE_ETHERNET_KSZ8851SNL)
277 eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(host, &devcfg);
280#if defined(USE_ETHERNET_W5500)
282#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
289#elif defined(USE_ETHERNET_DM9051)
291#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
294#elif defined(USE_ETHERNET_ENC28J60)
297 enc28j60_config.spi_devcfg->cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time((this->
clock_speed_ + 999999) / 1000000);
299#elif defined(USE_ETHERNET_CH390)
301#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
304#elif defined(USE_ETHERNET_KSZ8851SNL)
306#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
314 esp_eth_mac_t *mac =
nullptr;
315#elif defined(USE_ETHERNET_OPENETH)
316 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
322#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
323 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
324 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
326 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
327 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
333 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
334 esp32_emac_config.clock_config.rmii.clock_gpio =
335 static_cast<decltype(esp32_emac_config.clock_config.rmii.clock_gpio)
>(this->
clk_pin_);
338 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
341 switch (this->
type_) {
342#ifdef USE_ETHERNET_OPENETH
344 phy_config.autonego_timeout_ms = 1000;
345#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
346 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
348 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
353#if CONFIG_ETH_USE_ESP32_EMAC
354#ifdef USE_ETHERNET_LAN8720
356 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
360#ifdef USE_ETHERNET_RTL8201
362 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
366#ifdef USE_ETHERNET_DP83848
368 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
372#ifdef USE_ETHERNET_IP101
374 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
378#ifdef USE_ETHERNET_JL1101
386#ifdef USE_ETHERNET_KSZ8081
389 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
393#ifdef USE_ETHERNET_LAN8670
395 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
399#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
402#ifdef USE_ETHERNET_GENERIC
405#ifdef USE_ETHERNET_YT8531
408#if defined(USE_ETHERNET_GENERIC) || defined(USE_ETHERNET_YT8531)
409 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
414#ifdef USE_ETHERNET_SPI
415#if defined(USE_ETHERNET_W5500)
417 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
418 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
421#elif defined(USE_ETHERNET_DM9051)
423 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
424 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
427#elif defined(USE_ETHERNET_ENC28J60)
429 mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);
430 this->
phy_ = esp_eth_phy_new_enc28j60(&phy_config);
433#elif defined(USE_ETHERNET_CH390)
435 mac = esp_eth_mac_new_ch390(&ch390_config, &mac_config);
436 this->
phy_ = esp_eth_phy_new_ch390(&phy_config);
439#elif defined(USE_ETHERNET_KSZ8851SNL)
441 mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
442 this->
phy_ = esp_eth_phy_new_ksz8851snl(&phy_config);
453 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
455 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
456 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
458#ifndef USE_ETHERNET_SPI
459#ifdef USE_ETHERNET_KSZ8081
466#ifdef ESPHOME_ETHERNET_PHY_REGISTER_COUNT
472#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
473#ifdef USE_ETHERNET_GENERIC
481 bool autoneg_enable =
true;
482 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
483 ESPHL_ERROR_CHECK(err,
"Enable auto-negotiation failed");
486#ifdef USE_ETHERNET_YT8531
497 uint8_t mac_addr[MAC_ADDRESS_SIZE];
499 memcpy(mac_addr, this->
fixed_mac_->data(), MAC_ADDRESS_SIZE);
501 esp_read_mac(mac_addr, ESP_MAC_ETH);
503 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
504 ESPHL_ERROR_CHECK(err,
"set mac address error");
508 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
509#ifdef USE_ETHERNET_RX_PSRAM
511 if (esp_psram_is_initialized()) {
513 rx_psram_installed = err == ESP_OK;
514 if (!rx_psram_installed) {
515 ESP_LOGW(TAG,
"PSRAM RX path not installed: %s", esp_err_to_name(err));
519#if USE_NETWORK_IPV6 && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
524 uint8_t all_nodes[6] = {0x33, 0x33, 0x00, 0x00, 0x00, 0x01};
525 if (esp_err_t filter_err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_ADD_MAC_FILTER, all_nodes);
526 filter_err != ESP_OK) {
527 ESP_LOGD(TAG,
"IPv6 all-nodes multicast filter not added: %s", esp_err_to_name(filter_err));
534 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
536 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
539 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
879 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
889 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
892 esp_netif_ip_info_t info;
893#ifdef USE_ETHERNET_MANUAL_IP
903 info.netmask.addr = 0;
906 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
908 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
909 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
911 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
914 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
915 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
918 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
919 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
921#ifdef USE_ETHERNET_MANUAL_IP
928 esp_netif_dns_info_t dns{};
931 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_MAIN, &dns);
933 ESP_LOGE(TAG,
"Set main DNS failed: %s", esp_err_to_name(err));
938 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_BACKUP, &dns);
940 ESP_LOGE(TAG,
"Set backup DNS failed: %s", esp_err_to_name(err));
946 err = esp_netif_dhcpc_start(this->
eth_netif_);
947 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
948 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
961 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
963 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
970 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
1173 bool autoneg_enable =
true;
1174 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
1175 ESPHL_ERROR_CHECK(err,
"YT8531 enable auto-negotiation failed");
1180 esp_eth_phy_reg_rw_data_t phy_reg;
1182 phy_reg.reg_value_p = ®_val;
1186 phy_reg.reg_addr = 0x1E;
1187 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1188 ESPHL_ERROR_CHECK(err,
"YT8531 select Chip_Config failed");
1189 phy_reg.reg_addr = 0x1F;
1190 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1191 ESPHL_ERROR_CHECK(err,
"YT8531 read Chip_Config failed");
1192 reg_val |= (1U << 8);
1193 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1194 ESPHL_ERROR_CHECK(err,
"YT8531 write Chip_Config failed");
1198 phy_reg.reg_addr = 0x1E;
1199 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1200 ESPHL_ERROR_CHECK(err,
"YT8531 select RGMII_Config1 failed");
1201 phy_reg.reg_addr = 0x1F;
1202 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1203 ESPHL_ERROR_CHECK(err,
"YT8531 read RGMII_Config1 failed");
1204 reg_val = (reg_val & ~0x00FFU) | (13U << 4) | (13U << 0);
1205 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1206 ESPHL_ERROR_CHECK(err,
"YT8531 write RGMII_Config1 failed");