2019-04-17 12:06:00 +02:00
|
|
|
#pragma once
|
|
|
|
|
2021-09-20 11:47:51 +02:00
|
|
|
#include "esphome/core/component.h"
|
2023-09-05 12:35:20 +02:00
|
|
|
#include "esphome/core/defines.h"
|
2021-09-20 11:47:51 +02:00
|
|
|
#include "esphome/core/hal.h"
|
|
|
|
#include "esphome/components/network/ip_address.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2022-11-22 21:54:55 +01:00
|
|
|
#ifdef USE_ESP32
|
2022-11-20 23:49:30 +01:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esp_eth.h"
|
2022-11-20 23:49:30 +01:00
|
|
|
#include "esp_eth_mac.h"
|
|
|
|
#include "esp_netif.h"
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace ethernet {
|
|
|
|
|
|
|
|
enum EthernetType {
|
2023-04-12 23:22:08 +02:00
|
|
|
ETHERNET_TYPE_UNKNOWN = 0,
|
|
|
|
ETHERNET_TYPE_LAN8720,
|
2022-11-20 23:49:30 +01:00
|
|
|
ETHERNET_TYPE_RTL8201,
|
|
|
|
ETHERNET_TYPE_DP83848,
|
2022-08-31 03:45:30 +02:00
|
|
|
ETHERNET_TYPE_IP101,
|
2023-02-07 00:54:59 +01:00
|
|
|
ETHERNET_TYPE_JL1101,
|
2023-04-12 23:22:08 +02:00
|
|
|
ETHERNET_TYPE_KSZ8081,
|
2023-07-10 00:02:42 +02:00
|
|
|
ETHERNET_TYPE_KSZ8081RNA,
|
2024-03-01 01:46:08 +01:00
|
|
|
ETHERNET_TYPE_W5500,
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ManualIP {
|
2021-09-20 11:47:51 +02:00
|
|
|
network::IPAddress static_ip;
|
|
|
|
network::IPAddress gateway;
|
|
|
|
network::IPAddress subnet;
|
|
|
|
network::IPAddress dns1; ///< The first DNS server. 0.0.0.0 for default.
|
|
|
|
network::IPAddress dns2; ///< The second DNS server. 0.0.0.0 for default.
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
2021-07-15 05:45:41 +02:00
|
|
|
enum class EthernetComponentState {
|
|
|
|
STOPPED,
|
|
|
|
CONNECTING,
|
|
|
|
CONNECTED,
|
|
|
|
};
|
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
class EthernetComponent : public Component {
|
|
|
|
public:
|
|
|
|
EthernetComponent();
|
|
|
|
void setup() override;
|
|
|
|
void loop() override;
|
|
|
|
void dump_config() override;
|
|
|
|
float get_setup_priority() const override;
|
|
|
|
bool can_proceed() override;
|
2023-04-20 00:10:01 +02:00
|
|
|
void on_shutdown() override { powerdown(); }
|
2019-04-17 12:06:00 +02:00
|
|
|
bool is_connected();
|
|
|
|
|
2024-03-01 01:46:08 +01:00
|
|
|
#ifdef USE_ETHERNET_SPI
|
|
|
|
void set_clk_pin(uint8_t clk_pin);
|
|
|
|
void set_miso_pin(uint8_t miso_pin);
|
|
|
|
void set_mosi_pin(uint8_t mosi_pin);
|
|
|
|
void set_cs_pin(uint8_t cs_pin);
|
|
|
|
void set_interrupt_pin(uint8_t interrupt_pin);
|
|
|
|
void set_reset_pin(uint8_t reset_pin);
|
|
|
|
void set_clock_speed(int clock_speed);
|
|
|
|
#else
|
2019-04-17 12:06:00 +02:00
|
|
|
void set_phy_addr(uint8_t phy_addr);
|
2022-11-22 21:43:43 +01:00
|
|
|
void set_power_pin(int power_pin);
|
2019-04-17 12:06:00 +02:00
|
|
|
void set_mdc_pin(uint8_t mdc_pin);
|
|
|
|
void set_mdio_pin(uint8_t mdio_pin);
|
2023-03-08 22:03:11 +01:00
|
|
|
void set_clk_mode(emac_rmii_clock_mode_t clk_mode, emac_rmii_clock_gpio_t clk_gpio);
|
2024-03-01 01:46:08 +01:00
|
|
|
#endif
|
|
|
|
void set_type(EthernetType type);
|
2021-09-13 18:11:27 +02:00
|
|
|
void set_manual_ip(const ManualIP &manual_ip);
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2024-02-27 09:16:20 +01:00
|
|
|
network::IPAddresses get_ip_addresses();
|
2019-04-17 12:06:00 +02:00
|
|
|
std::string get_use_address() const;
|
|
|
|
void set_use_address(const std::string &use_address);
|
2023-04-20 00:10:01 +02:00
|
|
|
bool powerdown();
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
protected:
|
2022-11-20 23:49:30 +01:00
|
|
|
static void eth_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
|
|
|
static void got_ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
2023-07-31 00:20:55 +02:00
|
|
|
#if LWIP_IPV6
|
|
|
|
static void got_ip6_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
|
|
|
#endif /* LWIP_IPV6 */
|
2022-11-20 23:49:30 +01:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
void start_connect_();
|
|
|
|
void dump_connect_params_();
|
2023-07-10 00:02:42 +02:00
|
|
|
/// @brief Set `RMII Reference Clock Select` bit for KSZ8081.
|
|
|
|
void ksz8081_set_clock_reference_(esp_eth_mac_t *mac);
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
std::string use_address_;
|
2024-03-01 01:46:08 +01:00
|
|
|
#ifdef USE_ETHERNET_SPI
|
|
|
|
uint8_t clk_pin_;
|
|
|
|
uint8_t miso_pin_;
|
|
|
|
uint8_t mosi_pin_;
|
|
|
|
uint8_t cs_pin_;
|
|
|
|
uint8_t interrupt_pin_;
|
|
|
|
int reset_pin_{-1};
|
|
|
|
int phy_addr_spi_{-1};
|
|
|
|
int clock_speed_;
|
|
|
|
#else
|
2019-04-17 12:06:00 +02:00
|
|
|
uint8_t phy_addr_{0};
|
2022-11-22 21:43:43 +01:00
|
|
|
int power_pin_{-1};
|
2019-04-17 12:06:00 +02:00
|
|
|
uint8_t mdc_pin_{23};
|
|
|
|
uint8_t mdio_pin_{18};
|
2023-03-08 22:03:11 +01:00
|
|
|
emac_rmii_clock_mode_t clk_mode_{EMAC_CLK_EXT_IN};
|
|
|
|
emac_rmii_clock_gpio_t clk_gpio_{EMAC_CLK_IN_GPIO};
|
2024-03-01 01:46:08 +01:00
|
|
|
#endif
|
|
|
|
EthernetType type_{ETHERNET_TYPE_UNKNOWN};
|
2019-04-17 12:06:00 +02:00
|
|
|
optional<ManualIP> manual_ip_{};
|
|
|
|
|
2021-07-15 05:45:41 +02:00
|
|
|
bool started_{false};
|
2019-04-17 12:06:00 +02:00
|
|
|
bool connected_{false};
|
2024-02-27 09:16:20 +01:00
|
|
|
bool got_ipv4_address_{false};
|
2023-07-31 00:20:55 +02:00
|
|
|
#if LWIP_IPV6
|
|
|
|
uint8_t ipv6_count_{0};
|
|
|
|
#endif /* LWIP_IPV6 */
|
2021-07-15 05:45:41 +02:00
|
|
|
EthernetComponentState state_{EthernetComponentState::STOPPED};
|
2019-04-17 12:06:00 +02:00
|
|
|
uint32_t connect_begin_;
|
2022-11-20 23:49:30 +01:00
|
|
|
esp_netif_t *eth_netif_{nullptr};
|
|
|
|
esp_eth_handle_t eth_handle_;
|
2023-04-20 00:10:01 +02:00
|
|
|
esp_eth_phy_t *phy_{nullptr};
|
2019-04-17 12:06:00 +02:00
|
|
|
};
|
|
|
|
|
2021-09-24 18:02:28 +02:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
2019-04-17 12:06:00 +02:00
|
|
|
extern EthernetComponent *global_eth_component;
|
2023-02-07 00:54:59 +01:00
|
|
|
extern "C" esp_eth_phy_t *esp_eth_phy_new_jl1101(const eth_phy_config_t *config);
|
2019-04-17 12:06:00 +02:00
|
|
|
|
|
|
|
} // namespace ethernet
|
|
|
|
} // namespace esphome
|
|
|
|
|
2022-11-22 21:54:55 +01:00
|
|
|
#endif // USE_ESP32
|