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