10 #include "esp_event.h" 15 static const char *
const TAG =
"ethernet";
19 #define ESPHL_ERROR_CHECK(err, message) \ 20 if ((err) != ESP_OK) { \ 21 ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \ 22 this->mark_failed(); \ 29 ESP_LOGCONFIG(TAG,
"Setting up Ethernet...");
30 if (esp_reset_reason() != ESP_RST_DEEPSLEEP) {
36 err = esp_netif_init();
37 ESPHL_ERROR_CHECK(err,
"ETH netif init error");
38 err = esp_event_loop_create_default();
39 ESPHL_ERROR_CHECK(err,
"ETH event loop error");
41 esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
45 eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
49 eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
50 #if ESP_IDF_VERSION_MAJOR >= 5 51 eth_esp32_emac_config_t esp32_emac_config = ETH_ESP32_EMAC_DEFAULT_CONFIG();
52 esp32_emac_config.smi_mdc_gpio_num = this->
mdc_pin_;
53 esp32_emac_config.smi_mdio_gpio_num = this->
mdio_pin_;
54 esp32_emac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
55 esp32_emac_config.clock_config.rmii.clock_gpio = this->
clk_gpio_;
57 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&esp32_emac_config, &mac_config);
59 mac_config.smi_mdc_gpio_num = this->
mdc_pin_;
60 mac_config.smi_mdio_gpio_num = this->
mdio_pin_;
61 mac_config.clock_config.rmii.clock_mode = this->
clk_mode_;
62 mac_config.clock_config.rmii.clock_gpio = this->
clk_gpio_;
64 esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
67 switch (this->
type_) {
69 this->
phy_ = esp_eth_phy_new_lan87xx(&phy_config);
73 this->
phy_ = esp_eth_phy_new_rtl8201(&phy_config);
77 this->
phy_ = esp_eth_phy_new_dp83848(&phy_config);
81 this->
phy_ = esp_eth_phy_new_ip101(&phy_config);
90 #if ESP_IDF_VERSION_MAJOR >= 5 91 this->
phy_ = esp_eth_phy_new_ksz80xx(&phy_config);
93 this->
phy_ = esp_eth_phy_new_ksz8081(&phy_config);
103 esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(mac, this->
phy_);
105 err = esp_eth_driver_install(ð_config, &this->
eth_handle_);
106 ESPHL_ERROR_CHECK(err,
"ETH driver install error");
115 ESPHL_ERROR_CHECK(err,
"ETH netif attach error");
119 ESPHL_ERROR_CHECK(err,
"ETH event handler register error");
121 ESPHL_ERROR_CHECK(err,
"GOT IP event handler register error");
124 ESPHL_ERROR_CHECK(err,
"GOT IP6 event handler register error");
129 ESPHL_ERROR_CHECK(err,
"ETH start error");
133 const uint32_t now =
millis();
138 ESP_LOGI(TAG,
"Starting ethernet connection");
145 ESP_LOGI(TAG,
"Stopped ethernet connection");
149 ESP_LOGI(TAG,
"Connected via Ethernet!");
155 ESP_LOGW(TAG,
"Connecting via ethernet failed! Re-connecting...");
161 ESP_LOGI(TAG,
"Stopped ethernet connection");
164 ESP_LOGW(TAG,
"Connection via Ethernet lost! Re-connecting...");
170 esp_ip6_addr_t ip6_addr;
171 if (esp_netif_get_ip6_global(this->
eth_netif_, &ip6_addr) == 0 &&
172 esp_netif_ip6_get_addr_type(&ip6_addr) == ESP_IP6_ADDR_IS_GLOBAL) {
173 ESP_LOGCONFIG(TAG,
"IPv6 Addr (Global): " IPV6STR, IPV62STR(ip6_addr));
175 esp_netif_get_ip6_linklocal(this->
eth_netif_, &ip6_addr);
176 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(ip6_addr));
187 const char *eth_type;
188 switch (this->
type_) {
190 eth_type =
"LAN8720";
194 eth_type =
"RTL8201";
198 eth_type =
"DP83848";
210 eth_type =
"KSZ8081";
214 eth_type =
"KSZ8081RNA";
218 eth_type =
"Unknown";
222 ESP_LOGCONFIG(TAG,
"Ethernet:");
225 ESP_LOGCONFIG(TAG,
" Power Pin: %u", this->
power_pin_);
227 ESP_LOGCONFIG(TAG,
" MDC Pin: %u", this->
mdc_pin_);
228 ESP_LOGCONFIG(TAG,
" MDIO Pin: %u", this->
mdio_pin_);
229 ESP_LOGCONFIG(TAG,
" Type: %s", eth_type);
230 ESP_LOGCONFIG(TAG,
" PHY addr: %u", this->
phy_addr_);
238 esp_netif_ip_info_t ip;
244 const char *event_name;
247 case ETHERNET_EVENT_START:
248 event_name =
"ETH started";
249 global_eth_component->
started_ =
true;
251 case ETHERNET_EVENT_STOP:
252 event_name =
"ETH stopped";
253 global_eth_component->
started_ =
false;
256 case ETHERNET_EVENT_CONNECTED:
257 event_name =
"ETH connected";
259 case ETHERNET_EVENT_DISCONNECTED:
260 event_name =
"ETH disconnected";
267 ESP_LOGV(TAG,
"[Ethernet event] %s (num=%" PRId32
")", event_name, event);
273 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP (num=%" PRId32
")", event_id);
279 ESP_LOGV(TAG,
"[Ethernet event] ETH Got IP6 (num=%" PRId32
")", event_id);
292 ESP_LOGW(TAG,
"esp_netif_set_hostname failed: %s", esp_err_to_name(err));
295 esp_netif_ip_info_t info;
303 info.netmask.addr = 0;
306 esp_netif_dhcp_status_t
status = ESP_NETIF_DHCP_INIT;
308 err = esp_netif_dhcpc_get_status(this->
eth_netif_, &status);
309 ESPHL_ERROR_CHECK(err,
"DHCPC Get Status Failed!");
311 ESP_LOGV(TAG,
"DHCP Client Status: %d", status);
314 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
315 ESPHL_ERROR_CHECK(err,
"DHCPC stop error");
318 err = esp_netif_set_ip_info(this->
eth_netif_, &info);
319 ESPHL_ERROR_CHECK(err,
"DHCPC set IP info error");
325 dns_setserver(0, &d);
330 dns_setserver(1, &d);
333 err = esp_netif_dhcpc_start(this->
eth_netif_);
334 if (err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) {
335 ESPHL_ERROR_CHECK(err,
"DHCPC start error");
338 err = esp_netif_create_ip6_linklocal(this->
eth_netif_);
340 ESPHL_ERROR_CHECK(err,
"IPv6 local failed");
352 esp_netif_ip_info_t ip;
355 ESP_LOGCONFIG(TAG,
" Hostname: '%s'",
App.
get_name().c_str());
359 const ip_addr_t *dns_ip1 = dns_getserver(0);
360 const ip_addr_t *dns_ip2 = dns_getserver(1);
367 esp_ip6_addr_t ip6_addr;
368 esp_netif_get_ip6_linklocal(this->
eth_netif_, &ip6_addr);
369 ESP_LOGCONFIG(TAG,
" IPv6: " IPV6STR, IPV62STR(ip6_addr));
371 if (esp_netif_get_ip6_global(this->
eth_netif_, &ip6_addr) == 0 &&
372 esp_netif_ip6_get_addr_type(&ip6_addr) == ESP_IP6_ADDR_IS_GLOBAL) {
373 ESP_LOGCONFIG(TAG,
"IPv6 Addr (Global): " IPV6STR, IPV62STR(ip6_addr));
381 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_MAC_ADDR, &mac);
382 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_MAC error");
383 ESP_LOGCONFIG(TAG,
" MAC Address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
385 eth_duplex_t duplex_mode;
386 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_DUPLEX_MODE, &duplex_mode);
387 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_DUPLEX_MODE error");
388 ESP_LOGCONFIG(TAG,
" Is Full Duplex: %s", YESNO(duplex_mode == ETH_DUPLEX_FULL));
391 err = esp_eth_ioctl(this->
eth_handle_, ETH_CMD_G_SPEED, &speed);
392 ESPHL_ERROR_CHECK(err,
"ETH_CMD_G_SPEED error");
393 ESP_LOGCONFIG(TAG,
" Link Speed: %u", speed == ETH_SPEED_100M ? 100 : 10);
417 ESP_LOGI(TAG,
"Powering down ethernet PHY");
418 if (this->
phy_ ==
nullptr) {
419 ESP_LOGE(TAG,
"Ethernet PHY not assigned");
424 if (this->
phy_->pwrctl(this->phy_,
false) != ESP_OK) {
425 ESP_LOGE(TAG,
"Error powering down ethernet PHY");
432 #define KSZ80XX_PC2R_REG_ADDR (0x1F) 436 uint32_t phy_control_2;
437 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
438 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
439 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
450 if ((phy_control_2 & (1 << 7)) != (1 << 7)) {
451 phy_control_2 |= 1 << 7;
452 err = mac->write_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, phy_control_2);
453 ESPHL_ERROR_CHECK(err,
"Write PHY Control 2 failed");
454 err = mac->read_phy_reg(mac, this->
phy_addr_, KSZ80XX_PC2R_REG_ADDR, &(phy_control_2));
455 ESPHL_ERROR_CHECK(err,
"Read PHY Control 2 failed");
456 ESP_LOGVV(TAG,
"KSZ8081 PHY Control 2: %s",
format_hex_pretty((u_int8_t *) &phy_control_2, 2).c_str());
459 #undef KSZ80XX_PC2R_REG_ADDR bool can_proceed() override
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
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 ksz8081_set_clock_reference_(esp_eth_mac_t *mac)
Set RMII Reference Clock Select bit for KSZ8081.
void set_mdc_pin(uint8_t mdc_pin)
network::IPAddress get_ip_address()
EthernetComponentState state_
static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
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.
Implementation of SPI Controller mode.
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)