ETH optimizations

This commit is contained in:
proddy
2021-01-23 14:25:43 +01:00
parent f466453a8e
commit c9e831a527
11 changed files with 87 additions and 57 deletions

View File

@@ -21,7 +21,7 @@ void APSettingsService::reconfigureAP() {
void APSettingsService::loop() { void APSettingsService::loop() {
// if we have an ETH connection, quit // if we have an ETH connection, quit
if (ETH.linkUp()) { if (emsesp::System::ethernet_connected()) {
return; return;
} }
unsigned long currentMillis = uuid::get_uptime(); unsigned long currentMillis = uuid::get_uptime();

View File

@@ -8,10 +8,10 @@
#include <DNSServer.h> #include <DNSServer.h>
#include <IPAddress.h> #include <IPAddress.h>
#include <ETH.h>
#include <uuid/common.h> #include <uuid/common.h>
#include "../../src/system.h" // proddy added
#define MANAGE_NETWORK_DELAY 10000 #define MANAGE_NETWORK_DELAY 10000
#define AP_MODE_ALWAYS 0 #define AP_MODE_ALWAYS 0
@@ -59,8 +59,7 @@ class APSettings {
IPAddress subnetMask; IPAddress subnetMask;
bool operator==(const APSettings & settings) const { bool operator==(const APSettings & settings) const {
return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password && localIP == settings.localIP return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password && localIP == settings.localIP && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask;
&& gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask;
} }
static void read(APSettings & settings, JsonObject & root) { static void read(APSettings & settings, JsonObject & root) {

View File

@@ -34,8 +34,6 @@ void NTPSettingsService::WiFiEvent(WiFiEvent_t event) {
configureNTP(); configureNTP();
break; break;
default: default:
break; break;
} }

View File

@@ -6,7 +6,6 @@
#include <time.h> #include <time.h>
#include <lwip/apps/sntp.h> #include <lwip/apps/sntp.h>
#include <ETH.h>
#ifndef FACTORY_NTP_ENABLED #ifndef FACTORY_NTP_ENABLED
#define FACTORY_NTP_ENABLED true #define FACTORY_NTP_ENABLED true

View File

@@ -8,7 +8,7 @@ void NetworkStatus::networkStatus(AsyncWebServerRequest * request) {
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_NETWORK_STATUS_SIZE); AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_NETWORK_STATUS_SIZE);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
bool ethernet_connected = ETH.linkUp(); bool ethernet_connected = emsesp::System::ethernet_connected();
wl_status_t wifi_status = WiFi.status(); wl_status_t wifi_status = WiFi.status();
// see if Ethernet is connected // see if Ethernet is connected
@@ -36,7 +36,7 @@ void NetworkStatus::networkStatus(AsyncWebServerRequest * request) {
if (dnsIP2 != INADDR_NONE) { if (dnsIP2 != INADDR_NONE) {
root["dns_ip_2"] = dnsIP2.toString(); root["dns_ip_2"] = dnsIP2.toString();
} }
} else if (ETH.linkUp()) { } else if (ethernet_connected) {
// Ethernet // Ethernet
root["local_ip"] = ETH.localIP().toString(); root["local_ip"] = ETH.localIP().toString();
root["mac_address"] = ETH.macAddress(); root["mac_address"] = ETH.macAddress();

View File

@@ -2,9 +2,10 @@
#define NetworkStatus_h #define NetworkStatus_h
#include <WiFi.h> #include <WiFi.h>
#include <ETH.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include "../../src/system.h" // proddy added
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>

View File

@@ -10,6 +10,15 @@
#define WIFI_AP WIFI_MODE_AP #define WIFI_AP WIFI_MODE_AP
#define WIFI_AP_STA WIFI_MODE_APSTA #define WIFI_AP_STA WIFI_MODE_APSTA
typedef enum {
ETH_CLOCK_GPIO0_IN = 0, /*!< RMII clock input to GPIO0 */
ETH_CLOCK_GPIO0_OUT = 1, /*!< RMII clock output from GPIO0 */
ETH_CLOCK_GPIO16_OUT = 2, /*!< RMII clock output from GPIO16 */
ETH_CLOCK_GPIO17_OUT = 3 /*!< RMII clock output from GPIO17 */
} eth_clock_mode_t;
typedef enum { ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_MAX } eth_phy_type_t;
typedef enum { typedef enum {
SYSTEM_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */ SYSTEM_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */
SYSTEM_EVENT_SCAN_DONE, /**< ESP32 finish scanning AP */ SYSTEM_EVENT_SCAN_DONE, /**< ESP32 finish scanning AP */
@@ -159,7 +168,7 @@ class WiFiClass {
class ETHClass { class ETHClass {
public: public:
bool begin() { bool begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type, eth_clock_mode_t clock_mode) {
return false; return false;
}; };

View File

@@ -73,23 +73,23 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
case SYSTEM_EVENT_ETH_GOT_IP: case SYSTEM_EVENT_ETH_GOT_IP:
// prevent double calls // prevent double calls
if (!connected_) { if (!System::ethernet_connected()) {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
EMSESP::logger().info(F("Ethernet Connected with IP=%s, speed %d Mbps"), ETH.localIP().toString().c_str(), ETH.linkSpeed()); EMSESP::logger().info(F("Ethernet Connected with IP=%s, speed %d Mbps"), ETH.localIP().toString().c_str(), ETH.linkSpeed());
#endif #endif
EMSESP::system_.init_network(); // send out heartbeat MQTT as soon as we have a connection EMSESP::system_.init_network(); // send out heartbeat MQTT as soon as we have a connection
connected_ = true; System::ethernet_connected(true);
} }
break; break;
case SYSTEM_EVENT_ETH_DISCONNECTED: case SYSTEM_EVENT_ETH_DISCONNECTED:
EMSESP::logger().info(F("Ethernet Disconnected")); EMSESP::logger().info(F("Ethernet Disconnected"));
connected_ = false; System::ethernet_connected(false);
break; break;
case SYSTEM_EVENT_ETH_STOP: case SYSTEM_EVENT_ETH_STOP:
EMSESP::logger().info(F("Ethernet Stopped")); EMSESP::logger().info(F("Ethernet Stopped"));
connected_ = false; System::ethernet_connected(false);
break; break;
default: default:

View File

@@ -34,7 +34,6 @@ class WebStatusService {
WebStatusService(AsyncWebServer * server, SecurityManager * securityManager); WebStatusService(AsyncWebServer * server, SecurityManager * securityManager);
private: private:
bool connected_ = false;
void webStatusService(AsyncWebServerRequest * request); void webStatusService(AsyncWebServerRequest * request);
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info); void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info);
}; };

View File

@@ -42,6 +42,7 @@ uint16_t System::analog_ = 0;
bool System::analog_enabled_ = false; bool System::analog_enabled_ = false;
bool System::syslog_enabled_ = false; bool System::syslog_enabled_ = false;
std::string System::hostname_; std::string System::hostname_;
bool System::ethernet_connected_ = false;
// send on/off to a gpio pin // send on/off to a gpio pin
// value: true = HIGH, false = LOW // value: true = HIGH, false = LOW
@@ -181,8 +182,11 @@ void System::start(uint32_t heap_start) {
show_mem("Startup"); show_mem("Startup");
#endif #endif
// print boot message uint8_t ethernet_profile;
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); }); EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) {
LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); // print boot message
ethernet_profile = networkSettings.ethernet_profile;
});
// these commands respond to the topic "system" and take a payload like {cmd:"", data:"", id:""} // these commands respond to the topic "system" and take a payload like {cmd:"", data:"", id:""}
EMSESP::webSettingsService.read([&](WebSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
@@ -198,43 +202,57 @@ void System::start(uint32_t heap_start) {
#endif #endif
}); });
// start other services first
init();
// check ethernet profile, if we're using exclusive Ethernet then disabled wifi and AP/captive portal // check ethernet profile, if we're using exclusive Ethernet then disabled wifi and AP/captive portal
uint8_t ethernet_profile; if (ethernet_profile == 0) {
EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) { ethernet_profile = settings.ethernet_profile; }); return;
}
uint8_t phy_addr; // I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110)
int power; // Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source)
int mdc; // Pin# of the I²C clock signal for the Ethernet PHY
int mdio; // Pin# of the I²C IO signal for the Ethernet PHY
eth_phy_type_t type; // Type of the Ethernet PHY (LAN8720 or TLK110)
eth_clock_mode_t clock_mode; // ETH_CLOCK_GPIO0_IN or ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT for 50Hz inverted clock
if (ethernet_profile == 1) {
// LAN8720
phy_addr = 0;
power = -1;
mdc = 23;
mdio = 18;
type = ETH_PHY_LAN8720;
clock_mode = ETH_CLOCK_GPIO0_IN;
} else if (ethernet_profile == 2) {
// TLK110
phy_addr = 31;
power = -1;
mdc = 23;
mdio = 18;
type = ETH_PHY_TLK110;
clock_mode = ETH_CLOCK_GPIO0_IN;
}
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
uint8_t phy_addr = 0; // I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110) if (ETH.begin(phy_addr, power, mdc, mdio, type, clock_mode)) {
int power = -1; // Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source) // disable ssid and AP
int mdc = 23; // Pin# of the I²C clock signal for the Ethernet PHY EMSESP::esp8266React.getNetworkSettingsService()->update(
int mdio = 18; // Pin# of the I²C IO signal for the Ethernet PHY [&](NetworkSettings & settings) {
eth_phy_type_t type = ETH_PHY_LAN8720; // Type of the Ethernet PHY (LAN8720 or TLK110) settings.ssid == ""; // remove SSID
eth_clock_mode_t clock_mode = ETH_CLOCK_GPIO0_IN; // ETH_CLOCK_GPIO0_IN or ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT for 50Hz inverted clock return StateUpdateResult::CHANGED;
},
"local");
// see if we can start it using default settings EMSESP::esp8266React.getAPSettingsService()->update(
if (!ETH.begin(phy_addr, power, mdc, mdio, type, clock_mode)) { [&](APSettings & settings) {
// it failed. Now try again based on profile. 0 is the same as 1. 2 is for TLK110 settings.provisionMode = AP_MODE_NEVER;
if (ethernet_profile == 2) { return StateUpdateResult::CHANGED;
if (ETH.begin(31, power, mdc, mdio, ETH_PHY_TLK110, clock_mode)) { },
EMSESP::esp8266React.getNetworkSettingsService()->update( "local");
[&](NetworkSettings & settings) {
settings.ssid == ""; // remove SSID
return StateUpdateResult::CHANGED;
},
"local");
EMSESP::esp8266React.getAPSettingsService()->update(
[&](APSettings & settings) {
settings.provisionMode = AP_MODE_NEVER;
return StateUpdateResult::CHANGED;
},
"local");
}
}
} }
#endif #endif
// continue with init'ing services
init();
} }
void System::other_init() { void System::other_init() {
@@ -576,7 +594,7 @@ void System::show_system(uuid::console::Shell & shell) {
shell.println(); shell.println();
// show Ethernet // show Ethernet
if (ETH.linkUp()) { if (ethernet_connected()) {
shell.printfln(F("Ethernet: Connected")); shell.printfln(F("Ethernet: Connected"));
shell.printfln(F("MAC address: %s"), ETH.macAddress().c_str()); shell.printfln(F("MAC address: %s"), ETH.macAddress().c_str());
shell.printfln(F("Hostname: %s"), ETH.getHostname()); shell.printfln(F("Hostname: %s"), ETH.getHostname());

View File

@@ -22,18 +22,16 @@
#include <Arduino.h> #include <Arduino.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#if defined(ESP32)
#include "driver/adc.h"
#include <esp_wifi.h>
#include <esp_bt.h>
#endif
#include "helpers.h" #include "helpers.h"
#include "console.h" #include "console.h"
#include "mqtt.h" #include "mqtt.h"
#include "telegram.h" #include "telegram.h"
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
#include "driver/adc.h"
#include <esp_wifi.h>
#include <esp_bt.h>
#include <ETH.h>
#include <uuid/syslog.h> #include <uuid/syslog.h>
#endif #endif
@@ -85,6 +83,14 @@ class System {
hostname_ = hostname; hostname_ = hostname;
} }
static bool ethernet_connected() {
return ethernet_connected_;
}
static void ethernet_connected(bool b) {
ethernet_connected_ = b;
}
private: private:
static uuid::log::Logger logger_; static uuid::log::Logger logger_;
@@ -119,6 +125,7 @@ class System {
static bool upload_status_; // true if we're in the middle of a OTA firmware upload static bool upload_status_; // true if we're in the middle of a OTA firmware upload
static uint32_t heap_start_; static uint32_t heap_start_;
static uint16_t analog_; static uint16_t analog_;
static bool ethernet_connected_;
// settings // settings
static std::string hostname_; static std::string hostname_;