ESPHome  2024.11.0
ethernet_component.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
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 
15 namespace esphome {
16 namespace ethernet {
17 
29 };
30 
31 struct ManualIP {
37 };
38 
39 struct PHYRegister {
40  uint32_t address;
41  uint32_t value;
42  uint32_t page;
43 };
44 
46  STOPPED,
47  CONNECTING,
48  CONNECTED,
49 };
50 
51 class EthernetComponent : public Component {
52  public:
54  void setup() override;
55  void loop() override;
56  void dump_config() override;
57  float get_setup_priority() const override;
58  bool can_proceed() override;
59  void on_shutdown() override { powerdown(); }
60  bool is_connected();
61 
62 #ifdef USE_ETHERNET_SPI
63  void set_clk_pin(uint8_t clk_pin);
64  void set_miso_pin(uint8_t miso_pin);
65  void set_mosi_pin(uint8_t mosi_pin);
66  void set_cs_pin(uint8_t cs_pin);
67  void set_interrupt_pin(uint8_t interrupt_pin);
68  void set_reset_pin(uint8_t reset_pin);
69  void set_clock_speed(int clock_speed);
70 #ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
71  void set_polling_interval(uint32_t polling_interval);
72 #endif
73 #else
74  void set_phy_addr(uint8_t phy_addr);
75  void set_power_pin(int power_pin);
76  void set_mdc_pin(uint8_t mdc_pin);
77  void set_mdio_pin(uint8_t mdio_pin);
78  void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio);
79  void add_phy_register(PHYRegister register_value);
80 #endif
81  void set_type(EthernetType type);
82  void set_manual_ip(const ManualIP &manual_ip);
83 
85  network::IPAddress get_dns_address(uint8_t num);
86  std::string get_use_address() const;
87  void set_use_address(const std::string &use_address);
88  void get_eth_mac_address_raw(uint8_t *mac);
89  std::string get_eth_mac_address_pretty();
90  eth_duplex_t get_duplex_mode();
91  eth_speed_t get_link_speed();
92  bool powerdown();
93 
94  protected:
95  static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
96  static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
97 #if LWIP_IPV6
98  static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
99 #endif /* LWIP_IPV6 */
100 
101  void start_connect_();
102  void dump_connect_params_();
104  void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
106  void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
107 
108  std::string use_address_;
109 #ifdef USE_ETHERNET_SPI
110  uint8_t clk_pin_;
111  uint8_t miso_pin_;
112  uint8_t mosi_pin_;
113  uint8_t cs_pin_;
114  int interrupt_pin_{-1};
115  int reset_pin_{-1};
116  int phy_addr_spi_{-1};
118 #ifdef USE_ETHERNET_SPI_POLLING_SUPPORT
119  uint32_t polling_interval_{0};
120 #endif
121 #else
122  uint8_t phy_addr_{0};
123  int power_pin_{-1};
124  uint8_t mdc_pin_{23};
125  uint8_t mdio_pin_{18};
126  emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
127  emac_rmii_clock_gpio_t clk_gpio_{EMAC_CLK_IN_GPIO};
128  std::vector<PHYRegister> phy_registers_{};
129 #endif
131  optional<ManualIP> manual_ip_{};
132 
133  bool started_{false};
134  bool connected_{false};
135  bool got_ipv4_address_{false};
136 #if LWIP_IPV6
137  uint8_t ipv6_count_{0};
138 #endif /* LWIP_IPV6 */
140  uint32_t connect_begin_;
141  esp_netif_t *eth_netif_{nullptr};
142  esp_eth_handle_t eth_handle_;
143  esp_eth_phy_t *phy_{nullptr};
144 };
145 
146 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
148 extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
149 
150 } // namespace ethernet
151 } // namespace esphome
152 
153 #endif // USE_ESP32
void setup()
void loop()
network::IPAddress dns1
The first DNS server. 0.0.0.0 for default.
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
std::string get_use_address()
Get the active network hostname.
Definition: util.cpp:52
bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition: util.cpp:15
EthernetComponent * global_eth_component
network::IPAddresses get_ip_addresses()
Definition: util.cpp:40
network::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
uint8_t type
std::array< IPAddress, 5 > IPAddresses
Definition: ip_address.h:141
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t event_id
Definition: tt21100.cpp:15