13#ifdef USE_ETHERNET_LAN8670
14#include "esp_eth_phy_lan867x.h"
17#ifdef USE_ETHERNET_SPI
18#include <driver/gpio.h>
19#include <driver/spi_master.h>
24#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
26#ifdef USE_ESP32_VARIANT_ESP32P4
27#undef ETH_ESP32_EMAC_DEFAULT_CONFIG
28#define ETH_ESP32_EMAC_DEFAULT_CONFIG() \
30 .smi_gpio = {.mdc_num = 31, .mdio_num = 52}, .interface = EMAC_DATA_INTERFACE_RMII, \
31 .clock_config = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) 50}}, \
32 .dma_burst_len = ETH_DMA_BURST_LEN_32, .intr_priority = 0, \
34 {.rmii = {.tx_en_num = 49, .txd0_num = 34, .txd1_num = 35, .crs_dv_num = 28, .rxd0_num = 29, .rxd1_num = 30}}, \
35 .clock_config_out_in = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) -1}}, \
40static const char *
const TAG =
"ethernet";
43static constexpr size_t PHY_REG_SIZE = 2;
48 ESP_LOGE(TAG,
"%s: (%d) %s",
message, err, esp_err_to_name(err));
52#define ESPHL_ERROR_CHECK(err, message) \
53 if ((err) != ESP_OK) { \
54 this->log_error_and_mark_failed_(err, message); \
58#define ESPHL_ERROR_CHECK_RET(err, message, ret) \
59 if ((err) != ESP_OK) { \
60 this->log_error_and_mark_failed_(err, message); \
67 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
74#ifdef USE_ETHERNET_SPI
76 gpio_install_isr_service(0);
78 spi_bus_config_t buscfg = {
93#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || defined(USE_ESP32_VARIANT_ESP32C6) || \
94 defined(USE_ESP32_VARIANT_ESP32C61) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
95 auto host = SPI2_HOST;
97 auto host = SPI3_HOST;
100 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
101 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
104 err = esp_netif_init();
105 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
106 err = esp_event_loop_create_default();
107 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
109 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
113 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
114 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
116#ifdef USE_ETHERNET_SPI
117 spi_device_interface_config_t devcfg = {
123 .cs_ena_pretrans = 0,
124 .cs_ena_posttrans = 0,
134#if CONFIG_ETH_SPI_ETHERNET_W5500
135 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
137#if CONFIG_ETH_SPI_ETHERNET_DM9051
138 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
141#if CONFIG_ETH_SPI_ETHERNET_W5500
143#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
148#if CONFIG_ETH_SPI_ETHERNET_DM9051
150#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
158 esp_eth_mac_t *mac =
nullptr;
159#elif defined(USE_ETHERNET_OPENETH)
160 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
165 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
166#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
167 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
168 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
170 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
171 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
173 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
174 esp32_emac_config.clock_config.rmii.clock_gpio = (emac_rmii_clock_gpio_t) this->
clk_pin_;
176 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
179 switch (this->
type_) {
180#ifdef USE_ETHERNET_OPENETH
182 phy_config.autonego_timeout_ms = 1000;
183 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
187#if CONFIG_ETH_USE_ESP32_EMAC
188#ifdef USE_ETHERNET_LAN8720
190 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
194#ifdef USE_ETHERNET_RTL8201
196 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
200#ifdef USE_ETHERNET_DP83848
202 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
206#ifdef USE_ETHERNET_IP101
208 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
212#ifdef USE_ETHERNET_JL1101
218#ifdef USE_ETHERNET_KSZ8081
221 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
225#ifdef USE_ETHERNET_LAN8670
227 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
232#ifdef USE_ETHERNET_SPI
233#if CONFIG_ETH_SPI_ETHERNET_W5500
235 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
236 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
240#if CONFIG_ETH_SPI_ETHERNET_DM9051
242 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
243 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
254 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
256 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
257 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
259#ifndef USE_ETHERNET_SPI
260#ifdef USE_ETHERNET_KSZ8081
275 memcpy(mac_addr, this->
fixed_mac_->data(), 6);
277 esp_read_mac(mac_addr, ESP_MAC_ETH);
279 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
280 ESPHL_ERROR_CHECK(err,
"set mac address error");
284 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
288 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
290 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
293 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
298 ESPHL_ERROR_CHECK(err,
"ETH start error");
307 ESP_LOGI(TAG,
"Starting connection");
314 ESP_LOGI(TAG,
"Stopped connection");
318 ESP_LOGI(TAG,
"Connected");
323#ifdef USE_ETHERNET_CONNECT_TRIGGER
327 ESP_LOGW(TAG,
"Connecting failed; reconnecting");
333 ESP_LOGI(TAG,
"Stopped connection");
335#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
339 ESP_LOGW(TAG,
"Connection lost; reconnecting");
342#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
355 const char *eth_type;
356 switch (this->
type_) {
357#ifdef USE_ETHERNET_LAN8720
359 eth_type =
"LAN8720";
362#ifdef USE_ETHERNET_RTL8201
364 eth_type =
"RTL8201";
367#ifdef USE_ETHERNET_DP83848
369 eth_type =
"DP83848";
372#ifdef USE_ETHERNET_IP101
377#ifdef USE_ETHERNET_JL1101
382#ifdef USE_ETHERNET_KSZ8081
384 eth_type =
"KSZ8081";
388 eth_type =
"KSZ8081RNA";
391#if CONFIG_ETH_SPI_ETHERNET_W5500
396#if CONFIG_ETH_SPI_ETHERNET_DM9051
401#ifdef USE_ETHERNET_OPENETH
403 eth_type =
"OPENETH";
406#ifdef USE_ETHERNET_LAN8670
408 eth_type =
"LAN8670";
413 eth_type =
"Unknown";
422#ifdef USE_ETHERNET_SPI
429#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
439 " Clock Speed: %d MHz",
443 ESP_LOGCONFIG(TAG,
" Power Pin: %u", this->
power_pin_);
452 ESP_LOGCONFIG(TAG,
" Type: %s", eth_type);
459 esp_netif_ip_info_t ip;
460 esp_err_t err = esp_netif_get_ip_info(this->
eth_netif_, &ip);
462 ESP_LOGV(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
469 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
471 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
472 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
473 for (
int i = 0; i < count; i++) {
483 const ip_addr_t *dns_ip = dns_getserver(num);
488 const char *event_name;
491 case ETHERNET_EVENT_START:
492 event_name =
"ETH started";
496 case ETHERNET_EVENT_STOP:
497 event_name =
"ETH stopped";
502 case ETHERNET_EVENT_CONNECTED:
503 event_name =
"ETH connected";
505#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
511 case ETHERNET_EVENT_DISCONNECTED:
512 event_name =
"ETH disconnected";
520 ESP_LOGV(TAG,
"[Ethernet event] %s (num=%" PRId32
")", event_name, event);
525 ip_event_got_ip_t *
event = (ip_event_got_ip_t *) event_data;
526 const esp_netif_ip_info_t *ip_info = &
event->ip_info;
527 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
529#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
536#ifdef USE_ETHERNET_IP_STATE_LISTENERS
544 ip_event_got_ip6_t *
event = (ip_event_got_ip6_t *) event_data;
545 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
547#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
555#ifdef USE_ETHERNET_IP_STATE_LISTENERS
561#ifdef USE_ETHERNET_IP_STATE_LISTENERS
567 listener->on_ip_state(ips, dns1, dns2);
582 esp_err_t err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
584 ESP_LOGD(TAG,
"IPv6 link-local address created (retry succeeded)");
608 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
611 esp_netif_ip_info_t info;
612#ifdef USE_ETHERNET_MANUAL_IP
622 info.netmask.addr = 0;
625 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
627 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
628 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
630 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
633 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
634 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
637 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
638 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
640#ifdef USE_ETHERNET_MANUAL_IP
646 dns_setserver(0, &d);
651 dns_setserver(1, &d);
656 err = esp_netif_dhcpc_start(this->
eth_netif_);
657 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
658 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
669 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
671 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
673 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
680 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
693 esp_netif_ip_info_t ip;
699 dns_ip1 = dns_getserver(0);
700 dns_ip2 = dns_getserver(1);
704 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
705 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
706 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
707 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
708 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
709 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
718 " Is Full Duplex: %s\n"
723 this->get_eth_mac_address_pretty_into_buffer(mac_buf),
727 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
729 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
730 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
731 for (
int i = 0; i < count; i++) {
732 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
737#ifdef USE_ETHERNET_SPI
745#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
758#ifdef USE_ETHERNET_MANUAL_IP
770 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
771 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_MAC error");
774std::string EthernetComponent::get_eth_mac_address_pretty() {
775 char buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
780 std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) {
789 eth_duplex_t duplex_mode;
790 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
791 ESPHL_ERROR_CHECK_RET(err,
"ETH_CMD_G_DUPLEX_MODE error", ETH_DUPLEX_HALF);
799 ESPHL_ERROR_CHECK_RET(err,
"ETH_CMD_G_SPEED error", ETH_SPEED_10M);
804 ESP_LOGI(TAG,
"Powering down ethernet PHY");
805 if (this->
phy_ ==
nullptr) {
806 ESP_LOGE(TAG,
"Ethernet PHY not assigned");
812 if (this->
phy_->pwrctl(this->phy_,
false) != ESP_OK) {
813 ESP_LOGE(TAG,
"Error powering down ethernet PHY");
819#ifndef USE_ETHERNET_SPI
821#ifdef USE_ETHERNET_KSZ8081
827 uint32_t phy_control_2;
828 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
829 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
830#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
833 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty_to(hex_buf, (uint8_t *) &phy_control_2, PHY_REG_SIZE));
844 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
845 phy_control_2 |= 1 << 7;
846 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
847 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
848 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
849 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
850 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
859#ifdef USE_ETHERNET_RTL8201
860 constexpr uint8_t eth_phy_psr_reg_addr = 0x1F;
862 ESP_LOGD(TAG,
"Select PHY Register Page: 0x%02" PRIX32, register_data.
page);
863 err = mac->write_phy_reg(mac, this->
phy_addr_, eth_phy_psr_reg_addr, register_data.
page);
864 ESPHL_ERROR_CHECK(err,
"Select PHY Register page failed");
868 ESP_LOGD(TAG,
"Writing PHY reg 0x%02" PRIX32
" = 0x%04" PRIX32, register_data.
address, register_data.
value);
870 ESPHL_ERROR_CHECK(err,
"Writing PHY Register failed");
872#ifdef USE_ETHERNET_RTL8201
874 ESP_LOGD(TAG,
"Select PHY Register Page 0x00");
875 err = mac->write_phy_reg(mac, this->
phy_addr_, eth_phy_psr_reg_addr, 0x0);
876 ESPHL_ERROR_CHECK(err,
"Select PHY Register Page 0 failed");
const std::string & 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(const char *message=nullptr)
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.
void trigger(const Ts &...x)
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)
float get_setup_priority() const override
eth_duplex_t get_duplex_mode()
void notify_ip_state_listeners_()
void set_manual_ip(const ManualIP &manual_ip)
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 set_type(EthernetType type)
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()
StaticVector< EthernetIPStateListener *, ESPHOME_ETHERNET_IP_STATE_LISTENERS > ip_state_listeners_
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)
const char * get_use_address() const
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)
void set_use_address(const char *use_address)
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)
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)