ESPHome 2025.10.2
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 bool can_proceed() 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 void set_manual_ip(const ManualIP &manual_ip);
87 void set_fixed_mac(const std::array<uint8_t, 6> &mac) { this->fixed_mac_ = mac; }
88
91 std::string get_use_address() const;
92 void set_use_address(const std::string &use_address);
93 void get_eth_mac_address_raw(uint8_t *mac);
94 std::string get_eth_mac_address_pretty();
95 eth_duplex_t get_duplex_mode();
96 eth_speed_t get_link_speed();
97 bool powerdown();
98
99 protected:
100 static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
101 static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
102#if LWIP_IPV6
103 static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
104#endif /* LWIP_IPV6 */
105
106 void start_connect_();
107 void finish_connect_();
109 void log_error_and_mark_failed_(esp_err_t err, const char *message);
110#ifdef USE_ETHERNET_KSZ8081
112 void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
113#endif
115 void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
116
117 std::string use_address_;
118#ifdef USE_ETHERNET_SPI
119 uint8_t clk_pin_;
120 uint8_t miso_pin_;
121 uint8_t mosi_pin_;
122 uint8_t cs_pin_;
124 int reset_pin_{-1};
127#ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
128 uint32_t polling_interval_{0};
129#endif
130#else
131 // Group all 32-bit members first
132 int power_pin_{-1};
133 emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
134 std::vector<PHYRegister> phy_registers_{};
135
136 // Group all 8-bit members together
137 uint8_t clk_pin_{0};
138 uint8_t phy_addr_{0};
139 uint8_t mdc_pin_{23};
140 uint8_t mdio_pin_{18};
141#endif
144
145 // Group all uint8_t types together (enums and bools)
148 bool started_{false};
149 bool connected_{false};
150 bool got_ipv4_address_{false};
151#if LWIP_IPV6
152 uint8_t ipv6_count_{0};
153 bool ipv6_setup_done_{false};
154#endif /* LWIP_IPV6 */
155
156 // Pointers at the end (naturally aligned)
157 esp_netif_t *eth_netif_{nullptr};
158 esp_eth_handle_t eth_handle_;
159 esp_eth_phy_t *phy_{nullptr};
161};
162
163// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
165
166#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 4, 2)
167extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
168#endif
169
170} // namespace ethernet
171} // namespace esphome
172
173#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_use_address(const std::string &use_address)
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_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