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...");
29 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
35 err = esp_netif_init();
36 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
37 err = esp_event_loop_create_default();
38 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
40 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
44 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
45 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
50 mac_config.smi_mdc_gpio_num = this->
mdc_pin_;
51 mac_config.smi_mdio_gpio_num = this->
mdio_pin_;
52 mac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
53 mac_config.clock_config.rmii.clock_gpio = this->
clk_gpio_;
55 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
57 switch (this->
type_) {
59 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
63 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
67 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
71 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
79 this->
phy_ = esp_eth_phy_new_ksz8081(&phy_config);
88 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
90 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
91 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
94 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
98 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
100 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
104 ESPHL_ERROR_CHECK(err,
"ETH start error");
108 const uint32_t now =
millis();
113 ESP_LOGI(TAG,
"Starting ethernet connection");
120 ESP_LOGI(TAG,
"Stopped ethernet connection");
124 ESP_LOGI(TAG,
"Connected via Ethernet!");
130 ESP_LOGW(TAG,
"Connecting via ethernet failed! Re-connecting...");
136 ESP_LOGI(TAG,
"Stopped ethernet connection");
139 ESP_LOGW(TAG,
"Connection via Ethernet lost! Re-connecting...");
148 const char *eth_type;
149 switch (this->
type_) {
151 eth_type =
"LAN8720";
155 eth_type =
"RTL8201";
159 eth_type =
"DP83848";
171 eth_type =
"KSZ8081";
175 eth_type =
"Unknown";
179 ESP_LOGCONFIG(TAG,
"Ethernet:");
182 ESP_LOGCONFIG(TAG,
" Power Pin: %u", this->
power_pin_);
184 ESP_LOGCONFIG(TAG,
" MDC Pin: %u", this->
mdc_pin_);
185 ESP_LOGCONFIG(TAG,
" MDIO Pin: %u", this->
mdio_pin_);
186 ESP_LOGCONFIG(TAG,
" Type: %s", eth_type);
187 ESP_LOGCONFIG(TAG,
" PHY addr: %u", this->
phy_addr_);
195 esp_netif_ip_info_t ip;
201 const char *event_name;
204 case ETHERNET_EVENT_START:
205 event_name =
"ETH started";
206 global_eth_component->
started_ =
true;
208 case ETHERNET_EVENT_STOP:
209 event_name =
"ETH stopped";
210 global_eth_component->
started_ =
false;
213 case ETHERNET_EVENT_CONNECTED:
214 event_name =
"ETH connected";
216 case ETHERNET_EVENT_DISCONNECTED:
217 event_name =
"ETH disconnected";
224 ESP_LOGV(TAG,
"[Ethernet event] %s (num=%d)", event_name, event);
230 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP (num=%d)", event_id);
240 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
243 esp_netif_ip_info_t info;
245 info.ip.addr =
static_cast<uint32_t
>(this->
manual_ip_->static_ip);
246 info.gw.addr =
static_cast<uint32_t
>(this->
manual_ip_->gateway);
247 info.netmask.addr =
static_cast<uint32_t
>(this->
manual_ip_->subnet);
251 info.netmask.addr = 0;
254 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
256 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
257 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
259 ESP_LOGV(TAG,
"DHCP Client Status: %d", status);
262 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
263 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
266 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
267 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
273 d.type = IPADDR_TYPE_V4;
274 d.u_addr.ip4.addr =
static_cast<uint32_t
>(this->
manual_ip_->dns1);
276 d.addr =
static_cast<uint32_t
>(this->
manual_ip_->dns1);
278 dns_setserver(0, &d);
283 d.type = IPADDR_TYPE_V4;
284 d.u_addr.ip4.addr =
static_cast<uint32_t
>(this->
manual_ip_->dns2);
286 d.addr =
static_cast<uint32_t
>(this->
manual_ip_->dns2);
288 dns_setserver(1, &d);
291 err = esp_netif_dhcpc_start(this->
eth_netif_);
292 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
293 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
304 esp_netif_ip_info_t ip;
307 ESP_LOGCONFIG(TAG,
" Hostname: '%s'",
App.
get_name().c_str());
311 const ip_addr_t *dns_ip1 = dns_getserver(0);
312 const ip_addr_t *dns_ip2 = dns_getserver(1);
325 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_MAC_ADDR, &mac);
326 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_MAC error");
327 ESP_LOGCONFIG(TAG,
" MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
329 eth_duplex_t duplex_mode;
330 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
331 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_DUPLEX_MODE error");
332 ESP_LOGCONFIG(TAG,
" Is Full Duplex: %s", YESNO(duplex_mode == ETH_DUPLEX_FULL));
335 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_SPEED, &speed);
336 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_SPEED error");
337 ESP_LOGCONFIG(TAG,
" Link Speed: %u", speed == ETH_SPEED_100M ? 100 : 10);
361 ESP_LOGI(TAG,
"Powering down ethernet PHY");
362 if (this->
phy_ ==
nullptr) {
363 ESP_LOGE(TAG,
"Ethernet PHY not assigned");
368 if (this->
phy_->pwrctl(this->phy_,
false) != ESP_OK) {
369 ESP_LOGE(TAG,
"Error powering down ethernet PHY");
bool can_proceed() override
void set_manual_ip(const ManualIP &manual_ip)
esp_eth_phy_t * esp_eth_phy_new_jl1101(const eth_phy_config_t *config)
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
emac_rmii_clock_gpio_t clk_gpio_
void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio)
void set_type(EthernetType type)
void dump_connect_params_()
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 pre_setup().
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)
emac_rmii_clock_mode_t clk_mode_
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)