mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-12-04 20:23:06 +05:00
feat: add LAN8720 support and extend ESP32 Ethernet profiles
This commit is contained in:
1 parent
cea8231015
commit
54caf4fd7f
16 files changed
+441
-123
No files matched your search
+117
-37
@@ -2,6 +2,9 @@
|
||||
|
||||
#include <app.h>
|
||||
|
||||
#if USE_ETHERNET
|
||||
#include <ETH.h>
|
||||
#endif
|
||||
#include <FileData.h>
|
||||
#include <LittleFS.h>
|
||||
#include <cmath>
|
||||
@@ -292,6 +295,37 @@ static bool isSettingInteger(float value, float minValue, float maxValue) {
|
||||
return std::isfinite(value) && value >= minValue && value <= maxValue && std::trunc(value) == value;
|
||||
}
|
||||
|
||||
static const GpioValidator isValidOutputGpio = [](uint8_t gpio) {
|
||||
return GPIO_IS_VALID_OUTPUT(gpio);
|
||||
};
|
||||
|
||||
static const GpioValidator isValidInputGpio = [](uint8_t gpio) {
|
||||
return GPIO_IS_VALID_INPUT(gpio);
|
||||
};
|
||||
|
||||
static void parseJsonGpio(JsonVariantConst gpio, uint8_t& target, SettingResult& result,
|
||||
const GpioValidator& isValid = nullptr) {
|
||||
|
||||
if (gpio.is<JsonString>() && gpio.as<JsonString>().size() == 0) {
|
||||
result.accepted = true;
|
||||
if (target != GPIO_IS_NOT_CONFIGURED) {
|
||||
target = GPIO_IS_NOT_CONFIGURED;
|
||||
result.changed = true;
|
||||
}
|
||||
|
||||
} else if (gpio.is<uint8_t>()) {
|
||||
const auto value = gpio.as<uint8_t>();
|
||||
|
||||
if (value == GPIO_IS_NOT_CONFIGURED || (GPIO_IS_VALID(value) && (!isValid || isValid(value)))) {
|
||||
result.accepted = true;
|
||||
if (value != target) {
|
||||
target = value;
|
||||
result.changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Network settings
|
||||
|
||||
static bool isHostnameValid(const char* value) {
|
||||
@@ -353,13 +387,27 @@ JsonVariant toJson(const NetworkSettings& src, JsonVariant dst) {
|
||||
wifi[S_STA][S_CHANNEL] = src.wifi.sta.channel;
|
||||
|
||||
#if USE_ETHERNET
|
||||
auto gpio = dst[S_ETHERNET][S_GPIO].to<JsonObject>();
|
||||
gpio[S_CLK] = src.ethernet.w5500.clk;
|
||||
gpio[S_MOSI] = src.ethernet.w5500.mosi;
|
||||
gpio[S_MISO] = src.ethernet.w5500.miso;
|
||||
gpio[S_CS] = src.ethernet.w5500.cs;
|
||||
gpio[S_IRQ] = src.ethernet.w5500.irq;
|
||||
gpio[S_RST] = src.ethernet.w5500.rst;
|
||||
auto ethernet = dst[S_ETHERNET].to<JsonObject>();
|
||||
ethernet[S_TYPE] = static_cast<uint8_t>(src.ethernet.type);
|
||||
|
||||
#if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500
|
||||
auto w5500 = ethernet[S_W5500].to<JsonObject>();
|
||||
w5500[S_CLK] = src.ethernet.w5500.clk;
|
||||
w5500[S_MOSI] = src.ethernet.w5500.mosi;
|
||||
w5500[S_MISO] = src.ethernet.w5500.miso;
|
||||
w5500[S_CS] = src.ethernet.w5500.cs;
|
||||
w5500[S_IRQ] = src.ethernet.w5500.irq;
|
||||
w5500[S_RST] = src.ethernet.w5500.rst;
|
||||
#endif
|
||||
|
||||
#if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32
|
||||
auto lan8720 = ethernet[S_LAN8720].to<JsonObject>();
|
||||
lan8720[S_MDC] = src.ethernet.lan8720.mdc;
|
||||
lan8720[S_MDIO] = src.ethernet.lan8720.mdio;
|
||||
lan8720[S_POWER] = src.ethernet.lan8720.power;
|
||||
lan8720[S_PHY_ADDR] = src.ethernet.lan8720.phyAddr;
|
||||
lan8720[S_CLOCK_MODE] = src.ethernet.lan8720.clockMode;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
dst[S_TYPE] = static_cast<uint8_t>(src.type);
|
||||
@@ -545,47 +593,79 @@ SettingResult fromJson(JsonVariantConst src, NetworkSettings& dst) {
|
||||
}
|
||||
|
||||
#if USE_ETHERNET
|
||||
// Ethernet GPIOs use the same strict numeric contract as the other GPIO settings.
|
||||
const auto ethernet = src[S_ETHERNET];
|
||||
const auto parseGpio = [&result](JsonVariantConst gpio, uint8_t& target, bool output) {
|
||||
uint8_t value;
|
||||
if (gpio.is<JsonString>() && gpio.as<JsonString>().size() == 0) {
|
||||
value = GPIO_IS_NOT_CONFIGURED;
|
||||
if (ethernet[S_TYPE].is<uint8_t>()) {
|
||||
const auto value = static_cast<EthernetType>(ethernet[S_TYPE].as<uint8_t>());
|
||||
|
||||
} else if (gpio.is<uint8_t>()) {
|
||||
value = gpio.as<uint8_t>();
|
||||
bool valid = value == EthernetType::NONE;
|
||||
#if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500
|
||||
valid |= value == EthernetType::W5500;
|
||||
#endif
|
||||
|
||||
} else {
|
||||
return;
|
||||
#if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32
|
||||
valid |= value == EthernetType::LAN8720;
|
||||
#endif
|
||||
|
||||
if (valid) {
|
||||
result.accepted = true;
|
||||
if (value != dst.ethernet.type) {
|
||||
dst.ethernet.type = value;
|
||||
result.changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500
|
||||
const auto w5500 = ethernet[S_W5500];
|
||||
parseJsonGpio(w5500[S_CLK], dst.ethernet.w5500.clk, result, isValidOutputGpio);
|
||||
parseJsonGpio(w5500[S_MOSI], dst.ethernet.w5500.mosi, result, isValidOutputGpio);
|
||||
parseJsonGpio(w5500[S_MISO], dst.ethernet.w5500.miso, result);
|
||||
parseJsonGpio(w5500[S_CS], dst.ethernet.w5500.cs, result, isValidOutputGpio);
|
||||
parseJsonGpio(w5500[S_IRQ], dst.ethernet.w5500.irq, result);
|
||||
parseJsonGpio(w5500[S_RST], dst.ethernet.w5500.rst, result, isValidOutputGpio);
|
||||
#endif
|
||||
|
||||
#if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32
|
||||
const auto lan8720 = ethernet[S_LAN8720];
|
||||
parseJsonGpio(lan8720[S_MDC], dst.ethernet.lan8720.mdc, result, isValidOutputGpio);
|
||||
parseJsonGpio(lan8720[S_MDIO], dst.ethernet.lan8720.mdio, result, isValidOutputGpio);
|
||||
parseJsonGpio(lan8720[S_POWER], dst.ethernet.lan8720.power, result, isValidOutputGpio);
|
||||
|
||||
if (lan8720[S_PHY_ADDR].is<uint8_t>()) {
|
||||
const auto value = lan8720[S_PHY_ADDR].as<uint8_t>();
|
||||
|
||||
if (value <= 31) {
|
||||
result.accepted = true;
|
||||
if (value != dst.ethernet.lan8720.phyAddr) {
|
||||
dst.ethernet.lan8720.phyAddr = value;
|
||||
result.changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (value != GPIO_IS_NOT_CONFIGURED && !(output ? GPIO_IS_VALID_OUTPUT(value) : GPIO_IS_VALID_INPUT(value))) {
|
||||
return;
|
||||
}
|
||||
if (lan8720[S_CLOCK_MODE].is<uint8_t>()) {
|
||||
const auto value = lan8720[S_CLOCK_MODE].as<uint8_t>();
|
||||
|
||||
result.accepted = true;
|
||||
if (value != target) {
|
||||
target = value;
|
||||
result.changed = true;
|
||||
if (value <= 3) {
|
||||
result.accepted = true;
|
||||
if (value != dst.ethernet.lan8720.clockMode) {
|
||||
dst.ethernet.lan8720.clockMode = value;
|
||||
result.changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const auto gpio = ethernet[S_GPIO];
|
||||
parseGpio(gpio[S_CLK], dst.ethernet.w5500.clk, true);
|
||||
parseGpio(gpio[S_MOSI], dst.ethernet.w5500.mosi, true);
|
||||
parseGpio(gpio[S_MISO], dst.ethernet.w5500.miso, false);
|
||||
parseGpio(gpio[S_CS], dst.ethernet.w5500.cs, true);
|
||||
parseGpio(gpio[S_IRQ], dst.ethernet.w5500.irq, false);
|
||||
parseGpio(gpio[S_RST], dst.ethernet.w5500.rst, true);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (src[S_TYPE].is<uint8_t>()) {
|
||||
const auto value = static_cast<NetworkMode>(src[S_TYPE].as<uint8_t>());
|
||||
if (value == NetworkMode::STA
|
||||
#if USE_ETHERNET
|
||||
|| value == NetworkMode::ETH
|
||||
#endif
|
||||
) {
|
||||
|
||||
bool valid = value == NetworkMode::STA;
|
||||
#if USE_ETHERNET
|
||||
valid |= value == NetworkMode::ETH;
|
||||
#endif
|
||||
|
||||
if (valid) {
|
||||
result.accepted = true;
|
||||
if (value != dst.type) {
|
||||
dst.type = value;
|
||||
|
||||
+87
-34
@@ -26,6 +26,7 @@ static std::atomic<NetworkState> staState{NetworkState::NETWORK_DISABLED};
|
||||
static std::atomic<NetworkState> apState{NetworkState::NETWORK_DISABLED};
|
||||
#if USE_ETHERNET
|
||||
static std::atomic<bool> ethStarted{false};
|
||||
static eth_phy_type_t ethType = ETH_PHY_MAX;
|
||||
static std::atomic<NetworkState> ethState{NetworkState::NETWORK_DISABLED};
|
||||
#endif
|
||||
|
||||
@@ -183,21 +184,48 @@ String getNetworkMac() {
|
||||
return getNetworkInterface().macAddress();
|
||||
}
|
||||
|
||||
bool isEthernetConfigured(const NetworkSettings& config) {
|
||||
bool isEthernetConfigValid(const NetworkSettings& src) {
|
||||
#if USE_ETHERNET
|
||||
const auto& gpios = config.ethernet.w5500;
|
||||
#if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500
|
||||
if (src.ethernet.type == EthernetType::W5500) {
|
||||
const auto& config = src.ethernet.w5500;
|
||||
|
||||
return config.ethernet.type == EthernetType::W5500 &&
|
||||
GPIO_IS_VALID_OUTPUT(gpios.clk) &&
|
||||
GPIO_IS_VALID_OUTPUT(gpios.mosi) &&
|
||||
GPIO_IS_VALID_INPUT(gpios.miso) &&
|
||||
GPIO_IS_VALID_OUTPUT(gpios.cs) &&
|
||||
(gpios.irq == GPIO_IS_NOT_CONFIGURED || GPIO_IS_VALID_INPUT(gpios.irq)) &&
|
||||
(gpios.rst == GPIO_IS_NOT_CONFIGURED || GPIO_IS_VALID_OUTPUT(gpios.rst));
|
||||
return GPIO_IS_VALID_OUTPUT(config.clk) &&
|
||||
GPIO_IS_VALID_OUTPUT(config.mosi) &&
|
||||
GPIO_IS_VALID_INPUT(config.miso) &&
|
||||
GPIO_IS_VALID_OUTPUT(config.cs) &&
|
||||
(config.irq == GPIO_IS_NOT_CONFIGURED || GPIO_IS_VALID_INPUT(config.irq)) &&
|
||||
(config.rst == GPIO_IS_NOT_CONFIGURED || GPIO_IS_VALID_OUTPUT(config.rst));
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32
|
||||
if (src.ethernet.type == EthernetType::LAN8720) {
|
||||
const auto& config = src.ethernet.lan8720;
|
||||
static const uint8_t clockGpios[] = {0, 0, 16, 17}; // eth_clock_mode_t order
|
||||
|
||||
if (config.phyAddr > 31 || config.clockMode >= sizeof(clockGpios)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto clockGpio = clockGpios[config.clockMode];
|
||||
const auto isControlGpio = [clockGpio](uint8_t gpio) {
|
||||
// The ESP32 reserves the RMII data lines and the selected reference clock.
|
||||
return GPIO_IS_VALID_OUTPUT(gpio) && gpio != clockGpio &&
|
||||
gpio != ETH_RMII_TX_EN && gpio != ETH_RMII_TX0 && gpio != ETH_RMII_TX1 &&
|
||||
gpio != ETH_RMII_CRS_DV && gpio != ETH_RMII_RX0 && gpio != ETH_RMII_RX1_EN;
|
||||
};
|
||||
|
||||
return isControlGpio(config.mdc) && isControlGpio(config.mdio) &&
|
||||
(config.power == GPIO_IS_NOT_CONFIGURED || isControlGpio(config.power)) &&
|
||||
config.mdc != config.mdio && config.power != config.mdc && config.power != config.mdio;
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
(void)config;
|
||||
return false;
|
||||
(void)src;
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isNetworkInterfaceStarted(NetworkMode mode) {
|
||||
@@ -557,13 +585,18 @@ static bool stopEthernet() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// end() removes the device, but leaves the SPI bus with its previous GPIOs.
|
||||
const esp_err_t result = spi_bus_free(SPI2_HOST);
|
||||
if (result != ESP_OK && result != ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGE(L_NETWORK, "Failed release Ethernet SPI bus: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
#if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500
|
||||
// SPI bus free
|
||||
if (ethType == ETH_PHY_W5500) {
|
||||
const esp_err_t result = spi_bus_free(SPI2_HOST);
|
||||
if (result != ESP_OK && result != ESP_ERR_INVALID_STATE) {
|
||||
ESP_LOGE(L_NETWORK, "Failed release Ethernet SPI bus: %s", esp_err_to_name(result));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
ethType = ETH_PHY_MAX;
|
||||
postInterfaceStopped(ARDUINO_EVENT_ETH_STOP);
|
||||
return true;
|
||||
}
|
||||
@@ -583,27 +616,47 @@ static bool configureEthernet() {
|
||||
}
|
||||
|
||||
static bool startEthernet() {
|
||||
if ((ETH.handle() != nullptr || ETH.netif() != nullptr) && !stopEthernet()) {
|
||||
return false;
|
||||
// A previous begin() can leave driver resources allocated.
|
||||
if (ethType != ETH_PHY_MAX || ETH.handle() != nullptr || ETH.netif() != nullptr) {
|
||||
// Release them before starting the next driver.
|
||||
if (!stopEthernet()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const auto& gpios = networkSettings.ethernet.w5500;
|
||||
ESP_LOGI(L_NETWORK, "Initialize W5500 Ethernet");
|
||||
const bool initialized = ETH.begin(
|
||||
ETH_PHY_W5500,
|
||||
1,
|
||||
gpios.cs,
|
||||
gpios.irq == GPIO_IS_NOT_CONFIGURED ? -1 : gpios.irq, // optionally
|
||||
gpios.rst == GPIO_IS_NOT_CONFIGURED ? -1 : gpios.rst, // optionally
|
||||
SPI2_HOST,
|
||||
gpios.clk,
|
||||
gpios.miso,
|
||||
gpios.mosi,
|
||||
12
|
||||
);
|
||||
bool initialized = false;
|
||||
const auto type = networkSettings.ethernet.type;
|
||||
#if CONFIG_ETH_USE_SPI_ETHERNET && CONFIG_ETH_SPI_ETHERNET_W5500
|
||||
if (type == EthernetType::W5500) {
|
||||
const auto& config = networkSettings.ethernet.w5500;
|
||||
ESP_LOGI(L_NETWORK, "Initializing W5500 driver...");
|
||||
|
||||
ethType = ETH_PHY_W5500;
|
||||
initialized = ETH.begin(
|
||||
ETH_PHY_W5500, 1, config.cs,
|
||||
config.irq == GPIO_IS_NOT_CONFIGURED ? -1 : config.irq,
|
||||
config.rst == GPIO_IS_NOT_CONFIGURED ? -1 : config.rst,
|
||||
SPI2_HOST, config.clk, config.miso, config.mosi, 12
|
||||
);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if CONFIG_ETH_USE_ESP32_EMAC && CONFIG_IDF_TARGET_ESP32
|
||||
if (type == EthernetType::LAN8720) {
|
||||
const auto& config = networkSettings.ethernet.lan8720;
|
||||
ESP_LOGI(L_NETWORK, "Initializing LAN8720 driver...");
|
||||
|
||||
ethType = ETH_PHY_LAN8720;
|
||||
initialized = ETH.begin(
|
||||
ETH_PHY_LAN8720, config.phyAddr, config.mdc, config.mdio,
|
||||
config.power == GPIO_IS_NOT_CONFIGURED ? -1 : config.power,
|
||||
static_cast<eth_clock_mode_t>(config.clockMode)
|
||||
);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!initialized) {
|
||||
ESP_LOGE(L_NETWORK, "W5500 initialization failed, check GPIOs and wiring");
|
||||
ESP_LOGE(L_NETWORK, "Ethernet initialization failed!");
|
||||
stopEthernet();
|
||||
return false;
|
||||
}
|
||||
@@ -1120,7 +1173,7 @@ static Task networkTask("Network", [](void*) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (networkSettings.type == NetworkMode::ETH && !isEthernetConfigured()) {
|
||||
if (networkSettings.type == NetworkMode::ETH && !isEthernetConfigValid()) {
|
||||
#if USE_ETHERNET
|
||||
if (ethStarted.load()) {
|
||||
stopEthernet();
|
||||
|
||||
+2
-10
@@ -400,11 +400,7 @@ static void restApi() {
|
||||
webServer->on("/api/network/settings", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
// Prepare response
|
||||
auto *response = new AsyncJsonResponse();
|
||||
auto& doc = response->getRoot();
|
||||
toJson(networkSettings, doc);
|
||||
#if USE_ETHERNET
|
||||
doc[S_ETHERNET][S_CONFIGURED] = isEthernetConfigured();
|
||||
#endif
|
||||
toJson(networkSettings, response->getRoot());
|
||||
|
||||
// Send response
|
||||
response->setLength();
|
||||
@@ -437,13 +433,9 @@ static void restApi() {
|
||||
} else {
|
||||
// Prepare response
|
||||
auto *response = new AsyncJsonResponse();
|
||||
auto& responseDoc = response->getRoot();
|
||||
toJson(networkSettings, responseDoc);
|
||||
#if USE_ETHERNET
|
||||
responseDoc[S_ETHERNET][S_CONFIGURED] = isEthernetConfigured();
|
||||
#endif
|
||||
|
||||
// Send response
|
||||
toJson(networkSettings, response->getRoot());
|
||||
response->setLength();
|
||||
response->setCode(result.changed ? 201 : 200);
|
||||
request->send(response);
|
||||
|
||||
Reference in new issue
Block a user