ESPHome 2026.8.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_ETHERNET
11
12#ifdef USE_ESP32
13#include "esp_eth.h"
14#ifdef USE_ETHERNET_SPI
15#include "hal/spi_types.h"
16#endif
17#include "esp_eth_mac.h"
18#include "esp_eth_mac_esp.h"
19#include "esp_netif.h"
20#include "esp_mac.h"
21#include "esp_idf_version.h"
22
23#if CONFIG_ETH_USE_ESP32_EMAC
24extern "C" eth_esp32_emac_config_t eth_esp32_emac_default_config(void);
25#endif
26#endif // USE_ESP32
27
28#ifdef USE_RP2
29#if defined(USE_ETHERNET_W5500)
30#include <W5500lwIP.h>
31#elif defined(USE_ETHERNET_W5100)
32#include <W5100lwIP.h>
33#elif defined(USE_ETHERNET_W6100)
34#include <W6100lwIP.h>
35#elif defined(USE_ETHERNET_W6300)
36#include <W6300lwIP.h>
37// W6300 uses PIO QSPI, not Arduino SPI. The upstream Wiznet6300 class
38// incorrectly returns needsSPI()=true, causing LwipIntfDev::begin() to
39// call SPI.begin() which claims GPIOs that PIO QSPI needs.
40// This wrapper hides needsSPI() with a version returning false.
41class Wiznet6300NoSPI : public Wiznet6300 {
42 public:
43 using Wiznet6300::Wiznet6300;
44 constexpr bool needsSPI() const { return false; }
45};
46using Wiznet6300lwIPFixed = LwipIntfDev<Wiznet6300NoSPI>;
47#elif defined(USE_ETHERNET_ENC28J60)
48#include <ENC28J60lwIP.h>
49#else
50#error "Unsupported RP2040 SPI Ethernet type"
51#endif
52#endif
53
54namespace esphome::ethernet {
55
56#ifdef USE_ETHERNET_IP_STATE_LISTENERS
66 public:
67 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
68 const network::IPAddress &dns2) = 0;
69};
70#endif // USE_ETHERNET_IP_STATE_LISTENERS
71
92
100
106
107enum class EthernetComponentState : uint8_t {
108 STOPPED,
110 CONNECTED,
111};
112
113// Platform-neutral duplex/speed types
114#ifndef USE_ESP32
115// NOLINTBEGIN(readability-identifier-naming)
118// NOLINTEND(readability-identifier-naming)
119#endif
120
121class EthernetComponent final : public Component {
122 public:
124 void setup() override;
125 void loop() override;
126 void dump_config() override;
127 float get_setup_priority() const override;
128 void on_powerdown() override { powerdown(); }
130
131 // Per-interface lifecycle (parallels WiFiComponent::enable/disable/is_disabled).
132 // enable_on_boot defaults to true; when false, setup() runs all the driver/netif
133 // installation but skips esp_eth_start(), keeping the link cold until enable() is
134 // called. This is the primary lever for memory reclamation in multi-interface
135 // configurations where only one interface should carry traffic at a time.
136 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
137 void enable();
138 void disable();
139 bool is_disabled() { return this->disabled_; }
140 bool is_enabled() { return !this->disabled_; }
141
143#ifdef USE_ETHERNET_MANUAL_IP
144 void set_manual_ip(const ManualIP &manual_ip);
145#endif
146 void set_fixed_mac(const std::array<uint8_t, 6> &mac) { this->fixed_mac_ = mac; }
147
152 const char *get_use_address() const { return this->use_address_; }
153 void set_use_address(const char *use_address) { this->use_address_ = use_address; }
154 void get_eth_mac_address_raw(uint8_t *mac);
155 // Remove before 2026.9.0
156 ESPDEPRECATED("Use get_eth_mac_address_pretty_into_buffer() instead. Removed in 2026.9.0", "2026.3.0")
157 std::string get_eth_mac_address_pretty();
158 const char *get_eth_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
161 bool powerdown();
162
163#ifdef USE_ESP32
164 esp_eth_handle_t get_eth_handle() const { return this->eth_handle_; }
165
166#ifdef USE_ETHERNET_SPI
167 void set_clk_pin(uint8_t clk_pin);
168 void set_miso_pin(uint8_t miso_pin);
169 void set_mosi_pin(uint8_t mosi_pin);
170 void set_cs_pin(uint8_t cs_pin);
171 void set_interrupt_pin(uint8_t interrupt_pin);
172 void set_reset_pin(uint8_t reset_pin);
173 void set_clock_speed(int clock_speed);
174 void set_interface(spi_host_device_t interface);
175#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
176 void set_polling_interval(uint32_t polling_interval);
177#endif
178#else
179 void set_phy_addr(uint8_t phy_addr);
180 void set_power_pin(int power_pin);
181 void set_mdc_pin(uint8_t mdc_pin);
182 void set_mdio_pin(uint8_t mdio_pin);
183 void set_clk_pin(uint8_t clk_pin);
184 void set_clk_mode(emac_rmii_clock_mode_t clk_mode);
185 void add_phy_register(PHYRegister register_value);
186#endif // USE_ETHERNET_SPI
187#endif // USE_ESP32
188
189#ifdef USE_RP2
190 void set_clk_pin(uint8_t clk_pin);
191 void set_miso_pin(uint8_t miso_pin);
192 void set_mosi_pin(uint8_t mosi_pin);
193 void set_cs_pin(uint8_t cs_pin);
194 void set_interrupt_pin(int8_t interrupt_pin);
195 void set_reset_pin(int8_t reset_pin);
196#endif // USE_RP2
197
198#ifdef USE_ETHERNET_IP_STATE_LISTENERS
199 void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
200#endif
201
202#ifdef USE_ETHERNET_CONNECT_TRIGGER
204#endif
205#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
207#endif
208
209 protected:
210 void start_connect_();
211 void finish_connect_();
213
214#ifdef USE_ESP32
215 // ESP-IDF only: defers the SPI bus init, netif creation, MAC/PHY install, driver
216 // install, netif attach, and event handler registration (which together allocate
217 // ~3-8KB of DMA-capable internal SRAM via SPI driver state + eth driver RX queue)
218 // until ethernet actually needs to come up. Idempotent — guarded by the
219 // ethernet_initialized_ flag. Called from setup() when enable_on_boot_=true, or
220 // from enable() on first runtime enable. Mirrors wifi_lazy_init_() in WiFi.
221 void ethernet_lazy_init_();
222#endif
223
224#ifdef USE_ETHERNET_IP_STATE_LISTENERS
226#endif
227
228#ifdef USE_ESP32
229 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
230 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
231#if LWIP_IPV6
232 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
233#endif /* LWIP_IPV6 */
234 void log_error_and_mark_failed_(esp_err_t err, const char *message);
235#ifdef USE_ETHERNET_KSZ8081
237 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
238#endif
239#ifdef USE_ETHERNET_YT8531
242 void yt8531_phy_init_();
243#endif
245 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
246
247#ifdef USE_ETHERNET_SPI
248 uint8_t clk_pin_;
249 uint8_t miso_pin_;
250 uint8_t mosi_pin_;
251 uint8_t cs_pin_;
253 int reset_pin_{-1};
256 spi_host_device_t interface_{SPI2_HOST};
257#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
259#endif
260#else
261 // Group all 32-bit members first
262 int power_pin_{-1};
263 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
264 std::vector<PHYRegister> phy_registers_{};
265
266 // Group all 8-bit members together
267 uint8_t clk_pin_{0};
268 uint8_t phy_addr_{0};
269 uint8_t mdc_pin_{23};
270 uint8_t mdio_pin_{18};
271#endif // USE_ETHERNET_SPI
272
273 // ESP32 pointers
274 esp_netif_t *eth_netif_{nullptr};
275 esp_eth_handle_t eth_handle_;
276 esp_eth_phy_t *phy_{nullptr};
277#endif // USE_ESP32
278
279#ifdef USE_RP2
280 static constexpr uint32_t LINK_CHECK_INTERVAL = 500; // ms between link/IP polls
281#if defined(USE_ETHERNET_W5100)
282 static constexpr uint32_t RESET_DELAY_MS = 150; // W5100S PLL lock time
283#elif defined(USE_ETHERNET_W6300)
284 static constexpr uint32_t RESET_DELAY_MS = 100; // W6300 needs 100ms after hardware reset
285#else
286 static constexpr uint32_t RESET_DELAY_MS = 10;
287#endif
288#if defined(USE_ETHERNET_W5500)
289 Wiznet5500lwIP *eth_{nullptr};
290#elif defined(USE_ETHERNET_W5100)
291 Wiznet5100lwIP *eth_{nullptr};
292#elif defined(USE_ETHERNET_W6100)
293 Wiznet6100lwIP *eth_{nullptr};
294#elif defined(USE_ETHERNET_W6300)
296#elif defined(USE_ETHERNET_ENC28J60)
297 ENC28J60lwIP *eth_{nullptr};
298#else
299#error "Unsupported RP2040 SPI Ethernet type"
300#endif
302 uint8_t clk_pin_;
303 uint8_t miso_pin_;
304 uint8_t mosi_pin_;
305 uint8_t cs_pin_;
306 int8_t interrupt_pin_{-1};
307 int8_t reset_pin_{-1};
308#endif // USE_RP2
309
310 // Common members
311#ifdef USE_ETHERNET_MANUAL_IP
312 optional<ManualIP> manual_ip_{};
313#endif
315
316 // Group all uint8_t types together (enums and bools)
319 bool started_{false};
320 bool connected_{false};
321 bool got_ipv4_address_{false};
322 // Codegen-time YAML option. When false, setup() defers esp_eth_start().
323 bool enable_on_boot_{true};
324 // Mirror of "is the link intentionally stopped" — set when setup() honors
325 // enable_on_boot=false, cleared by enable(), set again by disable().
326 bool disabled_{false};
327#ifdef USE_ESP32
328 // Tracks whether ethernet_lazy_init_() has completed successfully. Allows enable()
329 // to be called at runtime after enable_on_boot:false without re-allocating, and
330 // ensures setup() skips the heavy init when enable_on_boot_ is false.
332#endif
333#if LWIP_IPV6
334 uint8_t ipv6_count_{0};
335 bool ipv6_setup_done_{false};
336#endif /* LWIP_IPV6 */
337
338 optional<std::array<uint8_t, 6>> fixed_mac_;
339
340#ifdef USE_ETHERNET_IP_STATE_LISTENERS
342#endif
343
344#ifdef USE_ETHERNET_CONNECT_TRIGGER
346#endif
347#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
349#endif
350 private:
351 // Stores a pointer to a string literal (static storage duration).
352 // ONLY set from Python-generated code with string literals - never dynamic strings.
353 const char *use_address_{nullptr};
354};
355
356// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
357extern EthernetComponent *global_eth_component;
358
359#ifdef USE_ESP32
360#if defined(USE_ETHERNET_JL1101) && (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0) || \
361 ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO))
362extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
363#endif
364#endif // USE_ESP32
365
366} // namespace esphome::ethernet
367
368#endif // USE_ETHERNET
constexpr bool needsSPI() const
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:222
void set_enable_on_boot(bool enable_on_boot)
void set_interface(spi_host_device_t interface)
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 yt8531_phy_init_()
Apply YT8531-specific config: re-enable auto-negotiation (disabled on reset) and set the RGMII Tx/Rx ...
void set_manual_ip(const ManualIP &manual_ip)
static constexpr uint32_t LINK_CHECK_INTERVAL
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)
static constexpr uint32_t RESET_DELAY_MS
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
Returns nullptr when no explicit use_address is configured and the address is derived at runtime from...
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 LogString * message
Definition component.cpp:35
uint16_t type
eth_esp32_emac_config_t eth_esp32_emac_default_config(void)
LwipIntfDev< Wiznet6300NoSPI > Wiznet6300lwIPFixed
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:299
ESPDEPRECATED("Use LightState::gamma_correct_lut() instead. Removed in 2026.9.0.", "2026.3.0") float gamma_correct(float value
Applies gamma correction of gamma to value.
STL namespace.
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