I'm using a custom board with an external 32 kHz crystal to keep track of time while the system is in deep sleep. It generally seems to work fine, but there are a couple of boards that display the message "32KHz xtal has been stopped" the moment they enter deep sleep.
I use the following code to test only the RTC on the board where it does not work:
Code: Select all
void app_main(void)
{
// rtc_clk_slow_freq_set(RTC_SLOW_FREQ_RTC);
ESP_LOGI(TAG, "Slow clock source before deep sleep: %d", rtc_clk_slow_freq_get());
int64_t time_before = rtc_time_get();
ESP_LOGI(TAG, "RTC time before deep sleep: %lld", time_before);
// Configure wakeup timer
esp_sleep_enable_timer_wakeup(5000000); // 5 seconds
// Force use of 32 kHz crystal
rtc_clk_32k_enable(true);
rtc_clk_slow_freq_set(RTC_SLOW_FREQ_32K_XTAL);
// Keep RTC peripherals powered
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
// Check crystal status
bool xtal_32k_status = rtc_clk_32k_enabled();
ESP_LOGI(TAG, "32kHz crystal is %s", xtal_32k_status ? "ACTIVE" : "INACTIVE");
ESP_LOGI(TAG, "Slow clock source after configuration: %d", rtc_clk_slow_freq_get());
esp_deep_sleep_start();
}
Code: Select all
I (495) main_task: Calling app_main()
I (495) rtc_test: Slow clock source before deep sleep: 1
I (505) rtc_test: RTC time before deep sleep: 3131998
I (505) rtc_test: 32kHz crystal is ACTIVE
I (515) rtc_test: Slow clock source after configuration: 1
Code: Select all
rtc_clk_slow_freq_set(RTC_SLOW_FREQ_RTC);
Is it possible that the Arduino code was never using the external crystal, or did it switch to using the internal one? There is no mention of this in the code.