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