ESPHome  2024.6.1
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 
28 };
29 
30 struct ManualIP {
36 };
37 
38 struct PHYRegister {
39  uint32_t address;
40  uint32_t value;
41  uint32_t page;
42 };
43 
45  STOPPED,
46  CONNECTING,
47  CONNECTED,
48 };
49 
50 class EthernetComponent : public Component {
51  public:
53  void setup() override;
54  void loop() override;
55  void dump_config() override;
56  float get_setup_priority() const override;
57  bool can_proceed() override;
58  void on_shutdown() override { powerdown(); }
59  bool is_connected();
60 
61 #ifdef USE_ETHERNET_SPI
62  void set_clk_pin(uint8_t clk_pin);
63  void set_miso_pin(uint8_t miso_pin);
64  void set_mosi_pin(uint8_t mosi_pin);
65  void set_cs_pin(uint8_t cs_pin);
66  void set_interrupt_pin(uint8_t interrupt_pin);
67  void set_reset_pin(uint8_t reset_pin);
68  void set_clock_speed(int clock_speed);
69 #else
70  void set_phy_addr(uint8_t phy_addr);
71  void set_power_pin(int power_pin);
72  void set_mdc_pin(uint8_t mdc_pin);
73  void set_mdio_pin(uint8_t mdio_pin);
74  void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio);
75  void add_phy_register(PHYRegister register_value);
76 #endif
77  void set_type(EthernetType type);
78  void set_manual_ip(const ManualIP &manual_ip);
79 
81  network::IPAddress get_dns_address(uint8_t num);
82  std::string get_use_address() const;
83  void set_use_address(const std::string &use_address);
84  void get_eth_mac_address_raw(uint8_t *mac);
85  std::string get_eth_mac_address_pretty();
86  eth_duplex_t get_duplex_mode();
87  eth_speed_t get_link_speed();
88  bool powerdown();
89 
90  protected:
91  static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
92  static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
93 #if LWIP_IPV6
94  static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
95 #endif /* LWIP_IPV6 */
96 
97  void start_connect_();
98  void dump_connect_params_();
100  void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
102  void write_phy_register_(esp_eth_mac_t *mac, PHYRegister register_data);
103 
104  std::string use_address_;
105 #ifdef USE_ETHERNET_SPI
106  uint8_t clk_pin_;
107  uint8_t miso_pin_;
108  uint8_t mosi_pin_;
109  uint8_t cs_pin_;
110  uint8_t interrupt_pin_;
111  int reset_pin_{-1};
112  int phy_addr_spi_{-1};
114 #else
115  uint8_t phy_addr_{0};
116  int power_pin_{-1};
117  uint8_t mdc_pin_{23};
118  uint8_t mdio_pin_{18};
119  emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
120  emac_rmii_clock_gpio_t clk_gpio_{EMAC_CLK_IN_GPIO};
121  std::vector<PHYRegister> phy_registers_{};
122 #endif
124  optional<ManualIP> manual_ip_{};
125 
126  bool started_{false};
127  bool connected_{false};
128  bool got_ipv4_address_{false};
129 #if LWIP_IPV6
130  uint8_t ipv6_count_{0};
131 #endif /* LWIP_IPV6 */
133  uint32_t connect_begin_;
134  esp_netif_t *eth_netif_{nullptr};
135  esp_eth_handle_t eth_handle_;
136  esp_eth_phy_t *phy_{nullptr};
137 };
138 
139 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
141 extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
142 
143 } // namespace ethernet
144 } // namespace esphome
145 
146 #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:139
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
uint8_t event_id
Definition: tt21100.cpp:15