supla-device
Loading...
Searching...
No Matches
esp32eth.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15*/
16
17/*
18 - for LAN8720 + ESP32 - using as less gpio as possible
19 - 50MHz oscillator disable on LAN8720 by bridging the oscillator pins 1 and 2
20 -ESP32 Gpio- -LAN8720 PIN -
21 GPIO22 - EMAC_TXD1 : TX1
22 GPIO19 - EMAC_TXD0 : TX0
23 GPIO21 - EMAC_TX_EN : TX_EN
24 GPIO26 - EMAC_RXD1 : RX1
25 GPIO25 - EMAC_RXD0 : RX0
26 GPIO27 - EMAC_RX_DV : CRS
27 GPIO17 - EMAC_TX_CLK : nINT/REFCLK (50MHz)
28 GPIO23 - SMI_MDC : MDC
29 GPIO18 - SMI_MDIO : MDIO
30 GND : GND
31 3V3 : VCC
32*/
33
34#ifndef SRC_SUPLA_NETWORK_ESP32ETH_H_
35#define SRC_SUPLA_NETWORK_ESP32ETH_H_
36
37#include <Arduino.h>
38#include <ETH.h>
39#include <supla/network/arduino_netif_config.h>
40#include <supla/network/netif_lan.h>
41#include <supla/supla_lib_config.h>
42#include <supla/log_wrapper.h>
43
44namespace Supla {
45class ESPETH;
46} // namespace Supla
47
48static Supla::ESPETH *thisEth = nullptr;
49
50namespace Supla {
51class ESPETH : public Supla::LAN {
52 public:
58 explicit ESPETH(uint8_t ethMode) {
59 thisEth = this;
60 if (ethMode == 0) {
61 ethAddress = 0;
62 } else {
63 ethAddress = 1;
64 }
65 }
66
79 ESPETH(eth_phy_type_t ethType,
80 int32_t phyAddr,
81 int mdc,
82 int mdio,
83 int power,
84 eth_clock_mode_t clkMode)
85 : ethType(ethType),
86 ethAddress(phyAddr),
87 mdcPin(mdc),
88 mdioPin(mdio),
89 powerPin(power),
90 clkMode(clkMode) {
91 thisEth = this;
92 }
93
94 ~ESPETH() {
95 if (thisEth == this) {
96 thisEth = nullptr;
97 }
98 }
99
100 static void networkEventHandler(arduino_event_id_t event) {
101 switch (event) {
102 case ARDUINO_EVENT_ETH_GOT_IP: {
103 if (thisEth) {
104 thisEth->setIpv4Addr(ETH.localIP());
105 }
106 Serial.print(F("[Ethernet] local IP: "));
107 Serial.println(ETH.localIP());
108 Serial.print(F("subnetMask: "));
109 Serial.println(ETH.subnetMask());
110 Serial.print(F("gatewayIP: "));
111 Serial.println(ETH.gatewayIP());
112 Serial.print(F("ETH MAC: "));
113 Serial.println(ETH.macAddress());
114 if (ETH.fullDuplex()) {
115 Serial.print(F("FULL_DUPLEX , "));
116 }
117 Serial.print(ETH.linkSpeed());
118 Serial.println(F("Mbps"));
119 break;
120 }
121 case ARDUINO_EVENT_ETH_DISCONNECTED: {
122 if (thisEth) {
123 SUPLA_LOG_INFO("[%s] Disconnected", thisEth->getIntfName());
124 thisEth->setIpv4Addr(0);
125 }
126 break;
127 }
128 }
129 }
130
131
132 void setup() override {
133 allowDisable = true;
134 if (initDone) {
135 return;
136 }
137
138 ::Network.onEvent(Supla::ESPETH::networkEventHandler);
139
140 SUPLA_LOG_INFO(
141 "[%s] setting up ETH (type %d, address %d, mdcPin %d, mdioPin %d, "
142 "powerPin %d, clkMode %d)",
143 thisEth->getIntfName(),
144 ethType,
145 ethAddress,
146 mdcPin,
147 mdioPin,
148 powerPin,
149 clkMode);
150 ETH.begin(ethType, ethAddress, mdcPin, mdioPin, powerPin, clkMode);
151 if (hasStaticIpConfig()) {
152 const auto &cfg = getNetifConfig();
153 IPAddress localIp = toArduinoIpAddress(cfg.ip);
154 IPAddress gateway = toArduinoIpAddress(cfg.gateway);
155 IPAddress subnet = toArduinoIpAddress(cfg.netmask);
156 IPAddress dns1 = toArduinoIpAddress(cfg.dns1);
157 IPAddress dns2 = toArduinoIpAddress(cfg.dns2);
158 if (!ETH.config(localIp, gateway, subnet, dns1, dns2)) {
159 SUPLA_LOG_WARNING("ETH static IP config failed, continuing");
160 }
161 }
162
163 initDone = true;
164
165 char newHostname[32] = {};
166 generateHostname(hostname, macSizeForHostname, newHostname);
167 strncpy(hostname, newHostname, sizeof(hostname) - 1);
168 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
169 ETH.setHostname(hostname);
170 }
171
172 void disable() override {
173 if (!allowDisable) {
174 return;
175 }
176
177 allowDisable = false;
178 SUPLA_LOG_DEBUG("[%s] disabling ETH connection", getIntfName());
179 DisconnectProtocols();
180 }
181
182 bool getMacAddr(uint8_t *mac) override {
183 if (initDone) {
184 ETH.macAddress(mac);
185 return true;
186 }
187 return false;
188 }
189
190 const char *getIntfName() const override {
191 return "ETH";
192 }
193
194 void setHostname(const char *prefix, int macSize) override {
195 macSizeForHostname = macSize;
196 strncpy(hostname, prefix, sizeof(hostname) - 1);
197 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
198 }
199
200 protected:
201 bool allowDisable = false;
202 int macSizeForHostname = 0;
203 bool initDone = false;
204 eth_phy_type_t ethType = ETH_PHY_LAN8720;
205 int32_t ethAddress = -1;
206 int mdcPin = 23; // GPIO for I2C clock
207 int mdioPin = 18; // GPIO for I2C IO
208 int powerPin = -1; // -1 to disable
209 eth_clock_mode_t clkMode = ETH_CLOCK_GPIO17_OUT;
210};
211
212} // namespace Supla
213
214#endif // SRC_SUPLA_NETWORK_ESP32ETH_H_
Definition esp32eth.h:51
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:79
ESPETH(uint8_t ethMode)
Constructor for LAN8720 or TLK110 (legacy).
Definition esp32eth.h:58
Definition netif_lan.h:25