Add core3 ethernet support on SPI for all ESP32 models

This commit is contained in:
Theo Arends 2024-02-14 16:19:51 +01:00
parent f98c66fa81
commit 080cdb4f0d
9 changed files with 58 additions and 27 deletions

View File

@ -54,13 +54,13 @@ void InfluxDbProcess(bool use_copy = false);
#endif #endif
#ifdef ESP32 #ifdef ESP32
#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4 //#if CONFIG_IDF_TARGET_ESP32 // ESP32/PICO-D4
#ifdef USE_ETHERNET #ifdef USE_ETHERNET
IPAddress EthernetLocalIP(void); IPAddress EthernetLocalIP(void);
char* EthernetHostname(void); char* EthernetHostname(void);
String EthernetMacAddress(void); String EthernetMacAddress(void);
#endif // USE_ETHERNET #endif // USE_ETHERNET
#endif // CONFIG_IDF_TARGET_ESP32 //#endif // CONFIG_IDF_TARGET_ESP32
#endif // ESP32 #endif // ESP32
/*********************************************************************************************\ /*********************************************************************************************\
@ -129,9 +129,9 @@ String EthernetMacAddress(void);
#else // Disable features not present in other ESP32 like ESP32C3, ESP32S2, ESP32S3 etc. #else // Disable features not present in other ESP32 like ESP32C3, ESP32S2, ESP32S3 etc.
#ifdef USE_ETHERNET //#ifdef USE_ETHERNET
#undef USE_ETHERNET // All non-ESP32 do not support ethernet //#undef USE_ETHERNET // All non-ESP32 do not support ethernet
#endif //#endif
#endif // CONFIG_IDF_TARGET_ESP32 #endif // CONFIG_IDF_TARGET_ESP32
/*-------------------------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------------------------*\

View File

@ -952,7 +952,8 @@ void CmndStatus(void)
#ifdef USE_TASMESH #ifdef USE_TASMESH
ResponseAppend_P(PSTR(",\"SoftAPMac\":\"%s\""), WiFi.softAPmacAddress().c_str()); ResponseAppend_P(PSTR(",\"SoftAPMac\":\"%s\""), WiFi.softAPmacAddress().c_str());
#endif // USE_TASMESH #endif // USE_TASMESH
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
#ifdef USE_IPV6 #ifdef USE_IPV6
ResponseAppend_P(PSTR(",\"Ethernet\":{\"" D_CMND_HOSTNAME "\":\"%s\",\"" ResponseAppend_P(PSTR(",\"Ethernet\":{\"" D_CMND_HOSTNAME "\":\"%s\",\""
D_CMND_IPADDRESS "\":\"%_I\",\"" D_JSON_GATEWAY "\":\"%_I\",\"" D_JSON_SUBNETMASK "\":\"%_I\",\"" D_CMND_IPADDRESS "\":\"%_I\",\"" D_JSON_GATEWAY "\":\"%_I\",\"" D_JSON_SUBNETMASK "\":\"%_I\",\""

View File

@ -525,7 +525,8 @@ String WifiGetIPv4Str(void)
bool EthernetGetIPv4(IPAddress *ip) bool EthernetGetIPv4(IPAddress *ip)
{ {
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
uint32_t wifi_uint = (uint32_t) EthernetLocalIP(); uint32_t wifi_uint = (uint32_t) EthernetLocalIP();
if (ip != nullptr) { *ip = wifi_uint; } if (ip != nullptr) { *ip = wifi_uint; }
return wifi_uint != 0; return wifi_uint != 0;
@ -661,7 +662,8 @@ void WifiDumpAddressesIPv6(void)
} }
AddLog(LOG_LEVEL_DEBUG, "IP : DNS: %s %s", IPAddress(dns_getserver(0)).toString().c_str(), IPAddress(dns_getserver(1)).toString(true).c_str()); AddLog(LOG_LEVEL_DEBUG, "IP : DNS: %s %s", IPAddress(dns_getserver(0)).toString().c_str(), IPAddress(dns_getserver(1)).toString(true).c_str());
AddLog(LOG_LEVEL_DEBUG, "WIF: v4IP: %_I v6IP: %s mainIP: %s", (uint32_t) WiFi.localIP(), WifiGetIPv6Str().c_str(), WifiGetIPStr().c_str()); AddLog(LOG_LEVEL_DEBUG, "WIF: v4IP: %_I v6IP: %s mainIP: %s", (uint32_t) WiFi.localIP(), WifiGetIPv6Str().c_str(), WifiGetIPStr().c_str());
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
AddLog(LOG_LEVEL_DEBUG, "ETH: v4IP %_I v6IP: %s mainIP: %s", (uint32_t) EthernetLocalIP(), EthernetGetIPv6Str().c_str(), EthernetGetIPStr().c_str()); AddLog(LOG_LEVEL_DEBUG, "ETH: v4IP %_I v6IP: %s mainIP: %s", (uint32_t) EthernetLocalIP(), EthernetGetIPv6Str().c_str(), EthernetGetIPStr().c_str());
#endif #endif
AddLog(LOG_LEVEL_DEBUG, "IP : ListeningIP %s", IPGetListeningAddressStr().c_str()); AddLog(LOG_LEVEL_DEBUG, "IP : ListeningIP %s", IPGetListeningAddressStr().c_str());
@ -681,7 +683,8 @@ bool IPGetListeningAddress(IPAddress * ip)
IPAddress ip_wifi; IPAddress ip_wifi;
bool has_wifi = WifiGetIP(&ip_wifi); bool has_wifi = WifiGetIP(&ip_wifi);
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
IPAddress ip_eth; IPAddress ip_eth;
bool has_eth = EthernetGetIP(&ip_eth); bool has_eth = EthernetGetIP(&ip_eth);
if (has_wifi && has_eth) { if (has_wifi && has_eth) {
@ -700,7 +703,8 @@ bool IPGetListeningAddress(IPAddress * ip)
*ip = IPAddress(); *ip = IPAddress();
return false; return false;
#else // USE_IPV6 #else // USE_IPV6
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
if (EthernetGetIP(ip)) { return true; } if (EthernetGetIP(ip)) { return true; }
#endif #endif
if (WifiGetIP(ip)) { return true; } if (WifiGetIP(ip)) { return true; }
@ -804,7 +808,8 @@ String WifiGetIPStr(void)
// Has a routable IP, whether IPv4 or IPv6, Wifi or Ethernet // Has a routable IP, whether IPv4 or IPv6, Wifi or Ethernet
bool HasIP(void) { bool HasIP(void) {
if (WifiHasIP()) return true; if (WifiHasIP()) return true;
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
if (EthernetHasIP()) return true; if (EthernetHasIP()) return true;
#endif #endif
return false; return false;

View File

@ -718,7 +718,8 @@ bool HttpCheckPriviledgedAccess(bool autorequestauth = true)
if ((referer.indexOf(hostname) == 7) || (referer.indexOf(WiFi.localIP().toString()) == 7)) { if ((referer.indexOf(hostname) == 7) || (referer.indexOf(WiFi.localIP().toString()) == 7)) {
return true; return true;
} }
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
hostname = EthernetHostname(); hostname = EthernetHostname();
hostname.toUpperCase(); hostname.toUpperCase();
// TODO rework if IPv6 // TODO rework if IPv6
@ -934,7 +935,8 @@ void WSContentSendStyle_P(const char* formatP, ...) {
(lip && sip) ? ", " : "", (lip && sip) ? ", " : "",
(sip) ? WiFi.softAPIP().toString().c_str() : ""); (sip) ? WiFi.softAPIP().toString().c_str() : "");
} }
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
eip = EthernetHasIP(); eip = EthernetHasIP();
if (eip) { if (eip) {
WSContentSend_P(PSTR("%s%s%s (%s)"), // tasmota-eth.local (192.168.2.13) WSContentSend_P(PSTR("%s%s%s (%s)"), // tasmota-eth.local (192.168.2.13)
@ -2469,7 +2471,8 @@ void HandleInformation(void) {
WSContentSend_P(PSTR("}1" D_DNS_SERVER "2}2%_I"), Settings->ipv4_address[4]); WSContentSend_P(PSTR("}1" D_DNS_SERVER "2}2%_I"), Settings->ipv4_address[4]);
#endif // USE_IPV6 #endif // USE_IPV6
} }
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
if (EthernetHasIP()) { if (EthernetHasIP()) {
if (show_hr) { if (show_hr) {
WSContentSeparatorIThin(); WSContentSeparatorIThin();

View File

@ -980,7 +980,8 @@ void MqttConnected(void) {
ResponseAppend_P(PSTR(",\"" D_JSON_IP6_LOCAL "\":\"%s\""), WifiGetIPv6LinkLocalStr().c_str()); ResponseAppend_P(PSTR(",\"" D_JSON_IP6_LOCAL "\":\"%s\""), WifiGetIPv6LinkLocalStr().c_str());
#endif // USE_IPV6 #endif // USE_IPV6
} }
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
if (static_cast<uint32_t>(EthernetLocalIP()) != 0) { if (static_cast<uint32_t>(EthernetLocalIP()) != 0) {
ResponseAppend_P(PSTR(",\"Ethernet\":{\"" D_CMND_HOSTNAME "\":\"%s\",\"" D_CMND_IPADDRESS "\":\"%_I\"}"), ResponseAppend_P(PSTR(",\"Ethernet\":{\"" D_CMND_HOSTNAME "\":\"%s\",\"" D_CMND_IPADDRESS "\":\"%_I\"}"),
EthernetHostname(), (uint32_t)EthernetLocalIP()); EthernetHostname(), (uint32_t)EthernetLocalIP());

View File

@ -1055,7 +1055,8 @@ void RulesEvery50ms(void)
TasmotaGlobal.rules_flag.wifi_disconnected = 0; TasmotaGlobal.rules_flag.wifi_disconnected = 0;
strncpy_P(json_event, PSTR("{\"WIFI\":{\"Disconnected\":1}}"), sizeof(json_event)); strncpy_P(json_event, PSTR("{\"WIFI\":{\"Disconnected\":1}}"), sizeof(json_event));
} }
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
else if (TasmotaGlobal.rules_flag.eth_connected) { else if (TasmotaGlobal.rules_flag.eth_connected) {
TasmotaGlobal.rules_flag.eth_connected = 0; TasmotaGlobal.rules_flag.eth_connected = 0;
strncpy_P(json_event, PSTR("{\"ETH\":{\"Connected\":1}}"), sizeof(json_event)); strncpy_P(json_event, PSTR("{\"ETH\":{\"Connected\":1}}"), sizeof(json_event));

View File

@ -35,7 +35,8 @@
void TasDiscoverMessage(void) { void TasDiscoverMessage(void) {
uint32_t ip_address = (uint32_t)WiFi.localIP(); uint32_t ip_address = (uint32_t)WiFi.localIP();
char* hostname = TasmotaGlobal.hostname; char* hostname = TasmotaGlobal.hostname;
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
if (static_cast<uint32_t>(EthernetLocalIP()) != 0) { if (static_cast<uint32_t>(EthernetLocalIP()) != 0) {
ip_address = (uint32_t)EthernetLocalIP(); ip_address = (uint32_t)EthernetLocalIP();
hostname = EthernetHostname(); hostname = EthernetHostname();

View File

@ -229,7 +229,8 @@ int hass_tele_period = 0;
void HassDiscoverMessage(void) { void HassDiscoverMessage(void) {
uint32_t ip_address = (uint32_t)WiFi.localIP(); uint32_t ip_address = (uint32_t)WiFi.localIP();
char* hostname = TasmotaGlobal.hostname; char* hostname = TasmotaGlobal.hostname;
#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET) //#if defined(ESP32) && CONFIG_IDF_TARGET_ESP32 && defined(USE_ETHERNET)
#if defined(ESP32) && defined(USE_ETHERNET)
if (static_cast<uint32_t>(EthernetLocalIP()) != 0) { if (static_cast<uint32_t>(EthernetLocalIP()) != 0) {
ip_address = (uint32_t)EthernetLocalIP(); ip_address = (uint32_t)EthernetLocalIP();
hostname = EthernetHostname(); hostname = EthernetHostname();

View File

@ -18,12 +18,12 @@
*/ */
#ifdef ESP32 #ifdef ESP32
#if CONFIG_IDF_TARGET_ESP32 //#if CONFIG_IDF_TARGET_ESP32
#ifdef USE_ETHERNET #ifdef USE_ETHERNET
/*********************************************************************************************\ /*********************************************************************************************\
* Ethernet support for ESP32 * Ethernet support for ESP32
* *
* Dedicated fixed Phy pins * Dedicated fixed Phy pins (EMAC)
* GPIO17 - EMAC_CLK_OUT_180 * GPIO17 - EMAC_CLK_OUT_180
* GPIO19 - EMAC_TXD0(RMII) * GPIO19 - EMAC_TXD0(RMII)
* GPIO21 - EMAC_TX_EN(RMII) * GPIO21 - EMAC_TX_EN(RMII)
@ -55,6 +55,13 @@
* #define ETH_CLKMODE ETH_CLOCK_GPIO0_IN * #define ETH_CLKMODE ETH_CLOCK_GPIO0_IN
* #define ETH_ADDRESS 1 * #define ETH_ADDRESS 1
* *
* Used SPI
* SPI_MOSI
* SPI_MISO
* SPI_CLK
* SPI_RST = Tasmota ETH_POWER
* SPI_IRQ = Tasmota ETH_MDIO
* SPI_CS = Tasmota ETH_MDC
\*********************************************************************************************/ \*********************************************************************************************/
#define XDRV_82 82 #define XDRV_82 82
@ -187,18 +194,26 @@ void EthernetSetIp(void) {
void EthernetInit(void) { void EthernetInit(void) {
if (!Settings->flag4.network_ethernet) { return; } if (!Settings->flag4.network_ethernet) { return; }
int eth_type = Settings->eth_type;
#if CONFIG_ETH_USE_ESP32_EMAC
if (WT32_ETH01 == TasmotaGlobal.module_type) { if (WT32_ETH01 == TasmotaGlobal.module_type) {
Settings->eth_address = 1; // EthAddress Settings->eth_address = 1; // EthAddress
Settings->eth_type = ETH_PHY_LAN8720; // EthType Settings->eth_type = ETH_PHY_LAN8720; // EthType
Settings->eth_clk_mode = ETH_CLOCK_GPIO0_IN; // EthClockMode Settings->eth_clk_mode = ETH_CLOCK_GPIO0_IN; // EthClockMode
} }
#else // No CONFIG_ETH_USE_ESP32_EMAC
if (Settings->eth_type < 7) {
Settings->eth_type = 8; // Select W5500 (SPI) for non-EMAC hardware
}
eth_type = Settings->eth_type -7; // As No EMAC support substract EMAC enums (According ETH.cpp debug info)
#endif // CONFIG_ETH_USE_ESP32_EMAC
if (!PinUsed(GPIO_ETH_PHY_MDC) && !PinUsed(GPIO_ETH_PHY_MDIO)) { if (!PinUsed(GPIO_ETH_PHY_MDC) && !PinUsed(GPIO_ETH_PHY_MDIO)) {
if (Settings->eth_type < 7) { if (Settings->eth_type < 7) {
// CONFIG_ETH_USE_ESP32_EMAC // CONFIG_ETH_USE_ESP32_EMAC
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_ETH "No ETH MDC and ETH MDIO GPIO defined")); AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_ETH "No ETH MDC and ETH MDIO GPIO defined"));
} else { } else {
// CONFIG_ETH_SPI_ETHERNET // ETH_SPI_SUPPORTS_CUSTOM
if (!PinUsed(GPIO_ETH_PHY_POWER)) { if (!PinUsed(GPIO_ETH_PHY_POWER)) {
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_ETH "No ETH MDC (SPI CS), ETH MDIO (SPI IRQ) and ETH POWER (SPI RST) GPIO defined")); AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_ETH "No ETH MDC (SPI CS), ETH MDIO (SPI IRQ) and ETH POWER (SPI RST) GPIO defined"));
} }
@ -220,15 +235,18 @@ void EthernetInit(void) {
bool init_ok = false; bool init_ok = false;
#if ESP_IDF_VERSION_MAJOR >= 5 #if ESP_IDF_VERSION_MAJOR >= 5
if (Settings->eth_type < 7) { if (Settings->eth_type < 7) {
// CONFIG_ETH_USE_ESP32_EMAC #if CONFIG_ETH_USE_ESP32_EMAC
init_ok = (ETH.begin((eth_phy_type_t)Settings->eth_type, Settings->eth_address, eth_mdc, eth_mdio, eth_power, (eth_clock_mode_t)Settings->eth_clk_mode)); init_ok = (ETH.begin((eth_phy_type_t)eth_type, Settings->eth_address, eth_mdc, eth_mdio, eth_power, (eth_clock_mode_t)Settings->eth_clk_mode));
#endif // CONFIG_ETH_USE_ESP32_EMAC
} else { } else {
// CONFIG_ETH_SPI_ETHERNET // ETH_SPI_SUPPORTS_CUSTOM
init_ok = (ETH.begin((eth_phy_type_t)Settings->eth_type, Settings->eth_address, eth_mdc, eth_mdio, eth_power, SPI, ETH_PHY_SPI_FREQ_MHZ)); // SPISettings(ETH_PHY_SPI_FREQ_MHZ * 1000 * 1000, MSBFIRST, SPI_MODE0); // 20MHz
SPI.begin(Pin(GPIO_SPI_CLK), Pin(GPIO_SPI_MISO), Pin(GPIO_SPI_MOSI), -1);
init_ok = (ETH.begin((eth_phy_type_t)eth_type, Settings->eth_address, eth_mdc, eth_mdio, eth_power, SPI, ETH_PHY_SPI_FREQ_MHZ));
} }
#else #else
init_ok = (ETH.begin(Settings->eth_address, eth_power, eth_mdc, eth_mdio, (eth_phy_type_t)Settings->eth_type, (eth_clock_mode_t)Settings->eth_clk_mode)); init_ok = (ETH.begin(Settings->eth_address, eth_power, eth_mdc, eth_mdio, (eth_phy_type_t)Settings->eth_type, (eth_clock_mode_t)Settings->eth_clk_mode));
#endif #endif // ESP_IDF_VERSION_MAJOR >= 5
if (!init_ok) { if (!init_ok) {
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_ETH "Bad PHY type or init error")); AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_ETH "Bad PHY type or init error"));
return; return;
@ -402,5 +420,5 @@ bool Xdrv82(uint32_t function) {
} }
#endif // USE_ETHERNET #endif // USE_ETHERNET
#endif // CONFIG_IDF_TARGET_ESP32 //#endif // CONFIG_IDF_TARGET_ESP32
#endif // ESP32 #endif // ESP32