170#ifdef USE_ETHERNET_SPI
172 gpio_install_isr_service(0);
174 spi_host_device_t host;
182 spi_bus_config_t buscfg = {
192 .max_transfer_sz = 0,
199 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
200 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
205 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
209 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
210 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
212#ifdef USE_ETHERNET_SPI
213 spi_device_interface_config_t devcfg = {
219 .cs_ena_pretrans = 0,
220 .cs_ena_posttrans = 0,
230#if defined(USE_ETHERNET_W5500)
231 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
232#elif defined(USE_ETHERNET_DM9051)
233 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
234#elif defined(USE_ETHERNET_ENC28J60)
235 eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(host, &devcfg);
236#elif defined(USE_ETHERNET_CH390)
237 eth_ch390_config_t ch390_config = ETH_CH390_DEFAULT_CONFIG(host, &devcfg);
240#if defined(USE_ETHERNET_W5500)
242#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
249#elif defined(USE_ETHERNET_DM9051)
251#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
254#elif defined(USE_ETHERNET_ENC28J60)
257 enc28j60_config.spi_devcfg->cs_ena_posttrans = enc28j60_cal_spi_cs_hold_time((this->
clock_speed_ + 999999) / 1000000);
259#elif defined(USE_ETHERNET_CH390)
261#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
269 esp_eth_mac_t *mac =
nullptr;
270#elif defined(USE_ETHERNET_OPENETH)
271 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
277#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
278 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
279 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
281 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
282 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
288 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
289 esp32_emac_config.clock_config.rmii.clock_gpio =
290 static_cast<decltype(esp32_emac_config.clock_config.rmii.clock_gpio)
>(this->
clk_pin_);
293 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
296 switch (this->
type_) {
297#ifdef USE_ETHERNET_OPENETH
299 phy_config.autonego_timeout_ms = 1000;
300#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
301 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
303 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
308#if CONFIG_ETH_USE_ESP32_EMAC
309#ifdef USE_ETHERNET_LAN8720
311 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
315#ifdef USE_ETHERNET_RTL8201
317 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
321#ifdef USE_ETHERNET_DP83848
323 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
327#ifdef USE_ETHERNET_IP101
329 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
333#ifdef USE_ETHERNET_JL1101
341#ifdef USE_ETHERNET_KSZ8081
344 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
348#ifdef USE_ETHERNET_LAN8670
350 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
354#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
357#ifdef USE_ETHERNET_GENERIC
360#ifdef USE_ETHERNET_YT8531
363#if defined(USE_ETHERNET_GENERIC) || defined(USE_ETHERNET_YT8531)
364 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
369#ifdef USE_ETHERNET_SPI
370#if defined(USE_ETHERNET_W5500)
372 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
373 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
376#elif defined(USE_ETHERNET_DM9051)
378 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
379 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
382#elif defined(USE_ETHERNET_ENC28J60)
384 mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);
385 this->
phy_ = esp_eth_phy_new_enc28j60(&phy_config);
388#elif defined(USE_ETHERNET_CH390)
390 mac = esp_eth_mac_new_ch390(&ch390_config, &mac_config);
391 this->
phy_ = esp_eth_phy_new_ch390(&phy_config);
402 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
404 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
405 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
407#ifndef USE_ETHERNET_SPI
408#ifdef USE_ETHERNET_KSZ8081
419#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
420#ifdef USE_ETHERNET_GENERIC
428 bool autoneg_enable =
true;
429 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
430 ESPHL_ERROR_CHECK(err,
"Enable auto-negotiation failed");
433#ifdef USE_ETHERNET_YT8531
444 uint8_t mac_addr[MAC_ADDRESS_SIZE];
446 memcpy(mac_addr, this->
fixed_mac_->data(), MAC_ADDRESS_SIZE);
448 esp_read_mac(mac_addr, ESP_MAC_ETH);
450 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
451 ESPHL_ERROR_CHECK(err,
"set mac address error");
455 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
459 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
461 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
464 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
778 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
781 esp_netif_ip_info_t info;
782#ifdef USE_ETHERNET_MANUAL_IP
792 info.netmask.addr = 0;
795 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
797 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
798 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
800 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
803 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
804 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
807 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
808 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
810#ifdef USE_ETHERNET_MANUAL_IP
817 esp_netif_dns_info_t dns{};
820 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_MAIN, &dns);
822 ESP_LOGE(TAG,
"Set main DNS failed: %s", esp_err_to_name(err));
827 err = esp_netif_set_dns_info(this->
eth_netif_, ESP_NETIF_DNS_BACKUP, &dns);
829 ESP_LOGE(TAG,
"Set backup DNS failed: %s", esp_err_to_name(err));
835 err = esp_netif_dhcpc_start(this->
eth_netif_);
836 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
837 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
848 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
850 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
852 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
859 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
871 ESP_LOGCONFIG(TAG,
" uninitialized/disabled");
874 esp_netif_ip_info_t ip;
880 dns_ip1 = dns_getserver(0);
881 dns_ip2 = dns_getserver(1);
885 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
886 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
887 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
888 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
889 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
890 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
891 uint16_t link_speed = 10;
896#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 1, 0)
897 case ETH_SPEED_1000M:
912 " Is Full Duplex: %s\n"
917 this->get_eth_mac_address_pretty_into_buffer(mac_buf),
921 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
923 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
924 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
925 for (
int i = 0; i < count; i++) {
926 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
1004 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
1005 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
1006#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
1009 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty_to(hex_buf, (uint8_t *) &phy_control_2, PHY_REG_SIZE));
1020 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
1021 phy_control_2 |= 1 << 7;
1022 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
1023 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
1024 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
1025 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
1026 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
1064 bool autoneg_enable =
true;
1065 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_AUTONEGO, &autoneg_enable);
1066 ESPHL_ERROR_CHECK(err,
"YT8531 enable auto-negotiation failed");
1071 esp_eth_phy_reg_rw_data_t phy_reg;
1073 phy_reg.reg_value_p = ®_val;
1077 phy_reg.reg_addr = 0x1E;
1078 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1079 ESPHL_ERROR_CHECK(err,
"YT8531 select Chip_Config failed");
1080 phy_reg.reg_addr = 0x1F;
1081 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1082 ESPHL_ERROR_CHECK(err,
"YT8531 read Chip_Config failed");
1083 reg_val |= (1U << 8);
1084 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1085 ESPHL_ERROR_CHECK(err,
"YT8531 write Chip_Config failed");
1089 phy_reg.reg_addr = 0x1E;
1090 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1091 ESPHL_ERROR_CHECK(err,
"YT8531 select RGMII_Config1 failed");
1092 phy_reg.reg_addr = 0x1F;
1093 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_READ_PHY_REG, &phy_reg);
1094 ESPHL_ERROR_CHECK(err,
"YT8531 read RGMII_Config1 failed");
1095 reg_val = (reg_val & ~0x00FFU) | (13U << 4) | (13U << 0);
1096 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_WRITE_PHY_REG, &phy_reg);
1097 ESPHL_ERROR_CHECK(err,
"YT8531 write RGMII_Config1 failed");