From d39539461bdd0bef4f00faf80d24c585e65a4136 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Wed, 12 Jun 2024 20:52:31 +0300 Subject: [PATCH 1/3] fix(xtal): Add a way to change the XTAL frequency Add support for boards like SparkFun ESP32 Thing that use 26MHz XTAL --- cores/esp32/esp32-hal-misc.c | 3 --- cores/esp32/main.cpp | 10 ++++++++++ variants/esp32thing/pins_arduino.h | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c index cf8edcf279f..82363b97bd0 100644 --- a/cores/esp32/esp32-hal-misc.c +++ b/cores/esp32/esp32-hal-misc.c @@ -252,9 +252,6 @@ extern bool btInUse(); void initArduino() { //init proper ref tick value for PLL (uncomment if REF_TICK is different than 1MHz) //ESP_REG(APB_CTRL_PLL_TICK_CONF_REG) = APB_CLK_FREQ / REF_CLK_FREQ - 1; -#ifdef F_CPU - setCpuFrequencyMhz(F_CPU / 1000000); -#endif #if CONFIG_SPIRAM_SUPPORT || CONFIG_SPIRAM psramInit(); #endif diff --git a/cores/esp32/main.cpp b/cores/esp32/main.cpp index 21f15083473..7e7b883cd76 100644 --- a/cores/esp32/main.cpp +++ b/cores/esp32/main.cpp @@ -1,6 +1,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_task_wdt.h" +#include "soc/rtc.h" #include "Arduino.h" #if (ARDUINO_USB_CDC_ON_BOOT | ARDUINO_USB_MSC_ON_BOOT | ARDUINO_USB_DFU_ON_BOOT) && !ARDUINO_USB_MODE #include "USB.h" @@ -78,6 +79,15 @@ void loopTask(void *pvParameters) { } extern "C" void app_main() { +#ifdef F_XTAL_MHZ +#if !CONFIG_IDF_TARGET_ESP32S2 // ESP32-S2 does not support rtc_clk_xtal_freq_update + rtc_clk_xtal_freq_update((rtc_xtal_freq_t)F_XTAL_MHZ); + rtc_clk_cpu_freq_set_xtal(); +#endif +#endif +#ifdef F_CPU + setCpuFrequencyMhz(F_CPU / 1000000); +#endif #if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE Serial.begin(); #endif diff --git a/variants/esp32thing/pins_arduino.h b/variants/esp32thing/pins_arduino.h index 66f81860f5a..41fda0cf1f3 100644 --- a/variants/esp32thing/pins_arduino.h +++ b/variants/esp32thing/pins_arduino.h @@ -3,6 +3,8 @@ #include +#define F_XTAL_MHZ 26 //SparkFun ESP32 Thing has 26MHz Crystal + static const uint8_t LED_BUILTIN = 5; #define BUILTIN_LED LED_BUILTIN // backward compatibility #define LED_BUILTIN LED_BUILTIN // allow testing #ifdef LED_BUILTIN From 9b111b65d25fc2be828a0c715f76e8bf357022a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Thu, 13 Jun 2024 05:23:07 +0000 Subject: [PATCH 2/3] ci(pre-commit): Apply automatic fixes --- cores/esp32/main.cpp | 2 +- variants/esp32thing/pins_arduino.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/main.cpp b/cores/esp32/main.cpp index 7e7b883cd76..6c4d50a9a84 100644 --- a/cores/esp32/main.cpp +++ b/cores/esp32/main.cpp @@ -80,7 +80,7 @@ void loopTask(void *pvParameters) { extern "C" void app_main() { #ifdef F_XTAL_MHZ -#if !CONFIG_IDF_TARGET_ESP32S2 // ESP32-S2 does not support rtc_clk_xtal_freq_update +#if !CONFIG_IDF_TARGET_ESP32S2 // ESP32-S2 does not support rtc_clk_xtal_freq_update rtc_clk_xtal_freq_update((rtc_xtal_freq_t)F_XTAL_MHZ); rtc_clk_cpu_freq_set_xtal(); #endif diff --git a/variants/esp32thing/pins_arduino.h b/variants/esp32thing/pins_arduino.h index 41fda0cf1f3..00abcdfb191 100644 --- a/variants/esp32thing/pins_arduino.h +++ b/variants/esp32thing/pins_arduino.h @@ -3,7 +3,7 @@ #include -#define F_XTAL_MHZ 26 //SparkFun ESP32 Thing has 26MHz Crystal +#define F_XTAL_MHZ 26 //SparkFun ESP32 Thing has 26MHz Crystal static const uint8_t LED_BUILTIN = 5; #define BUILTIN_LED LED_BUILTIN // backward compatibility From 93aeff2d0d3b9616357dbbd8f33f2af36d4f7ef7 Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Thu, 13 Jun 2024 09:01:05 +0300 Subject: [PATCH 3/3] feat(dbg): Print the XTAL frequency in the debug report --- cores/esp32/chip-debug-report.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/esp32/chip-debug-report.cpp b/cores/esp32/chip-debug-report.cpp index 982b01ffa8f..c5b9866e3cc 100644 --- a/cores/esp32/chip-debug-report.cpp +++ b/cores/esp32/chip-debug-report.cpp @@ -96,7 +96,8 @@ static void printChipInfo(void) { chip_report_printf(" Cores : %d\n", info.cores); rtc_cpu_freq_config_t conf; rtc_clk_cpu_freq_get_config(&conf); - chip_report_printf(" Frequency : %lu MHz\n", conf.freq_mhz); + chip_report_printf(" CPU Frequency : %lu MHz\n", conf.freq_mhz); + chip_report_printf(" XTAL Frequency : %d MHz\n", rtc_clk_xtal_freq_get()); chip_report_printf(" Embedded Flash : %s\n", (info.features & CHIP_FEATURE_EMB_FLASH) ? "Yes" : "No"); chip_report_printf(" Embedded PSRAM : %s\n", (info.features & CHIP_FEATURE_EMB_PSRAM) ? "Yes" : "No"); chip_report_printf(" 2.4GHz WiFi : %s\n", (info.features & CHIP_FEATURE_WIFI_BGN) ? "Yes" : "No");