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