61 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
68#ifdef USE_ETHERNET_SPI
70 gpio_install_isr_service(0);
72 spi_bus_config_t buscfg = {
87#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
88 defined(USE_ESP32_VARIANT_ESP32C6)
89 auto host = SPI2_HOST;
91 auto host = SPI3_HOST;
94 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
95 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
98 err = esp_netif_init();
99 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
100 err = esp_event_loop_create_default();
101 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
103 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
107 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
108 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
110#ifdef USE_ETHERNET_SPI
111 spi_device_interface_config_t devcfg = {
117 .cs_ena_pretrans = 0,
118 .cs_ena_posttrans = 0,
128#if CONFIG_ETH_SPI_ETHERNET_W5500
129 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
131#if CONFIG_ETH_SPI_ETHERNET_DM9051
132 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
135#if CONFIG_ETH_SPI_ETHERNET_W5500
137#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
142#if CONFIG_ETH_SPI_ETHERNET_DM9051
144#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
152 esp_eth_mac_t *mac =
nullptr;
153#elif defined(USE_ETHERNET_OPENETH)
154 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
159 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
160#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
161 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
162 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
164 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
165 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
167 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
168 esp32_emac_config.clock_config.rmii.clock_gpio = (emac_rmii_clock_gpio_t) this->
clk_pin_;
170 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
173 switch (this->
type_) {
174#ifdef USE_ETHERNET_OPENETH
176 phy_config.autonego_timeout_ms = 1000;
177 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
181#if CONFIG_ETH_USE_ESP32_EMAC
183 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
187 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
191 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
195 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
204 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
207#ifdef USE_ETHERNET_LAN8670
209 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
214#ifdef USE_ETHERNET_SPI
215#if CONFIG_ETH_SPI_ETHERNET_W5500
217 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
218 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
222#if CONFIG_ETH_SPI_ETHERNET_DM9051
224 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
225 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
236 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
238 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
239 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
241#ifndef USE_ETHERNET_SPI
242#ifdef USE_ETHERNET_KSZ8081
256 esp_read_mac(mac_addr, ESP_MAC_ETH);
257 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
258 ESPHL_ERROR_CHECK(err,
"set mac address error");
262 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
266 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
268 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
271 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
276 ESPHL_ERROR_CHECK(err,
"ETH start error");
544 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
547 esp_netif_ip_info_t info;
555 info.netmask.addr = 0;
558 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
560 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
561 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
563 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
566 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
567 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
570 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
571 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
578 dns_setserver(0, &d);
583 dns_setserver(1, &d);
586 err = esp_netif_dhcpc_start(this->
eth_netif_);
587 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
588 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
599 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
601 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
603 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
610 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
748 uint32_t phy_control_2;
749 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
750 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
751 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
762 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
763 phy_control_2 |= 1 << 7;
764 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
765 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
766 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
767 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
768 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());