3#if defined(USE_ETHERNET) && defined(USE_ESP32)
16#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
17#ifdef USE_ETHERNET_LAN8720
18#include "esp_eth_phy_lan87xx.h"
20#ifdef USE_ETHERNET_RTL8201
21#include "esp_eth_phy_rtl8201.h"
23#ifdef USE_ETHERNET_DP83848
24#include "esp_eth_phy_dp83848.h"
26#ifdef USE_ETHERNET_IP101
27#include "esp_eth_phy_ip101.h"
29#ifdef USE_ETHERNET_KSZ8081
30#include "esp_eth_phy_ksz80xx.h"
32#ifdef USE_ETHERNET_W5500
33#include "esp_eth_mac_w5500.h"
34#include "esp_eth_phy_w5500.h"
36#ifdef USE_ETHERNET_DM9051
37#include "esp_eth_mac_dm9051.h"
38#include "esp_eth_phy_dm9051.h"
43#ifdef USE_ETHERNET_LAN8670
44#include "esp_eth_phy_lan867x.h"
48#ifdef USE_ETHERNET_ENC28J60
49#include "esp_eth_enc28j60.h"
52#ifdef USE_ETHERNET_SPI
53#include <driver/gpio.h>
54#include <driver/spi_master.h>
59static const char *
const TAG =
"ethernet";
62static constexpr size_t PHY_REG_SIZE = 2;
65 ESP_LOGE(TAG,
"%s: (%d) %s",
message, err, esp_err_to_name(err));
69#define ESPHL_ERROR_CHECK(err, message) \
70 if ((err) != ESP_OK) { \
71 this->log_error_and_mark_failed_(err, message); \
75#define ESPHL_ERROR_CHECK_RET(err, message, ret) \
76 if ((err) != ESP_OK) { \
77 this->log_error_and_mark_failed_(err, message); \
87 ESP_LOGI(TAG,
"Starting connection");
94 ESP_LOGI(TAG,
"Stopped connection");
98 ESP_LOGI(TAG,
"Connected");
103#ifdef USE_ETHERNET_CONNECT_TRIGGER
107 ESP_LOGW(TAG,
"Connecting failed; reconnecting");
113 ESP_LOGI(TAG,
"Stopped connection");
115#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
119 ESP_LOGW(TAG,
"Connection lost; reconnecting");
122#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
135 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
142#ifdef USE_ETHERNET_SPI
144 gpio_install_isr_service(0);
146 spi_bus_config_t buscfg = {
156 .max_transfer_sz = 0,
161#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || defined(USE_ESP32_VARIANT_ESP32C6) || \
162 defined(USE_ESP32_VARIANT_ESP32C61) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
163 auto host = SPI2_HOST;
165 auto host = SPI3_HOST;
168 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
169 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
172 err = esp_netif_init();
173 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
174 err = esp_event_loop_create_default();
175 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
177 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
181 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
182 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
184#ifdef USE_ETHERNET_SPI
185 spi_device_interface_config_t devcfg = {
191 .cs_ena_pretrans = 0,
192 .cs_ena_posttrans = 0,
202#if defined(USE_ETHERNET_W5500)
203 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
204#elif defined(USE_ETHERNET_DM9051)
205 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
206#elif defined(USE_ETHERNET_ENC28J60)
207 eth_enc28j60_config_t enc28j60_config = ETH_ENC28J60_DEFAULT_CONFIG(host, &devcfg);
210#if defined(USE_ETHERNET_W5500)
212#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
215#elif defined(USE_ETHERNET_DM9051)
217#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
220#elif defined(USE_ETHERNET_ENC28J60)
228 esp_eth_mac_t *mac =
nullptr;
229#elif defined(USE_ETHERNET_OPENETH)
230 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
236#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
237 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
238 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
240 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
241 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
243 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
244 esp32_emac_config.clock_config.rmii.clock_gpio =
245 static_cast<decltype(esp32_emac_config.clock_config.rmii.clock_gpio)
>(this->
clk_pin_);
247 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
250 switch (this->
type_) {
251#ifdef USE_ETHERNET_OPENETH
253 phy_config.autonego_timeout_ms = 1000;
254#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
255 this->
phy_ = esp_eth_phy_new_generic(&phy_config);
257 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
262#if CONFIG_ETH_USE_ESP32_EMAC
263#ifdef USE_ETHERNET_LAN8720
265 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
269#ifdef USE_ETHERNET_RTL8201
271 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
275#ifdef USE_ETHERNET_DP83848
277 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
281#ifdef USE_ETHERNET_IP101
283 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
287#ifdef USE_ETHERNET_JL1101
295#ifdef USE_ETHERNET_KSZ8081
298 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
302#ifdef USE_ETHERNET_LAN8670
304 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
309#ifdef USE_ETHERNET_SPI
310#if defined(USE_ETHERNET_W5500)
312 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
313 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
316#elif defined(USE_ETHERNET_DM9051)
318 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
319 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
322#elif defined(USE_ETHERNET_ENC28J60)
324 mac = esp_eth_mac_new_enc28j60(&enc28j60_config, &mac_config);
325 this->
phy_ = esp_eth_phy_new_enc28j60(&phy_config);
336 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
338 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
339 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
341#ifndef USE_ETHERNET_SPI
342#ifdef USE_ETHERNET_KSZ8081
357 memcpy(mac_addr, this->
fixed_mac_->data(), 6);
359 esp_read_mac(mac_addr, ESP_MAC_ETH);
361 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
362 ESPHL_ERROR_CHECK(err,
"set mac address error");
366 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
370 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
372 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
375 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
380 ESPHL_ERROR_CHECK(err,
"ETH start error");
384 const char *eth_type;
385 switch (this->
type_) {
386#ifdef USE_ETHERNET_LAN8720
388 eth_type =
"LAN8720";
391#ifdef USE_ETHERNET_RTL8201
393 eth_type =
"RTL8201";
396#ifdef USE_ETHERNET_DP83848
398 eth_type =
"DP83848";
401#ifdef USE_ETHERNET_IP101
406#ifdef USE_ETHERNET_JL1101
411#ifdef USE_ETHERNET_KSZ8081
413 eth_type =
"KSZ8081";
417 eth_type =
"KSZ8081RNA";
420#if defined(USE_ETHERNET_W5500)
424#elif defined(USE_ETHERNET_DM9051)
428#elif defined(USE_ETHERNET_ENC28J60)
430 eth_type =
"ENC28J60";
433#ifdef USE_ETHERNET_OPENETH
435 eth_type =
"OPENETH";
438#ifdef USE_ETHERNET_LAN8670
440 eth_type =
"LAN8670";
445 eth_type =
"Unknown";
454#ifdef USE_ETHERNET_SPI
461#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
463 ESP_LOGCONFIG(TAG,
" Polling Interval: %" PRIu32
" ms", this->
polling_interval_);
471 " Clock Speed: %d MHz",
475 ESP_LOGCONFIG(TAG,
" Power Pin: %u", this->
power_pin_);
484 ESP_LOGCONFIG(TAG,
" Type: %s", eth_type);
489 esp_netif_ip_info_t ip;
490 esp_err_t err = esp_netif_get_ip_info(this->
eth_netif_, &ip);
492 ESP_LOGV(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
499 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
501 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
502 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
503 assert(count < addresses.size());
504 for (
int i = 0; i < count; i++) {
514 const ip_addr_t *dns_ip = dns_getserver(num);
519 const char *event_name;
522 case ETHERNET_EVENT_START:
523 event_name =
"ETH started";
527 case ETHERNET_EVENT_STOP:
528 event_name =
"ETH stopped";
533 case ETHERNET_EVENT_CONNECTED:
534 event_name =
"ETH connected";
536#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
542 case ETHERNET_EVENT_DISCONNECTED:
543 event_name =
"ETH disconnected";
551 ESP_LOGV(TAG,
"[Ethernet event] %s (num=%" PRId32
")", event_name, event);
556 ip_event_got_ip_t *
event = (ip_event_got_ip_t *) event_data;
557 const esp_netif_ip_info_t *ip_info = &
event->ip_info;
558 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
560#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
567#ifdef USE_ETHERNET_IP_STATE_LISTENERS
575 ip_event_got_ip6_t *
event = (ip_event_got_ip6_t *) event_data;
576 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
578#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
586#ifdef USE_ETHERNET_IP_STATE_LISTENERS
602 esp_err_t err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
604 ESP_LOGD(TAG,
"IPv6 link-local address created (retry succeeded)");
628 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
631 esp_netif_ip_info_t info;
632#ifdef USE_ETHERNET_MANUAL_IP
642 info.netmask.addr = 0;
645 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
647 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
648 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
650 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
653 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
654 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
657 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
658 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
660#ifdef USE_ETHERNET_MANUAL_IP
666 dns_setserver(0, &d);
671 dns_setserver(1, &d);
676 err = esp_netif_dhcpc_start(this->
eth_netif_);
677 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
678 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
689 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
691 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
693 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
700 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
711 esp_netif_ip_info_t ip;
717 dns_ip1 = dns_getserver(0);
718 dns_ip2 = dns_getserver(1);
722 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
723 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
724 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
725 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
726 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
727 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
736 " Is Full Duplex: %s\n"
741 this->get_eth_mac_address_pretty_into_buffer(mac_buf),
745 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
747 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
748 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
749 for (
int i = 0; i < count; i++) {
750 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
755#ifdef USE_ETHERNET_SPI
763#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
778 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
779 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_MAC error");
782std::string EthernetComponent::get_eth_mac_address_pretty() {
783 char buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
788 std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) {
798 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
799 ESPHL_ERROR_CHECK_RET(err,
"ETH_CMD_G_DUPLEX_MODE error",
ETH_DUPLEX_HALF);
807 ESPHL_ERROR_CHECK_RET(err,
"ETH_CMD_G_SPEED error",
ETH_SPEED_10M);
812 ESP_LOGI(TAG,
"Powering down ethernet PHY");
813 if (this->
phy_ ==
nullptr) {
814 ESP_LOGE(TAG,
"Ethernet PHY not assigned");
820 if (this->
phy_->pwrctl(this->phy_,
false) != ESP_OK) {
821 ESP_LOGE(TAG,
"Error powering down ethernet PHY");
827#ifndef USE_ETHERNET_SPI
829#ifdef USE_ETHERNET_KSZ8081
836 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
837 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
838#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
841 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty_to(hex_buf, (uint8_t *) &phy_control_2, PHY_REG_SIZE));
852 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
853 phy_control_2 |= 1 << 7;
854 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
855 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
856 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
857 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
858 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
867#ifdef USE_ETHERNET_RTL8201
868 constexpr uint8_t eth_phy_psr_reg_addr = 0x1F;
870 ESP_LOGD(TAG,
"Select PHY Register Page: 0x%02" PRIX32, register_data.
page);
871 err = mac->write_phy_reg(mac, this->
phy_addr_, eth_phy_psr_reg_addr, register_data.
page);
872 ESPHL_ERROR_CHECK(err,
"Select PHY Register page failed");
876 ESP_LOGD(TAG,
"Writing PHY reg 0x%02" PRIX32
" = 0x%04" PRIX32, register_data.
address, register_data.
value);
878 ESPHL_ERROR_CHECK(err,
"Writing PHY Register failed");
880#ifdef USE_ETHERNET_RTL8201
882 ESP_LOGD(TAG,
"Select PHY Register Page 0x00");
883 err = mac->write_phy_reg(mac, this->
phy_addr_, eth_phy_psr_reg_addr, 0x0);
884 ESPHL_ERROR_CHECK(err,
"Select PHY Register Page 0 failed");
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
void status_set_warning()
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
constexpr const char * c_str() const
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Trigger disconnect_trigger_
void set_clk_pin(uint8_t clk_pin)
std::vector< PHYRegister > phy_registers_
void get_eth_mac_address_raw(uint8_t *mac)
esp_eth_handle_t eth_handle_
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void set_clock_speed(int clock_speed)
void set_polling_interval(uint32_t polling_interval)
eth_duplex_t get_duplex_mode()
void notify_ip_state_listeners_()
network::IPAddresses get_ip_addresses()
void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data)
Set arbitratry PHY registers from config.
void dump_connect_params_()
static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void log_error_and_mark_failed_(esp_err_t err, const char *message)
EthernetComponentState state_
void set_phy_addr(uint8_t phy_addr)
network::IPAddress get_dns_address(uint8_t num)
void set_reset_pin(uint8_t reset_pin)
void add_phy_register(PHYRegister register_value)
void ksz8081_set_clock_reference_(esp_eth_mac_t *mac)
Set RMII Reference Clock Select bit for KSZ8081.
optional< ManualIP > manual_ip_
eth_speed_t get_link_speed()
void dump_config() override
emac_rmii_clock_mode_t clk_mode_
void set_interrupt_pin(uint8_t interrupt_pin)
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void set_miso_pin(uint8_t miso_pin)
optional< std::array< uint8_t, 6 > > fixed_mac_
void set_cs_pin(uint8_t cs_pin)
void set_mdc_pin(uint8_t mdc_pin)
void set_power_pin(int power_pin)
uint32_t polling_interval_
void set_mdio_pin(uint8_t mdio_pin)
void set_mosi_pin(uint8_t mosi_pin)
void set_clk_mode(emac_rmii_clock_mode_t clk_mode)
ESPDEPRECATED("Use get_eth_mac_address_pretty_into_buffer() instead. Removed in 2026.9.0", "2026.3.0") std const char * get_eth_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
eth_esp32_emac_config_t eth_esp32_emac_default_config(void)
constexpr uint8_t KSZ80XX_PC2R_REG_ADDR
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
EthernetComponent * global_eth_component
@ ETHERNET_TYPE_KSZ8081RNA
std::array< IPAddress, 5 > IPAddresses
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)