ESP32 改变时钟频率 (C)

问题描述 投票:0回答:1

我正在用 C 语言(使用 ESP32 的工具链)对 ESP32 进行编程,我正在尝试更改我的 ESP32 的时钟频率,但我不确定我是否做对了(我使用了文档 https:/ /docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/power_management.html#_CPPv418esp_pm_lock_type_t 但我没有找到任何代码示例)。我认为我已经正确完成了程序部分,但我认为它仍然不起作用。有没有办法弄清楚设置的时钟频率? 我也不确定文档中的这一部分: “ESP_PM_CPU_FREQ_MAX 要求 CPU 频率为通过 esp_pm_configure 设置的最大值。参数未使用,应设置为 0。” 最后一部分是否意味着我不应该使用这个 makro?

最后但同样重要的是,我不明白我是否必须更改我的 sdkconfig 或者是否有办法在 C 中完成所有事情?

我必须测量 ns 区域的项目时间,因此我需要 esp32 的最大性能。

代码:

// ____________________________________________________________________________
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);
  }
}

和主要功能的相关部分:

#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);
c clock esp32
1个回答
0
投票

当改变 MCU 时钟频率时,它会改变所有与频率相关的总线。例如 UART 和 I2C。这里有一个巧妙的技巧来演示时钟频率的变化如何影响

serial.print
输出。请注意,您计算出的串行波特率因每个时钟速度设置而异。

//function takes the following frequencies as valid values:
//  240, 160, 80    <<< For all XTAL types
//  40, 20, 10      <<< For 40MHz XTAL
//  26, 13          <<< For 26MHz XTAL
//  24, 12          <<< For 24MHz XTAL


// 32Bit MCUs . change for 16bit 
    uint32_t Freq = 0;
    int MCU_FREQUENCY_SERIAL_SPEED=115200;
    int SERIAL_DEFAULT_SPEED = 115200;
    
    void changeMcuFreq(int Freq){
      delay(500);
      setCpuFrequencyMhz(Freq);
      changeSerialBaudRate(Freq);
      delay(500);
    } 
    
    void serialMcuFreq(){
      Freq = getCpuFrequencyMhz();
      Serial.print("CPU Freq = ");
      Serial.print(Freq);
      Serial.println(" MHz");
      Freq = getXtalFrequencyMhz();
      Serial.print("XTAL Freq = ");
      Serial.print(Freq);
      Serial.println(" MHz");
      Freq = getApbFrequency();
      Serial.print("APB Freq = ");
      Serial.print(Freq/1000000);
      Serial.println(" MHz");  
    }
    
    void changeSerialBaudRate(uint32_t Freq){
        if (Freq < 80) {
          MCU_FREQUENCY_SERIAL_SPEED = 80 / Freq * SERIAL_DEFAULT_SPEED;
        }
        else {
          MCU_FREQUENCY_SERIAL_SPEED = SERIAL_DEFAULT_SPEED;
        }
        // Dont do the 4 lines below or it will crash the ESP32 device
        // Serial.end();
        // delay(1000);
        // Serial.begin(MCU_FREQUENCY_SERIAL_SPEED);
        //delay(1000);
        
        Serial.print("\nSerial Baud Rate Speed is ");
        Serial.println(MCU_FREQUENCY_SERIAL_SPEED);
    }
    
    
    void setup()
    {
    
      Serial.begin(MCU_FREQUENCY_SERIAL_SPEED);
      
    }
     
    void loop()
    {
      changeMcuFreq(240);
      serialMcuFreq();    
      delay(2000);
      
      changeMcuFreq(160);
      serialMcuFreq();    
      delay(2000);
    
      changeMcuFreq(80);
      serialMcuFreq();    
      delay(2000);
    
      changeMcuFreq(40);
      serialMcuFreq();    
      delay(2000);
    
      changeMcuFreq(20);
      serialMcuFreq();    
      delay(2000);
    
      changeMcuFreq(10);
      serialMcuFreq();    
      delay(2000);
    
    }

要查看此代码在 .ino 草图上的工作,请查看此 GitHub 存储库 https://github.com/aeonSolutions/aeonlabs-ESP32-C-Base-Firmware-Libraries

© www.soinside.com 2019 - 2024. All rights reserved.