RTC timer in deep sleep
Posted: Fri Jul 10, 2020 7:51 pm
Hi
I would know the time elapsed from start and end of the deep sleep. After some search I found a function (below my implementation):
This function is called in stub when the sleep ends; obviously for obtain the time elapsed I must call this function before enter the sleep, but in this case it crashs, I solved it by cloning the function without the RTC_IRAM_ATTR attribute.
The value that I found in the examples for RTC_CTNL_SLOWCLK_FREQ is 150000 and with this value the time provided diverges strongly from that provided by millis (and by esp_timer_get_time function) while with the value 160000 the clock delays about 10 seconds every hour.
To sum up:
I would know the time elapsed from start and end of the deep sleep. After some search I found a function (below my implementation):
Code: Select all
RTC_IRAM_ATTR uint64_t pw_getTime(void) {
SET_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_UPDATE_M);
while (GET_PERI_REG_MASK(RTC_CNTL_TIME_UPDATE_REG, RTC_CNTL_TIME_VALID_M) == 0) { }
uint64_t now = READ_PERI_REG(RTC_CNTL_TIME0_REG);
now |= ((uint64_t) READ_PERI_REG(RTC_CNTL_TIME1_REG)) << 32;
return now * 100 / (RTC_CTNL_SLOWCLK_FREQ / 10000); // scale RTC_CTNL_SLOWCLK_FREQ to avoid overflow;
}
The value that I found in the examples for RTC_CTNL_SLOWCLK_FREQ is 150000 and with this value the time provided diverges strongly from that provided by millis (and by esp_timer_get_time function) while with the value 160000 the clock delays about 10 seconds every hour.
To sum up:
- Why the function above crash when called outside the stub?
- What is the correct value for RTC_CTNL_SLOWCLK_FREQ?