Quellcode durchsuchen

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 <caco@ruinelli.ch>
CaCO3 vor 2 Jahren
Ursprung
Commit
f84f20b2e8

+ 1 - 1
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;
     }
 

+ 77 - 2
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<std::string> 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<std::string> 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<std::string> 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;
+}

+ 2 - 0
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

+ 1 - 0
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

+ 16 - 0
sd-card/html/edit_config_param.html

@@ -1345,6 +1345,20 @@ textarea {
 			<td>$TOOLTIP_System_RSSIThreshold</td>
 		</tr>
 
+		<tr class="expert"  id="System_CPUFrequency">
+			<td class="indent1">
+				<input type="checkbox" id="System_CPUFrequency_enabled" value="1"  onclick = 'InvertEnableItem("System", "CPUFrequency")' unchecked >
+				<label for=System_CPUFrequency_enabled><class id="System_CPUFrequency_text" style="color:black;">CPU Frequency</class></label>
+			</td>
+			<td>
+				<select id="System_CPUFrequency_value1">
+					<option value="160" selected>160 MHz</option>
+					<option value="240">240 MHz</option>
+				</select>
+			</td>
+			<td>$TOOLTIP_System_CPUFrequency</td>
+		</tr>
+
 	</table>
 
 	<p>
@@ -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);

+ 2 - 1
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;