RTC timer in deep sleep

rossati
Posts: 16
Joined: Mon May 11, 2020 7:06 am

RTC timer in deep sleep

Postby rossati » 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):

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;
}
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:
  • Why the function above crash when called outside the stub?
  • What is the correct value for RTC_CTNL_SLOWCLK_FREQ?
Thanks

boarchuz
Posts: 609
Joined: Tue Aug 21, 2018 5:28 am

Re: RTC timer in deep sleep

Postby boarchuz » Sat Jul 11, 2020 10:34 pm

1. I'm not sure, but given the source has a delay in it I'd suggest trying the same: https://github.com/espressif/esp-idf/bl ... ime.c#L143
especially if you're getting a WDT. Maybe the RTC has lower priority and you need to yield the bus for a moment to give it a chance to update.

2. Here's rtc slow ticks difference to calibrated microseconds: https://github.com/espressif/esp-idf/bl ... #L347-L348

rossati
Posts: 16
Joined: Mon May 11, 2020 7:06 am

Re: RTC timer in deep sleep

Postby rossati » Sun Jul 12, 2020 6:57 am

Thanks boarchuz.

Who is online

Users browsing this forum: No registered users and 76 guests