supla-device
Loading...
Searching...
No Matches
wt32_eth01.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
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19/* - for WT32-ETH01 - */
20
21#ifndef SRC_SUPLA_NETWORK_WT32_ETH01_H_
22#define SRC_SUPLA_NETWORK_WT32_ETH01_H_
23
24#include <Arduino.h>
25#include <ETH.h>
26#include <supla/network/netif_lan.h>
27#include <supla/supla_lib_config.h>
28#include <supla/log_wrapper.h>
29
30#ifdef ETH_CLK_MODE
31#undef ETH_CLK_MODE
32#endif
33#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
34
35// Pin# of the enable signal for the external crystal oscillator (-1 to disable
36// for internal APLL source)
37#define ETH_POWER_PIN 16
38
39// Type of the Ethernet PHY (LAN8720 or TLK110)
40#define ETH_TYPE ETH_PHY_LAN8720
41
42// I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110)
43#define ETH_ADDR 1
44
45// Pin# of the I²C clock signal for the Ethernet PHY
46#define ETH_MDC_PIN 23
47
48// Pin# of the I²C IO signal for the Ethernet PHY
49#define ETH_MDIO_PIN 18
50
51namespace Supla {
52class WT32_ETH01;
53} // namespace Supla
54
55static Supla::WT32_ETH01 *thisWtEth = nullptr;
56
57namespace Supla {
58class WT32_ETH01 : public Supla::LAN {
59 public:
60 explicit WT32_ETH01(uint8_t ethmode) {
61 thisWtEth = this;
62 if (ethmode == 0) {
63 ETH_ADDRESS = 0;
64 } else {
65 ETH_ADDRESS = 1;
66 }
67 }
68
69 ~WT32_ETH01() {
70 if (thisWtEth == this) {
71 thisWtEth = nullptr;
72 }
73 }
74
75 static void networkEventHandler(arduino_event_id_t event) {
76 switch (event) {
77 case ARDUINO_EVENT_ETH_GOT_IP: {
78 Serial.print(F("[Ethernet] local IP: "));
79 Serial.println(ETH.localIP());
80 Serial.print(F("subnetMask: "));
81 Serial.println(ETH.subnetMask());
82 Serial.print(F("gatewayIP: "));
83 Serial.println(ETH.gatewayIP());
84 Serial.print(F("ETH MAC: "));
85 Serial.println(ETH.macAddress());
86 if (ETH.fullDuplex()) {
87 Serial.print(F("FULL_DUPLEX , "));
88 }
89 Serial.print(ETH.linkSpeed());
90 Serial.println(F("Mbps"));
91 if (thisWtEth) {
92 thisWtEth->setIpv4Addr(ETH.localIP());
93 }
94 break;
95 }
96 case ARDUINO_EVENT_ETH_DISCONNECTED: {
97 Serial.println(F("[Ethernet] Disconnected"));
98 if (thisWtEth) {
99 thisWtEth->setIpv4Addr(0);
100 }
101 break;
102 }
103 }
104 }
105
106 void setup() override {
107 allowDisable = true;
108 if (initDone) {
109 return;
110 }
111
112 Serial.println(F("[Ethernet] establishing LAN connection"));
113 ::Network.onEvent(WT32_ETH01::networkEventHandler);
114 ETH.begin(ETH_TYPE,
115 ETH_ADDRESS,
116 ETH_MDC_PIN,
117 ETH_MDIO_PIN,
118 ETH_POWER_PIN,
119 ETH_CLK_MODE);
120 initDone = true;
121
122 char newHostname[32] = {};
123 generateHostname(hostname, macSizeForHostname, newHostname);
124 strncpy(hostname, newHostname, sizeof(hostname) - 1);
125 SUPLA_LOG_DEBUG("[%s] Network AP/hostname: %s", getIntfName(), hostname);
126 ETH.setHostname(hostname);
127 }
128
129 void disable() override {
130 if (!allowDisable) {
131 return;
132 }
133
134 allowDisable = false;
135 SUPLA_LOG_DEBUG("[%s] disabling ETH connection", getIntfName());
136 DisconnectProtocols();
137// ETH.end();
138 }
139
140 bool getMacAddr(uint8_t *mac) override {
141 if (initDone) {
142 ETH.macAddress(mac);
143 }
144 return true;
145 }
146
147 void setHostname(const char *prefix, int macSize) override {
148 macSizeForHostname = macSize;
149 strncpy(hostname, prefix, sizeof(hostname) - 1);
150 SUPLA_LOG_DEBUG("[%s] Network AP/hostname: %s", getIntfName(), hostname);
151 }
152
153 protected:
154 uint8_t ETH_ADDRESS = {};
155 bool allowDisable = false;
156 int macSizeForHostname = 0;
157 bool initDone = false;
158};
159}; // namespace Supla
160
161#endif // SRC_SUPLA_NETWORK_WT32_ETH01_H_
Definition netif_lan.h:25
Definition network.h:36
Definition wt32_eth01.h:58