supla-device
Loading...
Searching...
No Matches
esp32ethspi.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#ifndef SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
18#define SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
19
20#include <Arduino.h>
21#include <ETH.h>
22#include <supla/network/arduino_netif_config.h>
23#include <supla/network/netif_lan.h>
24#include <supla/supla_lib_config.h>
25#include <supla/log_wrapper.h>
26
27namespace Supla {
28class ESPETHSPI;
29} // namespace Supla
30
31static Supla::ESPETHSPI *thisSpiEth = nullptr;
32
33namespace Supla {
34class ESPETHSPI : public Supla::LAN {
35 public:
36 explicit ESPETHSPI(eth_phy_type_t type, int32_t phy_addr,
37 int cs, int irq, int rst, spi_host_device_t spi_host,
38 int sck = -1, int miso = -1, int mosi = -1) {
39 thisSpiEth = this;
40 ethspi_type = type;
41 ethspi_phy_addr = phy_addr;
42 cs_pin = cs;
43 irq_pin = irq;
44 rst_pin = rst;
45 ethspi_spi_host = spi_host;
46 sck_pin = sck;
47 miso_pin = miso;
48 mosi_pin = mosi;
49 }
50
51 ~ESPETHSPI() {
52 if (thisSpiEth == this) {
53 thisSpiEth = nullptr;
54 }
55 }
56
57 static void networkEventHandler(arduino_event_id_t event) {
58 switch (event) {
59 case ARDUINO_EVENT_ETH_GOT_IP: {
60 Serial.print(F("[Ethernet] local IP: "));
61 Serial.println(ETH.localIP());
62 Serial.print(F("subnetMask: "));
63 Serial.println(ETH.subnetMask());
64 Serial.print(F("gatewayIP: "));
65 Serial.println(ETH.gatewayIP());
66 Serial.print(F("ETH MAC: "));
67 Serial.println(ETH.macAddress());
68 if (ETH.fullDuplex()) {
69 Serial.print(F("FULL_DUPLEX , "));
70 }
71 Serial.print(ETH.linkSpeed());
72 Serial.println(F("Mbps"));
73 if (thisSpiEth) {
74 thisSpiEth->setIpv4Addr(ETH.localIP());
75 }
76 break;
77 }
78 case ARDUINO_EVENT_ETH_DISCONNECTED: {
79 Serial.println(F("[Ethernet] Disconnected"));
80 if (thisSpiEth) {
81 thisSpiEth->setIpv4Addr(0);
82 }
83 break;
84 }
85 }
86 }
87
88 void setup() override {
89 allowDisable = true;
90 if (initDone) {
91 return;
92 }
93
94 ::Network.onEvent(Supla::ESPETHSPI::networkEventHandler);
95
96 Serial.println(F("[Ethernet] establishing LAN connection"));
97 ETH.begin(ethspi_type,
98 ethspi_phy_addr,
99 cs_pin,
100 irq_pin,
101 rst_pin,
102 ethspi_spi_host,
103 sck_pin,
104 miso_pin,
105 mosi_pin);
106 if (hasStaticIpConfig()) {
107 const auto &cfg = getNetifConfig();
108 IPAddress localIp = toArduinoIpAddress(cfg.ip);
109 IPAddress gateway = toArduinoIpAddress(cfg.gateway);
110 IPAddress subnet = toArduinoIpAddress(cfg.netmask);
111 IPAddress dns1 = toArduinoIpAddress(cfg.dns1);
112 IPAddress dns2 = toArduinoIpAddress(cfg.dns2);
113 if (!ETH.config(localIp, gateway, subnet, dns1, dns2)) {
114 SUPLA_LOG_WARNING("ETH SPI static IP config failed, continuing");
115 }
116 }
117 initDone = true;
118
119 char newHostname[32] = {};
120 generateHostname(hostname, macSizeForHostname, newHostname);
121 strncpy(hostname, newHostname, sizeof(hostname) - 1);
122 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
123 ETH.setHostname(hostname);
124 }
125
126 void disable() override {
127 if (!allowDisable) {
128 return;
129 }
130
131 allowDisable = false;
132 SUPLA_LOG_DEBUG("[%s] disabling ETH connection", getIntfName());
133 DisconnectProtocols();
134 // ETH.end();
135 }
136
137 bool getMacAddr(uint8_t *mac) override {
138 if (initDone) {
139 ETH.macAddress(mac);
140 }
141 return true;
142 }
143
144 const char *getIntfName() const override {
145 return "ETH";
146 }
147
148 void setHostname(const char *prefix, int macSize) override {
149 macSizeForHostname = macSize;
150 strncpy(hostname, prefix, sizeof(hostname) - 1);
151 SUPLA_LOG_DEBUG("[%s] Network LAN/hostname: %s", getIntfName(), hostname);
152 }
153
154 protected:
155 eth_phy_type_t ethspi_type;
156 int32_t ethspi_phy_addr;
157 int cs_pin;
158 int irq_pin;
159 int rst_pin;
160 spi_host_device_t ethspi_spi_host;
161 int sck_pin;
162 int miso_pin;
163 int mosi_pin;
164 bool allowDisable = false;
165 int macSizeForHostname = 0;
166 bool initDone = false;
167};
168}; // namespace Supla
169
170
171#endif // SRC_SUPLA_NETWORK_ESP32ETHSPI_H_
Definition ip_address.h:30
Definition esp32ethspi.h:34
Definition netif_lan.h:25
Definition network.h:36