Skip to content

Commit 8b6483d

Browse files
committed
add XTAL freq getter and add cpu freq validation
1 parent c31ea22 commit 8b6483d

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

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

+30-3
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,32 @@ static uint32_t calculateApb(rtc_cpu_freq_config_t * conf){
126126

127127
void esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us); //private in IDF
128128

129-
bool setCpuFrequency(uint32_t cpu_freq_mhz){
129+
bool setCpuFrequencyMhz(uint32_t cpu_freq_mhz){
130130
rtc_cpu_freq_config_t conf, cconf;
131131
uint32_t capb, apb;
132+
//Get XTAL Frequency and calculate min CPU MHz
133+
rtc_xtal_freq_t xtal = rtc_clk_xtal_freq_get();
134+
uint32_t min_cpu_mhz = 10;
135+
if(xtal > RTC_XTAL_FREQ_AUTO){
136+
if(xtal < RTC_XTAL_FREQ_40M) {
137+
min_cpu_mhz = xtal / 2; //13Mhz for 26Mhz XTAL
138+
if(cpu_freq_mhz <= xtal && cpu_freq_mhz != xtal && cpu_freq_mhz != (xtal/2)){
139+
log_e("Bad frequency: %u MHz! Options are: 240, 160, 80, %u and %u MHz", cpu_freq_mhz, xtal, xtal/2);
140+
return false;
141+
}
142+
} else if(cpu_freq_mhz <= xtal && cpu_freq_mhz != xtal && cpu_freq_mhz != (xtal/2) && cpu_freq_mhz != (xtal/4)){
143+
log_e("Bad frequency: %u MHz! Options are: 240, 160, 80, %u, %u and %u MHz", cpu_freq_mhz, xtal, xtal/2, xtal/4);
144+
return false;
145+
}
146+
}
147+
if(cpu_freq_mhz > xtal && cpu_freq_mhz != 240 && cpu_freq_mhz != 160 && cpu_freq_mhz != 80){
148+
if(xtal >= RTC_XTAL_FREQ_40M){
149+
log_e("Bad frequency: %u MHz! Options are: 240, 160, 80, %u, %u and %u MHz", cpu_freq_mhz, xtal, xtal/2, xtal/4);
150+
} else {
151+
log_e("Bad frequency: %u MHz! Options are: 240, 160, 80, %u and %u MHz", cpu_freq_mhz, xtal, xtal/2);
152+
}
153+
return false;
154+
}
132155
//Get current CPU clock configuration
133156
rtc_clk_cpu_freq_get_config(&cconf);
134157
//return if frequency has not changed
@@ -144,7 +167,7 @@ bool setCpuFrequency(uint32_t cpu_freq_mhz){
144167
capb = calculateApb(&cconf);
145168
//New APB
146169
apb = calculateApb(&conf);
147-
log_i("%s: %u / %u = %u Mhz, APB: %u Hz", (conf.source == RTC_CPU_FREQ_SRC_PLL)?"PLL":((conf.source == RTC_CPU_FREQ_SRC_APLL)?"APLL":((conf.source == RTC_CPU_FREQ_SRC_XTAL)?"XTAL":"8M")), conf.source_freq_mhz, conf.div, conf.freq_mhz, apb);
170+
log_d("%s: %u / %u = %u Mhz, APB: %u Hz", (conf.source == RTC_CPU_FREQ_SRC_PLL)?"PLL":((conf.source == RTC_CPU_FREQ_SRC_APLL)?"APLL":((conf.source == RTC_CPU_FREQ_SRC_XTAL)?"XTAL":"8M")), conf.source_freq_mhz, conf.div, conf.freq_mhz, apb);
148171
//Call peripheral functions before the APB change
149172
if(apb_change_callbacks){
150173
triggerApbChangeCallback(APB_BEFORE_CHANGE, capb, apb);
@@ -171,12 +194,16 @@ bool setCpuFrequency(uint32_t cpu_freq_mhz){
171194
return true;
172195
}
173196

174-
uint32_t getCpuFrequency(){
197+
uint32_t getCpuFrequencyMhz(){
175198
rtc_cpu_freq_config_t conf;
176199
rtc_clk_cpu_freq_get_config(&conf);
177200
return conf.freq_mhz;
178201
}
179202

203+
uint32_t getXtalFrequencyMhz(){
204+
return rtc_clk_xtal_freq_get();
205+
}
206+
180207
uint32_t getApbFrequency(){
181208
rtc_cpu_freq_config_t conf;
182209
rtc_clk_cpu_freq_get_config(&conf);

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ bool addApbChangeCallback(void * arg, apb_change_cb_t cb);
3131
bool removeApbChangeCallback(void * arg, apb_change_cb_t cb);
3232

3333
//function takes the following frequencies as valid values:
34-
// 240, 160, 80 <<< For all XTAL types
35-
// 40, 20, 13, 10, 8, 5, 4, 3, 2, 1 <<< For 40MHz XTAL
36-
// 26, 13, 5, 4, 3, 2, 1 <<< For 26MHz XTAL
37-
// 24, 12, 8, 6, 4, 3, 2, 1 <<< For 24MHz XTAL
38-
bool setCpuFrequency(uint32_t cpu_freq_mhz);
39-
40-
uint32_t getCpuFrequency(); // In MHz
41-
uint32_t getApbFrequency(); // In Hz
34+
// 240, 160, 80 <<< For all XTAL types
35+
// 40, 20, 10 <<< For 40MHz XTAL
36+
// 26, 13 <<< For 26MHz XTAL
37+
// 24, 12 <<< For 24MHz XTAL
38+
bool setCpuFrequencyMhz(uint32_t cpu_freq_mhz);
39+
40+
uint32_t getCpuFrequencyMhz(); // In MHz
41+
uint32_t getXtalFrequencyMhz(); // In MHz
42+
uint32_t getApbFrequency(); // In Hz
4243

4344
#ifdef __cplusplus
4445
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void initArduino()
149149
//init proper ref tick value for PLL (uncomment if REF_TICK is different than 1MHz)
150150
//ESP_REG(APB_CTRL_PLL_TICK_CONF_REG) = APB_CLK_FREQ / REF_CLK_FREQ - 1;
151151
#ifdef F_CPU
152-
setCpuFrequency(F_CPU/1000000);
152+
setCpuFrequencyMhz(F_CPU/1000000);
153153
#endif
154154
#if CONFIG_SPIRAM_SUPPORT
155155
psramInit();

0 commit comments

Comments
 (0)