64 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
71#ifdef USE_ETHERNET_SPI
73 gpio_install_isr_service(0);
75 spi_bus_config_t buscfg = {
90#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
91 defined(USE_ESP32_VARIANT_ESP32C6)
92 auto host = SPI2_HOST;
94 auto host = SPI3_HOST;
97 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
98 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
101 err = esp_netif_init();
102 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
103 err = esp_event_loop_create_default();
104 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
106 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
110 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
111 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
113#ifdef USE_ETHERNET_SPI
114 spi_device_interface_config_t devcfg = {
120 .cs_ena_pretrans = 0,
121 .cs_ena_posttrans = 0,
131#if CONFIG_ETH_SPI_ETHERNET_W5500
132 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
134#if CONFIG_ETH_SPI_ETHERNET_DM9051
135 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
138#if CONFIG_ETH_SPI_ETHERNET_W5500
140#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
145#if CONFIG_ETH_SPI_ETHERNET_DM9051
147#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
155 esp_eth_mac_t *mac =
nullptr;
156#elif defined(USE_ETHERNET_OPENETH)
157 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
162 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
163#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
164 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
165 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
167 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
168 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
170 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
171 esp32_emac_config.clock_config.rmii.clock_gpio = (emac_rmii_clock_gpio_t) this->
clk_pin_;
173 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
176 switch (this->
type_) {
177#ifdef USE_ETHERNET_OPENETH
179 phy_config.autonego_timeout_ms = 1000;
180 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
184#if CONFIG_ETH_USE_ESP32_EMAC
186 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
190 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
194 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
198 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
207 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
210#ifdef USE_ETHERNET_LAN8670
212 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
217#ifdef USE_ETHERNET_SPI
218#if CONFIG_ETH_SPI_ETHERNET_W5500
220 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
221 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
225#if CONFIG_ETH_SPI_ETHERNET_DM9051
227 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
228 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
239 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
241 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
242 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
244#ifndef USE_ETHERNET_SPI
245#ifdef USE_ETHERNET_KSZ8081
260 memcpy(mac_addr, this->
fixed_mac_->data(), 6);
262 esp_read_mac(mac_addr, ESP_MAC_ETH);
264 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
265 ESPHL_ERROR_CHECK(err,
"set mac address error");
269 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
273 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
275 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
278 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
283 ESPHL_ERROR_CHECK(err,
"ETH start error");
551 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
554 esp_netif_ip_info_t info;
562 info.netmask.addr = 0;
565 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
567 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
568 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
570 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
573 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
574 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
577 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
578 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
585 dns_setserver(0, &d);
590 dns_setserver(1, &d);
593 err = esp_netif_dhcpc_start(this->
eth_netif_);
594 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
595 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
606 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
608 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
610 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
617 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
755 uint32_t phy_control_2;
756 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
757 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
758 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
769 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
770 phy_control_2 |= 1 << 7;
771 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
772 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
773 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
774 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
775 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());