Skip to content

Commit f7c56e3

Browse files
committed
Properly set esp_timer and FreeRTOS tick dividers
1 parent daea578 commit f7c56e3

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

Diff for: cores/esp32/esp32-hal-cpu.c

+8-15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "freertos/FreeRTOS.h"
1717
#include "freertos/semphr.h"
1818
#include "freertos/task.h"
19+
#include "freertos/xtensa_timer.h"
1920
#include "esp_attr.h"
2021
#include "esp_log.h"
2122
#include "soc/rtc.h"
@@ -33,18 +34,6 @@ typedef struct apb_change_cb_s {
3334
static apb_change_t * apb_change_callbacks = NULL;
3435
static xSemaphoreHandle apb_change_lock = NULL;
3536

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-
4837
static uint32_t calculateApb(rtc_cpu_freq_config_t * conf){
4938
if(conf->freq_mhz >= 80){
5039
return 80000000;
@@ -130,11 +119,13 @@ bool removeApbChangeCallback(void * arg, apb_change_cb_t cb){
130119
return true;
131120
}
132121

122+
void esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us); //private in IDF
123+
133124
bool setCpuFrequency(uint32_t cpu_freq_mhz){
134125
rtc_cpu_freq_config_t conf, cconf;
135126
uint32_t capb, apb;
136127
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){
138129
return true;
139130
}
140131
if(!rtc_clk_cpu_freq_mhz_to_config(cpu_freq_mhz, &conf)){
@@ -148,9 +139,11 @@ bool setCpuFrequency(uint32_t cpu_freq_mhz){
148139
triggerApbChangeCallback(APB_BEFORE_CHANGE, capb, apb);
149140
}
150141
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;
153144
if(capb != apb && apb_change_callbacks){
145+
//Update esp_timer divisor
146+
esp_timer_impl_update_apb_freq(apb / 1000000);
154147
triggerApbChangeCallback(APB_AFTER_CHANGE, capb, apb);
155148
}
156149
return true;

Diff for: cores/esp32/esp32-hal-cpu.h

-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ bool setCpuFrequency(uint32_t cpu_freq_mhz);
4040
uint32_t getCpuFrequency(); // In MHz
4141
uint32_t getApbFrequency(); // In Hz
4242

43-
uint64_t micros64(); // 64bit version of micros()
44-
4543
#ifdef __cplusplus
4644
}
4745
#endif

Diff for: cores/esp32/esp32-hal-misc.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,15 @@ void disableCore1WDT(){
101101
#endif
102102

103103
unsigned long IRAM_ATTR micros(){
104-
return (unsigned long) (micros64());
104+
return (unsigned long) (esp_timer_get_time());
105105
}
106106

107107
unsigned long IRAM_ATTR millis(){
108-
return (unsigned long) (micros64() / 1000);
108+
return (unsigned long) (esp_timer_get_time() / 1000);
109+
}
110+
111+
void delay(uint32_t ms){
112+
vTaskDelay(ms / portTICK_PERIOD_MS);
109113
}
110114

111115
void IRAM_ATTR delayMicroseconds(uint32_t us)

0 commit comments

Comments
 (0)