ESPHome 2026.1.4
Loading...
Searching...
No Matches
ethernet_component.h
Go to the documentation of this file.
1#pragma once
2
5#include "esphome/core/hal.h"
8
9#ifdef USE_ESP32
10
11#include "esp_eth.h"
12#include "esp_eth_mac.h"
13#include "esp_netif.h"
14#include "esp_mac.h"
15#include "esp_idf_version.h"
16
17namespace esphome {
18namespace ethernet {
19
20#ifdef USE_ETHERNET_IP_STATE_LISTENERS
30 public:
31 virtual void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1,
32 const network::IPAddress &dns2) = 0;
33};
34#endif // USE_ETHERNET_IP_STATE_LISTENERS
35
50
58
60 uint32_t address;
61 uint32_t value;
62 uint32_t page;
63};
64
65enum class EthernetComponentState : uint8_t {
66 STOPPED,
69};
70
72 public:
74 void setup() override;
75 void loop() override;
76 void dump_config() override;
77 float get_setup_priority() const override;
78 void on_powerdown() override { powerdown(); }
79 bool is_connected();
80
81#ifdef USE_ETHERNET_SPI
82 void set_clk_pin(uint8_t clk_pin);
83 void set_miso_pin(uint8_t miso_pin);
84 void set_mosi_pin(uint8_t mosi_pin);
85 void set_cs_pin(uint8_t cs_pin);
86 void set_interrupt_pin(uint8_t interrupt_pin);
87 void set_reset_pin(uint8_t reset_pin);
88 void set_clock_speed(int clock_speed);
89#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
90 void set_polling_interval(uint32_t polling_interval);
91#endif
92#else
93 void set_phy_addr(uint8_t phy_addr);
94 void set_power_pin(int power_pin);
95 void set_mdc_pin(uint8_t mdc_pin);
96 void set_mdio_pin(uint8_t mdio_pin);
97 void set_clk_pin(uint8_t clk_pin);
98 void set_clk_mode(emac_rmii_clock_mode_t clk_mode);
99 void add_phy_register(PHYRegister register_value);
100#endif
102#ifdef USE_ETHERNET_MANUAL_IP
103 void set_manual_ip(const ManualIP &manual_ip);
104#endif
105 void set_fixed_mac(const std::array<uint8_t, 6> &mac) { this->fixed_mac_ = mac; }
106
109 const char *get_use_address() const;
110 void set_use_address(const char *use_address);
111 void get_eth_mac_address_raw(uint8_t *mac);
112 std::string get_eth_mac_address_pretty();
113 const char *get_eth_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
114 eth_duplex_t get_duplex_mode();
115 eth_speed_t get_link_speed();
116 bool powerdown();
117
118#ifdef USE_ETHERNET_IP_STATE_LISTENERS
119 void add_ip_state_listener(EthernetIPStateListener *listener) { this->ip_state_listeners_.push_back(listener); }
120#endif
121
122 protected:
123 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
124 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
125#if LWIP_IPV6
126 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
127#endif /* LWIP_IPV6 */
128#ifdef USE_ETHERNET_IP_STATE_LISTENERS
130#endif
131
132 void start_connect_();
133 void finish_connect_();
135 void log_error_and_mark_failed_(esp_err_t err, const char *message);
136#ifdef USE_ETHERNET_KSZ8081
138 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
139#endif
141 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
142
143#ifdef USE_ETHERNET_SPI
144 uint8_t clk_pin_;
145 uint8_t miso_pin_;
146 uint8_t mosi_pin_;
147 uint8_t cs_pin_;
149 int reset_pin_{-1};
152#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
153 uint32_t polling_interval_{0};
154#endif
155#else
156 // Group all 32-bit members first
157 int power_pin_{-1};
158 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
159 std::vector<PHYRegister> phy_registers_{};
160
161 // Group all 8-bit members together
162 uint8_t clk_pin_{0};
163 uint8_t phy_addr_{0};
164 uint8_t mdc_pin_{23};
165 uint8_t mdio_pin_{18};
166#endif
167#ifdef USE_ETHERNET_MANUAL_IP
169#endif
171
172 // Group all uint8_t types together (enums and bools)
175 bool started_{false};
176 bool connected_{false};
177 bool got_ipv4_address_{false};
178#if LWIP_IPV6
179 uint8_t ipv6_count_{0};
180 bool ipv6_setup_done_{false};
181#endif /* LWIP_IPV6 */
182
183 // Pointers at the end (naturally aligned)
184 esp_netif_t *eth_netif_{nullptr};
185 esp_eth_handle_t eth_handle_;
186 esp_eth_phy_t *phy_{nullptr};
188
189#ifdef USE_ETHERNET_IP_STATE_LISTENERS
191#endif
192
193 private:
194 // Stores a pointer to a string literal (static storage duration).
195 // ONLY set from Python-generated code with string literals - never dynamic strings.
196 const char *use_address_{""};
197};
198
199// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
200extern EthernetComponent *global_eth_component;
201
202#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
203extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
204#endif
205
206} // namespace ethernet
207} // namespace esphome
208
209#endif // USE_ESP32
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:132
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)
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)
void set_interrupt_pin(uint8_t interrupt_pin)
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)
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:38
uint16_t type
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:180
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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