ESPHome  2024.3.2
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 
14 namespace esphome {
15 namespace ethernet {
16 
27 };
28 
29 struct ManualIP {
35 };
36 
38  STOPPED,
39  CONNECTING,
40  CONNECTED,
41 };
42 
43 class EthernetComponent : public Component {
44  public:
46  void setup() override;
47  void loop() override;
48  void dump_config() override;
49  float get_setup_priority() const override;
50  bool can_proceed() override;
51  void on_shutdown() override { powerdown(); }
52  bool is_connected();
53 
54 #ifdef USE_ETHERNET_SPI
55  void set_clk_pin(uint8_t clk_pin);
56  void set_miso_pin(uint8_t miso_pin);
57  void set_mosi_pin(uint8_t mosi_pin);
58  void set_cs_pin(uint8_t cs_pin);
59  void set_interrupt_pin(uint8_t interrupt_pin);
60  void set_reset_pin(uint8_t reset_pin);
61  void set_clock_speed(int clock_speed);
62 #else
63  void set_phy_addr(uint8_t phy_addr);
64  void set_power_pin(int power_pin);
65  void set_mdc_pin(uint8_t mdc_pin);
66  void set_mdio_pin(uint8_t mdio_pin);
67  void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio);
68 #endif
69  void set_type(EthernetType type);
70  void set_manual_ip(const ManualIP &manual_ip);
71 
73  std::string get_use_address() const;
74  void set_use_address(const std::string &use_address);
75  bool powerdown();
76 
77  protected:
78  static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
79  static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
80 #if LWIP_IPV6
81  static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
82 #endif /* LWIP_IPV6 */
83 
84  void start_connect_();
85  void dump_connect_params_();
87  void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
88 
89  std::string use_address_;
90 #ifdef USE_ETHERNET_SPI
91  uint8_t clk_pin_;
92  uint8_t miso_pin_;
93  uint8_t mosi_pin_;
94  uint8_t cs_pin_;
95  uint8_t interrupt_pin_;
96  int reset_pin_{-1};
97  int phy_addr_spi_{-1};
99 #else
100  uint8_t phy_addr_{0};
101  int power_pin_{-1};
102  uint8_t mdc_pin_{23};
103  uint8_t mdio_pin_{18};
104  emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
105  emac_rmii_clock_gpio_t clk_gpio_{EMAC_CLK_IN_GPIO};
106 #endif
108  optional<ManualIP> manual_ip_{};
109 
110  bool started_{false};
111  bool connected_{false};
112  bool got_ipv4_address_{false};
113 #if LWIP_IPV6
114  uint8_t ipv6_count_{0};
115 #endif /* LWIP_IPV6 */
117  uint32_t connect_begin_;
118  esp_netif_t *eth_netif_{nullptr};
119  esp_eth_handle_t eth_handle_;
120  esp_eth_phy_t *phy_{nullptr};
121 };
122 
123 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
125 extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
126 
127 } // namespace ethernet
128 } // namespace esphome
129 
130 #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:138
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