From a259746016f27dcde885700aaf5bafff3be8c812 Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Tue, 10 Mar 2020 19:23:55 +0800 Subject: [PATCH 1/2] esp32: Add a Kconfig option- Number of attempts to repeat 32k XTAL calibration Closes: IDF-1479 --- components/esp32/Kconfig | 10 ++++++++++ components/esp32/clk.c | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index eb898326d..aacffc115 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -524,6 +524,16 @@ menu "ESP32-specific" In case more value will help improve the definition of the launch of the crystal. If the crystal could not start, it will be switched to internal RC. + config ESP32_RTC_XTAL_CAL_RETRY + int "Number of attempts to repeat 32k XTAL calibration" + default 1 + depends on ESP32_RTC_CLK_SRC_EXT_CRYS + help + Number of attempts to repeat 32k XTAL calibration + before giving up and switching to the internal RC. + Increase this option if the 32k crystal oscillator + does not start and switches to internal RC. + config ESP32_RTC_XTAL_BOOTSTRAP_CYCLES int "Bootstrap cycles for external 32kHz crystal" depends on ESP32_RTC_CLK_SRC_EXT_CRYS diff --git a/components/esp32/clk.c b/components/esp32/clk.c index 304861a10..d12abd3ab 100644 --- a/components/esp32/clk.c +++ b/components/esp32/clk.c @@ -41,8 +41,19 @@ */ #define SLOW_CLK_CAL_CYCLES CONFIG_ESP32_RTC_CLK_CAL_CYCLES +#ifdef CONFIG_ESP32_RTC_XTAL_CAL_RETRY +#define RTC_XTAL_CAL_RETRY CONFIG_ESP32_RTC_XTAL_CAL_RETRY +#else +#define RTC_XTAL_CAL_RETRY 1 +#endif + #define MHZ (1000000) +/* Lower threshold for a reasonably-looking calibration value for a 32k XTAL. + * The ideal value (assuming 32768 Hz frequency) is 1000000/32768*(2**19) = 16*10^6. + */ +#define MIN_32K_XTAL_CAL_VAL 15000000L + /* Indicates that this 32k oscillator gets input from external oscillator, rather * than a crystal. */ @@ -172,6 +183,11 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) { rtc_slow_freq_t rtc_slow_freq = slow_clk & RTC_CNTL_ANA_CLK_RTC_SEL_V; uint32_t cal_val = 0; + /* number of times to repeat 32k XTAL calibration + * before giving up and switching to the internal RC + */ + int retry_32k_xtal = RTC_XTAL_CAL_RETRY; + do { if (rtc_slow_freq == RTC_SLOW_FREQ_32K_XTAL) { /* 32k XTAL oscillator needs to be enabled and running before it can @@ -190,7 +206,10 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) // When SLOW_CLK_CAL_CYCLES is set to 0, clock calibration will not be performed at startup. if (SLOW_CLK_CAL_CYCLES > 0) { cal_val = rtc_clk_cal(RTC_CAL_32K_XTAL, SLOW_CLK_CAL_CYCLES); - if (cal_val == 0 || cal_val < 15000000L) { + if (cal_val == 0 || cal_val < MIN_32K_XTAL_CAL_VAL) { + if (retry_32k_xtal-- > 0) { + continue; + } ESP_EARLY_LOGW(TAG, "32 kHz XTAL not found, switching to internal 150 kHz oscillator"); rtc_slow_freq = RTC_SLOW_FREQ_RTC; } From df2ea2527f5b873af84b768c300ea00e6389d1af Mon Sep 17 00:00:00 2001 From: KonstantinKondrashov Date: Tue, 10 Mar 2020 19:24:28 +0800 Subject: [PATCH 2/2] esp32s2: Add a Kconfig option- Number of attempts to repeat 32k XTAL calibration --- components/esp32s2/Kconfig | 10 ++++++++++ components/esp32s2/clk.c | 12 +++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/components/esp32s2/Kconfig b/components/esp32s2/Kconfig index 04aa92425..22f37f069 100644 --- a/components/esp32s2/Kconfig +++ b/components/esp32s2/Kconfig @@ -457,6 +457,16 @@ menu "ESP32S2-specific" In case more value will help improve the definition of the launch of the crystal. If the crystal could not start, it will be switched to internal RC. + config ESP32S2_RTC_XTAL_CAL_RETRY + int "Number of attempts to repeat 32k XTAL calibration" + default 3 + depends on ESP32S2_RTC_CLK_SRC_EXT_CRYS + help + Number of attempts to repeat 32k XTAL calibration + before giving up and switching to the internal RC. + Increase this option if the 32k crystal oscillator + does not start and switches to internal RC. + config ESP32S2_DISABLE_BASIC_ROM_CONSOLE bool "Permanently disable BASIC ROM Console" default n diff --git a/components/esp32s2/clk.c b/components/esp32s2/clk.c index f698eb29d..49b8ec135 100644 --- a/components/esp32s2/clk.c +++ b/components/esp32s2/clk.c @@ -42,6 +42,12 @@ */ #define SLOW_CLK_CAL_CYCLES CONFIG_ESP32S2_RTC_CLK_CAL_CYCLES +#ifdef CONFIG_ESP32S2_RTC_XTAL_CAL_RETRY +#define RTC_XTAL_CAL_RETRY CONFIG_ESP32S2_RTC_XTAL_CAL_RETRY +#else +#define RTC_XTAL_CAL_RETRY 1 +#endif + #define MHZ (1000000) /* Lower threshold for a reasonably-looking calibration value for a 32k XTAL. @@ -148,11 +154,7 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk) /* number of times to repeat 32k XTAL calibration * before giving up and switching to the internal RC */ -#ifdef CONFIG_IDF_TARGET_ESP32 - int retry_32k_xtal = 1; /* don't change the behavior for the ESP32 */ -#else - int retry_32k_xtal = 3; -#endif + int retry_32k_xtal = RTC_XTAL_CAL_RETRY; do { if (rtc_slow_freq == RTC_SLOW_FREQ_32K_XTAL) {