ESPHome  2023.11.6
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 
26 };
27 
28 struct ManualIP {
34 };
35 
37  STOPPED,
38  CONNECTING,
39  CONNECTED,
40 };
41 
42 class EthernetComponent : public Component {
43  public:
45  void setup() override;
46  void loop() override;
47  void dump_config() override;
48  float get_setup_priority() const override;
49  bool can_proceed() override;
50  void on_shutdown() override { powerdown(); }
51  bool is_connected();
52 
53  void set_phy_addr(uint8_t phy_addr);
54  void set_power_pin(int power_pin);
55  void set_mdc_pin(uint8_t mdc_pin);
56  void set_mdio_pin(uint8_t mdio_pin);
57  void set_type(EthernetType type);
58  void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio);
59  void set_manual_ip(const ManualIP &manual_ip);
60 
62  std::string get_use_address() const;
63  void set_use_address(const std::string &use_address);
64  bool powerdown();
65 
66  protected:
67  static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
68  static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
69 #if LWIP_IPV6
70  static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
71 #endif /* LWIP_IPV6 */
72 
73  void start_connect_();
74  void dump_connect_params_();
76  void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
77 
78  std::string use_address_;
79  uint8_t phy_addr_{0};
80  int power_pin_{-1};
81  uint8_t mdc_pin_{23};
82  uint8_t mdio_pin_{18};
84  emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
85  emac_rmii_clock_gpio_t clk_gpio_{EMAC_CLK_IN_GPIO};
86  optional<ManualIP> manual_ip_{};
87 
88  bool started_{false};
89  bool connected_{false};
90 #if LWIP_IPV6
91  bool got_ipv6_{false};
92  uint8_t ipv6_count_{0};
93 #endif /* LWIP_IPV6 */
95  uint32_t connect_begin_;
96  esp_netif_t *eth_netif_{nullptr};
97  esp_eth_handle_t eth_handle_;
98  esp_eth_phy_t *phy_{nullptr};
99 };
100 
101 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
103 extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
104 
105 } // namespace ethernet
106 } // namespace esphome
107 
108 #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::IPAddress dns2
The second DNS server. 0.0.0.0 for default.
uint8_t type
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t event_id
Definition: tt21100.cpp:15
network::IPAddress get_ip_address()
Definition: util.cpp:40