diff --git a/src/src/ESPEasyCore/ESPEasy_setup.cpp b/src/src/ESPEasyCore/ESPEasy_setup.cpp index 84a6384a30..c825dec171 100644 --- a/src/src/ESPEasyCore/ESPEasy_setup.cpp +++ b/src/src/ESPEasyCore/ESPEasy_setup.cpp @@ -46,38 +46,46 @@ #ifdef ESP32 -#if ESP_IDF_VERSION_MAJOR < 5 -#include -#include -#include -#include -#else -#include -#include -#include -#endif - -#if CONFIG_IDF_TARGET_ESP32 -#if ESP_IDF_VERSION_MAJOR < 5 -# include "hal/efuse_ll.h" -# include "hal/efuse_hal.h" -#else -#include -#endif -#endif - -#endif +# if ESP_IDF_VERSION_MAJOR < 5 +# include +# include +# include +# include +# else // if ESP_IDF_VERSION_MAJOR < 5 +# include +# include +# include +# endif // if ESP_IDF_VERSION_MAJOR < 5 + +# if CONFIG_IDF_TARGET_ESP32 +# if ESP_IDF_VERSION_MAJOR < 5 +# include "hal/efuse_ll.h" +# include "hal/efuse_hal.h" +# else // if ESP_IDF_VERSION_MAJOR < 5 +# include +# include + +# if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 2, 0) + +// IDF5.3 fix esp_gpio_reserve used in init PSRAM. +# include "esp_private/esp_gpio_reserve.h" +# endif // if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 2, 0) +# endif // if ESP_IDF_VERSION_MAJOR < 5 +# endif // if CONFIG_IDF_TARGET_ESP32 + +#endif // ifdef ESP32 #ifdef USE_RTOS_MULTITASKING + void RTOS_TaskServers(void *parameter) { while (true) { delay(100); web_server.handleClient(); - #if FEATURE_ESPEASY_P2P + # if FEATURE_ESPEASY_P2P checkUDP(); - #endif + # endif // if FEATURE_ESPEASY_P2P } } @@ -106,7 +114,6 @@ void RTOS_HandleSchedule(void *parameter) #endif // ifdef USE_RTOS_MULTITASKING - /*********************************************************************************************\ * ISR call back function for handling the watchdog. \*********************************************************************************************/ @@ -122,39 +129,75 @@ void sw_watchdog_callback(void *arg) void ESPEasy_setup() { #if defined(ESP8266_DISABLE_EXTRA4K) || defined(USE_SECOND_HEAP) -// disable_extra4k_at_link_time(); -#endif + + // disable_extra4k_at_link_time(); +#endif // if defined(ESP8266_DISABLE_EXTRA4K) || defined(USE_SECOND_HEAP) #ifdef PHASE_LOCKED_WAVEFORM enablePhaseLockedWaveform(); -#endif +#endif // ifdef PHASE_LOCKED_WAVEFORM #ifdef USE_SECOND_HEAP HeapSelectDram ephemeral; -#endif +#endif // ifdef USE_SECOND_HEAP #ifdef ESP32 -#ifdef DISABLE_ESP32_BROWNOUT - DisableBrownout(); // Workaround possible weak LDO resulting in brownout detection during Wifi connection -#endif // DISABLE_ESP32_BROWNOUT +# ifdef DISABLE_ESP32_BROWNOUT + DisableBrownout(); // Workaround possible weak LDO resulting in brownout detection during Wifi connection +# endif // DISABLE_ESP32_BROWNOUT -#ifdef BOARD_HAS_PSRAM +# ifdef BOARD_HAS_PSRAM psramInit(); -#endif +# endif // ifdef BOARD_HAS_PSRAM + +# if CONFIG_IDF_TARGET_ESP32 -#if CONFIG_IDF_TARGET_ESP32 // restore GPIO16/17 if no PSRAM is found if (!FoundPSRAM()) { // test if the CPU is not pico - #if ESP_IDF_VERSION_MAJOR < 5 - uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG); - #else - uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_PACKAGE); - #endif + # if ESP_IDF_VERSION_MAJOR < 5 + uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG); uint32_t pkg_version = chip_ver & 0x7; - if (pkg_version <= 3) { // D0WD, S0WD, D2WD - gpio_reset_pin(GPIO_NUM_16); - gpio_reset_pin(GPIO_NUM_17); + # else // if ESP_IDF_VERSION_MAJOR < 5 + uint32_t pkg_version = bootloader_common_get_chip_ver_pkg(); + uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_PACKAGE); + # endif // if ESP_IDF_VERSION_MAJOR < 5 + + if (pkg_version <= 7) { // D0WD, S0WD, D2WD +#ifdef CORE32SOLO1 + gpio_num_t PSRAM_CLK = GPIO_NUM_17; + gpio_num_t PSRAM_CS = GPIO_NUM_16; +#else + gpio_num_t PSRAM_CLK = static_cast(CONFIG_D0WD_PSRAM_CLK_IO); + gpio_num_t PSRAM_CS = static_cast(CONFIG_D0WD_PSRAM_CS_IO); + + switch (pkg_version) + { + case EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5: + PSRAM_CLK = static_cast(CONFIG_D2WD_PSRAM_CLK_IO); + PSRAM_CS = static_cast(CONFIG_D2WD_PSRAM_CS_IO); + break; + case EFUSE_RD_CHIP_VER_PKG_ESP32PICOD4: + case EFUSE_RD_CHIP_VER_PKG_ESP32PICOV302: + PSRAM_CLK = static_cast(GPIO_NUM_NC); + PSRAM_CS = static_cast(CONFIG_PICO_PSRAM_CS_IO); + break; + } +#endif +# if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 2, 0) + + // Thanks Theo Arends from Tasmota + if (PSRAM_CLK != GPIO_NUM_NC) { + esp_gpio_revoke(BIT64(PSRAM_CLK) | BIT64(PSRAM_CS)); + } else { + esp_gpio_revoke(BIT64(PSRAM_CS)); + } +# endif // if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 2, 0) + + if (PSRAM_CLK != GPIO_NUM_NC) { + gpio_reset_pin(PSRAM_CLK); + } + gpio_reset_pin(PSRAM_CS); } } -#endif // if CONFIG_IDF_TARGET_ESP32 +# endif // if CONFIG_IDF_TARGET_ESP32 initADC(); #endif // ESP32 #ifndef BUILD_NO_RAM_TRACKER @@ -162,27 +205,28 @@ void ESPEasy_setup() lowestRAM = FreeMem(); #endif // ifndef BUILD_NO_RAM_TRACKER -/* -#ifdef ESP32 -{ - ESPEasy_NVS_Helper preferences; - ResetFactoryDefaultPreference.init(preferences); -} -#endif -*/ + /* + #ifdef ESP32 + { + ESPEasy_NVS_Helper preferences; + ResetFactoryDefaultPreference.init(preferences); + } + #endif + */ #ifndef BUILD_NO_DEBUG -// checkAll_internalCommands(); -#endif + + // checkAll_internalCommands(); +#endif // ifndef BUILD_NO_DEBUG PluginSetup(); CPluginSetup(); - + initWiFi(); WiFiEventData.clearAll(); #ifndef BUILD_MINIMAL_OTA run_compiletime_checks(); -#endif +#endif // ifndef BUILD_MINIMAL_OTA #ifdef ESP8266 // ets_isr_attach(8, sw_watchdog_callback, nullptr); // Set a callback for feeding the watchdog. @@ -212,8 +256,9 @@ void ESPEasy_setup() initLog(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("initLog()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER #ifdef BOARD_HAS_PSRAM + if (FoundPSRAM()) { if (UsePSRAM()) { addLog(LOG_LEVEL_INFO, F("Using PSRAM")); @@ -221,7 +266,7 @@ void ESPEasy_setup() addLog(LOG_LEVEL_ERROR, F("PSRAM found, unable to use")); } } - #endif + #endif // ifdef BOARD_HAS_PSRAM if (SpiffsSectors() < 32) { @@ -253,7 +298,7 @@ void ESPEasy_setup() readBootCause(); { - String log = F("INIT : "); + String log = F("INIT : "); log += getLastBootCauseString(); if (readFromRTC()) @@ -296,38 +341,39 @@ void ESPEasy_setup() } #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("RTC init")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER fileSystemCheck(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("fileSystemCheck()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER // progMemMD5check(); LoadSettings(); #if FEATURE_DEFINE_SERIAL_CONSOLE_PORT ESPEasy_Console.reInit(); -#endif +#endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("LoadSettings()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER #ifdef ESP32 + // Configure dynamic frequency scaling: // maximum and minimum frequencies are set in sdkconfig, // automatic light sleep is enabled if tickless idle support is enabled. ESP_PM_CONFIG_T pm_config = { - .max_freq_mhz = getCPU_MaxFreqMHz(), - .min_freq_mhz = Settings.EcoPowerMode() ? getCPU_MinFreqMHz() : getCPU_MaxFreqMHz(), -#if CONFIG_FREERTOS_USE_TICKLESS_IDLE - .light_sleep_enable = Settings.EcoPowerMode() -#else - .light_sleep_enable = false -#endif + .max_freq_mhz = getCPU_MaxFreqMHz(), + .min_freq_mhz = Settings.EcoPowerMode() ? getCPU_MinFreqMHz() : getCPU_MaxFreqMHz(), +# if CONFIG_FREERTOS_USE_TICKLESS_IDLE + .light_sleep_enable = Settings.EcoPowerMode() +# else + .light_sleep_enable = false +# endif // if CONFIG_FREERTOS_USE_TICKLESS_IDLE }; esp_pm_configure(&pm_config); -#endif +#endif // ifdef ESP32 #ifndef BUILD_NO_RAM_TRACKER @@ -336,7 +382,7 @@ void ESPEasy_setup() hardwareInit(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("hardwareInit()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER node_time.restoreFromRTC(); @@ -350,10 +396,11 @@ void ESPEasy_setup() toDisable = disableController(toDisable); } #if FEATURE_NOTIFIER + if (toDisable != 0) { toDisable = disableNotification(toDisable); } - #endif + #endif // if FEATURE_NOTIFIER if (toDisable != 0) { toDisable = disableRules(toDisable); @@ -367,10 +414,11 @@ void ESPEasy_setup() toDisable = disableAllControllers(toDisable); } #if FEATURE_NOTIFIER + if (toDisable != 0) { toDisable = disableAllNotifications(toDisable); } -#endif +#endif // if FEATURE_NOTIFIER } #if FEATURE_ETHERNET @@ -378,7 +426,8 @@ void ESPEasy_setup() // This only works after LoadSettings(); // Do not call setNetworkMedium here as that may try to clean up settings. active_network_medium = Settings.NetworkMedium; - #else + #else // if FEATURE_ETHERNET + if (Settings.NetworkMedium == NetworkMedium_t::Ethernet) { Settings.NetworkMedium = NetworkMedium_t::WIFI; } @@ -387,6 +436,7 @@ void ESPEasy_setup() setNetworkMedium(Settings.NetworkMedium); bool initWiFi = active_network_medium == NetworkMedium_t::WIFI; + // FIXME TD-er: Must add another check for 'delayed start WiFi' for poorly designed ESP8266 nodes. @@ -394,6 +444,7 @@ void ESPEasy_setup() WiFi_AP_Candidates.clearCache(); WiFi_AP_Candidates.load_knownCredentials(); setSTA(true); + if (!WiFi_AP_Candidates.hasCandidates()) { WiFiEventData.wifiSetup = true; RTC.clearLastWiFi(); // Must scan all channels @@ -412,22 +463,24 @@ void ESPEasy_setup() WiFi_AP_Candidates.clearCache(); processScanDone(); WiFi_AP_Candidates.load_knownCredentials(); + if (!WiFi_AP_Candidates.hasCandidates()) { addLog(LOG_LEVEL_INFO, F("Setup: Scan all channels")); WifiScan(false); } -// setWifiMode(WIFI_OFF); + + // setWifiMode(WIFI_OFF); } #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("WifiScan()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER // setWifiMode(WIFI_STA); checkRuleSets(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("checkRuleSets()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER // if different version, eeprom settings structure has changed. Full Reset needed @@ -447,38 +500,40 @@ void ESPEasy_setup() initSerial(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("initSerial()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER if (loglevelActiveFor(LOG_LEVEL_INFO)) { addLogMove(LOG_LEVEL_INFO, concat(F("INIT : Free RAM:"), FreeMem())); } -# ifndef BUILD_NO_DEBUG +#ifndef BUILD_NO_DEBUG + if (Settings.UseSerial && (Settings.SerialLogLevel >= LOG_LEVEL_DEBUG_MORE)) { ESPEasy_Console.setDebugOutput(true); } -#endif +#endif // ifndef BUILD_NO_DEBUG timermqtt_interval = 100; // Interval for checking MQTT timerAwakeFromDeepSleep = millis(); CPluginInit(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("CPluginInit()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER #if FEATURE_NOTIFIER NPluginInit(); - #ifndef BUILD_NO_RAM_TRACKER + # ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("NPluginInit()")); - #endif + # endif #endif // if FEATURE_NOTIFIER PluginInit(); initSerial(); // Plugins may have altered serial, so re-init serial - + #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("PluginInit()")); #endif + if (loglevelActiveFor(LOG_LEVEL_INFO)) { String log; log.reserve(80); @@ -491,16 +546,16 @@ void ESPEasy_setup() addLogMove(LOG_LEVEL_INFO, log); } -/* - if ((getDeviceCount() + 1) >= PLUGIN_MAX) { - addLog(LOG_LEVEL_ERROR, concat(F("Programming error! - Increase PLUGIN_MAX ("), getDeviceCount()) + ')'); - } -*/ + /* + if ((getDeviceCount() + 1) >= PLUGIN_MAX) { + addLog(LOG_LEVEL_ERROR, concat(F("Programming error! - Increase PLUGIN_MAX ("), getDeviceCount()) + ')'); + } + */ clearAllCaches(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("clearAllCaches()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER if (Settings.UseRules && isDeepSleepEnabled()) { @@ -515,48 +570,51 @@ void ESPEasy_setup() rulesProcessing(event); // TD-er: Process events in the setup() now. } #ifdef ESP32 + if (Settings.UseRules) { const uint32_t gpio_strap = GPIO_REG_READ(GPIO_STRAP_REG); -// BOOT_MODE_GET(); - // Event values: + // BOOT_MODE_GET(); + + // Event values: // ESP32 : GPIO-5, GPIO-15, GPIO-4, GPIO-2, GPIO-0, GPIO-12 // ESP32-C3: bit 0: GPIO2, bit 2: GPIO8, bit 3: GPIO9 // ESP32-S2: Unclear what bits represent which strapping state. // ESP32-S3: bit5 ~ bit2 correspond to strapping pins GPIO3, GPIO45, GPIO0, and GPIO46 respectively. String event = F("System#BootMode="); - event += bitRead(gpio_strap, 0); + event += bitRead(gpio_strap, 0); event += ','; - event += bitRead(gpio_strap, 1); + event += bitRead(gpio_strap, 1); event += ','; - event += bitRead(gpio_strap, 2); + event += bitRead(gpio_strap, 2); event += ','; - event += bitRead(gpio_strap, 3); + event += bitRead(gpio_strap, 3); event += ','; - event += bitRead(gpio_strap, 4); + event += bitRead(gpio_strap, 4); event += ','; - event += bitRead(gpio_strap, 5); + event += bitRead(gpio_strap, 5); rulesProcessing(event); } - #endif + #endif // ifdef ESP32 #if FEATURE_ETHERNET + if (Settings.ETH_Pin_power_rst != -1) { GPIO_Write(PLUGIN_GPIO, Settings.ETH_Pin_power_rst, 1); } - #endif + #endif // if FEATURE_ETHERNET NetworkConnectRelaxed(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("NetworkConnectRelaxed()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER setWebserverRunning(true); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("setWebserverRunning()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER #if FEATURE_REPORTING @@ -565,16 +623,16 @@ void ESPEasy_setup() #if FEATURE_ARDUINO_OTA ArduinoOTAInit(); - #ifndef BUILD_NO_RAM_TRACKER + # ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("ArduinoOTAInit()")); - #endif + # endif #endif // if FEATURE_ARDUINO_OTA if (node_time.systemTimePresent()) { node_time.initTime(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("node_time.initTime()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER } if (Settings.UseRules) @@ -583,13 +641,13 @@ void ESPEasy_setup() rulesProcessing(event); // TD-er: Process events in the setup() now. #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("rulesProcessing(System#Boot)")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER } writeDefaultCSS(); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("writeDefaultCSS()")); - #endif + #endif // ifndef BUILD_NO_RAM_TRACKER #ifdef USE_RTOS_MULTITASKING @@ -606,9 +664,9 @@ void ESPEasy_setup() RTOS_HandleSchedule, /* Function to implement the task */ "RTOS_HandleSchedule", /* Name of the task */ 16384, /* Stack size in words */ - nullptr, /* Task input parameter */ + nullptr, /* Task input parameter */ 1, /* Priority of the task */ - nullptr, /* Task handle. */ + nullptr, /* Task handle. */ 1); /* Core where the task should run */ } #endif // ifdef USE_RTOS_MULTITASKING @@ -624,6 +682,5 @@ void ESPEasy_setup() Scheduler.setIntervalTimerOverride(SchedulerIntervalTimer_e::TIMER_STATISTICS, 2222); #ifndef BUILD_NO_RAM_TRACKER logMemUsageAfter(F("Scheduler.setIntervalTimerOverride")); - #endif - + #endif // ifndef BUILD_NO_RAM_TRACKER } diff --git a/src/src/Helpers/Hardware_device_info.cpp b/src/src/Helpers/Hardware_device_info.cpp index 08c0ff5aee..1924bc112c 100644 --- a/src/src/Helpers/Hardware_device_info.cpp +++ b/src/src/Helpers/Hardware_device_info.cpp @@ -540,10 +540,11 @@ const __FlashStringHelper* getChipModel() { */ # if ESP_IDF_VERSION_MAJOR < 5 uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG); + pkg_version = chip_ver & 0x7; # else // if ESP_IDF_VERSION_MAJOR < 5 uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_PACKAGE); # endif // if ESP_IDF_VERSION_MAJOR < 5 - pkg_version = chip_ver & 0x7; + // AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("HDW: ESP32 Model %d, Revision %d, Core %d, Package %d"), chip_info.model, chip_revision, // chip_info.cores, chip_ver);