Page 1 of 1

Questions about the dynamic frequency setting of CPU

Posted: Wed Mar 21, 2018 1:43 am
by Gaoqianjin
I want to change the CPU frequency dynamically
code :
static const char *TAG = "power_save";
#if CONFIG_PM_ENABLE
static esp_pm_lock_handle_t lock_handle;
rtc_cpu_freq_t max_freq;
rtc_clk_cpu_freq_from_mhz(CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ, &max_freq);
esp_pm_config_esp32_t pm_config = {
.max_cpu_freq = max_freq,
.min_cpu_freq = RTC_CPU_FREQ_XTAL
};
ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
ESP_ERROR_CHECK(esp_pm_lock_create(ESP_PM_CPU_FREQ_MAX,0,NULL,&lock_handle));
ESP_ERROR_CHECK(esp_pm_lock_acquire(lock_handle));
//I think CPU works at the maximum power at this time.
ESP_ERROR_CHECK(esp_pm_lock_release(lock_handle));
//I think CPU works at low power at this time
#endif // CONFIG_PM_ENABLE

I add delay between frequency switching
However, the working current of the development board has not changed.
Can you tell me where the problem is?

Re: Questions about the dynamic frequency setting of CPU

Posted: Wed Mar 21, 2018 2:49 am
by ESP_igrr
//I think CPU works at low power at this time
This is not so — the system will be able to go the a lower power mode only when not RTOS tasks are running.
This is because on each core, FreeRTOS will take ESP_PM_CPU_FREQ_MAX lock whenever there is any code to be run.
So your task needs to block on some synchronization primitive (mutex, queue,...) and no other tasks should run — then the system will go into lower power mode.

Re: Questions about the dynamic frequency setting of CPU

Posted: Wed Mar 21, 2018 6:00 am
by Gaoqianjin
How to dynamically switch CPU frequency
For example, switch from 240M to 80M. from 80M to 240M.
thanks very much

Re: Questions about the dynamic frequency setting of CPU

Posted: Thu Mar 22, 2018 10:15 am
by ESP_igrr
In the simplest way, switching working frequency can look like this:
https://gist.github.com/igrr/a0c3b05f16 ... 8d943036e6

However, take note that:

1) There is currently a bug in IDF which makes this kind of switching between 80/160 and 240MHz impossible. I.e. you can switch between 80 and 160, but can't switch 80<->240 and 160<->240. The fix is in review and should be on Github soon.

2) Frequency switching is not possible while any power management lock is being held. This is why there is a vTaskDelay in the code — to wait until the only task in this example is idle, then frequency can be switched.

Re: Questions about the dynamic frequency setting of CPU

Posted: Mon Apr 09, 2018 6:52 am
by Gaoqianjin
Excuse me,Can you give an example of wifi as an AP and udp-multicast?

Re: Questions about the dynamic frequency setting of CPU

Posted: Tue Apr 10, 2018 1:10 am
by Gaoqianjin
Hee, I've done it

Re: Questions about the dynamic frequency setting of CPU

Posted: Wed Jun 06, 2018 9:31 am
by nooooooob
ESP_igrr wrote:In the simplest way, switching working frequency can look like this:
https://gist.github.com/igrr/a0c3b05f16 ... 8d943036e6

However, take note that:

1) There is currently a bug in IDF which makes this kind of switching between 80/160 and 240MHz impossible. I.e. you can switch between 80 and 160, but can't switch 80<->240 and 160<->240. The fix is in review and should be on Github soon.

2) Frequency switching is not possible while any power management lock is being held. This is why there is a vTaskDelay in the code — to wait until the only task in this example is idle, then frequency can be switched.
Hi I'm also trying to change the cpu frequency. I tried your code and they are really useful, I enabled the PM DFS setting in the menuconfig and I used your code to set the cpu frequency to 40MHz but the current is still higher than expected, I noticed there is an 8Mhz crystal in the esp32. I wonder if there is any way to chose 8MHz as my cpu frequency when I don't need a very high processing speed? Thanks!

Re: Questions about the dynamic frequency setting of CPU

Posted: Wed Jun 06, 2018 9:55 am
by ESP_igrr
I enabled the PM DFS setting in the menuconfig and I used your code to set the cpu frequency to 40MHz but the current is still higher than expected, I noticed there is an 8Mhz crystal in the esp32. I wonder if there is any way to chose 8MHz as my cpu frequency when I don't need a very high processing speed? Thanks!
ESP32 does not allow us to set CPU frequency below APB frequency, and APB frequency needs to be set to 80MHz to use WiFi, BT, and some other peripherals. For this reason pm_configure does not allow setting maximum CPU frequency to a value lower than 80MHz. It is a restriction we are planning to relax eventually, along with adding support other frequencies between XTAL and 2MHz.

Re: Questions about the dynamic frequency setting of CPU

Posted: Thu Jun 07, 2018 1:24 am
by nooooooob
ESP_igrr wrote:
I enabled the PM DFS setting in the menuconfig and I used your code to set the cpu frequency to 40MHz but the current is still higher than expected, I noticed there is an 8Mhz crystal in the esp32. I wonder if there is any way to chose 8MHz as my cpu frequency when I don't need a very high processing speed? Thanks!
ESP32 does not allow us to set CPU frequency below APB frequency, and APB frequency needs to be set to 80MHz to use WiFi, BT, and some other peripherals. For this reason pm_configure does not allow setting maximum CPU frequency to a value lower than 80MHz. It is a restriction we are planning to relax eventually, along with adding support other frequencies between XTAL and 2MHz.
But I think I already set the frequency to 40 MHz because the current is about 17 mA and the function returned success when I use it.By the way I'm not using WIFI nor BT.I will keep my config I'm current using. If there's any other alternatives to lower the frequency please keep me posted.Thanks for your help!