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>
25#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
27#ifdef USE_ESP32_VARIANT_ESP32P4
28#undef ETH_ESP32_EMAC_DEFAULT_CONFIG
29#define ETH_ESP32_EMAC_DEFAULT_CONFIG() \
31 .smi_gpio = {.mdc_num = 31, .mdio_num = 52}, .interface = EMAC_DATA_INTERFACE_RMII, \
32 .clock_config = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) 50}}, \
33 .dma_burst_len = ETH_DMA_BURST_LEN_32, .intr_priority = 0, \
35 {.rmii = {.tx_en_num = 49, .txd0_num = 34, .txd1_num = 35, .crs_dv_num = 28, .rxd0_num = 29, .rxd1_num = 30}}, \
36 .clock_config_out_in = {.rmii = {.clock_mode = EMAC_CLK_EXT_IN, .clock_gpio = (emac_rmii_clock_gpio_t) -1}}, \
41static const char *
const TAG =
"ethernet";
44static constexpr size_t PHY_REG_SIZE = 2;
49 ESP_LOGE(TAG,
"%s: (%d) %s",
message, err, esp_err_to_name(err));
53#define ESPHL_ERROR_CHECK(err, message) \
54 if ((err) != ESP_OK) { \
55 this->log_error_and_mark_failed_(err, message); \
59#define ESPHL_ERROR_CHECK_RET(err, message, ret) \
60 if ((err) != ESP_OK) { \
61 this->log_error_and_mark_failed_(err, message); \
68 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
75#ifdef USE_ETHERNET_SPI
77 gpio_install_isr_service(0);
79 spi_bus_config_t buscfg = {
94#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C5) || defined(USE_ESP32_VARIANT_ESP32C6) || \
95 defined(USE_ESP32_VARIANT_ESP32C61) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
96 auto host = SPI2_HOST;
98 auto host = SPI3_HOST;
101 err = spi_bus_initialize(host, &buscfg, SPI_DMA_CH_AUTO);
102 ESPHL_ERROR_CHECK(err,
"SPI bus initialize error");
105 err = esp_netif_init();
106 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
107 err = esp_event_loop_create_default();
108 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
110 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
114 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
115 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
117#ifdef USE_ETHERNET_SPI
118 spi_device_interface_config_t devcfg = {
124 .cs_ena_pretrans = 0,
125 .cs_ena_posttrans = 0,
135#if CONFIG_ETH_SPI_ETHERNET_W5500
136 eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(host, &devcfg);
138#if CONFIG_ETH_SPI_ETHERNET_DM9051
139 eth_dm9051_config_t dm9051_config = ETH_DM9051_DEFAULT_CONFIG(host, &devcfg);
142#if CONFIG_ETH_SPI_ETHERNET_W5500
144#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
149#if CONFIG_ETH_SPI_ETHERNET_DM9051
151#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
159 esp_eth_mac_t *mac =
nullptr;
160#elif defined(USE_ETHERNET_OPENETH)
161 esp_eth_mac_t *mac = esp_eth_mac_new_openeth(&mac_config);
166 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
167#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
168 esp32_emac_config.smi_gpio.mdc_num = this->
mdc_pin_;
169 esp32_emac_config.smi_gpio.mdio_num = this->
mdio_pin_;
171 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
172 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
174 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
175 esp32_emac_config.clock_config.rmii.clock_gpio = (emac_rmii_clock_gpio_t) this->
clk_pin_;
177 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
180 switch (this->
type_) {
181#ifdef USE_ETHERNET_OPENETH
183 phy_config.autonego_timeout_ms = 1000;
184 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
188#if CONFIG_ETH_USE_ESP32_EMAC
189#ifdef USE_ETHERNET_LAN8720
191 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
195#ifdef USE_ETHERNET_RTL8201
197 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
201#ifdef USE_ETHERNET_DP83848
203 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
207#ifdef USE_ETHERNET_IP101
209 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
213#ifdef USE_ETHERNET_JL1101
219#ifdef USE_ETHERNET_KSZ8081
222 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
226#ifdef USE_ETHERNET_LAN8670
228 this->
phy_ = esp_eth_phy_new_lan867x(&phy_config);
233#ifdef USE_ETHERNET_SPI
234#if CONFIG_ETH_SPI_ETHERNET_W5500
236 mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
237 this->
phy_ = esp_eth_phy_new_w5500(&phy_config);
241#if CONFIG_ETH_SPI_ETHERNET_DM9051
243 mac = esp_eth_mac_new_dm9051(&dm9051_config, &mac_config);
244 this->
phy_ = esp_eth_phy_new_dm9051(&phy_config);
255 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
257 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
258 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
260#ifndef USE_ETHERNET_SPI
261#ifdef USE_ETHERNET_KSZ8081
276 memcpy(mac_addr, this->
fixed_mac_->data(), 6);
278 esp_read_mac(mac_addr, ESP_MAC_ETH);
280 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_S_MAC_ADDR, mac_addr);
281 ESPHL_ERROR_CHECK(err,
"set mac address error");
285 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
289 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
291 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
294 ESPHL_ERROR_CHECK(err,
"GOT IPv6 event handler register error");
299 ESPHL_ERROR_CHECK(err,
"ETH start error");
308 ESP_LOGI(TAG,
"Starting connection");
315 ESP_LOGI(TAG,
"Stopped connection");
319 ESP_LOGI(TAG,
"Connected");
324#ifdef USE_ETHERNET_CONNECT_TRIGGER
328 ESP_LOGW(TAG,
"Connecting failed; reconnecting");
334 ESP_LOGI(TAG,
"Stopped connection");
336#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
340 ESP_LOGW(TAG,
"Connection lost; reconnecting");
343#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
356 const char *eth_type;
357 switch (this->
type_) {
358#ifdef USE_ETHERNET_LAN8720
360 eth_type =
"LAN8720";
363#ifdef USE_ETHERNET_RTL8201
365 eth_type =
"RTL8201";
368#ifdef USE_ETHERNET_DP83848
370 eth_type =
"DP83848";
373#ifdef USE_ETHERNET_IP101
378#ifdef USE_ETHERNET_JL1101
383#ifdef USE_ETHERNET_KSZ8081
385 eth_type =
"KSZ8081";
389 eth_type =
"KSZ8081RNA";
392#if CONFIG_ETH_SPI_ETHERNET_W5500
397#if CONFIG_ETH_SPI_ETHERNET_DM9051
402#ifdef USE_ETHERNET_OPENETH
404 eth_type =
"OPENETH";
407#ifdef USE_ETHERNET_LAN8670
409 eth_type =
"LAN8670";
414 eth_type =
"Unknown";
423#ifdef USE_ETHERNET_SPI
430#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
440 " Clock Speed: %d MHz",
444 ESP_LOGCONFIG(TAG,
" Power Pin: %u", this->
power_pin_);
453 ESP_LOGCONFIG(TAG,
" Type: %s", eth_type);
460 esp_netif_ip_info_t ip;
461 esp_err_t err = esp_netif_get_ip_info(this->
eth_netif_, &ip);
463 ESP_LOGV(TAG,
"esp_netif_get_ip_info failed: %s", esp_err_to_name(err));
470 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
472 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
473 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
474 for (
int i = 0; i < count; i++) {
484 const ip_addr_t *dns_ip = dns_getserver(num);
489 const char *event_name;
492 case ETHERNET_EVENT_START:
493 event_name =
"ETH started";
497 case ETHERNET_EVENT_STOP:
498 event_name =
"ETH stopped";
503 case ETHERNET_EVENT_CONNECTED:
504 event_name =
"ETH connected";
506#if defined(USE_ETHERNET_IP_STATE_LISTENERS) && defined(USE_ETHERNET_MANUAL_IP)
512 case ETHERNET_EVENT_DISCONNECTED:
513 event_name =
"ETH disconnected";
521 ESP_LOGV(TAG,
"[Ethernet event] %s (num=%" PRId32
")", event_name, event);
526 ip_event_got_ip_t *
event = (ip_event_got_ip_t *) event_data;
527 const esp_netif_ip_info_t *ip_info = &
event->ip_info;
528 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP " IPSTR, IP2STR(&ip_info->ip));
530#if USE_NETWORK_IPV6 && (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
537#ifdef USE_ETHERNET_IP_STATE_LISTENERS
545 ip_event_got_ip6_t *
event = (ip_event_got_ip6_t *) event_data;
546 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IPv6: " IPV6STR, IPV62STR(event->ip6_info.ip));
548#if (USE_NETWORK_MIN_IPV6_ADDR_COUNT > 0)
556#ifdef USE_ETHERNET_IP_STATE_LISTENERS
562#ifdef USE_ETHERNET_IP_STATE_LISTENERS
568 listener->on_ip_state(ips, dns1, dns2);
583 esp_err_t err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
585 ESP_LOGD(TAG,
"IPv6 link-local address created (retry succeeded)");
609 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
612 esp_netif_ip_info_t info;
613#ifdef USE_ETHERNET_MANUAL_IP
623 info.netmask.addr = 0;
626 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
628 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
629 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
631 ESP_LOGV(TAG,
"DHCP Client Status: %d",
status);
634 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
635 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
638 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
639 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
641#ifdef USE_ETHERNET_MANUAL_IP
647 dns_setserver(0, &d);
652 dns_setserver(1, &d);
657 err = esp_netif_dhcpc_start(this->
eth_netif_);
658 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
659 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
670 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
672 if (err == ESP_ERR_ESP_NETIF_INVALID_PARAMS) {
674 ESPHL_ERROR_CHECK(err,
"esp_netif_create_ip6_linklocal invalid parameters");
681 ESP_LOGW(TAG,
"esp_netif_create_ip6_linklocal failed: %s", esp_err_to_name(err));
694 esp_netif_ip_info_t ip;
700 dns_ip1 = dns_getserver(0);
701 dns_ip2 = dns_getserver(1);
705 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
706 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
707 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
708 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
709 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
710 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
719 " Is Full Duplex: %s\n"
724 this->get_eth_mac_address_pretty_into_buffer(mac_buf),
728 struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
730 count = esp_netif_get_all_ip6(this->
eth_netif_, if_ip6s);
731 assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
732 for (
int i = 0; i < count; i++) {
733 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(if_ip6s[i]));
738#ifdef USE_ETHERNET_SPI
746#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
759#ifdef USE_ETHERNET_MANUAL_IP
771 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_MAC_ADDR, mac);
772 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_MAC error");
776 char buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
781 std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) {
790 eth_duplex_t duplex_mode;
791 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
792 ESPHL_ERROR_CHECK_RET(err,
"ETH_CMD_G_DUPLEX_MODE error", ETH_DUPLEX_HALF);
800 ESPHL_ERROR_CHECK_RET(err,
"ETH_CMD_G_SPEED error", ETH_SPEED_10M);
805 ESP_LOGI(TAG,
"Powering down ethernet PHY");
806 if (this->
phy_ ==
nullptr) {
807 ESP_LOGE(TAG,
"Ethernet PHY not assigned");
813 if (this->
phy_->pwrctl(this->phy_,
false) != ESP_OK) {
814 ESP_LOGE(TAG,
"Error powering down ethernet PHY");
820#ifndef USE_ETHERNET_SPI
822#ifdef USE_ETHERNET_KSZ8081
828 uint32_t phy_control_2;
829 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
830 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
831#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
834 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty_to(hex_buf, (uint8_t *) &phy_control_2, PHY_REG_SIZE));
845 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
846 phy_control_2 |= 1 << 7;
847 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
848 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
849 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
850 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
851 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
860#ifdef USE_ETHERNET_RTL8201
861 constexpr uint8_t eth_phy_psr_reg_addr = 0x1F;
863 ESP_LOGD(TAG,
"Select PHY Register Page: 0x%02" PRIX32, register_data.
page);
864 err = mac->write_phy_reg(mac, this->
phy_addr_, eth_phy_psr_reg_addr, register_data.
page);
865 ESPHL_ERROR_CHECK(err,
"Select PHY Register page failed");
870 "Writing to PHY Register Address: 0x%02" PRIX32
"\n"
871 "Writing to PHY Register Value: 0x%04" PRIX32,
874 ESPHL_ERROR_CHECK(err,
"Writing PHY Register failed");
876#ifdef USE_ETHERNET_RTL8201
878 ESP_LOGD(TAG,
"Select PHY Register Page 0x00");
879 err = mac->write_phy_reg(mac, this->
phy_addr_, eth_phy_psr_reg_addr, 0x0);
880 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.
virtual 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)
std::string get_eth_mac_address_pretty()
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)
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
Providing packet encoding functions for exchanging data with a remote host.
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 IRAM_ATTR 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)