ESPHome 2026.5.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
128#ifdef USE_ETHERNET_MANUAL_IP
129 void set_manual_ip(const ManualIP &manual_ip);
130#endif
131 void set_fixed_mac(const std::array<uint8_t, 6> &mac) { this->fixed_mac_ = mac; }
132
135 const char *get_use_address() const { return this->use_address_; }
136 void set_use_address(const char *use_address) { this->use_address_ = use_address; }
137 void get_eth_mac_address_raw(uint8_t *mac);
138 // Remove before 2026.9.0
139 ESPDEPRECATED("Use get_eth_mac_address_pretty_into_buffer() instead. Removed in 2026.9.0", "2026.3.0")
140 std::string get_eth_mac_address_pretty();
141 const char *get_eth_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
144 bool powerdown();
145
146#ifdef USE_ESP32
147 esp_eth_handle_t get_eth_handle() const { return this->eth_handle_; }
148
149#ifdef USE_ETHERNET_SPI
150 void set_clk_pin(uint8_t clk_pin);
151 void set_miso_pin(uint8_t miso_pin);
152 void set_mosi_pin(uint8_t mosi_pin);
153 void set_cs_pin(uint8_t cs_pin);
154 void set_interrupt_pin(uint8_t interrupt_pin);
155 void set_reset_pin(uint8_t reset_pin);
156 void set_clock_speed(int clock_speed);
157 void set_interface(spi_host_device_t interface);
158#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
159 void set_polling_interval(uint32_t polling_interval);
160#endif
161#else
162 void set_phy_addr(uint8_t phy_addr);
163 void set_power_pin(int power_pin);
164 void set_mdc_pin(uint8_t mdc_pin);
165 void set_mdio_pin(uint8_t mdio_pin);
166 void set_clk_pin(uint8_t clk_pin);
167 void set_clk_mode(emac_rmii_clock_mode_t clk_mode);
168 void add_phy_register(PHYRegister register_value);
169#endif // USE_ETHERNET_SPI
170#endif // USE_ESP32
171
172#ifdef USE_RP2040
173 void set_clk_pin(uint8_t clk_pin);
174 void set_miso_pin(uint8_t miso_pin);
175 void set_mosi_pin(uint8_t mosi_pin);
176 void set_cs_pin(uint8_t cs_pin);
177 void set_interrupt_pin(int8_t interrupt_pin);
178 void set_reset_pin(int8_t reset_pin);
179#endif // USE_RP2040
180
181#ifdef USE_ETHERNET_IP_STATE_LISTENERS
182 void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
183#endif
184
185#ifdef USE_ETHERNET_CONNECT_TRIGGER
187#endif
188#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
190#endif
191
192 protected:
193 void start_connect_();
194 void finish_connect_();
196
197#ifdef USE_ETHERNET_IP_STATE_LISTENERS
199#endif
200
201#ifdef USE_ESP32
202 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
203 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
204#if LWIP_IPV6
205 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
206#endif /* LWIP_IPV6 */
207 void log_error_and_mark_failed_(esp_err_t err, const char *message);
208#ifdef USE_ETHERNET_KSZ8081
210 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
211#endif
213 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
214
215#ifdef USE_ETHERNET_SPI
216 uint8_t clk_pin_;
217 uint8_t miso_pin_;
218 uint8_t mosi_pin_;
219 uint8_t cs_pin_;
221 int reset_pin_{-1};
224 spi_host_device_t interface_{SPI3_HOST};
225#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
227#endif
228#else
229 // Group all 32-bit members first
230 int power_pin_{-1};
231 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
232 std::vector<PHYRegister> phy_registers_{};
233
234 // Group all 8-bit members together
235 uint8_t clk_pin_{0};
236 uint8_t phy_addr_{0};
237 uint8_t mdc_pin_{23};
238 uint8_t mdio_pin_{18};
239#endif // USE_ETHERNET_SPI
240
241 // ESP32 pointers
242 esp_netif_t *eth_netif_{nullptr};
243 esp_eth_handle_t eth_handle_;
244 esp_eth_phy_t *phy_{nullptr};
245#endif // USE_ESP32
246
247#ifdef USE_RP2040
248 static constexpr uint32_t LINK_CHECK_INTERVAL = 500; // ms between link/IP polls
249#if defined(USE_ETHERNET_W5100)
250 static constexpr uint32_t RESET_DELAY_MS = 150; // W5100S PLL lock time
251#elif defined(USE_ETHERNET_W6300)
252 static constexpr uint32_t RESET_DELAY_MS = 100; // W6300 needs 100ms after hardware reset
253#else
254 static constexpr uint32_t RESET_DELAY_MS = 10;
255#endif
256#if defined(USE_ETHERNET_W5500)
257 Wiznet5500lwIP *eth_{nullptr};
258#elif defined(USE_ETHERNET_W5100)
259 Wiznet5100lwIP *eth_{nullptr};
260#elif defined(USE_ETHERNET_W6100)
261 Wiznet6100lwIP *eth_{nullptr};
262#elif defined(USE_ETHERNET_W6300)
264#elif defined(USE_ETHERNET_ENC28J60)
265 ENC28J60lwIP *eth_{nullptr};
266#else
267#error "Unsupported RP2040 SPI Ethernet type"
268#endif
270 uint8_t clk_pin_;
271 uint8_t miso_pin_;
272 uint8_t mosi_pin_;
273 uint8_t cs_pin_;
274 int8_t interrupt_pin_{-1};
275 int8_t reset_pin_{-1};
276#endif // USE_RP2040
277
278 // Common members
279#ifdef USE_ETHERNET_MANUAL_IP
280 optional<ManualIP> manual_ip_{};
281#endif
283
284 // Group all uint8_t types together (enums and bools)
287 bool started_{false};
288 bool connected_{false};
289 bool got_ipv4_address_{false};
290#if LWIP_IPV6
291 uint8_t ipv6_count_{0};
292 bool ipv6_setup_done_{false};
293#endif /* LWIP_IPV6 */
294
295 optional<std::array<uint8_t, 6>> fixed_mac_;
296
297#ifdef USE_ETHERNET_IP_STATE_LISTENERS
299#endif
300
301#ifdef USE_ETHERNET_CONNECT_TRIGGER
303#endif
304#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
306#endif
307 private:
308 // Stores a pointer to a string literal (static storage duration).
309 // ONLY set from Python-generated code with string literals - never dynamic strings.
310 const char *use_address_{""};
311};
312
313// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
314extern EthernetComponent *global_eth_component;
315
316#ifdef USE_ESP32
317#if defined(USE_ETHERNET_JL1101) && (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0) || \
318 ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2) || !defined(PLATFORMIO))
319extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
320#endif
321#endif // USE_ESP32
322
323} // namespace esphome::ethernet
324
325#endif // USE_ETHERNET
constexpr bool needsSPI() const
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:224
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:210
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 char * 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: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