Skip to content

Commit

Permalink
Merge pull request #117 from kivancsikert/pm/fix-esp32s2-cpu-freq
Browse files Browse the repository at this point in the history
Set ESP32-S2 minimum frequency to 80 MHz
  • Loading branch information
lptr authored Mar 4, 2024
2 parents 3ec0a26 + dfd2724 commit e041a43
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
15 changes: 3 additions & 12 deletions src/devices/Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <Arduino.h>

#include <esp32/clk.h>
#include <esp_pm.h>

#ifndef FARMHUB_LOG_LEVEL
Expand Down Expand Up @@ -48,18 +49,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
Expand Down Expand Up @@ -91,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";
Expand Down
17 changes: 9 additions & 8 deletions src/kernel/SleepManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

// 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 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 CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
#define MIN_CPU_FREQ_MHZ 40
#endif

namespace farmhub::kernel {
Expand Down Expand Up @@ -59,14 +60,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));
Expand Down

0 comments on commit e041a43

Please sign in to comment.