ESPHome  2022.5.1
ethernet_component.cpp
Go to the documentation of this file.
1 #include "ethernet_component.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/util.h"
5 
6 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
7 
8 #include <eth_phy/phy_lan8720.h>
9 #include <eth_phy/phy_tlk110.h>
10 #include <lwip/dns.h>
11 
13 #ifndef ESP_IDF_VERSION_VAL
14 #define ESP_IDF_VERSION_VAL(major, minor, patch) (((major) << 16) | ((minor) << 8) | (patch))
15 #endif
16 
17 // Defined in WiFiGeneric.cpp, sets global initialized flag, starts network event task queue and calls
18 // tcpip_adapter_init()
19 extern void tcpipInit(); // NOLINT(readability-identifier-naming)
20 
21 namespace esphome {
22 namespace ethernet {
23 
24 static const char *const TAG = "ethernet";
25 
26 EthernetComponent *global_eth_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
27 
28 #define ESPHL_ERROR_CHECK(err, message) \
29  if ((err) != ESP_OK) { \
30  ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \
31  this->mark_failed(); \
32  return; \
33  }
34 
35 EthernetComponent::EthernetComponent() { global_eth_component = this; }
37  ESP_LOGCONFIG(TAG, "Setting up Ethernet...");
38 
39  auto f = std::bind(&EthernetComponent::on_wifi_event_, this, std::placeholders::_1, std::placeholders::_2);
40  WiFi.onEvent(f);
41 
42  if (this->power_pin_ != nullptr) {
43  this->power_pin_->setup();
44  }
45 
46  switch (this->type_) {
47  case ETHERNET_TYPE_LAN8720: {
48  memcpy(&this->eth_config_, &phy_lan8720_default_ethernet_config, sizeof(eth_config_t));
49  break;
50  }
51  case ETHERNET_TYPE_TLK110: {
52  memcpy(&this->eth_config_, &phy_tlk110_default_ethernet_config, sizeof(eth_config_t));
53  break;
54  }
55  default: {
56  this->mark_failed();
57  return;
58  }
59  }
60 
61  this->eth_config_.phy_addr = static_cast<eth_phy_base_t>(this->phy_addr_);
62  this->eth_config_.clock_mode = this->clk_mode_;
64  this->eth_config_.tcpip_input = tcpip_adapter_eth_input;
65 
66  if (this->power_pin_ != nullptr) {
67  this->orig_power_enable_fun_ = this->eth_config_.phy_power_enable;
69  }
70 
71  tcpipInit();
72 
73  esp_err_t err;
74  err = esp_eth_init(&this->eth_config_);
75  ESPHL_ERROR_CHECK(err, "ETH init error");
76  err = esp_eth_enable();
77  ESPHL_ERROR_CHECK(err, "ETH enable error");
78 }
80  const uint32_t now = millis();
81 
82  switch (this->state_) {
84  if (this->started_) {
85  ESP_LOGI(TAG, "Starting ethernet connection");
87  this->start_connect_();
88  }
89  break;
91  if (!this->started_) {
92  ESP_LOGI(TAG, "Stopped ethernet connection");
94  } else if (this->connected_) {
95  // connection established
96  ESP_LOGI(TAG, "Connected via Ethernet!");
98 
99  this->dump_connect_params_();
100  this->status_clear_warning();
101  } else if (now - this->connect_begin_ > 15000) {
102  ESP_LOGW(TAG, "Connecting via ethernet failed! Re-connecting...");
103  this->start_connect_();
104  }
105  break;
107  if (!this->started_) {
108  ESP_LOGI(TAG, "Stopped ethernet connection");
110  } else if (!this->connected_) {
111  ESP_LOGW(TAG, "Connection via Ethernet lost! Re-connecting...");
113  this->start_connect_();
114  }
115  break;
116  }
117 }
119  ESP_LOGCONFIG(TAG, "Ethernet:");
120  this->dump_connect_params_();
121  LOG_PIN(" Power Pin: ", this->power_pin_);
122  ESP_LOGCONFIG(TAG, " MDC Pin: %u", this->mdc_pin_);
123  ESP_LOGCONFIG(TAG, " MDIO Pin: %u", this->mdio_pin_);
124  ESP_LOGCONFIG(TAG, " Type: %s", this->type_ == ETHERNET_TYPE_LAN8720 ? "LAN8720" : "TLK110");
125 }
129  tcpip_adapter_ip_info_t ip;
130  tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
131  return {ip.ip.addr};
132 }
133 
134 void EthernetComponent::on_wifi_event_(system_event_id_t event, system_event_info_t info) {
135  const char *event_name;
136 
137  switch (event) {
138  case SYSTEM_EVENT_ETH_START:
139  event_name = "ETH started";
140  this->started_ = true;
141  break;
142  case SYSTEM_EVENT_ETH_STOP:
143  event_name = "ETH stopped";
144  this->started_ = false;
145  this->connected_ = false;
146  break;
147  case SYSTEM_EVENT_ETH_CONNECTED:
148  event_name = "ETH connected";
149  break;
150  case SYSTEM_EVENT_ETH_DISCONNECTED:
151  event_name = "ETH disconnected";
152  this->connected_ = false;
153  break;
154  case SYSTEM_EVENT_ETH_GOT_IP:
155  event_name = "ETH Got IP";
156  this->connected_ = true;
157  break;
158  default:
159  return;
160  }
161 
162  ESP_LOGV(TAG, "[Ethernet event] %s (num=%d)", event_name, event);
163 }
164 
166  this->connect_begin_ = millis();
167  this->status_set_warning();
168 
169  esp_err_t err;
170  err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_ETH, App.get_name().c_str());
171  if (err != ERR_OK) {
172  ESP_LOGW(TAG, "tcpip_adapter_set_hostname failed: %s", esp_err_to_name(err));
173  }
174 
175  tcpip_adapter_ip_info_t info;
176  if (this->manual_ip_.has_value()) {
177  info.ip.addr = static_cast<uint32_t>(this->manual_ip_->static_ip);
178  info.gw.addr = static_cast<uint32_t>(this->manual_ip_->gateway);
179  info.netmask.addr = static_cast<uint32_t>(this->manual_ip_->subnet);
180  } else {
181  info.ip.addr = 0;
182  info.gw.addr = 0;
183  info.netmask.addr = 0;
184  }
185 
186  err = tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_ETH);
187  if (err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) {
188  ESPHL_ERROR_CHECK(err, "DHCPC stop error");
189  }
190  err = tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_ETH, &info);
191  ESPHL_ERROR_CHECK(err, "DHCPC set IP info error");
192 
193  if (this->manual_ip_.has_value()) {
194  if (uint32_t(this->manual_ip_->dns1) != 0) {
195  ip_addr_t d;
196  d.type = IPADDR_TYPE_V4;
197  d.u_addr.ip4.addr = static_cast<uint32_t>(this->manual_ip_->dns1);
198  dns_setserver(0, &d);
199  }
200  if (uint32_t(this->manual_ip_->dns1) != 0) {
201  ip_addr_t d;
202  d.type = IPADDR_TYPE_V4;
203  d.u_addr.ip4.addr = static_cast<uint32_t>(this->manual_ip_->dns2);
204  dns_setserver(1, &d);
205  }
206  } else {
207  err = tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_ETH);
208  if (err != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED) {
209  ESPHL_ERROR_CHECK(err, "DHCPC start error");
210  }
211  }
212 
213  this->connect_begin_ = millis();
214  this->status_set_warning();
215 }
217  phy_rmii_configure_data_interface_pins();
218  phy_rmii_smi_configure_pins(global_eth_component->mdc_pin_, global_eth_component->mdio_pin_);
219 }
221  global_eth_component->power_pin_->digital_write(enable);
222  // power up takes some time, datasheet says max 300┬Ás
223  delay(1);
224  global_eth_component->orig_power_enable_fun_(enable);
225 }
228  tcpip_adapter_ip_info_t ip;
229  tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_ETH, &ip);
230  ESP_LOGCONFIG(TAG, " IP Address: %s", network::IPAddress(ip.ip.addr).str().c_str());
231  ESP_LOGCONFIG(TAG, " Hostname: '%s'", App.get_name().c_str());
232  ESP_LOGCONFIG(TAG, " Subnet: %s", network::IPAddress(ip.netmask.addr).str().c_str());
233  ESP_LOGCONFIG(TAG, " Gateway: %s", network::IPAddress(ip.gw.addr).str().c_str());
234 
235 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(3, 3, 4)
236  const ip_addr_t *dns_ip1 = dns_getserver(0);
237  const ip_addr_t *dns_ip2 = dns_getserver(1);
238 #else
239  ip_addr_t tmp_ip1 = dns_getserver(0);
240  const ip_addr_t *dns_ip1 = &tmp_ip1;
241  ip_addr_t tmp_ip2 = dns_getserver(1);
242  const ip_addr_t *dns_ip2 = &tmp_ip2;
243 #endif
244  ESP_LOGCONFIG(TAG, " DNS1: %s", network::IPAddress(dns_ip1->u_addr.ip4.addr).str().c_str());
245  ESP_LOGCONFIG(TAG, " DNS2: %s", network::IPAddress(dns_ip2->u_addr.ip4.addr).str().c_str());
246  uint8_t mac[6];
247  esp_eth_get_mac(mac);
248  ESP_LOGCONFIG(TAG, " MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
249  ESP_LOGCONFIG(TAG, " Is Full Duplex: %s", YESNO(this->eth_config_.phy_get_duplex_mode()));
250  ESP_LOGCONFIG(TAG, " Link Up: %s", YESNO(this->eth_config_.phy_check_link()));
251  ESP_LOGCONFIG(TAG, " Link Speed: %u", this->eth_config_.phy_get_speed_mode() ? 100 : 10);
252 }
253 void EthernetComponent::set_phy_addr(uint8_t phy_addr) { this->phy_addr_ = phy_addr; }
254 void EthernetComponent::set_power_pin(GPIOPin *power_pin) { this->power_pin_ = power_pin; }
255 void EthernetComponent::set_mdc_pin(uint8_t mdc_pin) { this->mdc_pin_ = mdc_pin; }
256 void EthernetComponent::set_mdio_pin(uint8_t mdio_pin) { this->mdio_pin_ = mdio_pin; }
258 void EthernetComponent::set_clk_mode(eth_clock_mode_t clk_mode) { this->clk_mode_ = clk_mode; }
259 void EthernetComponent::set_manual_ip(const ManualIP &manual_ip) { this->manual_ip_ = manual_ip; }
261  if (this->use_address_.empty()) {
262  return App.get_name() + ".local";
263  }
264  return this->use_address_;
265 }
266 void EthernetComponent::set_use_address(const std::string &use_address) { this->use_address_ = use_address; }
267 
268 } // namespace ethernet
269 } // namespace esphome
270 
271 #endif // USE_ESP32_FRAMEWORK_ARDUINO
virtual void digital_write(bool value)=0
static void eth_phy_power_enable(bool enable)
void set_manual_ip(const ManualIP &manual_ip)
std::string str() const
Definition: ip_address.h:28
virtual void setup()=0
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:26
EthernetComponent * global_eth_component
void set_clk_mode(eth_clock_mode_t clk_mode)
void status_clear_warning()
Definition: component.cpp:148
uint8_t type
Application App
Global storage of Application pointer - only one Application can exist.
const std::string & get_name() const
Get the name of this Application set by set_name().
Definition: application.h:128
void status_set_warning()
Definition: component.cpp:140
void set_use_address(const std::string &use_address)
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:111
void on_wifi_event_(system_event_id_t event, system_event_info_t info)
Definition: a4988.cpp:4
eth_phy_power_enable_func orig_power_enable_fun_
void tcpipInit()
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:27