Commit 66d33f79 authored by me-no-dev's avatar me-no-dev

Add support for CPU Frequency switching

Experimental!
parent 0de0d3f7
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "esp_bt.h" #include "esp_bt.h"
#endif //CONFIG_BT_ENABLED #endif //CONFIG_BT_ENABLED
#include <sys/time.h> #include <sys/time.h>
#include "soc/rtc.h"
#include "esp32-hal.h" #include "esp32-hal.h"
//Undocumented!!! Get chip temperature in Farenheit //Undocumented!!! Get chip temperature in Farenheit
...@@ -41,19 +42,41 @@ void yield() ...@@ -41,19 +42,41 @@ void yield()
vPortYield(); vPortYield();
} }
static uint32_t _cpu_freq_mhz = 240;
bool cpuFrequencySet(uint32_t cpu_freq_mhz){
if(_cpu_freq_mhz == cpu_freq_mhz){
return true;
}
rtc_cpu_freq_config_t conf;
if(!rtc_clk_cpu_freq_mhz_to_config(cpu_freq_mhz, &conf)){
log_e("CPU clock could not be set to %u MHz", cpu_freq_mhz);
return false;
}
rtc_clk_cpu_freq_set_config(&conf);
_cpu_freq_mhz = conf.freq_mhz;
return true;
}
uint32_t cpuFrequencyGet(){
rtc_cpu_freq_config_t conf;
rtc_clk_cpu_freq_get_config(&conf);
return conf.freq_mhz;
}
unsigned long IRAM_ATTR micros() unsigned long IRAM_ATTR micros()
{ {
return (unsigned long) esp_timer_get_time(); return (unsigned long) (esp_timer_get_time() * (240 / _cpu_freq_mhz));
} }
unsigned long IRAM_ATTR millis() unsigned long IRAM_ATTR millis()
{ {
return (unsigned long) (esp_timer_get_time() / 1000); return (unsigned long) (micros() / 1000);
} }
void delay(uint32_t ms) void delay(uint32_t ms)
{ {
vTaskDelay(ms / portTICK_PERIOD_MS); vTaskDelay(ms / portTICK_PERIOD_MS / (240 / _cpu_freq_mhz));
} }
void IRAM_ATTR delayMicroseconds(uint32_t us) void IRAM_ATTR delayMicroseconds(uint32_t us)
......
...@@ -72,6 +72,9 @@ void yield(void); ...@@ -72,6 +72,9 @@ void yield(void);
//returns chip temperature in Celsius //returns chip temperature in Celsius
float temperatureRead(); float temperatureRead();
bool cpuFrequencySet(uint32_t cpu_freq_mhz);
uint32_t cpuFrequencyGet();
unsigned long micros(); unsigned long micros();
unsigned long millis(); unsigned long millis();
void delay(uint32_t); void delay(uint32_t);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment