14 static const char *
const TAG =
"ethernet";
18 #define ESPHL_ERROR_CHECK(err, message) \ 19 if ((err) != ESP_OK) { \ 20 ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \ 21 this->mark_failed(); \ 28 ESP_LOGCONFIG(TAG,
"Setting up Ethernet...");
33 err = esp_netif_init();
34 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
35 err = esp_event_loop_create_default();
36 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
38 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
42 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
43 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
49 mac_config.smi_mdc_gpio_num = this->
mdc_pin_;
50 mac_config.smi_mdio_gpio_num = this->
mdio_pin_;
51 mac_config.clock_config.rmii.clock_mode = this->
clk_mode_ == EMAC_CLK_IN_GPIO ? EMAC_CLK_EXT_IN : EMAC_CLK_OUT;
52 mac_config.clock_config.rmii.clock_gpio = this->
clk_mode_;
54 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
57 switch (this->
type_) {
59 phy = esp_eth_phy_new_lan87xx(&phy_config);
63 phy = esp_eth_phy_new_rtl8201(&phy_config);
67 phy = esp_eth_phy_new_dp83848(&phy_config);
71 phy = esp_eth_phy_new_ip101(&phy_config);
80 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
82 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
83 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
86 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
90 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
92 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
96 ESPHL_ERROR_CHECK(err,
"ETH start error");
100 const uint32_t now =
millis();
105 ESP_LOGI(TAG,
"Starting ethernet connection");
112 ESP_LOGI(TAG,
"Stopped ethernet connection");
116 ESP_LOGI(TAG,
"Connected via Ethernet!");
122 ESP_LOGW(TAG,
"Connecting via ethernet failed! Re-connecting...");
128 ESP_LOGI(TAG,
"Stopped ethernet connection");
131 ESP_LOGW(TAG,
"Connection via Ethernet lost! Re-connecting...");
140 std::string eth_type;
141 switch (this->
type_) {
143 eth_type =
"LAN8720";
147 eth_type =
"RTL8201";
151 eth_type =
"DP83848";
159 eth_type =
"Unknown";
163 ESP_LOGCONFIG(TAG,
"Ethernet:");
166 ESP_LOGCONFIG(TAG,
" Power Pin: %u", this->
power_pin_);
168 ESP_LOGCONFIG(TAG,
" MDC Pin: %u", this->
mdc_pin_);
169 ESP_LOGCONFIG(TAG,
" MDIO Pin: %u", this->
mdio_pin_);
170 ESP_LOGCONFIG(TAG,
" Type: %s", eth_type.c_str());
178 esp_netif_ip_info_t ip;
184 const char *event_name;
187 case ETHERNET_EVENT_START:
188 event_name =
"ETH started";
189 global_eth_component->
started_ =
true;
191 case ETHERNET_EVENT_STOP:
192 event_name =
"ETH stopped";
193 global_eth_component->
started_ =
false;
196 case ETHERNET_EVENT_CONNECTED:
197 event_name =
"ETH connected";
199 case ETHERNET_EVENT_DISCONNECTED:
200 event_name =
"ETH disconnected";
207 ESP_LOGV(TAG,
"[Ethernet event] %s (num=%d)", event_name, event);
213 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP (num=%d)", event_id);
223 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
226 esp_netif_ip_info_t info;
228 info.ip.addr =
static_cast<uint32_t
>(this->
manual_ip_->static_ip);
229 info.gw.addr =
static_cast<uint32_t
>(this->
manual_ip_->gateway);
230 info.netmask.addr =
static_cast<uint32_t
>(this->
manual_ip_->subnet);
234 info.netmask.addr = 0;
237 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
239 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
240 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
242 ESP_LOGV(TAG,
"DHCP Client Status: %d", status);
245 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
246 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
249 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
250 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
255 d.type = IPADDR_TYPE_V4;
256 d.u_addr.ip4.addr =
static_cast<uint32_t
>(this->
manual_ip_->dns1);
257 dns_setserver(0, &d);
261 d.type = IPADDR_TYPE_V4;
262 d.u_addr.ip4.addr =
static_cast<uint32_t
>(this->
manual_ip_->dns2);
263 dns_setserver(1, &d);
266 err = esp_netif_dhcpc_start(this->
eth_netif_);
267 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
268 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
279 esp_netif_ip_info_t ip;
282 ESP_LOGCONFIG(TAG,
" Hostname: '%s'",
App.
get_name().c_str());
286 const ip_addr_t *dns_ip1 = dns_getserver(0);
287 const ip_addr_t *dns_ip2 = dns_getserver(1);
295 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_MAC_ADDR, &mac);
296 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_MAC error");
297 ESP_LOGCONFIG(TAG,
" MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
299 eth_duplex_t duplex_mode;
300 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
301 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_DUPLEX_MODE error");
302 ESP_LOGCONFIG(TAG,
" Is Full Duplex: %s", YESNO(duplex_mode == ETH_DUPLEX_FULL));
305 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_SPEED, &speed);
306 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_SPEED error");
307 ESP_LOGCONFIG(TAG,
" Link Speed: %u", speed == ETH_SPEED_100M ? 100 : 10);
bool can_proceed() override
void set_manual_ip(const ManualIP &manual_ip)
void set_power_pin(int power_pin)
void set_mdio_pin(uint8_t mdio_pin)
void set_mdc_pin(uint8_t mdc_pin)
network::IPAddress get_ip_address()
EthernetComponentState state_
uint32_t IRAM_ATTR HOT millis()
EthernetComponent * global_eth_component
void set_type(EthernetType type)
void dump_connect_params_()
emac_rmii_clock_gpio_t clk_mode_
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
void status_clear_warning()
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().
void status_set_warning()
void set_use_address(const std::string &use_address)
virtual void mark_failed()
Mark this component as failed.
void dump_config() override
void set_phy_addr(uint8_t phy_addr)
optional< ManualIP > manual_ip_
float get_setup_priority() const override
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
esp_eth_handle_t eth_handle_
std::string get_use_address() const
void IRAM_ATTR HOT delay(uint32_t ms)
void set_clk_mode(emac_rmii_clock_gpio_t clk_mode)