From 1d895e58e7b131682cc2ac7b5dcd2ac3ef84dcb3 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Thu, 13 Jun 2024 10:26:54 +0300 Subject: [PATCH] fix(xtal): Add a way to change the XTAL frequency for SparkFun ESP32 Thing (#9844) * fix(xtal): Add a way to change the XTAL frequency Add support for boards like SparkFun ESP32 Thing that use 26MHz XTAL * ci(pre-commit): Apply automatic fixes * feat(dbg): Print the XTAL frequency in the debug report --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- cores/esp32/chip-debug-report.cpp | 3 ++- cores/esp32/esp32-hal-misc.c | 3 --- cores/esp32/main.cpp | 10 ++++++++++ variants/esp32thing/pins_arduino.h | 2 ++ 4 files changed, 14 insertions(+), 4 deletions(-) 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"); 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..6c4d50a9a84 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..00abcdfb191 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