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/netif_lan.h>
40#include <supla/supla_lib_config.h>
41#include <supla/log_wrapper.h>
42
43namespace Supla {
44class ESPETH;
45} // namespace Supla
46
47static Supla::ESPETH *thisEth = nullptr;
48
49namespace Supla {
50class ESPETH : public Supla::LAN {
51 public:
57 explicit ESPETH(uint8_t ethMode) {
58 thisEth = this;
59 if (ethMode == 0) {
60 ethAddress = 0;
61 } else {
62 ethAddress = 1;
63 }
64 }
65
78 ESPETH(eth_phy_type_t ethType,
79 int32_t phyAddr,
80 int mdc,
81 int mdio,
82 int power,
83 eth_clock_mode_t clkMode)
84 : ethType(ethType),
85 ethAddress(phyAddr),
86 mdcPin(mdc),
87 mdioPin(mdio),
88 powerPin(power),
89 clkMode(clkMode) {
90 thisEth = this;
91 }
92
93 ~ESPETH() {
94 if (thisEth == this) {
95 thisEth = nullptr;
96 }
97 }
98
99 static void networkEventHandler(arduino_event_id_t event) {
100 switch (event) {
101 case ARDUINO_EVENT_ETH_GOT_IP: {
102 if (thisEth) {
103 thisEth->setIpv4Addr(ETH.localIP());
104 }
105 Serial.print(F("[Ethernet] local IP: "));
106 Serial.println(ETH.localIP());
107 Serial.print(F("subnetMask: "));
108 Serial.println(ETH.subnetMask());
109 Serial.print(F("gatewayIP: "));
110 Serial.println(ETH.gatewayIP());
111 Serial.print(F("ETH MAC: "));
112 Serial.println(ETH.macAddress());
113 if (ETH.fullDuplex()) {
114 Serial.print(F("FULL_DUPLEX , "));
115 }
116 Serial.print(ETH.linkSpeed());
117 Serial.println(F("Mbps"));
118 break;
119 }
120 case ARDUINO_EVENT_ETH_DISCONNECTED: {
121 if (thisEth) {
122 SUPLA_LOG_INFO("[%s] Disconnected", thisEth->getIntfName());
123 thisEth->setIpv4Addr(0);
124 }
125 break;
126 }
127 }
128 }
129
130
131 void setup() override {
132 allowDisable = true;
133 if (initDone) {
134 return;
135 }
136
137 ::Network.onEvent(Supla::ESPETH::networkEventHandler);
138
139 SUPLA_LOG_INFO(
140 "[%s] setting up ETH (type %d, address %d, mdcPin %d, mdioPin %d, "
141 "powerPin %d, clkMode %d)",
142 thisEth->getIntfName(),
143 ethType,
144 ethAddress,
145 mdcPin,
146 mdioPin,
147 powerPin,
148 clkMode);
149 ETH.begin(ethType, ethAddress, mdcPin, mdioPin, powerPin, clkMode);
150
151 initDone = true;
152
153 char newHostname[32] = {};
154 generateHostname(hostname, macSizeForHostname, newHostname);
155 strncpy(hostname, newHostname, sizeof(hostname) - 1);
156 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
157 ETH.setHostname(hostname);
158 }
159
160 void disable() override {
161 if (!allowDisable) {
162 return;
163 }
164
165 allowDisable = false;
166 SUPLA_LOG_DEBUG("[%s] disabling ETH connection", getIntfName());
167 DisconnectProtocols();
168 }
169
170 bool getMacAddr(uint8_t *mac) override {
171 if (initDone) {
172 ETH.macAddress(mac);
173 return true;
174 }
175 return false;
176 }
177
178 const char *getIntfName() const override {
179 return "ETH";
180 }
181
182 void setHostname(const char *prefix, int macSize) override {
183 macSizeForHostname = macSize;
184 strncpy(hostname, prefix, sizeof(hostname) - 1);
185 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
186 }
187
188 protected:
189 bool allowDisable = false;
190 int macSizeForHostname = 0;
191 bool initDone = false;
192 eth_phy_type_t ethType = ETH_PHY_LAN8720;
193 int32_t ethAddress = -1;
194 int mdcPin = 23; // GPIO for I2C clock
195 int mdioPin = 18; // GPIO for I2C IO
196 int powerPin = -1; // -1 to disable
197 eth_clock_mode_t clkMode = ETH_CLOCK_GPIO17_OUT;
198};
199
200} // namespace Supla
201
202#endif // SRC_SUPLA_NETWORK_ESP32ETH_H_
Definition esp32eth.h:50
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:78
ESPETH(uint8_t ethMode)
Constructor for LAN8720 or TLK110 (legacy)
Definition esp32eth.h:57
Definition netif_lan.h:25