ESPHome 2026.3.0
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_eth_mac_esp.h"
15#include "esp_netif.h"
16#include "esp_mac.h"
17#include "esp_idf_version.h"
18
19#if CONFIG_ETH_USE_ESP32_EMAC
20extern "C" eth_esp32_emac_config_t eth_esp32_emac_default_config(void);
21#endif
22
23namespace esphome::ethernet {
24
25#ifdef USE_ETHERNET_IP_STATE_LISTENERS
35 public:
36 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
37 const network::IPAddress &dns2) = 0;
38};
39#endif // USE_ETHERNET_IP_STATE_LISTENERS
40
55
63
69
70enum class EthernetComponentState : uint8_t {
71 STOPPED,
74};
75
77 public:
79 void setup() override;
80 void loop() override;
81 void dump_config() override;
82 float get_setup_priority() const override;
83 void on_powerdown() override { powerdown(); }
85
86#ifdef USE_ETHERNET_SPI
87 void set_clk_pin(uint8_t clk_pin);
88 void set_miso_pin(uint8_t miso_pin);
89 void set_mosi_pin(uint8_t mosi_pin);
90 void set_cs_pin(uint8_t cs_pin);
91 void set_interrupt_pin(uint8_t interrupt_pin);
92 void set_reset_pin(uint8_t reset_pin);
93 void set_clock_speed(int clock_speed);
94#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
95 void set_polling_interval(uint32_t polling_interval);
96#endif
97#else
98 void set_phy_addr(uint8_t phy_addr);
99 void set_power_pin(int power_pin);
100 void set_mdc_pin(uint8_t mdc_pin);
101 void set_mdio_pin(uint8_t mdio_pin);
102 void set_clk_pin(uint8_t clk_pin);
103 void set_clk_mode(emac_rmii_clock_mode_t clk_mode);
104 void add_phy_register(PHYRegister register_value);
105#endif
107#ifdef USE_ETHERNET_MANUAL_IP
108 void set_manual_ip(const ManualIP &manual_ip);
109#endif
110 void set_fixed_mac(const std::array<uint8_t, 6> &mac) { this->fixed_mac_ = mac; }
111
114 const char *get_use_address() const;
115 void set_use_address(const char *use_address);
116 void get_eth_mac_address_raw(uint8_t *mac);
117 // Remove before 2026.9.0
118 ESPDEPRECATED("Use get_eth_mac_address_pretty_into_buffer() instead. Removed in 2026.9.0", "2026.3.0")
119 std::string get_eth_mac_address_pretty();
120 const char *get_eth_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
121 eth_duplex_t get_duplex_mode();
122 eth_speed_t get_link_speed();
123 esp_eth_handle_t get_eth_handle() const { return this->eth_handle_; }
124 bool powerdown();
125
126#ifdef USE_ETHERNET_IP_STATE_LISTENERS
127 void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
128#endif
129
130#ifdef USE_ETHERNET_CONNECT_TRIGGER
132#endif
133#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
135#endif
136 protected:
137 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
138 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
139#if LWIP_IPV6
140 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
141#endif /* LWIP_IPV6 */
142#ifdef USE_ETHERNET_IP_STATE_LISTENERS
144#endif
145
146 void start_connect_();
147 void finish_connect_();
149 void log_error_and_mark_failed_(esp_err_t err, const char *message);
150#ifdef USE_ETHERNET_KSZ8081
152 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
153#endif
155 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
156
157#ifdef USE_ETHERNET_SPI
158 uint8_t clk_pin_;
159 uint8_t miso_pin_;
160 uint8_t mosi_pin_;
161 uint8_t cs_pin_;
163 int reset_pin_{-1};
166#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
168#endif
169#else
170 // Group all 32-bit members first
171 int power_pin_{-1};
172 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
173 std::vector<PHYRegister> phy_registers_{};
174
175 // Group all 8-bit members together
176 uint8_t clk_pin_{0};
177 uint8_t phy_addr_{0};
178 uint8_t mdc_pin_{23};
179 uint8_t mdio_pin_{18};
180#endif
181#ifdef USE_ETHERNET_MANUAL_IP
182 optional<ManualIP> manual_ip_{};
183#endif
185
186 // Group all uint8_t types together (enums and bools)
189 bool started_{false};
190 bool connected_{false};
191 bool got_ipv4_address_{false};
192#if LWIP_IPV6
193 uint8_t ipv6_count_{0};
194 bool ipv6_setup_done_{false};
195#endif /* LWIP_IPV6 */
196
197 // Pointers at the end (naturally aligned)
198 esp_netif_t *eth_netif_{nullptr};
199 esp_eth_handle_t eth_handle_;
200 esp_eth_phy_t *phy_{nullptr};
201 optional<std::array<uint8_t, 6>> fixed_mac_;
202
203#ifdef USE_ETHERNET_IP_STATE_LISTENERS
205#endif
206
207#ifdef USE_ETHERNET_CONNECT_TRIGGER
209#endif
210#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
212#endif
213 private:
214 // Stores a pointer to a string literal (static storage duration).
215 // ONLY set from Python-generated code with string literals - never dynamic strings.
216 const char *use_address_{""};
217};
218
219// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
220extern EthernetComponent *global_eth_component;
221
222#if defined(USE_ETHERNET_JL1101) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO))
223extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
224#endif
225
226} // namespace esphome::ethernet
227
228#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
eth_esp32_emac_config_t eth_esp32_emac_default_config(void)
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
static void uint32_t
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