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();
48 mac_config.smi_mdc_gpio_num = this->
mdc_pin_;
49 mac_config.smi_mdio_gpio_num = this->
mdio_pin_;
50 mac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
51 mac_config.clock_config.rmii.clock_gpio = this->
clk_gpio_;
53 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
56 switch (this->
type_) {
58 phy = esp_eth_phy_new_lan87xx(&phy_config);
62 phy = esp_eth_phy_new_rtl8201(&phy_config);
66 phy = esp_eth_phy_new_dp83848(&phy_config);
70 phy = esp_eth_phy_new_ip101(&phy_config);
83 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, phy);
85 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
86 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
89 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
93 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
95 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
99 ESPHL_ERROR_CHECK(err,
"ETH start error");
103 const uint32_t now =
millis();
108 ESP_LOGI(TAG,
"Starting ethernet connection");
115 ESP_LOGI(TAG,
"Stopped ethernet connection");
119 ESP_LOGI(TAG,
"Connected via Ethernet!");
125 ESP_LOGW(TAG,
"Connecting via ethernet failed! Re-connecting...");
131 ESP_LOGI(TAG,
"Stopped ethernet connection");
134 ESP_LOGW(TAG,
"Connection via Ethernet lost! Re-connecting...");
143 std::string eth_type;
144 switch (this->
type_) {
146 eth_type =
"LAN8720";
150 eth_type =
"RTL8201";
154 eth_type =
"DP83848";
162 eth_type =
"Unknown";
166 ESP_LOGCONFIG(TAG,
"Ethernet:");
169 ESP_LOGCONFIG(TAG,
" Power Pin: %u", this->
power_pin_);
171 ESP_LOGCONFIG(TAG,
" MDC Pin: %u", this->
mdc_pin_);
172 ESP_LOGCONFIG(TAG,
" MDIO Pin: %u", this->
mdio_pin_);
173 ESP_LOGCONFIG(TAG,
" Type: %s", eth_type.c_str());
181 esp_netif_ip_info_t ip;
187 const char *event_name;
190 case ETHERNET_EVENT_START:
191 event_name =
"ETH started";
192 global_eth_component->
started_ =
true;
194 case ETHERNET_EVENT_STOP:
195 event_name =
"ETH stopped";
196 global_eth_component->
started_ =
false;
199 case ETHERNET_EVENT_CONNECTED:
200 event_name =
"ETH connected";
202 case ETHERNET_EVENT_DISCONNECTED:
203 event_name =
"ETH disconnected";
210 ESP_LOGV(TAG,
"[Ethernet event] %s (num=%d)", event_name, event);
216 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP (num=%d)", event_id);
226 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
229 esp_netif_ip_info_t info;
231 info.ip.addr =
static_cast<uint32_t
>(this->
manual_ip_->static_ip);
232 info.gw.addr =
static_cast<uint32_t
>(this->
manual_ip_->gateway);
233 info.netmask.addr =
static_cast<uint32_t
>(this->
manual_ip_->subnet);
237 info.netmask.addr = 0;
240 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
242 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
243 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
245 ESP_LOGV(TAG,
"DHCP Client Status: %d", status);
248 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
249 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
252 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
253 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
258 d.type = IPADDR_TYPE_V4;
259 d.u_addr.ip4.addr =
static_cast<uint32_t
>(this->
manual_ip_->dns1);
260 dns_setserver(0, &d);
264 d.type = IPADDR_TYPE_V4;
265 d.u_addr.ip4.addr =
static_cast<uint32_t
>(this->
manual_ip_->dns2);
266 dns_setserver(1, &d);
269 err = esp_netif_dhcpc_start(this->
eth_netif_);
270 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
271 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
282 esp_netif_ip_info_t ip;
285 ESP_LOGCONFIG(TAG,
" Hostname: '%s'",
App.
get_name().c_str());
289 const ip_addr_t *dns_ip1 = dns_getserver(0);
290 const ip_addr_t *dns_ip2 = dns_getserver(1);
298 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_MAC_ADDR, &mac);
299 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_MAC error");
300 ESP_LOGCONFIG(TAG,
" MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
302 eth_duplex_t duplex_mode;
303 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
304 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_DUPLEX_MODE error");
305 ESP_LOGCONFIG(TAG,
" Is Full Duplex: %s", YESNO(duplex_mode == ETH_DUPLEX_FULL));
308 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_SPEED, &speed);
309 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_SPEED error");
310 ESP_LOGCONFIG(TAG,
" Link Speed: %u", speed == ETH_SPEED_100M ? 100 : 10);
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)