supla-device
Loading...
Searching...
No Matches
esp32eth.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4/*
5 - for LAN8720 + ESP32 - using as less gpio as possible
6 - 50MHz oscillator disable on LAN8720 by bridging the oscillator pins 1 and 2
7 -ESP32 Gpio- -LAN8720 PIN -
8 GPIO22 - EMAC_TXD1 : TX1
9 GPIO19 - EMAC_TXD0 : TX0
10 GPIO21 - EMAC_TX_EN : TX_EN
11 GPIO26 - EMAC_RXD1 : RX1
12 GPIO25 - EMAC_RXD0 : RX0
13 GPIO27 - EMAC_RX_DV : CRS
14 GPIO17 - EMAC_TX_CLK : nINT/REFCLK (50MHz)
15 GPIO23 - SMI_MDC : MDC
16 GPIO18 - SMI_MDIO : MDIO
17 GND : GND
18 3V3 : VCC
19*/
20
21#ifndef SRC_SUPLA_NETWORK_ESP32ETH_H_
22#define SRC_SUPLA_NETWORK_ESP32ETH_H_
23
24#include <Arduino.h>
25#include <ETH.h>
26#include <supla/network/arduino_netif_config.h>
27#include <supla/network/netif_lan.h>
28#include <supla/supla_lib_config.h>
29#include <supla/log_wrapper.h>
30
31namespace Supla {
32class ESPETH;
33} // namespace Supla
34
35static Supla::ESPETH *thisEth = nullptr;
36
37namespace Supla {
38class ESPETH : public Supla::LAN {
39 public:
45 explicit ESPETH(uint8_t ethMode) {
46 thisEth = this;
47 if (ethMode == 0) {
48 ethAddress = 0;
49 } else {
50 ethAddress = 1;
51 }
52 }
53
66 ESPETH(eth_phy_type_t ethType,
67 int32_t phyAddr,
68 int mdc,
69 int mdio,
70 int power,
71 eth_clock_mode_t clkMode)
72 : ethType(ethType),
73 ethAddress(phyAddr),
74 mdcPin(mdc),
75 mdioPin(mdio),
76 powerPin(power),
77 clkMode(clkMode) {
78 thisEth = this;
79 }
80
81 ~ESPETH() {
82 if (thisEth == this) {
83 thisEth = nullptr;
84 }
85 }
86
87 static void networkEventHandler(arduino_event_id_t event) {
88 switch (event) {
89 case ARDUINO_EVENT_ETH_GOT_IP: {
90 if (thisEth) {
91 thisEth->setIpv4Addr(ETH.localIP());
92 }
93 IPAddress localIP = ETH.localIP();
94 IPAddress subnetMaskIP = ETH.subnetMask();
95 IPAddress gatewayIP = ETH.gatewayIP();
96 uint8_t mac[6] = {};
97 ETH.macAddress(mac);
98 SUPLA_LOG_INFO("localIP: %d.%d.%d.%d",
99 localIP[0],
100 localIP[1],
101 localIP[2],
102 localIP[3]);
103 SUPLA_LOG_INFO("subnetMaskIP: %d.%d.%d.%d",
104 subnetMaskIP[0],
105 subnetMaskIP[1],
106 subnetMaskIP[2],
107 subnetMaskIP[3]);
108 SUPLA_LOG_INFO("gatewayIP: %d.%d.%d.%d",
109 gatewayIP[0],
110 gatewayIP[1],
111 gatewayIP[2],
112 gatewayIP[3]);
113 SUPLA_LOG_INFO("ETH MAC: %02X:%02X:%02X:%02X:%02X:%02X",
114 mac[0],
115 mac[1],
116 mac[2],
117 mac[3],
118 mac[4],
119 mac[5]);
120 SUPLA_LOG_INFO("speed: %d Mbps", ETH.linkSpeed());
121 if (ETH.fullDuplex()) {
122 SUPLA_LOG_INFO("FULL_DUPLEX");
123 }
124 break;
125 }
126 case ARDUINO_EVENT_ETH_DISCONNECTED: {
127 if (thisEth) {
128 SUPLA_LOG_INFO("[%s] Disconnected", thisEth->getIntfName());
129 thisEth->setIpv4Addr(0);
130 }
131 break;
132 }
133 }
134 }
135
136
137 void setup() override {
138 allowDisable = true;
139 if (initDone) {
140 return;
141 }
142
143 ::Network.onEvent(Supla::ESPETH::networkEventHandler);
144
145 SUPLA_LOG_INFO(
146 "[%s] setting up ETH (type %d, address %d, mdcPin %d, mdioPin %d, "
147 "powerPin %d, clkMode %d)",
148 thisEth->getIntfName(),
149 ethType,
150 ethAddress,
151 mdcPin,
152 mdioPin,
153 powerPin,
154 clkMode);
155 ETH.begin(ethType, ethAddress, mdcPin, mdioPin, powerPin, clkMode);
156 if (hasStaticIpConfig()) {
157 const auto &cfg = getNetifConfig();
158 IPAddress localIp = toArduinoIpAddress(cfg.ip);
159 IPAddress gateway = toArduinoIpAddress(cfg.gateway);
160 IPAddress subnet = toArduinoIpAddress(cfg.netmask);
161 IPAddress dns1 = toArduinoIpAddress(cfg.dns1);
162 IPAddress dns2 = toArduinoIpAddress(cfg.dns2);
163 if (!ETH.config(localIp, gateway, subnet, dns1, dns2)) {
164 SUPLA_LOG_WARNING("ETH static IP config failed, continuing");
165 }
166 }
167
168 initDone = true;
169
170 char newHostname[32] = {};
171 generateHostname(hostname, macSizeForHostname, newHostname);
172 strncpy(hostname, newHostname, sizeof(hostname) - 1);
173 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
174 ETH.setHostname(hostname);
175 }
176
177 void disable() override {
178 if (!allowDisable) {
179 return;
180 }
181
182 allowDisable = false;
183 SUPLA_LOG_DEBUG("[%s] disabling ETH connection", getIntfName());
184 DisconnectProtocols();
185 }
186
187 bool getMacAddr(uint8_t *mac) override {
188 if (initDone) {
189 ETH.macAddress(mac);
190 return true;
191 }
192 return false;
193 }
194
195 const char *getIntfName() const override {
196 return "ETH";
197 }
198
199 void setHostname(const char *prefix, int macSize) override {
200 macSizeForHostname = macSize;
201 strncpy(hostname, prefix, sizeof(hostname) - 1);
202 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
203 }
204
205 protected:
206 bool allowDisable = false;
207 int macSizeForHostname = 0;
208 bool initDone = false;
209 eth_phy_type_t ethType = ETH_PHY_LAN8720;
210 int32_t ethAddress = -1;
211 int mdcPin = 23; // GPIO for I2C clock
212 int mdioPin = 18; // GPIO for I2C IO
213 int powerPin = -1; // -1 to disable
214 eth_clock_mode_t clkMode = ETH_CLOCK_GPIO17_OUT;
215};
216
217} // namespace Supla
218
219#endif // SRC_SUPLA_NETWORK_ESP32ETH_H_
Definition ip_address.h:15
Definition esp32eth.h:38
ESPETH(eth_phy_type_t ethType, int32_t phyAddr, int mdc, int mdio, int power, eth_clock_mode_t clkMode)
Constructor for ETH_PHY configuration.
Definition esp32eth.h:66
ESPETH(uint8_t ethMode)
Constructor for LAN8720 or TLK110 (legacy)
Definition esp32eth.h:45
Definition netif_lan.h:10