From 790de1824d76feae97363b678e08a0085ceef379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3r=C3=A1nt=20Pint=C3=A9r?= Date: Tue, 5 Mar 2024 00:17:53 +0100 Subject: [PATCH 1/4] Fix ESP32-S2 frequency Apperently this MCU is not happy at 40 MHz. --- src/kernel/SleepManager.hpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/kernel/SleepManager.hpp b/src/kernel/SleepManager.hpp index cc1f6913..68ffc9af 100644 --- a/src/kernel/SleepManager.hpp +++ b/src/kernel/SleepManager.hpp @@ -10,14 +10,17 @@ // FIXME Why do we need to define these manually? #if CONFIG_IDF_TARGET_ESP32 -typedef esp_pm_config_esp32_t esp_pm_config_t; -#define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ +#error "ESP32 is not supported" #elif CONFIG_IDF_TARGET_ESP32S2 typedef esp_pm_config_esp32s2_t esp_pm_config_t; #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ +#define MAX_CPU_FREQ_MHZ 240 +#define MIN_CPU_FREQ_MHZ 80 #elif CONFIG_IDF_TARGET_ESP32S3 typedef esp_pm_config_esp32s3_t esp_pm_config_t; #define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ +#define MAX_CPU_FREQ_MHZ 240 +#define MIN_CPU_FREQ_MHZ 40 #endif namespace farmhub::kernel { @@ -59,14 +62,14 @@ class SleepManager { private: void configurePowerManagement(bool enableLightSleep) { - Log.verboseln("Configuring power management, light sleep is %s", - enableLightSleep ? "enabled" : "disabled"); + Log.verboseln("Configuring power management, CPU max/min at %d/%d MHz, light sleep is %s", + MAX_CPU_FREQ_MHZ, MIN_CPU_FREQ_MHZ, enableLightSleep ? "enabled" : "disabled"); // 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 = 240, - .min_freq_mhz = 40, + .max_freq_mhz = MAX_CPU_FREQ_MHZ, + .min_freq_mhz = MIN_CPU_FREQ_MHZ, .light_sleep_enable = enableLightSleep }; ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); From 80126afb63dd38b3a854f8aab65e2e53716a1dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3r=C3=A1nt=20Pint=C3=A9r?= Date: Tue, 5 Mar 2024 00:28:09 +0100 Subject: [PATCH 2/4] Remove unnecessary lines --- src/devices/Device.hpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/devices/Device.hpp b/src/devices/Device.hpp index e38227eb..51f65c72 100644 --- a/src/devices/Device.hpp +++ b/src/devices/Device.hpp @@ -48,18 +48,6 @@ typedef farmhub::devices::Mk6Config TDeviceConfiguration; #error "No device defined" #endif -// FIXME Why do we need to define these manually? -#if CONFIG_IDF_TARGET_ESP32 -typedef esp_pm_config_esp32_t esp_pm_config_t; -#define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ -#elif CONFIG_IDF_TARGET_ESP32S2 -typedef esp_pm_config_esp32s2_t esp_pm_config_t; -#define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ -#elif CONFIG_IDF_TARGET_ESP32S3 -typedef esp_pm_config_esp32s3_t esp_pm_config_t; -#define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ -#endif - namespace farmhub::devices { #ifdef FARMHUB_DEBUG From c4d5d8085c2d6b5ddc02761d60d0205961d23b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3r=C3=A1nt=20Pint=C3=A9r?= Date: Tue, 5 Mar 2024 00:28:20 +0100 Subject: [PATCH 3/4] Print CPU speed in debug mode --- src/devices/Device.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/devices/Device.hpp b/src/devices/Device.hpp index 51f65c72..9f024f42 100644 --- a/src/devices/Device.hpp +++ b/src/devices/Device.hpp @@ -5,6 +5,7 @@ #include +#include #include #ifndef FARMHUB_LOG_LEVEL @@ -79,6 +80,8 @@ class ConsolePrinter : public Print { status += ", heap: \033[33m" + String(float(ESP.getFreeHeap()) / 1024.0f, 2) + "\033[0m kB"; + status += ", CPU: \033[33m" + String(esp_clk_cpu_freq() / 1000000) + "\033[0m MHz"; + BatteryDriver* battery = this->battery.load(); if (battery != nullptr) { status += ", battery: \033[33m" + String(battery->getVoltage(), 2) + "\033[0m V"; From dfd2724c940538bb35f4a1f8dd953ae9ec62a569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3r=C3=A1nt=20Pint=C3=A9r?= Date: Tue, 5 Mar 2024 00:28:43 +0100 Subject: [PATCH 4/4] Take max CPU speed from SDK configuration --- src/kernel/SleepManager.hpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/kernel/SleepManager.hpp b/src/kernel/SleepManager.hpp index 68ffc9af..cfe57b75 100644 --- a/src/kernel/SleepManager.hpp +++ b/src/kernel/SleepManager.hpp @@ -13,13 +13,11 @@ #error "ESP32 is not supported" #elif CONFIG_IDF_TARGET_ESP32S2 typedef esp_pm_config_esp32s2_t esp_pm_config_t; -#define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ -#define MAX_CPU_FREQ_MHZ 240 +#define MAX_CPU_FREQ_MHZ CONFIG_ESP32S2_DEFAULT_CPU_FREQ_MHZ #define MIN_CPU_FREQ_MHZ 80 #elif CONFIG_IDF_TARGET_ESP32S3 typedef esp_pm_config_esp32s3_t esp_pm_config_t; -#define DEFAULT_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ -#define MAX_CPU_FREQ_MHZ 240 +#define MAX_CPU_FREQ_MHZ CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ #define MIN_CPU_FREQ_MHZ 40 #endif