Questions about the dynamic frequency setting of CPU

Gaoqianjin
Posts: 11
Joined: Tue Mar 20, 2018 7:36 am

Questions about the dynamic frequency setting of CPU

Postby Gaoqianjin » Wed Mar 21, 2018 1:43 am

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?

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Questions about the dynamic frequency setting of CPU

Postby ESP_igrr » Wed Mar 21, 2018 2:49 am

//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.

Gaoqianjin
Posts: 11
Joined: Tue Mar 20, 2018 7:36 am

Re: Questions about the dynamic frequency setting of CPU

Postby Gaoqianjin » Wed Mar 21, 2018 6:00 am

How to dynamically switch CPU frequency
For example, switch from 240M to 80M. from 80M to 240M.
thanks very much

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Questions about the dynamic frequency setting of CPU

Postby ESP_igrr » Thu Mar 22, 2018 10:15 am

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.

Gaoqianjin
Posts: 11
Joined: Tue Mar 20, 2018 7:36 am

Re: Questions about the dynamic frequency setting of CPU

Postby Gaoqianjin » Mon Apr 09, 2018 6:52 am

Excuse me,Can you give an example of wifi as an AP and udp-multicast?

Gaoqianjin
Posts: 11
Joined: Tue Mar 20, 2018 7:36 am

Re: Questions about the dynamic frequency setting of CPU

Postby Gaoqianjin » Tue Apr 10, 2018 1:10 am

Hee, I've done it

nooooooob
Posts: 10
Joined: Mon May 28, 2018 4:44 am

Re: Questions about the dynamic frequency setting of CPU

Postby nooooooob » Wed Jun 06, 2018 9:31 am

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!

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

Re: Questions about the dynamic frequency setting of CPU

Postby ESP_igrr » Wed Jun 06, 2018 9:55 am

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.

nooooooob
Posts: 10
Joined: Mon May 28, 2018 4:44 am

Re: Questions about the dynamic frequency setting of CPU

Postby nooooooob » Thu Jun 07, 2018 1:24 am

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!

Who is online

Users browsing this forum: No registered users and 155 guests