ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ethernet_component_rp2.cpp
Go to the documentation of this file.
2
3#if defined(USE_ETHERNET) && defined(USE_RP2)
4
7#include "esphome/core/log.h"
8
10
11#include <SPI.h>
12#include <lwip/dns.h>
13#include <lwip/netif.h>
14
15namespace esphome::ethernet {
16
17ESPHOME_LOG_TAG(TAG, "ethernet");
18
20 // Configure SPI pins
21#if !defined(USE_ETHERNET_W6300) && defined(USE_ETHERNET_SPI)
22 SPI.setRX(this->miso_pin_);
23 SPI.setTX(this->mosi_pin_);
24 SPI.setSCK(this->clk_pin_);
25#endif
26 // W6300 uses PIO QSPI with hardcoded pins, not Arduino SPI.
27 // SPI pin config is skipped; Wiznet6300lwIPFixed (needsSPI()=false)
28 // prevents LwipIntfDev::begin() from calling SPI.begin().
29
30#ifdef USE_ETHERNET_SPI
31 // Toggle reset pin if configured
32 if (this->reset_pin_ >= 0) {
33 rp2::RP2GPIOPin reset_pin;
34 reset_pin.set_pin(this->reset_pin_);
35 reset_pin.set_flags(gpio::FLAG_OUTPUT);
36 reset_pin.setup();
37 reset_pin.digital_write(false);
38 delay(1); // NOLINT
39 reset_pin.digital_write(true);
40 // W5100S needs 150ms for PLL lock; W5500/ENC28J60 need ~10ms
41 delay(RESET_DELAY_MS); // NOLINT
42 }
43#endif
44
45 // Create the Ethernet device instance
46#if defined(USE_ETHERNET_W5500)
47 this->eth_ = new Wiznet5500lwIP(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
48#elif defined(USE_ETHERNET_W5100)
49 this->eth_ = new Wiznet5100lwIP(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
50#elif defined(USE_ETHERNET_W6100)
51 this->eth_ = new Wiznet6100lwIP(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
52#elif defined(USE_ETHERNET_W6300)
53 this->eth_ = new Wiznet6300lwIPFixed(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
54#elif defined(USE_ETHERNET_ENC28J60)
55 this->eth_ = new ENC28J60lwIP(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
56#elif defined(USE_ETHERNET_USB)
57 this->eth_ = new NCMEthernetlwIP(); // NOLINT
58#endif
59
60 // Set hostname before begin() so the LWIP netif gets it
61 this->eth_->hostname(App.get_name().c_str());
62
63 // Configure static IP (must be done before begin())
64#ifdef USE_ETHERNET_MANUAL_IP
65 {
66 const ManualIP manual_ip = load_manual_ip();
67 this->eth_->config(IPAddress(manual_ip.static_ip), IPAddress(manual_ip.gateway), IPAddress(manual_ip.subnet),
68 IPAddress(manual_ip.dns1), IPAddress(manual_ip.dns2));
69 }
70#endif
71
72 // Begin with fixed MAC or auto-generated
73 bool success;
74 if (this->fixed_mac_.has_value()) {
75 success = this->eth_->begin(this->fixed_mac_->data());
76 } else {
77 success = this->eth_->begin();
78 }
79
80 if (!success) {
81 ESP_LOGE(TAG, "Failed to initialize Ethernet");
82 delete this->eth_; // NOLINT(cppcoreguidelines-owning-memory)
83 this->eth_ = nullptr;
84 this->mark_failed();
85 return;
86 }
87
88 // Make this the default interface for routing
89 this->eth_->setDefault(true);
90
91 // The arduino-pico LwipIntfDev automatically handles packet processing
92 // via __addEthernetPacketHandler when no interrupt pin is used,
93 // or via GPIO interrupt when one is provided.
94
95 // Don't set started_ here — let the link polling in loop() set it
96 // when the link is actually up. Setting it prematurely causes
97 // a "Starting → Stopped → Starting" log sequence because the chip
98 // needs time after begin() before the PHY link is ready.
99}
100
102 // On RP2040, we need to poll connection state since there are no events.
104
105 // Throttle link/IP polling to avoid excessive SPI transactions.
106 // W5500/ENC28J60 read PHY register via SPI on every linkStatus() call.
107 // W5100 can't detect link state, so we skip the SPI read and assume link-up.
108 // connected() reads netif->ip_addr without LwIPLock, but this is a single
109 // 32-bit aligned read (atomic on ARM) — worst case is a one-iteration-stale
110 // value, which is benign for polling.
111 if (this->eth_ != nullptr && now - this->last_link_check_ >= LINK_CHECK_INTERVAL) {
112 this->last_link_check_ = now;
113#if defined(USE_ETHERNET_W5100)
114 // W5100 can't detect link (isLinkDetectable() returns false), so linkStatus()
115 // returns Unknown — assume link is up after successful begin()
116 bool link_up = true;
117#else
118 bool link_up = this->eth_->linkStatus() == LinkON;
119#endif
120 bool has_ip = this->eth_->connected();
121
122 if (!link_up) {
123 if (this->started_) {
124 this->started_ = false;
125 this->connected_ = false;
126 }
127 } else {
128 if (!this->started_) {
129 this->started_ = true;
130 }
131 bool was_connected = this->connected_;
132 this->connected_ = has_ip;
133 if (this->connected_ && !was_connected) {
134#ifdef USE_ETHERNET_IP_STATE_LISTENERS
136#endif
137 }
138 }
139 }
140
141 // State machine
142 switch (this->state_) {
144 if (this->started_) {
145 ESP_LOGI(TAG, "Starting connection");
147 this->start_connect_();
148 }
149 break;
151 if (!this->started_) {
152 ESP_LOGI(TAG, "Stopped connection");
154 } else if (this->connected_) {
155 // connection established
156 ESP_LOGI(TAG, "Connected");
158
159 this->dump_connect_params_();
160 this->status_clear_warning();
161#ifdef USE_ETHERNET_CONNECT_TRIGGER
163#endif
165 } else if (now - this->connect_begin_ > 15000) {
166 ESP_LOGW(TAG, "Connecting failed; reconnecting");
167 this->start_connect_();
168 }
169 break;
171 if (!this->started_) {
172 ESP_LOGI(TAG, "Stopped connection");
174#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
176#endif
178 } else if (!this->connected_) {
179 ESP_LOGW(TAG, "Connection lost; reconnecting");
181 this->start_connect_();
182#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
184#endif
186 } else {
187 this->finish_connect_();
188 }
189 break;
190 }
191}
192
194#if defined(USE_ETHERNET_W5500)
195 const char *type_str = "W5500";
196#elif defined(USE_ETHERNET_W5100)
197 const char *type_str = "W5100";
198#elif defined(USE_ETHERNET_W6100)
199 const char *type_str = "W6100";
200#elif defined(USE_ETHERNET_W6300)
201 const char *type_str = "W6300";
202#elif defined(USE_ETHERNET_ENC28J60)
203 const char *type_str = "ENC28J60";
204#elif defined(USE_ETHERNET_USB)
205 const char *type_str = "USB";
206#else
207 const char *type_str = "Unknown";
208#endif
209#if defined(USE_ETHERNET_W6300)
210 // W6300 uses PIO QSPI with hardcoded pins — SPI pin fields are not used
211 ESP_LOGCONFIG(TAG,
212 "Ethernet:\n"
213 " Type: %s (PIO QSPI)\n"
214 " Connected: %s\n"
215 " IRQ Pin: %d\n"
216 " Reset Pin: %d",
217 type_str, YESNO(this->is_connected()), this->interrupt_pin_, this->reset_pin_);
218#elif defined(USE_ETHERNET_SPI)
219 ESP_LOGCONFIG(TAG,
220 "Ethernet:\n"
221 " Type: %s\n"
222 " Connected: %s\n"
223 " CLK Pin: %u\n"
224 " MISO Pin: %u\n"
225 " MOSI Pin: %u\n"
226 " CS Pin: %u\n"
227 " IRQ Pin: %d\n"
228 " Reset Pin: %d",
229 type_str, YESNO(this->is_connected()), this->clk_pin_, this->miso_pin_, this->mosi_pin_, this->cs_pin_,
230 this->interrupt_pin_, this->reset_pin_);
231#else
232 ESP_LOGCONFIG(TAG,
233 "Ethernet:\n"
234 " Type: %s\n"
235 " Connected: %s",
236 type_str, YESNO(this->is_connected()));
237#endif
238 this->dump_connect_params_();
239}
240
242 network::IPAddresses addresses;
243 if (this->eth_ != nullptr) {
244 LwIPLock lock;
245 addresses[0] = network::IPAddress(this->eth_->localIP());
246 }
247 return addresses;
248}
249
250network::IPAddress EthernetComponent::get_dns_address(uint8_t num) {
251 LwIPLock lock;
252 const ip_addr_t *dns_ip = dns_getserver(num);
253 return dns_ip;
254}
255
257 if (this->eth_ != nullptr) {
258 this->eth_->macAddress(mac);
259 } else {
260 memset(mac, 0, MAC_ADDRESS_SIZE);
261 }
262}
263
265 std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) {
266 uint8_t mac[MAC_ADDRESS_SIZE];
268 format_mac_addr_upper(mac, buf.data());
269 return buf.data();
270}
271
273 // W5100, W5500, and ENC28J60 are full-duplex on RP2040
274 return ETH_DUPLEX_FULL;
275}
276
278#if defined(USE_ETHERNET_ENC28J60) || defined(USE_ETHERNET_USB)
279 // ENC28J60 is 10Mbps only. USB is either 12 or 1.5Mbps depending on negotiation.
280 return ETH_SPEED_10M;
281#else
282 // W5100 and W5500 are 100Mbps
283 return ETH_SPEED_100M;
284#endif
285}
286
288 ESP_LOGI(TAG, "Powering down ethernet");
289#ifndef USE_ETHERNET_USB
290 // NCM end() disconnects USB; TinyUSB then panics on the next transfer before the reboot
291 if (this->eth_ != nullptr) {
292 this->eth_->end();
293 }
294#endif
295 this->connected_ = false;
296 this->started_ = false;
297 return true;
298}
299
301 this->got_ipv4_address_ = false;
302 this->connect_begin_ = millis();
303 this->status_set_warning(LOG_STR("waiting for IP configuration"));
304
305 // Hostname is already set in setup() via LwipIntf::setHostname()
306
307#ifdef USE_ETHERNET_MANUAL_IP
308 // Static IP was already configured before begin() in setup()
309 // Set DNS servers
310 const ManualIP manual_ip = load_manual_ip();
311 LwIPLock lock;
312 if (manual_ip.dns1.is_set()) {
313 ip_addr_t d = manual_ip.dns1;
314 dns_setserver(0, &d);
315 }
316 if (manual_ip.dns2.is_set()) {
317 ip_addr_t d = manual_ip.dns2;
318 dns_setserver(1, &d);
319 }
320#endif
321}
322
324 // No additional work needed on RP2040 for now
325 // IPv6 link-local could be added here in the future
326}
327
329 if (this->eth_ == nullptr) {
330 return;
331 }
332
333 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
334 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
335 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
336 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
337 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
338 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
339
340 // Copy all lwIP state under the lock to avoid races with IRQ callbacks
341 ip_addr_t ip_addr, netmask, gw, dns1_addr, dns2_addr;
342 {
343 LwIPLock lock;
344 auto *netif = this->eth_->getNetIf();
345 ip_addr = netif->ip_addr;
346 netmask = netif->netmask;
347 gw = netif->gw;
348 dns1_addr = *dns_getserver(0);
349 dns2_addr = *dns_getserver(1);
350 }
351 ESP_LOGCONFIG(TAG,
352 " IP Address: %s\n"
353 " Hostname: '%s'\n"
354 " Subnet: %s\n"
355 " Gateway: %s\n"
356 " DNS1: %s\n"
357 " DNS2: %s\n"
358 " MAC Address: %s",
359 network::IPAddress(&ip_addr).str_to(ip_buf), App.get_name().c_str(),
360 network::IPAddress(&netmask).str_to(subnet_buf), network::IPAddress(&gw).str_to(gateway_buf),
361 network::IPAddress(&dns1_addr).str_to(dns1_buf), network::IPAddress(&dns2_addr).str_to(dns2_buf),
362 this->get_eth_mac_address_pretty_into_buffer(mac_buf));
363}
364
366 // RP2040 uses arduino-pico's LwipIntfDev which manages link state internally;
367 // there is no clean enable/disable hook today. The YAML option is accepted on
368 // RP2040 for schema parity but has no effect.
369 if (!this->disabled_)
370 return;
371 ESP_LOGW(TAG, "enable_on_boot/disable not supported");
372 this->disabled_ = false;
373}
374
376 if (this->disabled_)
377 return;
378 ESP_LOGW(TAG, "enable_on_boot/disable not supported");
379 this->disabled_ = true;
380}
381
382} // namespace esphome::ethernet
383
384#endif // USE_ETHERNET && USE_RP2
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:318
constexpr const char * c_str() const
Definition string_ref.h:78
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:588
static constexpr uint32_t LINK_CHECK_INTERVAL
network::IPAddress get_dns_address(uint8_t num)
optional< std::array< uint8_t, MAC_ADDRESS_SIZE > > fixed_mac_
static constexpr uint32_t RESET_DELAY_MS
void notify_connect_state_listeners_(bool connected)
const char * get_eth_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
uint32_t ip_addr
LwipIntfDev< Wiznet6300NoSPI > Wiznet6300lwIPFixed
ESPHOME_LOG_TAG(TAG, "ethernet")
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:368
void HOT delay(uint32_t ms)
Definition hal.cpp:78
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:2001
static void uint32_t
SemaphoreHandle_t lock