I am programming an ESP32 with C (with the toolchain for ESP32) and I'm trying to change the clock frequency of my ESP32 but I'm quite unsure if I'm doing it right (I used the documenation https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/power_management.html#_CPPv418esp_pm_lock_type_t but I didn't find any code examples). I think I've done the program part right but I think it still doesn't work. Is there a way to figure out the the clock frequency that is set? I'm also unsure about this part in documentation: "ESP_PM_CPU_FREQ_MAX Require CPU frequency to be at the maximum value set via esp_pm_configure. Argument is unused and should be set to 0." Does the last part mean I shouldn't use this makro?
Last but not least I didnt't understand if I have to change my sdkconfig or is there a way to do everything in C?
I have to measure for a project times in region of ns hence I need maximum performance of esp32.
Code:
// ____________________________________________________________________________
esp_pm_config_esp32_t config_clock_struct;
// ____________________________________________________________________________
esp_pm_config_esp32_t* pointer_config_clock = &config_clock_struct;
// ____________________________________________________________________________
esp_pm_lock_handle_t handle;
void config_clock(int max_cpu_freq, int min_cpu_freq, bool light_sleep_enable,
esp_pm_config_esp32_t* pointer_config_clock) {
pointer_config_clock->max_freq_mhz = max_cpu_freq;
pointer_config_clock->min_freq_mhz = min_cpu_freq;
pointer_config_clock->light_sleep_enable = light_sleep_enable;
esp_err_t status = esp_pm_configure(pointer_config_clock);
if (status == ESP_OK) {
return;
} else if (status == ESP_ERR_INVALID_ARG) {
printf("Error %d: Configuration values aren't correct.", status);
exit(1);
} else if (status == ESP_ERR_NOT_SUPPORTED) {
printf("Error %d: Combination of values aren't supported or CONFIG_PM_ENABLE isn't enabled in sdkconfig.",
status);
exit(1);
}
}
// ____________________________________________________________________________
void init_clock(int max_cpu_freq, int min_cpu_freq, bool light_sleep_enable,
esp_pm_lock_type_t lock_type, int arg, const char* name,
esp_pm_config_esp32_t* pointer_config_clock, esp_pm_lock_handle_t* out_handle) {
config_clock(max_cpu_freq, min_cpu_freq, light_sleep_enable, pointer_config_clock);
esp_err_t status = esp_pm_lock_create(lock_type, arg, name, out_handle);
if (status == ESP_OK) {
return;
} else if (status == ESP_ERR_NO_MEM) {
printf("Error %d: Struct can't allocated.", status);
} else if (status == ESP_ERR_INVALID_ARG) {
printf("Error: %d: Invalid arguments.", status);
} else if(ESP_ERR_NOT_SUPPORTED == status) {
printf("Error %d: config pm is not enabled in sdkconfig.", status);
}
}
And the relevant part of the main function:
#define MAX_FREQUENCY 240
#define MIN_FREQUENCY 40
#define DISABLE_SLEEP 0
init_clock(MAX_FREQUENCY, MIN_FREQUENCY, DISABLE_SLEEP, ESP_PM_CPU_FREQ_MAX, ESP_PM_CPU_FREQ_MAX,
"measure mode", pointer_config_clock, &handle);
esp_err_t status_acquire = esp_pm_lock_acquire(handle);
max_freq_mhz min_freq_mhz light_sleep_enableyielded amongst other things: programmersought.com/article/97514225639 and look at:ESP_PM_CPU_FREQ_MAXet. al. You're already using that, but what about:ESP_PM_APB_FREQ_MAX? That page also uses theesp_pm_configurefunction. In [your]main, how ispointer_config_clockset up. Does it point to the global (i.e. any uninited fields should be zeroed). Maybe, usingmemset(ptr,0,sizeof(esp_pm_config_esp32_t));to be sure.