ESPHome 2026.11.0b6
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)
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 // Toggle reset pin if configured
31 if (this->reset_pin_ >= 0) {
32 rp2::RP2GPIOPin reset_pin;
33 reset_pin.set_pin(this->reset_pin_);
34 reset_pin.set_flags(gpio::FLAG_OUTPUT);
35 reset_pin.setup();
36 reset_pin.digital_write(false);
37 delay(1); // NOLINT
38 reset_pin.digital_write(true);
39 // W5100S needs 150ms for PLL lock; W5500/ENC28J60 need ~10ms
40 delay(RESET_DELAY_MS); // NOLINT
41 }
42
43 // Create the SPI Ethernet device instance
44#if defined(USE_ETHERNET_W5500)
45 this->eth_ = new Wiznet5500lwIP(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
46#elif defined(USE_ETHERNET_W5100)
47 this->eth_ = new Wiznet5100lwIP(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
48#elif defined(USE_ETHERNET_W6100)
49 this->eth_ = new Wiznet6100lwIP(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
50#elif defined(USE_ETHERNET_W6300)
51 this->eth_ = new Wiznet6300lwIPFixed(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
52#elif defined(USE_ETHERNET_ENC28J60)
53 this->eth_ = new ENC28J60lwIP(this->cs_pin_, SPI, this->interrupt_pin_); // NOLINT
54#endif
55
56 // Set hostname before begin() so the LWIP netif gets it
57 this->eth_->hostname(App.get_name().c_str());
58
59 // Configure static IP (must be done before begin())
60#ifdef USE_ETHERNET_MANUAL_IP
61 {
62 const ManualIP manual_ip = load_manual_ip();
63 this->eth_->config(IPAddress(manual_ip.static_ip), IPAddress(manual_ip.gateway), IPAddress(manual_ip.subnet),
64 IPAddress(manual_ip.dns1), IPAddress(manual_ip.dns2));
65 }
66#endif
67
68 // Begin with fixed MAC or auto-generated
69 bool success;
70 if (this->fixed_mac_.has_value()) {
71 success = this->eth_->begin(this->fixed_mac_->data());
72 } else {
73 success = this->eth_->begin();
74 }
75
76 if (!success) {
77 ESP_LOGE(TAG, "Failed to initialize Ethernet");
78 delete this->eth_; // NOLINT(cppcoreguidelines-owning-memory)
79 this->eth_ = nullptr;
80 this->mark_failed();
81 return;
82 }
83
84 // Make this the default interface for routing
85 this->eth_->setDefault(true);
86
87 // The arduino-pico LwipIntfDev automatically handles packet processing
88 // via __addEthernetPacketHandler when no interrupt pin is used,
89 // or via GPIO interrupt when one is provided.
90
91 // Don't set started_ here — let the link polling in loop() set it
92 // when the link is actually up. Setting it prematurely causes
93 // a "Starting → Stopped → Starting" log sequence because the chip
94 // needs time after begin() before the PHY link is ready.
95}
96
98 // On RP2040, we need to poll connection state since there are no events.
100
101 // Throttle link/IP polling to avoid excessive SPI transactions.
102 // W5500/ENC28J60 read PHY register via SPI on every linkStatus() call.
103 // W5100 can't detect link state, so we skip the SPI read and assume link-up.
104 // connected() reads netif->ip_addr without LwIPLock, but this is a single
105 // 32-bit aligned read (atomic on ARM) — worst case is a one-iteration-stale
106 // value, which is benign for polling.
107 if (this->eth_ != nullptr && now - this->last_link_check_ >= LINK_CHECK_INTERVAL) {
108 this->last_link_check_ = now;
109#if defined(USE_ETHERNET_W5100)
110 // W5100 can't detect link (isLinkDetectable() returns false), so linkStatus()
111 // returns Unknown — assume link is up after successful begin()
112 bool link_up = true;
113#else
114 bool link_up = this->eth_->linkStatus() == LinkON;
115#endif
116 bool has_ip = this->eth_->connected();
117
118 if (!link_up) {
119 if (this->started_) {
120 this->started_ = false;
121 this->connected_ = false;
122 }
123 } else {
124 if (!this->started_) {
125 this->started_ = true;
126 }
127 bool was_connected = this->connected_;
128 this->connected_ = has_ip;
129 if (this->connected_ && !was_connected) {
130#ifdef USE_ETHERNET_IP_STATE_LISTENERS
132#endif
133 }
134 }
135 }
136
137 // State machine
138 switch (this->state_) {
140 if (this->started_) {
141 ESP_LOGI(TAG, "Starting connection");
143 this->start_connect_();
144 }
145 break;
147 if (!this->started_) {
148 ESP_LOGI(TAG, "Stopped connection");
150 } else if (this->connected_) {
151 // connection established
152 ESP_LOGI(TAG, "Connected");
154
155 this->dump_connect_params_();
156 this->status_clear_warning();
157#ifdef USE_ETHERNET_CONNECT_TRIGGER
159#endif
160 } else if (now - this->connect_begin_ > 15000) {
161 ESP_LOGW(TAG, "Connecting failed; reconnecting");
162 this->start_connect_();
163 }
164 break;
166 if (!this->started_) {
167 ESP_LOGI(TAG, "Stopped connection");
169#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
171#endif
172 } else if (!this->connected_) {
173 ESP_LOGW(TAG, "Connection lost; reconnecting");
175 this->start_connect_();
176#ifdef USE_ETHERNET_DISCONNECT_TRIGGER
178#endif
179 } else {
180 this->finish_connect_();
181 }
182 break;
183 }
184}
185
187#if defined(USE_ETHERNET_W5500)
188 const char *type_str = "W5500";
189#elif defined(USE_ETHERNET_W5100)
190 const char *type_str = "W5100";
191#elif defined(USE_ETHERNET_W6100)
192 const char *type_str = "W6100";
193#elif defined(USE_ETHERNET_W6300)
194 const char *type_str = "W6300";
195#elif defined(USE_ETHERNET_ENC28J60)
196 const char *type_str = "ENC28J60";
197#else
198 const char *type_str = "Unknown";
199#endif
200#if defined(USE_ETHERNET_W6300)
201 // W6300 uses PIO QSPI with hardcoded pins — SPI pin fields are not used
202 ESP_LOGCONFIG(TAG,
203 "Ethernet:\n"
204 " Type: %s (PIO QSPI)\n"
205 " Connected: %s\n"
206 " IRQ Pin: %d\n"
207 " Reset Pin: %d",
208 type_str, YESNO(this->is_connected()), this->interrupt_pin_, this->reset_pin_);
209#else
210 ESP_LOGCONFIG(TAG,
211 "Ethernet:\n"
212 " Type: %s\n"
213 " Connected: %s\n"
214 " CLK Pin: %u\n"
215 " MISO Pin: %u\n"
216 " MOSI Pin: %u\n"
217 " CS Pin: %u\n"
218 " IRQ Pin: %d\n"
219 " Reset Pin: %d",
220 type_str, YESNO(this->is_connected()), this->clk_pin_, this->miso_pin_, this->mosi_pin_, this->cs_pin_,
221 this->interrupt_pin_, this->reset_pin_);
222#endif
223 this->dump_connect_params_();
224}
225
227 network::IPAddresses addresses;
228 if (this->eth_ != nullptr) {
229 LwIPLock lock;
230 addresses[0] = network::IPAddress(this->eth_->localIP());
231 }
232 return addresses;
233}
234
235network::IPAddress EthernetComponent::get_dns_address(uint8_t num) {
236 LwIPLock lock;
237 const ip_addr_t *dns_ip = dns_getserver(num);
238 return dns_ip;
239}
240
242 if (this->eth_ != nullptr) {
243 this->eth_->macAddress(mac);
244 } else {
245 memset(mac, 0, MAC_ADDRESS_SIZE);
246 }
247}
248
250 std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) {
251 uint8_t mac[MAC_ADDRESS_SIZE];
253 format_mac_addr_upper(mac, buf.data());
254 return buf.data();
255}
256
258 // W5100, W5500, and ENC28J60 are full-duplex on RP2040
259 return ETH_DUPLEX_FULL;
260}
261
263#ifdef USE_ETHERNET_ENC28J60
264 // ENC28J60 is 10Mbps only
265 return ETH_SPEED_10M;
266#else
267 // W5100 and W5500 are 100Mbps
268 return ETH_SPEED_100M;
269#endif
270}
271
273 ESP_LOGI(TAG, "Powering down ethernet");
274 if (this->eth_ != nullptr) {
275 this->eth_->end();
276 }
277 this->connected_ = false;
278 this->started_ = false;
279 return true;
280}
281
283 this->got_ipv4_address_ = false;
284 this->connect_begin_ = millis();
285 this->status_set_warning(LOG_STR("waiting for IP configuration"));
286
287 // Hostname is already set in setup() via LwipIntf::setHostname()
288
289#ifdef USE_ETHERNET_MANUAL_IP
290 // Static IP was already configured before begin() in setup()
291 // Set DNS servers
292 const ManualIP manual_ip = load_manual_ip();
293 LwIPLock lock;
294 if (manual_ip.dns1.is_set()) {
295 ip_addr_t d = manual_ip.dns1;
296 dns_setserver(0, &d);
297 }
298 if (manual_ip.dns2.is_set()) {
299 ip_addr_t d = manual_ip.dns2;
300 dns_setserver(1, &d);
301 }
302#endif
303}
304
306 // No additional work needed on RP2040 for now
307 // IPv6 link-local could be added here in the future
308}
309
311 if (this->eth_ == nullptr) {
312 return;
313 }
314
315 char ip_buf[network::IP_ADDRESS_BUFFER_SIZE];
316 char subnet_buf[network::IP_ADDRESS_BUFFER_SIZE];
317 char gateway_buf[network::IP_ADDRESS_BUFFER_SIZE];
318 char dns1_buf[network::IP_ADDRESS_BUFFER_SIZE];
319 char dns2_buf[network::IP_ADDRESS_BUFFER_SIZE];
320 char mac_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
321
322 // Copy all lwIP state under the lock to avoid races with IRQ callbacks
323 ip_addr_t ip_addr, netmask, gw, dns1_addr, dns2_addr;
324 {
325 LwIPLock lock;
326 auto *netif = this->eth_->getNetIf();
327 ip_addr = netif->ip_addr;
328 netmask = netif->netmask;
329 gw = netif->gw;
330 dns1_addr = *dns_getserver(0);
331 dns2_addr = *dns_getserver(1);
332 }
333 ESP_LOGCONFIG(TAG,
334 " IP Address: %s\n"
335 " Hostname: '%s'\n"
336 " Subnet: %s\n"
337 " Gateway: %s\n"
338 " DNS1: %s\n"
339 " DNS2: %s\n"
340 " MAC Address: %s",
341 network::IPAddress(&ip_addr).str_to(ip_buf), App.get_name().c_str(),
342 network::IPAddress(&netmask).str_to(subnet_buf), network::IPAddress(&gw).str_to(gateway_buf),
343 network::IPAddress(&dns1_addr).str_to(dns1_buf), network::IPAddress(&dns2_addr).str_to(dns2_buf),
344 this->get_eth_mac_address_pretty_into_buffer(mac_buf));
345}
346
348 // RP2040 uses arduino-pico's LwipIntfDev which manages link state internally;
349 // there is no clean enable/disable hook today. The YAML option is accepted on
350 // RP2040 for schema parity but has no effect.
351 if (!this->disabled_)
352 return;
353 ESP_LOGW(TAG, "enable_on_boot/disable not supported");
354 this->disabled_ = false;
355}
356
358 if (this->disabled_)
359 return;
360 ESP_LOGW(TAG, "enable_on_boot/disable not supported");
361 this->disabled_ = true;
362}
363
364} // namespace esphome::ethernet
365
366#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:309
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:557
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
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:85
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:1754
static void uint32_t
SemaphoreHandle_t lock