57 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
64#ifdef USE_ETHERNET_SPI
66 gpio_install_isr_service(0);
68 spi_bus_config_t buscfg = {
83#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
84 defined(USE_ESP32_VARIANT_ESP32C6)
85 auto host = SPI2_HOST;
87 auto host = SPI3_HOST;
90 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
91 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
94 err = esp_netif_init();
95 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
96 err = esp_event_loop_create_default();
97 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
99 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
103 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
104 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
106#ifdef USE_ETHERNET_SPI
107 spi_device_interface_config_t devcfg = {
113 .cs_ena_pretrans = 0,
114 .cs_ena_posttrans = 0,
124#if CONFIG_ETH_SPI_ETHERNET_W5500
125 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
127#if CONFIG_ETH_SPI_ETHERNET_DM9051
128 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
131#if CONFIG_ETH_SPI_ETHERNET_W5500
133#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
138#if CONFIG_ETH_SPI_ETHERNET_DM9051
140#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
148 esp_eth_mac_t *mac =
nullptr;
149#elif defined(USE_ETHERNET_OPENETH)
150 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
155 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
156#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
157 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
158 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
160 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
161 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
163 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
164 esp32_emac_config.clock_config.rmii.clock_gpio = (emac_rmii_clock_gpio_t) this->
clk_pin_;
166 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
169 switch (this->
type_) {
170#ifdef USE_ETHERNET_OPENETH
172 phy_config.autonego_timeout_ms = 1000;
173 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
177#if CONFIG_ETH_USE_ESP32_EMAC
179 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
183 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
187 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
191 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
200 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
204#ifdef USE_ETHERNET_SPI
205#if CONFIG_ETH_SPI_ETHERNET_W5500
207 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
208 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
212#if CONFIG_ETH_SPI_ETHERNET_DM9051
214 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
215 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
226 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
228 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
229 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
231#ifndef USE_ETHERNET_SPI
232#ifdef USE_ETHERNET_KSZ8081
246 esp_read_mac(mac_addr, ESP_MAC_ETH);
247 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
248 ESPHL_ERROR_CHECK(err,
"set mac address error");
252 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
256 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
258 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
261 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
266 ESPHL_ERROR_CHECK(err,
"ETH start error");
528 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
531 esp_netif_ip_info_t info;
539 info.netmask.addr = 0;
542 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
544 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
545 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
547 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
550 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
551 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
554 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
555 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
562 dns_setserver(0, &d);
567 dns_setserver(1, &d);
570 err = esp_netif_dhcpc_start(this->
eth_netif_);
571 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
572 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
583 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
585 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
587 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
594 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
732 uint32_t phy_control_2;
733 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
734 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
735 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
746 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
747 phy_control_2 |= 1 << 7;
748 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
749 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
750 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
751 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
752 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());