From f84f20b2e895ba1de561369faf76d8119fd7d816 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Sun, 5 Mar 2023 08:31:07 +0100 Subject: [PATCH] Set CPU frequency (#2125) * move Wifi, LWIP and BSSI to PSRAm * Allow setting the CPU frequency to 240 MHz (default: 160 MHz) * . * . * . * . * Update sdkconfig.defaults --------- Co-authored-by: CaCO3 --- .../components/jomjol_time_sntp/time_sntp.cpp | 2 +- code/main/main.cpp | 79 ++++++++++++++++++- code/sdkconfig.defaults | 2 + sd-card/config/config.ini | 1 + sd-card/html/edit_config_param.html | 16 ++++ sd-card/html/readconfigparam.js | 3 +- 6 files changed, 99 insertions(+), 4 deletions(-) diff --git a/code/components/jomjol_time_sntp/time_sntp.cpp b/code/components/jomjol_time_sntp/time_sntp.cpp index 5a15d26f..e4e1c64d 100644 --- a/code/components/jomjol_time_sntp/time_sntp.cpp +++ b/code/components/jomjol_time_sntp/time_sntp.cpp @@ -151,7 +151,7 @@ bool setupTime() { ConfigFile configFile = ConfigFile(CONFIG_FILE); if (!configFile.ConfigFileExists()){ - LogFile.WriteToFile(ESP_LOG_INFO, TAG, "No ConfigFile defined - exit setupTime() "); + LogFile.WriteToFile(ESP_LOG_WARN, TAG, "No ConfigFile defined - exit setupTime()!"); return false; } diff --git a/code/main/main.cpp b/code/main/main.cpp index 2cd5af00..ed5b2ab1 100644 --- a/code/main/main.cpp +++ b/code/main/main.cpp @@ -12,6 +12,7 @@ //#include "esp_psram.h" // Comming in IDF 5.0, see https://docs.espressif.com/projects/esp-idf/en/v5.0-beta1/esp32/migration-guides/release-5.x/system.html?highlight=esp_psram_get_size //#include "spiram.h" #include "esp32/spiram.h" +#include "esp_pm.h" // SD-Card //////////////////// @@ -88,6 +89,7 @@ extern std::string getHTMLcommit(void); std::vector splitString(const std::string& str); void migrateConfiguration(void); +bool setCpuFrequency(void); static const char *TAG = "MAIN"; @@ -238,6 +240,12 @@ extern "C" void app_main(void) // ******************************************** setupTime(); // NTP time service: Status of time synchronization will be checked after every round (server_tflite.cpp) + + // Set CPU Frequency + // ******************************************** + setCpuFrequency(); + + // SD card: Create further mandatory directories (if not already existing) // Correct creation of these folders will be checked with function "SDCardCheckFolderFilePresence" // ******************************************** @@ -435,8 +443,8 @@ extern "C" void app_main(void) // ******************************************** esp_chip_info_t chipInfo; esp_chip_info(&chipInfo); - LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Device info: CPU frequency: " + std::to_string(CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ) + - "Mhz, CPU cores: " + std::to_string(chipInfo.cores) + + + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Device info: CPU cores: " + std::to_string(chipInfo.cores) + ", Chip revision: " + std::to_string(chipInfo.revision)); // Print SD-Card info @@ -696,3 +704,70 @@ std::vector splitString(const std::string& str) { return found; }*/ + + +bool setCpuFrequency(void) { + ConfigFile configFile = ConfigFile(CONFIG_FILE); + string cpuFrequency = "160"; + esp_pm_config_esp32_t pm_config; + + if (!configFile.ConfigFileExists()){ + LogFile.WriteToFile(ESP_LOG_WARN, TAG, "No ConfigFile defined - exit setCpuFrequency()!"); + return false; + } + + std::vector splitted; + std::string line = ""; + bool disabledLine = false; + bool eof = false; + + + /* Load config from config file */ + while ((!configFile.GetNextParagraph(line, disabledLine, eof) || + (line.compare("[System]") != 0)) && !eof) {} + if (eof) { + return false; + } + + if (disabledLine) { + return false; + } + + while (configFile.getNextLine(&line, disabledLine, eof) && + !configFile.isNewParagraph(line)) { + splitted = ZerlegeZeile(line); + + if (toUpper(splitted[0]) == "CPUFREQUENCY") { + cpuFrequency = splitted[1]; + break; + } + } + + if (esp_pm_get_configuration(&pm_config) != ESP_OK) { + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read CPU Frequency!"); + return false; + } + + if (cpuFrequency == "160") { // 160 is the default + // No change needed + } + else if (cpuFrequency == "240") { + pm_config.max_freq_mhz = 240; + pm_config.min_freq_mhz = pm_config.max_freq_mhz; + if (esp_pm_configure(&pm_config) != ESP_OK) { + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to set new CPU frequency!"); + return false; + } + } + else { + LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unknown CPU frequency: " + cpuFrequency + "! " + "It must be 160 or 240!"); + return false; + } + + if (esp_pm_get_configuration(&pm_config) == ESP_OK) { + LogFile.WriteToFile(ESP_LOG_INFO, TAG, string("CPU frequency: ") + to_string(pm_config.max_freq_mhz) + " MHz"); + } + + return true; +} \ No newline at end of file diff --git a/code/sdkconfig.defaults b/code/sdkconfig.defaults index cfc879d2..44d8e272 100644 --- a/code/sdkconfig.defaults +++ b/code/sdkconfig.defaults @@ -156,3 +156,5 @@ CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=4864 #I (2112) esp_himem: Initialized. Using last 8 32KB address blocks for bank switching on 4352 KB of physical memory. CONFIG_SPIRAM_BANKSWITCH_ENABLE=n #CONFIG_SPIRAM_BANKSWITCH_RESERVE is not set + +CONFIG_PM_ENABLE=y diff --git a/sd-card/config/config.ini b/sd-card/config/config.ini index f703dd60..02bcd4dc 100644 --- a/sd-card/config/config.ini +++ b/sd-card/config/config.ini @@ -107,4 +107,5 @@ TimeZone = CET-1CEST,M3.5.0,M10.5.0/3 ;TimeServer = pool.ntp.org ;Hostname = undefined ;RSSIThreshold = 0 +CPUFrequency = 160 SetupMode = true diff --git a/sd-card/html/edit_config_param.html b/sd-card/html/edit_config_param.html index 2ae92890..0853b0b6 100644 --- a/sd-card/html/edit_config_param.html +++ b/sd-card/html/edit_config_param.html @@ -1345,6 +1345,20 @@ textarea { $TOOLTIP_System_RSSIThreshold + + + + + + + + + $TOOLTIP_System_CPUFrequency + +

@@ -1824,6 +1838,7 @@ function UpdateInput() { WriteParameter(param, category, "System", "Hostname", true); WriteParameter(param, category, "System", "TimeServer", true); WriteParameter(param, category, "System", "RSSIThreshold", true); + WriteParameter(param, category, "System", "CPUFrequency", true); WriteModelFiles(); } @@ -1961,6 +1976,7 @@ function ReadParameterAll() ReadParameter(param, "System", "Hostname", true); ReadParameter(param, "System", "TimeServer", true); ReadParameter(param, "System", "RSSIThreshold", true); + ReadParameter(param, "System", "CPUFrequency", true); var sel = document.getElementById("Numbers_value1"); UpdateInputIndividual(sel); diff --git a/sd-card/html/readconfigparam.js b/sd-card/html/readconfigparam.js index fd9632e4..da1715ed 100644 --- a/sd-card/html/readconfigparam.js +++ b/sd-card/html/readconfigparam.js @@ -262,6 +262,7 @@ function ParseConfig() { ParamAddValue(param, catname, "TimeServer"); ParamAddValue(param, catname, "Hostname"); ParamAddValue(param, catname, "RSSIThreshold"); + ParamAddValue(param, catname, "CPUFrequency"); ParamAddValue(param, catname, "SetupMode"); @@ -315,7 +316,7 @@ function ParseConfig() { param["DataLogging"]["DataFilesRetention"]["value1"] = "3"; } - // Downward compatiblity: Create RSSIThreshold if not available + // Downward compatibility: Create RSSIThreshold if not available if (param["System"]["RSSIThreshold"]["found"] == false) { param["System"]["RSSIThreshold"]["found"] = true;