ESPHome 2026.1.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"
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
34
42
44 uint32_t address;
45 uint32_t value;
46 uint32_t page;
47};
48
49enum class EthernetComponentState : uint8_t {
50 STOPPED,
53};
54
56 public:
58 void setup() override;
59 void loop() override;
60 void dump_config() override;
61 float get_setup_priority() const override;
62 void on_powerdown() override { powerdown(); }
63 bool is_connected();
64
65#ifdef USE_ETHERNET_SPI
66 void set_clk_pin(uint8_t clk_pin);
67 void set_miso_pin(uint8_t miso_pin);
68 void set_mosi_pin(uint8_t mosi_pin);
69 void set_cs_pin(uint8_t cs_pin);
70 void set_interrupt_pin(uint8_t interrupt_pin);
71 void set_reset_pin(uint8_t reset_pin);
72 void set_clock_speed(int clock_speed);
73#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
74 void set_polling_interval(uint32_t polling_interval);
75#endif
76#else
77 void set_phy_addr(uint8_t phy_addr);
78 void set_power_pin(int power_pin);
79 void set_mdc_pin(uint8_t mdc_pin);
80 void set_mdio_pin(uint8_t mdio_pin);
81 void set_clk_pin(uint8_t clk_pin);
82 void set_clk_mode(emac_rmii_clock_mode_t clk_mode);
83 void add_phy_register(PHYRegister register_value);
84#endif
86#ifdef USE_ETHERNET_MANUAL_IP
87 void set_manual_ip(const ManualIP &manual_ip);
88#endif
89 void set_fixed_mac(const std::array<uint8_t, 6> &mac) { this->fixed_mac_ = mac; }
90
93 const char *get_use_address() const;
94 void set_use_address(const char *use_address);
95 void get_eth_mac_address_raw(uint8_t *mac);
96 std::string get_eth_mac_address_pretty();
97 const char *get_eth_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
98 eth_duplex_t get_duplex_mode();
99 eth_speed_t get_link_speed();
100 bool powerdown();
101
102 protected:
103 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
104 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
105#if LWIP_IPV6
106 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
107#endif /* LWIP_IPV6 */
108
109 void start_connect_();
110 void finish_connect_();
112 void log_error_and_mark_failed_(esp_err_t err, const char *message);
113#ifdef USE_ETHERNET_KSZ8081
115 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
116#endif
118 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
119
120#ifdef USE_ETHERNET_SPI
121 uint8_t clk_pin_;
122 uint8_t miso_pin_;
123 uint8_t mosi_pin_;
124 uint8_t cs_pin_;
126 int reset_pin_{-1};
129#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
130 uint32_t polling_interval_{0};
131#endif
132#else
133 // Group all 32-bit members first
134 int power_pin_{-1};
135 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
136 std::vector<PHYRegister> phy_registers_{};
137
138 // Group all 8-bit members together
139 uint8_t clk_pin_{0};
140 uint8_t phy_addr_{0};
141 uint8_t mdc_pin_{23};
142 uint8_t mdio_pin_{18};
143#endif
144#ifdef USE_ETHERNET_MANUAL_IP
146#endif
148
149 // Group all uint8_t types together (enums and bools)
152 bool started_{false};
153 bool connected_{false};
154 bool got_ipv4_address_{false};
155#if LWIP_IPV6
156 uint8_t ipv6_count_{0};
157 bool ipv6_setup_done_{false};
158#endif /* LWIP_IPV6 */
159
160 // Pointers at the end (naturally aligned)
161 esp_netif_t *eth_netif_{nullptr};
162 esp_eth_handle_t eth_handle_;
163 esp_eth_phy_t *phy_{nullptr};
165
166 private:
167 // Stores a pointer to a string literal (static storage duration).
168 // ONLY set from Python-generated code with string literals - never dynamic strings.
169 const char *use_address_{""};
170};
171
172// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
173extern EthernetComponent *global_eth_component;
174
175#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
176extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
177#endif
178
179} // namespace ethernet
180} // namespace esphome
181
182#endif // USE_ESP32
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)
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.
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)
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:158
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