41 ESP_LOGCONFIG(TAG,
"Setting up Ethernet...");
42 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
49#ifdef USE_ETHERNET_SPI
51 gpio_install_isr_service(0);
53 spi_bus_config_t buscfg = {
68#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
69 defined(USE_ESP32_VARIANT_ESP32C6)
70 auto host = SPI2_HOST;
72 auto host = SPI3_HOST;
75 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
76 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
79 err = esp_netif_init();
80 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
81 err = esp_event_loop_create_default();
82 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
84 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
88 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
89 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
91#ifdef USE_ETHERNET_SPI
92 spi_device_interface_config_t devcfg = {
99 .cs_ena_posttrans = 0,
109#if USE_ESP_IDF && (ESP_IDF_VERSION_MAJOR >= 5)
110 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
112 spi_device_handle_t spi_handle =
nullptr;
113 err = spi_bus_add_device(host, &devcfg, &spi_handle);
114 ESPHL_ERROR_CHECK(err,
"SPI bus add device error");
116 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
119#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
125 esp_eth_mac_t *mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
126#elif defined(USE_ETHERNET_OPENETH)
127 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
132#if ESP_IDF_VERSION_MAJOR >= 5
133 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
134 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
135 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
136 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
137 esp32_emac_config.clock_config.rmii.clock_gpio = this->
clk_gpio_;
139 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
141 mac_config.smi_mdc_gpio_num = this->
mdc_pin_;
142 mac_config.smi_mdio_gpio_num = this->
mdio_pin_;
143 mac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
144 mac_config.clock_config.rmii.clock_gpio = this->
clk_gpio_;
146 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
150 switch (this->
type_) {
151#ifdef USE_ETHERNET_OPENETH
153 phy_config.autonego_timeout_ms = 1000;
154 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
158#if CONFIG_ETH_USE_ESP32_EMAC
160 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
164 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
168 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
172 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
181#if ESP_IDF_VERSION_MAJOR >= 5
182 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
184 this->
phy_ = esp_eth_phy_new_ksz8081(&phy_config);
189#ifdef USE_ETHERNET_SPI
191 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
201 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
203 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
204 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
206#ifndef USE_ETHERNET_SPI
219 esp_read_mac(mac_addr, ESP_MAC_ETH);
220 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
221 ESPHL_ERROR_CHECK(err,
"set mac address error");
225 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
229 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
231 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
234 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
239 ESPHL_ERROR_CHECK(err,
"ETH start error");
513 esp_netif_ip_info_t ip;
516 ESP_LOGCONFIG(TAG,
" Hostname: '%s'",
App.
get_name().c_str());
520 const ip_addr_t *dns_ip1 = dns_getserver(0);
521 const ip_addr_t *dns_ip2 = dns_getserver(1);
527 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
529 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
530 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
531 for (
int i = 0; i < count; i++) {
532 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
537 ESP_LOGCONFIG(TAG,
" Is Full Duplex: %s", YESNO(this->
get_duplex_mode() == ETH_DUPLEX_FULL));
538 ESP_LOGCONFIG(TAG,
" Link Speed: %u", this->
get_link_speed() == ETH_SPEED_100M ? 100 : 10);
625 uint32_t phy_control_2;
626 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
627 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
628 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
639 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
640 phy_control_2 |= 1 << 7;
641 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
642 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
643 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
644 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
645 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());