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
244 esp_read_mac(mac_addr, ESP_MAC_ETH);
245 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
246 ESPHL_ERROR_CHECK(err,
"set mac address error");
250 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
254 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
256 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
259 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
264 ESPHL_ERROR_CHECK(err,
"ETH start error");
683 uint32_t phy_control_2;
684 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
685 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
686 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
697 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
698 phy_control_2 |= 1 << 7;
699 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
700 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
701 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
702 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
703 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());