I am experimenting with a cpu clock frequency reduction on ESP32 mcu. I have a clock frequency of 240 MHz in normal operation mode and switch down to 10 MHz in reduced power consumption mode. However, after reducing the frequency, I can see that the ported freeRTOS timing of functions is reduced - e.g. vTaskDelay(1 / PortTICK_RATE_MS) - delays a task for 1 ms with 240 MHz, but with reduced frequency to 10 MHz, the task is delayed for 24 ms. It seems, the frequency is reduced, but the "system tick" is not altered, accordingly. After looking through ESP_IDF code, I have found an _xt_tick_divisor extern variable, but altering it manually does not seem to give the desired result. Could anyone provide information, on how would it be possible to resolve the freeRTOS timing issue after the frequency reduction?
I am using esp_pm_configure() function of ESP-IDF v4.3.1 to reduce the cpu frequency. And I am aware, that APB frequency is reduced, as well. https://docs.espressif.com/projects/esp ... gurationPv
- //just a sketch
- #include "esp_err.h"
- #include "esp_pm.h"
- #include "esp32/clk.h"
- #include "freertos/xtensa_timer.h"
- #define TEN_IN_SIXTH (1000000)
- #define DEEP_SLEEP_CPU_SPEED (10 * TEN_IN_SIXTH)
- #define NORMAL_CPU_SPEED (240 * TEN_IN_SIXTH)
- //frequency reduction function
- uint8_t set_CPU_speed(uint32_t speed) {
- uint8_t err = ESP_OK;
- const int low_freq = DEEP_SLEEP_CPU_SPEED / TEN_IN_SIXTH;
- const int hi_freq = NORMAL_CPU_SPEED / TEN_IN_SIXTH;
- switch (speed) {
- case DEEP_SLEEP_CPU_SPEED:
- esp_pm_configuration.min_freq_mhz = low_freq;
- esp_pm_configuration.max_freq_mhz = low_freq;
- err = esp_pm_configure(&esp_pm_configuration);
- if (err == ESP_OK) {
- _xt_tick_divisor = DEEP_SLEEP_CPU_SPEED / XT_TICK_PER_SEC;
- }
- break;
- case NORMAL_CPU_SPEED:
- esp_pm_configuration.min_freq_mhz = hi_freq;
- esp_pm_configuration.max_freq_mhz = hi_freq;
- err = esp_pm_configure(&esp_pm_configuration);
- if (err == ESP_OK) {
- _xt_tick_divisor = NORMAL_CPU_SPEED / XT_TICK_PER_SEC;
- }
- break;
- default:
- err = 0xFF;
- break;
- }
- return err;
- }
- CONFIG_FREERTOS_HZ=1000