ESPHome 2026.8.0b4
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
93
101
107
108enum class EthernetComponentState : uint8_t {
109 STOPPED,
111 CONNECTED,
112};
113
114// Platform-neutral duplex/speed types
115#ifndef USE_ESP32
116// NOLINTBEGIN(readability-identifier-naming)
119// NOLINTEND(readability-identifier-naming)
120#endif
121
122class EthernetComponent final : public Component {
123 public:
125 void setup() override;
126 void loop() override;
127 void dump_config() override;
128 float get_setup_priority() const override;
129 void on_powerdown() override { powerdown(); }
131
132 // Per-interface lifecycle (parallels WiFiComponent::enable/disable/is_disabled).
133 // enable_on_boot defaults to true; when false, setup() runs all the driver/netif
134 // installation but skips esp_eth_start(), keeping the link cold until enable() is
135 // called. This is the primary lever for memory reclamation in multi-interface
136 // configurations where only one interface should carry traffic at a time.
137 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
138 void enable();
139 void disable();
140 bool is_disabled() { return this->disabled_; }
141 bool is_enabled() { return !this->disabled_; }
142
143#ifdef USE_ESP32
147#endif
148
150#ifdef USE_ETHERNET_MANUAL_IP
151 void set_manual_ip(const ManualIP &manual_ip);
152#endif
153 void set_fixed_mac(const std::array<uint8_t, MAC_ADDRESS_SIZE> &mac) { this->fixed_mac_ = mac; }
154
159 const char *get_use_address() const { return this->use_address_; }
160 void set_use_address(const char *use_address) { this->use_address_ = use_address; }
161 void get_eth_mac_address_raw(uint8_t *mac);
162 // Remove before 2026.9.0
163 ESPDEPRECATED("Use get_eth_mac_address_pretty_into_buffer() instead. Removed in 2026.9.0", "2026.3.0")
164 std::string get_eth_mac_address_pretty();
165 const char *get_eth_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
168 bool powerdown();
169
170#ifdef USE_ESP32
171 esp_eth_handle_t get_eth_handle() const { return this->eth_handle_; }
172
173#ifdef USE_ETHERNET_SPI
174 void set_clk_pin(uint8_t clk_pin);
175 void set_miso_pin(uint8_t miso_pin);
176 void set_mosi_pin(uint8_t mosi_pin);
177 void set_cs_pin(uint8_t cs_pin);
178 void set_interrupt_pin(uint8_t interrupt_pin);
179 void set_reset_pin(uint8_t reset_pin);
180 void set_clock_speed(int clock_speed);
181 void set_interface(spi_host_device_t interface);
182#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
183 void set_polling_interval(uint32_t polling_interval);
184#endif
185#else
186 void set_phy_addr(uint8_t phy_addr);
187 void set_power_pin(int power_pin);
188 void set_mdc_pin(uint8_t mdc_pin);
189 void set_mdio_pin(uint8_t mdio_pin);
190 void set_clk_pin(uint8_t clk_pin);
191 void set_clk_mode(emac_rmii_clock_mode_t clk_mode);
192 void add_phy_register(PHYRegister register_value);
193#endif // USE_ETHERNET_SPI
194#endif // USE_ESP32
195
196#ifdef USE_RP2
197 void set_clk_pin(uint8_t clk_pin);
198 void set_miso_pin(uint8_t miso_pin);
199 void set_mosi_pin(uint8_t mosi_pin);
200 void set_cs_pin(uint8_t cs_pin);
201 void set_interrupt_pin(int8_t interrupt_pin);
202 void set_reset_pin(int8_t reset_pin);
203#endif // USE_RP2
204
205#ifdef USE_ETHERNET_IP_STATE_LISTENERS
206 void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
207#endif
208
209#ifdef USE_ETHERNET_CONNECT_TRIGGER
211#endif
212#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
214#endif
215
216 protected:
217 void start_connect_();
218 void finish_connect_();
220
221#ifdef USE_ESP32
222 // ESP-IDF only: defers the SPI bus init, netif creation, MAC/PHY install, driver
223 // install, netif attach, and event handler registration (which together allocate
224 // ~3-8KB of DMA-capable internal SRAM via SPI driver state + eth driver RX queue)
225 // until ethernet actually needs to come up. Idempotent — guarded by the
226 // ethernet_initialized_ flag. Called from setup() when enable_on_boot_=true, or
227 // from enable() on first runtime enable. Mirrors wifi_lazy_init_() in WiFi.
228 void ethernet_lazy_init_();
229#endif
230
231#ifdef USE_ETHERNET_IP_STATE_LISTENERS
233#endif
234
235#ifdef USE_ESP32
236 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
237 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
238#if LWIP_IPV6
239 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
240#endif /* LWIP_IPV6 */
241 void log_error_and_mark_failed_(esp_err_t err, const char *message);
242#ifdef USE_ETHERNET_KSZ8081
244 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
245#endif
246#ifdef USE_ETHERNET_YT8531
249 void yt8531_phy_init_();
250#endif
252 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
253
254#ifdef USE_ETHERNET_SPI
255 uint8_t clk_pin_;
256 uint8_t miso_pin_;
257 uint8_t mosi_pin_;
258 uint8_t cs_pin_;
260 int reset_pin_{-1};
263 spi_host_device_t interface_{SPI2_HOST};
264#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
266#endif
267#else
268 // Group all 32-bit members first
269 int power_pin_{-1};
270 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
271 std::vector<PHYRegister> phy_registers_{};
272
273 // Group all 8-bit members together
274 uint8_t clk_pin_{0};
275 uint8_t phy_addr_{0};
276 uint8_t mdc_pin_{23};
277 uint8_t mdio_pin_{18};
278#endif // USE_ETHERNET_SPI
279
280 // ESP32 pointers
282 esp_eth_handle_t eth_handle_;
283 esp_eth_phy_t *phy_{nullptr};
284#endif // USE_ESP32
285
286#ifdef USE_RP2
287 static constexpr uint32_t LINK_CHECK_INTERVAL = 500; // ms between link/IP polls
288#if defined(USE_ETHERNET_W5100)
289 static constexpr uint32_t RESET_DELAY_MS = 150; // W5100S PLL lock time
290#elif defined(USE_ETHERNET_W6300)
291 static constexpr uint32_t RESET_DELAY_MS = 100; // W6300 needs 100ms after hardware reset
292#else
293 static constexpr uint32_t RESET_DELAY_MS = 10;
294#endif
295#if defined(USE_ETHERNET_W5500)
296 Wiznet5500lwIP *eth_{nullptr};
297#elif defined(USE_ETHERNET_W5100)
298 Wiznet5100lwIP *eth_{nullptr};
299#elif defined(USE_ETHERNET_W6100)
300 Wiznet6100lwIP *eth_{nullptr};
301#elif defined(USE_ETHERNET_W6300)
303#elif defined(USE_ETHERNET_ENC28J60)
304 ENC28J60lwIP *eth_{nullptr};
305#else
306#error "Unsupported RP2040 SPI Ethernet type"
307#endif
309 uint8_t clk_pin_;
310 uint8_t miso_pin_;
311 uint8_t mosi_pin_;
312 uint8_t cs_pin_;
313 int8_t interrupt_pin_{-1};
314 int8_t reset_pin_{-1};
315#endif // USE_RP2
316
317 // Common members
318#ifdef USE_ETHERNET_MANUAL_IP
319 optional<ManualIP> manual_ip_{};
320#endif
322
323 // Group all uint8_t types together (enums and bools)
326 bool started_{false};
327 bool connected_{false};
328 bool got_ipv4_address_{false};
329 // Codegen-time YAML option. When false, setup() defers esp_eth_start().
330 bool enable_on_boot_{true};
331 // Mirror of "is the link intentionally stopped" — set when setup() honors
332 // enable_on_boot=false, cleared by enable(), set again by disable().
333 bool disabled_{false};
334#ifdef USE_ESP32
335 // Tracks whether ethernet_lazy_init_() has completed successfully. Allows enable()
336 // to be called at runtime after enable_on_boot:false without re-allocating, and
337 // ensures setup() skips the heavy init when enable_on_boot_ is false.
339#endif
340#if LWIP_IPV6
341 uint8_t ipv6_count_{0};
342 bool ipv6_setup_done_{false};
343#endif /* LWIP_IPV6 */
344
345 optional<std::array<uint8_t, MAC_ADDRESS_SIZE>> fixed_mac_;
346
347#ifdef USE_ETHERNET_IP_STATE_LISTENERS
349#endif
350
351#ifdef USE_ETHERNET_CONNECT_TRIGGER
353#endif
354#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
356#endif
357 private:
358 // Stores a pointer to a string literal (static storage duration).
359 // ONLY set from Python-generated code with string literals - never dynamic strings.
360 const char *use_address_{nullptr};
361};
362
363// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
364extern EthernetComponent *global_eth_component;
365
366#ifdef USE_ESP32
367#if defined(USE_ETHERNET_JL1101) && (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0) || \
368 ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO))
369extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
370#endif
371#endif // USE_ESP32
372
373} // namespace esphome::ethernet
374
375#endif // USE_ETHERNET
constexpr bool needsSPI() const
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:227
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)
esp_netif_t * get_esp_netif()
esp_netif handle, used by network for default-route arbitration.
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, MAC_ADDRESS_SIZE > &mac)
optional< std::array< uint8_t, MAC_ADDRESS_SIZE > > fixed_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...
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.
struct esp_netif_obj esp_netif_t
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