ESPHome 2026.10.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#ifdef USE_SPI
18#endif
19#endif
20#include "esp_eth_mac.h"
21#include "esp_eth_mac_esp.h"
22#include "esp_netif.h"
23#include "esp_mac.h"
24#include "esp_idf_version.h"
25
26#if CONFIG_ETH_USE_ESP32_EMAC
27extern "C" eth_esp32_emac_config_t eth_esp32_emac_default_config(void);
28#endif
29#endif // USE_ESP32
30
31#ifdef USE_RP2
32#if defined(USE_ETHERNET_W5500)
33#include <W5500lwIP.h>
34#elif defined(USE_ETHERNET_W5100)
35#include <W5100lwIP.h>
36#elif defined(USE_ETHERNET_W6100)
37#include <W6100lwIP.h>
38#elif defined(USE_ETHERNET_W6300)
39#include <W6300lwIP.h>
40// W6300 uses PIO QSPI, not Arduino SPI. The upstream Wiznet6300 class
41// incorrectly returns needsSPI()=true, causing LwipIntfDev::begin() to
42// call SPI.begin() which claims GPIOs that PIO QSPI needs.
43// This wrapper hides needsSPI() with a version returning false.
44class Wiznet6300NoSPI : public Wiznet6300 {
45 public:
46 using Wiznet6300::Wiznet6300;
47 constexpr bool needsSPI() const { return false; }
48};
49using Wiznet6300lwIPFixed = LwipIntfDev<Wiznet6300NoSPI>;
50#elif defined(USE_ETHERNET_ENC28J60)
51#include <ENC28J60lwIP.h>
52#else
53#error "Unsupported RP2040 SPI Ethernet type"
54#endif
55#endif
56
57namespace esphome::ethernet {
58
59#ifdef USE_ETHERNET_IP_STATE_LISTENERS
69 public:
70 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
71 const network::IPAddress &dns2) = 0;
72};
73#endif // USE_ETHERNET_IP_STATE_LISTENERS
74
96
104
110
111enum class EthernetComponentState : uint8_t {
112 STOPPED,
114 CONNECTED,
115};
116
117// Platform-neutral duplex/speed types
118#ifndef USE_ESP32
119// NOLINTBEGIN(readability-identifier-naming)
122// NOLINTEND(readability-identifier-naming)
123#endif
124
125class EthernetComponent final : public Component {
126 public:
128 void setup() override;
129 void loop() override;
130 void dump_config() override;
131 float get_setup_priority() const override { return setup_priority::ETHERNET; }
132 void on_powerdown() override { powerdown(); }
134
135 // Per-interface lifecycle (parallels WiFiComponent::enable/disable/is_disabled).
136 // enable_on_boot defaults to true; when false, setup() runs all the driver/netif
137 // installation but skips esp_eth_start(), keeping the link cold until enable() is
138 // called. This is the primary lever for memory reclamation in multi-interface
139 // configurations where only one interface should carry traffic at a time.
140 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
141 void enable();
142 void disable();
143 bool is_disabled() { return this->disabled_; }
144 bool is_enabled() { return !this->disabled_; }
145
146#ifdef USE_ESP32
150#endif
151
153#ifdef USE_ETHERNET_MANUAL_IP
154 void set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; }
155#endif
156 void set_fixed_mac(const std::array<uint8_t, MAC_ADDRESS_SIZE> &mac) { this->fixed_mac_ = mac; }
157
162 const char *get_use_address() const { return this->use_address_; }
163 void set_use_address(const char *use_address) { this->use_address_ = use_address; }
164 void get_eth_mac_address_raw(uint8_t *mac);
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) { this->clk_pin_ = clk_pin; }
175 void set_miso_pin(uint8_t miso_pin) { this->miso_pin_ = miso_pin; }
176 void set_mosi_pin(uint8_t mosi_pin) { this->mosi_pin_ = mosi_pin; }
177 void set_cs_pin(uint8_t cs_pin) { this->cs_pin_ = cs_pin; }
178 void set_interrupt_pin(uint8_t interrupt_pin) { this->interrupt_pin_ = interrupt_pin; }
179 void set_reset_pin(uint8_t reset_pin) { this->reset_pin_ = reset_pin; }
180 void set_clock_speed(int clock_speed) { this->clock_speed_ = clock_speed; }
181 void set_interface(spi_host_device_t interface) { this->interface_ = interface; }
182#ifdef USE_SPI
183 void set_spi_parent(spi::SPIComponent *parent) { this->spi_parent_ = parent; }
184#endif
185#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
186 void set_polling_interval(uint32_t polling_interval) { this->polling_interval_ = polling_interval; }
187#endif
188#else
189 void set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; }
190 void set_power_pin(int power_pin) { this->power_pin_ = power_pin; }
191 void set_mdc_pin(uint8_t mdc_pin) { this->mdc_pin_ = mdc_pin; }
192 void set_mdio_pin(uint8_t mdio_pin) { this->mdio_pin_ = mdio_pin; }
193 void set_clk_pin(uint8_t clk_pin) { this->clk_pin_ = clk_pin; }
194 void set_clk_mode(emac_rmii_clock_mode_t clk_mode) { this->clk_mode_ = clk_mode; }
195 void add_phy_register(PHYRegister register_value);
196#endif // USE_ETHERNET_SPI
197#endif // USE_ESP32
198
199#ifdef USE_RP2
200 void set_clk_pin(uint8_t clk_pin) { this->clk_pin_ = clk_pin; }
201 void set_miso_pin(uint8_t miso_pin) { this->miso_pin_ = miso_pin; }
202 void set_mosi_pin(uint8_t mosi_pin) { this->mosi_pin_ = mosi_pin; }
203 void set_cs_pin(uint8_t cs_pin) { this->cs_pin_ = cs_pin; }
204 void set_interrupt_pin(int8_t interrupt_pin) { this->interrupt_pin_ = interrupt_pin; }
205 void set_reset_pin(int8_t reset_pin) { this->reset_pin_ = reset_pin; }
206#endif // USE_RP2
207
208#ifdef USE_ETHERNET_IP_STATE_LISTENERS
209 void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
210#endif
211
212#ifdef USE_ETHERNET_CONNECT_TRIGGER
214#endif
215#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
217#endif
218
219 protected:
220 void start_connect_();
221 void finish_connect_();
223
224#ifdef USE_ESP32
225 // ESP-IDF only: defers the SPI bus init, netif creation, MAC/PHY install, driver
226 // install, netif attach, and event handler registration (which together allocate
227 // ~3-8KB of DMA-capable internal SRAM via SPI driver state + eth driver RX queue)
228 // until ethernet actually needs to come up. Idempotent — guarded by the
229 // ethernet_initialized_ flag. Called from setup() when enable_on_boot_=true, or
230 // from enable() on first runtime enable. Mirrors wifi_lazy_init_() in WiFi.
231 void ethernet_lazy_init_();
232#endif
233
234#ifdef USE_ETHERNET_IP_STATE_LISTENERS
236#endif
237
238#ifdef USE_ESP32
239 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
240 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
241#if LWIP_IPV6
242 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
243#endif /* LWIP_IPV6 */
244 void log_error_and_mark_failed_(esp_err_t err, const char *message);
245#ifdef USE_ETHERNET_KSZ8081
247 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
248#endif
249#ifdef USE_ETHERNET_YT8531
252 void yt8531_phy_init_();
253#endif
255 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
256
257#ifdef USE_ETHERNET_SPI
258 uint8_t clk_pin_;
259 uint8_t miso_pin_;
260 uint8_t mosi_pin_;
261 uint8_t cs_pin_;
263 int reset_pin_{-1};
266 spi_host_device_t interface_{SPI2_HOST};
267#ifdef USE_SPI
268 // When set, the SPI bus is owned and initialized by this spi component
269 // and the ethernet chip only adds a device to it.
271#endif
272#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
274#endif
275#else
276 // Group all 32-bit members first
277 int power_pin_{-1};
278 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
279 std::vector<PHYRegister> phy_registers_{};
280
281 // Group all 8-bit members together
282 uint8_t clk_pin_{0};
283 uint8_t phy_addr_{0};
284 uint8_t mdc_pin_{23};
285 uint8_t mdio_pin_{18};
286#endif // USE_ETHERNET_SPI
287
288 // ESP32 pointers
290 esp_eth_handle_t eth_handle_;
291 esp_eth_phy_t *phy_{nullptr};
292#endif // USE_ESP32
293
294#ifdef USE_RP2
295 static constexpr uint32_t LINK_CHECK_INTERVAL = 500; // ms between link/IP polls
296#if defined(USE_ETHERNET_W5100)
297 static constexpr uint32_t RESET_DELAY_MS = 150; // W5100S PLL lock time
298#elif defined(USE_ETHERNET_W6300)
299 static constexpr uint32_t RESET_DELAY_MS = 100; // W6300 needs 100ms after hardware reset
300#else
301 static constexpr uint32_t RESET_DELAY_MS = 10;
302#endif
303#if defined(USE_ETHERNET_W5500)
304 Wiznet5500lwIP *eth_{nullptr};
305#elif defined(USE_ETHERNET_W5100)
306 Wiznet5100lwIP *eth_{nullptr};
307#elif defined(USE_ETHERNET_W6100)
308 Wiznet6100lwIP *eth_{nullptr};
309#elif defined(USE_ETHERNET_W6300)
311#elif defined(USE_ETHERNET_ENC28J60)
312 ENC28J60lwIP *eth_{nullptr};
313#else
314#error "Unsupported RP2040 SPI Ethernet type"
315#endif
317 uint8_t clk_pin_;
318 uint8_t miso_pin_;
319 uint8_t mosi_pin_;
320 uint8_t cs_pin_;
321 int8_t interrupt_pin_{-1};
322 int8_t reset_pin_{-1};
323#endif // USE_RP2
324
325 // Common members
326#ifdef USE_ETHERNET_MANUAL_IP
327 optional<ManualIP> manual_ip_{};
328#endif
330
331 // Group all uint8_t types together (enums and bools)
334 bool started_{false};
335 bool connected_{false};
336 bool got_ipv4_address_{false};
337 // Codegen-time YAML option. When false, setup() defers esp_eth_start().
338 bool enable_on_boot_{true};
339 // Mirror of "is the link intentionally stopped" — set when setup() honors
340 // enable_on_boot=false, cleared by enable(), set again by disable().
341 bool disabled_{false};
342#ifdef USE_ESP32
343 // Tracks whether ethernet_lazy_init_() has completed successfully. Allows enable()
344 // to be called at runtime after enable_on_boot:false without re-allocating, and
345 // ensures setup() skips the heavy init when enable_on_boot_ is false.
347#endif
348#if LWIP_IPV6
349 uint8_t ipv6_count_{0};
350 bool ipv6_setup_done_{false};
351#endif /* LWIP_IPV6 */
352
353 optional<std::array<uint8_t, MAC_ADDRESS_SIZE>> fixed_mac_;
354
355#ifdef USE_ETHERNET_IP_STATE_LISTENERS
357#endif
358
359#ifdef USE_ETHERNET_CONNECT_TRIGGER
361#endif
362#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
364#endif
365 private:
366 // Stores a pointer to a string literal (static storage duration).
367 // ONLY set from Python-generated code with string literals - never dynamic strings.
368 const char *use_address_{nullptr};
369};
370
371// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
372extern EthernetComponent *global_eth_component;
373
374#ifdef USE_ESP32
375#if defined(USE_ETHERNET_JL1101) && (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0) || \
376 ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO))
377extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
378#endif
379#endif // USE_ESP32
380
381} // namespace esphome::ethernet
382
383#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)
void set_spi_parent(spi::SPIComponent *parent)
esp_netif_t * get_esp_netif()
esp_netif handle, used by network for default-route arbitration.
void add_ip_state_listener(EthernetIPStateListener *listener)
void set_interrupt_pin(int8_t interrupt_pin)
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_
void set_interrupt_pin(uint8_t interrupt_pin)
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)
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:301
constexpr float ETHERNET
Definition component.h:51
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