216#ifdef USE_ETHERNET_SPI
218 gpio_install_isr_service(0);
220 spi_host_device_t host;
228 spi_bus_config_t buscfg = {
238 .max_transfer_sz = 0,
245 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
246 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
251 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
255 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
256 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
258#ifdef USE_ETHERNET_SPI
259 spi_device_interface_config_t devcfg = {
265 .cs_ena_pretrans = 0,
266 .cs_ena_posttrans = 0,
276#if defined(USE_ETHERNET_W5500)
277 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
278#elif defined(USE_ETHERNET_DM9051)
279 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
280#elif defined(USE_ETHERNET_ENC28J60)
281 eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(host, &devcfg);
282#elif defined(USE_ETHERNET_CH390)
283 eth_ch390_config_t ch390_config = ETH_CH390_DEFAULT_CONFIG(host, &devcfg);
284#elif defined(USE_ETHERNET_KSZ8851SNL)
285 eth_ksz8851snl_config_t ksz8851snl_config = ETH_KSZ8851SNL_DEFAULT_CONFIG(host, &devcfg);
288#if defined(USE_ETHERNET_W5500)
289#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
291#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
296#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
304#elif defined(USE_ETHERNET_DM9051)
306#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
309#elif defined(USE_ETHERNET_ENC28J60)
312 enc28j60_config.spi_devcfg->cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time((this->
clock_speed_ + 999999) / 1000000);
314#elif defined(USE_ETHERNET_CH390)
316#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
319#elif defined(USE_ETHERNET_KSZ8851SNL)
321#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
329 esp_eth_mac_t *mac =
nullptr;
330#elif defined(USE_ETHERNET_OPENETH)
331 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
337#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
338 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
339 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
341 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
342 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
348 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
349 esp32_emac_config.clock_config.rmii.clock_gpio =
350 static_cast<decltype(esp32_emac_config.clock_config.rmii.clock_gpio)
>(this->
clk_pin_);
353 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
356 switch (this->
type_) {
357#ifdef USE_ETHERNET_OPENETH
359 phy_config.autonego_timeout_ms = 1000;
360#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
361 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
363 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
368#if CONFIG_ETH_USE_ESP32_EMAC
369#ifdef USE_ETHERNET_LAN8720
371 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
375#ifdef USE_ETHERNET_RTL8201
377 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
381#ifdef USE_ETHERNET_DP83848
383 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
387#ifdef USE_ETHERNET_IP101
389 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
393#ifdef USE_ETHERNET_JL1101
401#ifdef USE_ETHERNET_KSZ8081
404 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
408#ifdef USE_ETHERNET_LAN8670
410 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
414#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
417#ifdef USE_ETHERNET_GENERIC
420#ifdef USE_ETHERNET_YT8531
423#if defined(USE_ETHERNET_GENERIC) || defined(USE_ETHERNET_YT8531)
424 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
429#ifdef USE_ETHERNET_SPI
430#if defined(USE_ETHERNET_W5500)
432 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
433 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
436#elif defined(USE_ETHERNET_DM9051)
438 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
439 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
442#elif defined(USE_ETHERNET_ENC28J60)
444 mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);
445 this->
phy_ = esp_eth_phy_new_enc28j60(&phy_config);
448#elif defined(USE_ETHERNET_CH390)
450 mac = esp_eth_mac_new_ch390(&ch390_config, &mac_config);
451 this->
phy_ = esp_eth_phy_new_ch390(&phy_config);
454#elif defined(USE_ETHERNET_KSZ8851SNL)
456 mac = esp_eth_mac_new_ksz8851snl(&ksz8851snl_config, &mac_config);
457 this->
phy_ = esp_eth_phy_new_ksz8851snl(&phy_config);
468 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
470 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
471 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
473#ifndef USE_ETHERNET_SPI
474#ifdef USE_ETHERNET_KSZ8081
481#ifdef ESPHOME_ETHERNET_PHY_REGISTER_COUNT
487#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
488#ifdef USE_ETHERNET_GENERIC
496 bool autoneg_enable =
true;
497 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
498 ESPHL_ERROR_CHECK(err,
"Enable auto-negotiation failed");
501#ifdef USE_ETHERNET_YT8531
512 uint8_t mac_addr[MAC_ADDRESS_SIZE];
514 memcpy(mac_addr, this->
fixed_mac_->data(), MAC_ADDRESS_SIZE);
516 esp_read_mac(mac_addr, ESP_MAC_ETH);
518 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
519 ESPHL_ERROR_CHECK(err,
"set mac address error");
523 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
524#ifdef USE_ETHERNET_RX_PSRAM
526 if (esp_psram_is_initialized()) {
528 rx_psram_installed = err == ESP_OK;
529 if (!rx_psram_installed) {
530 ESP_LOGW(TAG,
"PSRAM RX path not installed: %s", esp_err_to_name(err));
534#if USE_NETWORK_IPV6 && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0)
539 uint8_t all_nodes[6] = {0x33, 0x33, 0x00, 0x00, 0x00, 0x01};
540 if (esp_err_t filter_err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_ADD_MAC_FILTER, all_nodes);
541 filter_err != ESP_OK) {
542 ESP_LOGD(TAG,
"IPv6 all-nodes multicast filter not added: %s", esp_err_to_name(filter_err));
549 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
551 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
554 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
893 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
903 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
906 esp_netif_ip_info_t info{};
907#ifdef USE_ETHERNET_MANUAL_IP
911 info.netmask = manual_ip.
subnet;
914 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
916 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
917 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
919 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
922 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
923 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
926 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
927 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
929#ifdef USE_ETHERNET_MANUAL_IP
935 esp_netif_dns_info_t dns{};
937 dns.ip = manual_ip.
dns1;
938 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_MAIN, &dns);
940 ESP_LOGE(TAG,
"Set main DNS failed: %s", esp_err_to_name(err));
944 dns.ip = manual_ip.
dns2;
945 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_BACKUP, &dns);
947 ESP_LOGE(TAG,
"Set backup DNS failed: %s", esp_err_to_name(err));
951 err = esp_netif_dhcpc_start(this->
eth_netif_);
952 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
953 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
966 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
968 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
975 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
1178 bool autoneg_enable =
true;
1179 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
1180 ESPHL_ERROR_CHECK(err,
"YT8531 enable auto-negotiation failed");
1185 esp_eth_phy_reg_rw_data_t phy_reg;
1187 phy_reg.reg_value_p = ®_val;
1191 phy_reg.reg_addr = 0x1E;
1192 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1193 ESPHL_ERROR_CHECK(err,
"YT8531 select Chip_Config failed");
1194 phy_reg.reg_addr = 0x1F;
1195 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1196 ESPHL_ERROR_CHECK(err,
"YT8531 read Chip_Config failed");
1197 reg_val |= (1U << 8);
1198 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1199 ESPHL_ERROR_CHECK(err,
"YT8531 write Chip_Config failed");
1203 phy_reg.reg_addr = 0x1E;
1204 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1205 ESPHL_ERROR_CHECK(err,
"YT8531 select RGMII_Config1 failed");
1206 phy_reg.reg_addr = 0x1F;
1207 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1208 ESPHL_ERROR_CHECK(err,
"YT8531 read RGMII_Config1 failed");
1209 reg_val = (reg_val & ~0x00FFU) | (13U << 4) | (13U << 0);
1210 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1211 ESPHL_ERROR_CHECK(err,
"YT8531 write RGMII_Config1 failed");