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 -
19 using as less gpio as possible
20 - 50MHz oscillator disable on LAN8720 by bridging the oscillator pins 1 and 2
21 -ESP32 Gpio- -LAN8720 PIN -
22 GPIO22 - EMAC_TXD1 : TX1
23 GPIO19 - EMAC_TXD0 : TX0
24 GPIO21 - EMAC_TX_EN : TX_EN
25 GPIO26 - EMAC_RXD1 : RX1
26 GPIO25 - EMAC_RXD0 : RX0
27 GPIO27 - EMAC_RX_DV : CRS
28 GPIO17 - EMAC_TX_CLK : nINT/REFCLK (50MHz)
29 GPIO23 - SMI_MDC : MDC
30 GPIO18 - SMI_MDIO : MDIO
31 GND : GND
32 3V3 : VCC
33*/
34
35#ifndef SRC_SUPLA_NETWORK_ESP32ETH_H_
36#define SRC_SUPLA_NETWORK_ESP32ETH_H_
37
38#include <Arduino.h>
39#include <ETH.h>
40#include <supla/network/netif_lan.h>
41#include <supla/supla_lib_config.h>
42#include <supla/log_wrapper.h>
43
44#ifdef ETH_CLK_MODE
45#undef ETH_CLK_MODE
46#endif
47#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
48// #define ETH_CLK_MODE ETH_CLOCK_GPIO0_OUT // unstable!!
49
50// Pin# of the enable signal for the external crystal oscillator (-1 to disable
51// for internal APLL source)
52#define ETH_POWER_PIN -1
53
54// Type of the Ethernet PHY (LAN8720 or TLK110)
55#define ETH_TYPE ETH_PHY_LAN8720
56
57// I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110)
58#define ETH_ADDR 1
59
60// Pin# of the I²C clock signal for the Ethernet PHY
61#define ETH_MDC_PIN 23
62
63// Pin# of the I²C IO signal for the Ethernet PHY
64#define ETH_MDIO_PIN 18
65
66namespace Supla {
67class ESPETH;
68} // namespace Supla
69
70static Supla::ESPETH *thisEth = nullptr;
71
72namespace Supla {
73class ESPETH : public Supla::LAN {
74 public:
75 explicit ESPETH(uint8_t ethmode) {
76 thisEth = this;
77 if (ethmode == 0) {
78 ETH_ADDRESS = 0;
79 } else {
80 ETH_ADDRESS = 1;
81 }
82 }
83
84 ~ESPETH() {
85 if (thisEth == this) {
86 thisEth = nullptr;
87 }
88 }
89
90 static void networkEventHandler(arduino_event_id_t event) {
91 switch (event) {
92 case ARDUINO_EVENT_ETH_GOT_IP: {
93 Serial.print(F("[Ethernet] local IP: "));
94 Serial.println(ETH.localIP());
95 Serial.print(F("subnetMask: "));
96 Serial.println(ETH.subnetMask());
97 Serial.print(F("gatewayIP: "));
98 Serial.println(ETH.gatewayIP());
99 Serial.print(F("ETH MAC: "));
100 Serial.println(ETH.macAddress());
101 if (ETH.fullDuplex()) {
102 Serial.print(F("FULL_DUPLEX , "));
103 }
104 Serial.print(ETH.linkSpeed());
105 Serial.println(F("Mbps"));
106 if (thisEth) {
107 thisEth->setIpv4Addr(ETH.localIP());
108 }
109 break;
110 }
111 case ARDUINO_EVENT_ETH_DISCONNECTED: {
112 Serial.println(F("[Ethernet] Disconnected"));
113 if (thisEth) {
114 thisEth->setIpv4Addr(0);
115 }
116 break;
117 }
118 }
119 }
120
121
122 void setup() override {
123 allowDisable = true;
124 if (initDone) {
125 return;
126 }
127
128 ::Network.onEvent(Supla::ESPETH::networkEventHandler);
129
130 Serial.println(F("[Ethernet] establishing LAN connection"));
131 ETH.begin(ETH_TYPE,
132 ETH_ADDRESS,
133 ETH_MDC_PIN,
134 ETH_MDIO_PIN,
135 ETH_POWER_PIN,
136 ETH_CLK_MODE);
137 initDone = true;
138
139 char newHostname[32] = {};
140 generateHostname(hostname, macSizeForHostname, newHostname);
141 strncpy(hostname, newHostname, sizeof(hostname) - 1);
142 SUPLA_LOG_DEBUG("[%s] Network AP/hostname: %s", getIntfName(), hostname);
143 ETH.setHostname(hostname);
144 }
145
146 void disable() override {
147 if (!allowDisable) {
148 return;
149 }
150
151 allowDisable = false;
152 SUPLA_LOG_DEBUG("[%s] disabling ETH connection", getIntfName());
153 DisconnectProtocols();
154// ETH.end();
155 }
156
157 bool getMacAddr(uint8_t *mac) override {
158 if (initDone) {
159 ETH.macAddress(mac);
160 }
161 return true;
162 }
163
164 void setHostname(const char *prefix, int macSize) override {
165 macSizeForHostname = macSize;
166 strncpy(hostname, prefix, sizeof(hostname) - 1);
167 SUPLA_LOG_DEBUG("[%s] Network AP/hostname: %s", getIntfName(), hostname);
168 }
169
170 protected:
171 uint8_t ETH_ADDRESS = {};
172 bool allowDisable = false;
173 int macSizeForHostname = 0;
174 bool initDone = false;
175};
176}; // namespace Supla
177
178
179#endif // SRC_SUPLA_NETWORK_ESP32ETH_H_
Definition esp32eth.h:73
Definition netif_lan.h:25
Definition network.h:36