ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
ethernet_component.h
Go to the documentation of this file.
1#pragma once
2
5#include "esphome/core/hal.h"
9
10#ifdef USE_ESP32
11
12#include "esp_eth.h"
13#include "esp_eth_mac.h"
14#include "esp_netif.h"
15#include "esp_mac.h"
16#include "esp_idf_version.h"
17
18namespace esphome::ethernet {
19
20#ifdef USE_ETHERNET_IP_STATE_LISTENERS
30 public:
31 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
32 const network::IPAddress &dns2) = 0;
33};
34#endif // USE_ETHERNET_IP_STATE_LISTENERS
35
50
58
60 uint32_t address;
61 uint32_t value;
62 uint32_t page;
63};
64
65enum class EthernetComponentState : uint8_t {
66 STOPPED,
69};
70
72 public:
74 void setup() override;
75 void loop() override;
76 void dump_config() override;
77 float get_setup_priority() const override;
78 void on_powerdown() override { powerdown(); }
79 bool is_connected();
80
81#ifdef USE_ETHERNET_SPI
82 void set_clk_pin(uint8_t clk_pin);
83 void set_miso_pin(uint8_t miso_pin);
84 void set_mosi_pin(uint8_t mosi_pin);
85 void set_cs_pin(uint8_t cs_pin);
86 void set_interrupt_pin(uint8_t interrupt_pin);
87 void set_reset_pin(uint8_t reset_pin);
88 void set_clock_speed(int clock_speed);
89#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
90 void set_polling_interval(uint32_t polling_interval);
91#endif
92#else
93 void set_phy_addr(uint8_t phy_addr);
94 void set_power_pin(int power_pin);
95 void set_mdc_pin(uint8_t mdc_pin);
96 void set_mdio_pin(uint8_t mdio_pin);
97 void set_clk_pin(uint8_t clk_pin);
98 void set_clk_mode(emac_rmii_clock_mode_t clk_mode);
99 void add_phy_register(PHYRegister register_value);
100#endif
102#ifdef USE_ETHERNET_MANUAL_IP
103 void set_manual_ip(const ManualIP &manual_ip);
104#endif
105 void set_fixed_mac(const std::array<uint8_t, 6> &mac) { this->fixed_mac_ = mac; }
106
109 const char *get_use_address() const;
110 void set_use_address(const char *use_address);
111 void get_eth_mac_address_raw(uint8_t *mac);
112 // Remove before 2026.9.0
113 ESPDEPRECATED("Use get_eth_mac_address_pretty_into_buffer() instead. Removed in 2026.9.0", "2026.3.0")
114 std::string get_eth_mac_address_pretty();
115 const char *get_eth_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
116 eth_duplex_t get_duplex_mode();
117 eth_speed_t get_link_speed();
118 bool powerdown();
119
120#ifdef USE_ETHERNET_IP_STATE_LISTENERS
121 void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
122#endif
123
124#ifdef USE_ETHERNET_CONNECT_TRIGGER
126#endif
127#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
129#endif
130 protected:
131 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
132 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
133#if LWIP_IPV6
134 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
135#endif /* LWIP_IPV6 */
136#ifdef USE_ETHERNET_IP_STATE_LISTENERS
138#endif
139
140 void start_connect_();
141 void finish_connect_();
143 void log_error_and_mark_failed_(esp_err_t err, const char *message);
144#ifdef USE_ETHERNET_KSZ8081
146 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
147#endif
149 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
150
151#ifdef USE_ETHERNET_SPI
152 uint8_t clk_pin_;
153 uint8_t miso_pin_;
154 uint8_t mosi_pin_;
155 uint8_t cs_pin_;
157 int reset_pin_{-1};
160#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
161 uint32_t polling_interval_{0};
162#endif
163#else
164 // Group all 32-bit members first
165 int power_pin_{-1};
166 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
167 std::vector<PHYRegister> phy_registers_{};
168
169 // Group all 8-bit members together
170 uint8_t clk_pin_{0};
171 uint8_t phy_addr_{0};
172 uint8_t mdc_pin_{23};
173 uint8_t mdio_pin_{18};
174#endif
175#ifdef USE_ETHERNET_MANUAL_IP
177#endif
179
180 // Group all uint8_t types together (enums and bools)
183 bool started_{false};
184 bool connected_{false};
185 bool got_ipv4_address_{false};
186#if LWIP_IPV6
187 uint8_t ipv6_count_{0};
188 bool ipv6_setup_done_{false};
189#endif /* LWIP_IPV6 */
190
191 // Pointers at the end (naturally aligned)
192 esp_netif_t *eth_netif_{nullptr};
193 esp_eth_handle_t eth_handle_;
194 esp_eth_phy_t *phy_{nullptr};
196
197#ifdef USE_ETHERNET_IP_STATE_LISTENERS
199#endif
200
201#ifdef USE_ETHERNET_CONNECT_TRIGGER
203#endif
204#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
206#endif
207 private:
208 // Stores a pointer to a string literal (static storage duration).
209 // ONLY set from Python-generated code with string literals - never dynamic strings.
210 const char *use_address_{""};
211};
212
213// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
214extern EthernetComponent *global_eth_component;
215
216#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
217extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
218#endif
219
220} // namespace esphome::ethernet
221
222#endif // USE_ESP32
ESPDEPRECATED("Use mark_failed(LOG_STR(\"static string literal\")) instead. Do NOT use .c_str() from temporary " "strings. Will stop working in 2026.6.0", "2025.12.0") void mark_failed(const char *message)
Definition component.h:176
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:209
std::vector< PHYRegister > phy_registers_
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void set_polling_interval(uint32_t polling_interval)
void set_manual_ip(const ManualIP &manual_ip)
void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data)
Set arbitratry PHY registers from config.
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)
void add_ip_state_listener(EthernetIPStateListener *listener)
network::IPAddress get_dns_address(uint8_t num)
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.
StaticVector< EthernetIPStateListener *, ESPHOME_ETHERNET_IP_STATE_LISTENERS > ip_state_listeners_
void set_fixed_mac(const std::array< uint8_t, 6 > &mac)
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)
optional< std::array< uint8_t, 6 > > fixed_mac_
void set_use_address(const char *use_address)
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)
Listener interface for Ethernet IP state changes.
virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1, const network::IPAddress &dns2)=0
const char * message
Definition component.cpp:38
uint16_t type
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
EthernetComponent * global_eth_component
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:187
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
uint8_t event_id
Definition tt21100.cpp:3