Skip to content

Commit

Permalink
Merge ethernet_esp32 and esthernet_spi.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
fvanroie committed Jun 11, 2022
1 parent 2af1f48 commit 0f95052
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 220 deletions.
10 changes: 5 additions & 5 deletions include/hasp_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,25 @@ static WiFiSpiClass WiFi;

#if HASP_USE_ETHERNET > 0
#if defined(ARDUINO_ARCH_ESP32)
#include "sys/net/hasp_ethernet_esp32.h"
#if HASP_USE_SPI_ETHERNET > 0
#include <ETHSPI.h>
#include "sys/net/hasp_ethernet_spi.h"
#warning Using ESP32 Ethernet SPI W5500
#define HASP_ETHERNET ETHSPI

#else
#include <ETH.h>

#define ETH_ADDR 0
#define ETH_POWER_PIN -1
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define NRST 5
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_CLKMODE ETH_CLOCK_GPIO17_OUT

#include "sys/net/hasp_ethernet_esp32.h"
#include <ETH.h>
#warning Using ESP32 Ethernet LAN8720
#define HASP_ETHERNET ETH
#endif // HASP_USE_SPI_ETHERNET

#else
#if USE_BUILTIN_ETHERNET > 0
#include <LwIP.h>
Expand Down
2 changes: 1 addition & 1 deletion src/hasp/hasp_attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ static hasp_attribute_type_t special_attribute_src(lv_obj_t* obj, const char* pa
}

} else {
#if defined(ARDUINO) && defined(ARDUINO_ARCH_ESP32)
#if defined(ARDUINO) && defined(ARDUINO_ARCH_ESP32) && 1
HTTPClient http;
http.begin(payload);

Expand Down
4 changes: 1 addition & 3 deletions src/hasp/hasp_dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,7 @@ void dispatch_reboot(bool saveConfig)
#if HASP_USE_CONFIG > 0
debugStop();
#endif
#if HASP_USE_WIFI > 0
wifiStop();
#endif
networkStop();
LOG_VERBOSE(TAG_MSGR, F("-------------------------------------"));
LOG_TRACE(TAG_MSGR, F(D_DISPATCH_REBOOT));

Expand Down
1 change: 1 addition & 0 deletions src/hasp_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ enum {
TAG_MQTT_RCV = 67,
TAG_FTP = 68,
TAG_TIME = 69,
TAG_NETW = 70,

TAG_LVGL = 90,
TAG_LVFS = 91,
Expand Down
61 changes: 41 additions & 20 deletions src/sys/net/hasp_ethernet_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,46 @@
#include "hal/hasp_hal.h"
#include "dev/device.h"

#if HASP_USE_ETHERNET > 0 && defined(ARDUINO_ARCH_ESP32) && HASP_USE_SPI_ETHERNET == 0
#if HASP_USE_ETHERNET > 0 && defined(ARDUINO_ARCH_ESP32) && defined(HASP_ETHERNET)

bool eth_connected = false;
IPAddress ip;

void ethernet_get_ipaddress(char* buffer, size_t len)
{
IPAddress ip = HASP_ETHERNET.localIP();
snprintf_P(buffer, len, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
}

void EthernetEvent(WiFiEvent_t event)
{
switch(event) {
case SYSTEM_EVENT_ETH_START:
case ARDUINO_EVENT_ETH_START:
LOG_TRACE(TAG_ETH, F(D_SERVICE_STARTED));
// set eth hostname here
ETH.setHostname(haspDevice.get_hostname());
HASP_ETHERNET.setHostname(haspDevice.get_hostname());
break;
case SYSTEM_EVENT_ETH_CONNECTED:
case ARDUINO_EVENT_ETH_CONNECTED:
LOG_TRACE(TAG_ETH, F(D_SERVICE_CONNECTED));
eth_connected = true;
break;
case SYSTEM_EVENT_ETH_GOT_IP:
LOG_TRACE(TAG_ETH, F(D_INFO_MAC_ADDRESS " %s"), ETH.macAddress().c_str());
ip = ETH.localIP();
case ARDUINO_EVENT_ETH_GOT_IP:
LOG_TRACE(TAG_ETH, F(D_INFO_MAC_ADDRESS " %s"), HASP_ETHERNET.macAddress().c_str());
ip = HASP_ETHERNET.localIP();
LOG_TRACE(TAG_ETH, F("IPv4: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
if(ETH.fullDuplex()) {
if(HASP_ETHERNET.fullDuplex()) {
LOG_TRACE(TAG_ETH, F(D_INFO_FULL_DUPLEX));
}
LOG_TRACE(TAG_ETH, F(D_INFO_LINK_SPEED " %d Mbps"), ETH.linkSpeed());
LOG_TRACE(TAG_ETH, F(D_INFO_LINK_SPEED " %d Mbps"), HASP_ETHERNET.linkSpeed());
eth_connected = true;
networkStart(); // Start network services
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
case ARDUINO_EVENT_ETH_DISCONNECTED:
LOG_TRACE(TAG_ETH, F(D_SERVICE_DISCONNECTED));
eth_connected = false;
networkStop(); // Stop network services
break;
case SYSTEM_EVENT_ETH_STOP:
case ARDUINO_EVENT_ETH_STOP:
LOG_WARNING(TAG_ETH, F(D_SERVICE_STOPPED));
eth_connected = false;
break;
Expand All @@ -51,8 +58,22 @@ void EthernetEvent(WiFiEvent_t event)

void ethernetSetup()
{
#if HASP_USE_WIFI == 0
// Need to make sure we get the Ethernet Events
WiFi.begin();
WiFi.mode(WIFI_OFF);
#endif

bool started = false;
WiFi.onEvent(EthernetEvent);
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLKMODE);
#if HASP_USE_SPI_ETHERNET > 0
started = HASP_ETHERNET.begin(ETHSPI_MOSI_GPIO, ETHSPI_MISO_GPIO, ETHSPI_SCLK_GPIO, ETHSPI_CS_GPIO, ETHSPI_INT_GPIO,
ETHSPI_HOST);
#else
started = HASP_ETHERNET.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLKMODE);
#endif

if(started) LOG_TRACE(TAG_ETH, F("ETHSPI Started "));
}

IRAM_ATTR void ethernetLoop(void)
Expand All @@ -67,8 +88,8 @@ bool ethernetEvery5Seconds()
void ethernet_get_statusupdate(char* buffer, size_t len)
{
snprintf_P(buffer, len, PSTR("\"eth\":\"%s\",\"link\":\"%d Mbps\",\"ip\":\"%s\",\"mac\":\"%s\","),
eth_connected ? F("on") : F("off"), ETH.linkSpeed(), ETH.localIP().toString().c_str(),
ETH.macAddress().c_str());
eth_connected ? F("on") : F("off"), HASP_ETHERNET.linkSpeed(),
HASP_ETHERNET.localIP().toString().c_str(), HASP_ETHERNET.macAddress().c_str());
}

void ethernet_get_info(JsonDocument& doc)
Expand All @@ -79,17 +100,17 @@ void ethernet_get_info(JsonDocument& doc)

JsonObject info = doc.createNestedObject(F(D_INFO_ETHERNET));

buffer = ETH.linkSpeed();
buffer = HASP_ETHERNET.linkSpeed();
buffer += F(" Mbps");
if(ETH.fullDuplex()) {
if(HASP_ETHERNET.fullDuplex()) {
buffer += F(" " D_INFO_FULL_DUPLEX);
}

info[F(D_INFO_LINK_SPEED)] = buffer;
info[F(D_INFO_IP_ADDRESS)] = ETH.localIP().toString();
info[F(D_INFO_GATEWAY)] = ETH.gatewayIP().toString();
info[F(D_INFO_DNS_SERVER)] = ETH.dnsIP().toString();
info[F(D_INFO_MAC_ADDRESS)] = ETH.macAddress();
info[F(D_INFO_IP_ADDRESS)] = HASP_ETHERNET.localIP().toString();
info[F(D_INFO_GATEWAY)] = HASP_ETHERNET.gatewayIP().toString();
info[F(D_INFO_DNS_SERVER)] = HASP_ETHERNET.dnsIP().toString();
info[F(D_INFO_MAC_ADDRESS)] = HASP_ETHERNET.macAddress();
}

#endif
3 changes: 1 addition & 2 deletions src/sys/net/hasp_ethernet_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

#include "ArduinoJson.h"

static bool eth_connected = false;

void ethernetSetup();
IRAM_ATTR void ethernetLoop(void);

bool ethernetEverySecond();
bool ethernetEvery5Seconds();
void ethernet_get_statusupdate(char* buffer, size_t len);
void ethernet_get_ipaddress(char* buffer, size_t len);

void ethernet_get_info(JsonDocument& doc);

Expand Down
103 changes: 0 additions & 103 deletions src/sys/net/hasp_ethernet_spi.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/sys/net/hasp_ethernet_spi.h

This file was deleted.

2 changes: 0 additions & 2 deletions src/sys/net/hasp_ethernet_stm32.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include "ArduinoJson.h"

static bool eth_connected = false;

void ethernetSetup();
void ethernetLoop(void);

Expand Down
Loading

0 comments on commit 0f95052

Please sign in to comment.