10 #include "esp_event.h" 12 #ifdef USE_ETHERNET_SPI 13 #include <driver/gpio.h> 14 #include <driver/spi_master.h> 20 static const char *
const TAG =
"ethernet";
24 #define ESPHL_ERROR_CHECK(err, message) \ 25 if ((err) != ESP_OK) { \ 26 ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \ 27 this->mark_failed(); \ 31 #define ESPHL_ERROR_CHECK_RET(err, message, ret) \ 32 if ((err) != ESP_OK) { \ 33 ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \ 34 this->mark_failed(); \ 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 // Configure SPI interface and Ethernet driver for specific SPI module 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");
243 const uint32_t now =
millis();
248 ESP_LOGI(TAG,
"Starting ethernet connection");
255 ESP_LOGI(TAG,
"Stopped ethernet connection");
259 ESP_LOGI(TAG,
"Connected via Ethernet!");
265 ESP_LOGW(TAG,
"Connecting via ethernet failed! Re-connecting...");
271 ESP_LOGI(TAG,
"Stopped ethernet connection");
274 ESP_LOGW(TAG,
"Connection via Ethernet lost! Re-connecting...");
283 const char *eth_type;
284 switch (this->
type_) {
286 eth_type =
"LAN8720";
290 eth_type =
"RTL8201";
294 eth_type =
"DP83848";
306 eth_type =
"KSZ8081";
310 eth_type =
"KSZ8081RNA";
318 eth_type =
"OPENETH";
322 eth_type =
"Unknown";
326 ESP_LOGCONFIG(TAG,
"Ethernet:");
328 #ifdef USE_ETHERNET_SPI 329 ESP_LOGCONFIG(TAG,
" CLK Pin: %u", this->
clk_pin_);
330 ESP_LOGCONFIG(TAG,
" MISO Pin: %u", this->
miso_pin_);
331 ESP_LOGCONFIG(TAG,
" MOSI Pin: %u", this->
mosi_pin_);
332 ESP_LOGCONFIG(TAG,
" CS Pin: %u", this->
cs_pin_);
333 #ifdef USE_ETHERNET_SPI_POLLING_SUPPORT 341 ESP_LOGCONFIG(TAG,
" Reset Pin: %d", this->
reset_pin_);
342 ESP_LOGCONFIG(TAG,
" Clock Speed: %d MHz", this->
clock_speed_ / 1000000);
345 ESP_LOGCONFIG(TAG,
" Power Pin: %u", this->
power_pin_);
347 ESP_LOGCONFIG(TAG,
" MDC Pin: %u", this->
mdc_pin_);
348 ESP_LOGCONFIG(TAG,
" MDIO Pin: %u", this->
mdio_pin_);
349 ESP_LOGCONFIG(TAG,
" PHY addr: %u", this->
phy_addr_);
351 ESP_LOGCONFIG(TAG,
" Type: %s", eth_type);
360 esp_netif_ip_info_t ip;
361 esp_err_t err = esp_netif_get_ip_info(this->
eth_netif_, &ip);
363 ESP_LOGV(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
370 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
372 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
373 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
374 for (
int i = 0; i < count; i++) {
383 const ip_addr_t *dns_ip = dns_getserver(num);
388 const char *event_name;
391 case ETHERNET_EVENT_START:
392 event_name =
"ETH started";
393 global_eth_component->
started_ =
true;
395 case ETHERNET_EVENT_STOP:
396 event_name =
"ETH stopped";
397 global_eth_component->
started_ =
false;
400 case ETHERNET_EVENT_CONNECTED:
401 event_name =
"ETH connected";
403 case ETHERNET_EVENT_DISCONNECTED:
404 event_name =
"ETH disconnected";
411 ESP_LOGV(TAG,
"[Ethernet event] %s (num=%" PRId32
")", event_name, event);
416 ip_event_got_ip_t *
event = (ip_event_got_ip_t *) event_data;
417 const esp_netif_ip_info_t *ip_info = &
event->ip_info;
418 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
420 #if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0) 421 global_eth_component->
connected_ = global_eth_component->
ipv6_count_ >= USE_NETWORK_MIN_IPV6_ADDR_COUNT;
430 ip_event_got_ip6_t *
event = (ip_event_got_ip6_t *) event_data;
431 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
433 #if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0) 453 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
456 esp_netif_ip_info_t info;
464 info.netmask.addr = 0;
467 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
469 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
470 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
472 ESP_LOGV(TAG,
"DHCP Client Status: %d", status);
475 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
476 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
479 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
480 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
486 dns_setserver(0, &d);
491 dns_setserver(1, &d);
494 err = esp_netif_dhcpc_start(this->
eth_netif_);
495 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
496 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
500 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
502 ESPHL_ERROR_CHECK(err,
"Enable IPv6 link local failed");
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);
541 #ifdef USE_ETHERNET_SPI 549 #ifdef USE_ETHERNET_SPI_POLLING_SUPPORT 577 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
578 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_MAC error");
584 return str_snprintf(
"%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
589 eth_duplex_t duplex_mode;
590 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
591 ESPHL_ERROR_CHECK_RET(err,
"ETH_CMD_G_DUPLEX_MODE error", ETH_DUPLEX_HALF);
598 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_SPEED, &speed);
599 ESPHL_ERROR_CHECK_RET(err,
"ETH_CMD_G_SPEED error", ETH_SPEED_10M);
604 ESP_LOGI(TAG,
"Powering down ethernet PHY");
605 if (this->
phy_ ==
nullptr) {
606 ESP_LOGE(TAG,
"Ethernet PHY not assigned");
611 if (this->
phy_->pwrctl(this->phy_,
false) != ESP_OK) {
612 ESP_LOGE(TAG,
"Error powering down ethernet PHY");
618 #ifndef USE_ETHERNET_SPI 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());
651 constexpr uint8_t eth_phy_psr_reg_addr = 0x1F;
654 ESP_LOGD(TAG,
"Select PHY Register Page: 0x%02" PRIX32, register_data.
page);
655 err = mac->write_phy_reg(mac, this->
phy_addr_, eth_phy_psr_reg_addr, register_data.
page);
656 ESPHL_ERROR_CHECK(err,
"Select PHY Register page failed");
659 ESP_LOGD(TAG,
"Writing to PHY Register Address: 0x%02" PRIX32, register_data.
address);
660 ESP_LOGD(TAG,
"Writing to PHY Register Value: 0x%04" PRIX32, register_data.
value);
662 ESPHL_ERROR_CHECK(err,
"Writing PHY Register failed");
665 ESP_LOGD(TAG,
"Select PHY Register Page 0x00");
666 err = mac->write_phy_reg(mac, this->
phy_addr_, eth_phy_psr_reg_addr, 0x0);
667 ESPHL_ERROR_CHECK(err,
"Select PHY Register Page 0 failed");
bool can_proceed() override
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
void set_manual_ip(const ManualIP &manual_ip)
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
void set_power_pin(int power_pin)
void set_mdio_pin(uint8_t mdio_pin)
void status_set_warning(const char *message="unspecified")
void ksz8081_set_clock_reference_(esp_eth_mac_t *mac)
Set RMII Reference Clock Select bit for KSZ8081.
eth_duplex_t get_duplex_mode()
void set_mdc_pin(uint8_t mdc_pin)
void set_interrupt_pin(uint8_t interrupt_pin)
EthernetComponentState state_
network::IPAddress get_dns_address(uint8_t num)
void set_miso_pin(uint8_t miso_pin)
eth_speed_t get_link_speed()
void set_reset_pin(uint8_t reset_pin)
static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void set_clk_pin(uint8_t clk_pin)
constexpr uint8_t KSZ80XX_PC2R_REG_ADDR
void set_mosi_pin(uint8_t mosi_pin)
uint32_t IRAM_ATTR HOT millis()
EthernetComponent * global_eth_component
emac_rmii_clock_gpio_t clk_gpio_
void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio)
void set_type(EthernetType type)
void dump_connect_params_()
network::IPAddresses get_ip_addresses()
void set_clock_speed(int clock_speed)
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void set_cs_pin(uint8_t cs_pin)
void status_clear_warning()
Application App
Global storage of Application pointer - only one Application can exist.
const std::string & get_name() const
Get the name of this Application set by pre_setup().
std::array< IPAddress, 5 > IPAddresses
void set_use_address(const std::string &use_address)
uint32_t polling_interval_
void add_phy_register(PHYRegister register_value)
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
void dump_config() override
void set_phy_addr(uint8_t phy_addr)
emac_rmii_clock_mode_t clk_mode_
void set_polling_interval(uint32_t polling_interval)
std::string get_eth_mac_address_pretty()
void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data)
Set arbitratry PHY registers from config.
std::vector< PHYRegister > phy_registers_
optional< ManualIP > manual_ip_
std::string str_snprintf(const char *fmt, size_t len,...)
float get_setup_priority() const override
void get_eth_mac_address_raw(uint8_t *mac)
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
esp_eth_handle_t eth_handle_
std::string get_use_address() const
void IRAM_ATTR HOT delay(uint32_t ms)
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).