16
16
#include "freertos/FreeRTOS.h"
17
17
#include "freertos/semphr.h"
18
18
#include "freertos/task.h"
19
+ #include "freertos/xtensa_timer.h"
19
20
#include "esp_attr.h"
20
21
#include "esp_log.h"
21
22
#include "soc/rtc.h"
@@ -33,18 +34,6 @@ typedef struct apb_change_cb_s {
33
34
static apb_change_t * apb_change_callbacks = NULL ;
34
35
static xSemaphoreHandle apb_change_lock = NULL ;
35
36
36
- static uint32_t _cpu_freq_mhz = CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ ;
37
- static uint32_t _sys_time_multiplier = 1 ;
38
-
39
- uint64_t IRAM_ATTR micros64 (){
40
- return (uint64_t )(esp_timer_get_time ()) * _sys_time_multiplier ;
41
- }
42
-
43
- //ToDo: figure out how to set FreeRTOS tick properly
44
- void delay (uint32_t ms ){
45
- vTaskDelay ((ms * _cpu_freq_mhz ) / (portTICK_PERIOD_MS * CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ ));
46
- }
47
-
48
37
static uint32_t calculateApb (rtc_cpu_freq_config_t * conf ){
49
38
if (conf -> freq_mhz >= 80 ){
50
39
return 80000000 ;
@@ -130,11 +119,13 @@ bool removeApbChangeCallback(void * arg, apb_change_cb_t cb){
130
119
return true;
131
120
}
132
121
122
+ void esp_timer_impl_update_apb_freq (uint32_t apb_ticks_per_us ); //private in IDF
123
+
133
124
bool setCpuFrequency (uint32_t cpu_freq_mhz ){
134
125
rtc_cpu_freq_config_t conf , cconf ;
135
126
uint32_t capb , apb ;
136
127
rtc_clk_cpu_freq_get_config (& cconf );
137
- if (cconf .freq_mhz == cpu_freq_mhz && _cpu_freq_mhz == cpu_freq_mhz ){
128
+ if (cconf .freq_mhz == cpu_freq_mhz ){
138
129
return true;
139
130
}
140
131
if (!rtc_clk_cpu_freq_mhz_to_config (cpu_freq_mhz , & conf )){
@@ -148,9 +139,11 @@ bool setCpuFrequency(uint32_t cpu_freq_mhz){
148
139
triggerApbChangeCallback (APB_BEFORE_CHANGE , capb , apb );
149
140
}
150
141
rtc_clk_cpu_freq_set_config_fast (& conf );
151
- _cpu_freq_mhz = conf . freq_mhz ;
152
- _sys_time_multiplier = 80000000 / apb ;
142
+ //Update FreeRTOS Tick Divisor
143
+ _xt_tick_divisor = cpu_freq_mhz * 1000000 / XT_TICK_PER_SEC ;
153
144
if (capb != apb && apb_change_callbacks ){
145
+ //Update esp_timer divisor
146
+ esp_timer_impl_update_apb_freq (apb / 1000000 );
154
147
triggerApbChangeCallback (APB_AFTER_CHANGE , capb , apb );
155
148
}
156
149
return true;
0 commit comments