Is it possible to use the RTC to track how long the chip was sleeping

Thomas1
Posts: 11
Joined: Wed Jan 23, 2019 7:40 pm

Is it possible to use the RTC to track how long the chip was sleeping

Postby Thomas1 » Wed Jan 23, 2019 8:04 pm

Hi,
I'm putting the chip to sleep after a few seconds of inactivity, then wake it up through interrupts on a pin. I need a way of knowing how long the chip was asleep. It need not be accurate to the second, so I thought the built in RTC should be enough for this job. However, I couldn't find a way of doing this.

The relevant code is as follows:

Code: Select all

#define SLEEPTIMEOUT 3000

bool wasAsleep = false;
int lastAtive;

void setup(void)
{
  lastActive = millis();
}
void loop()
{

  // Check if the device has just woken up from sleep
  if (wasAsleep)
  {
    // On waking
    wasAsleep = false;
    lastActive = millis();

    // This is where I'd like to know how long the chip was asleep!

  }
  if (millis() - lastActive > SLEEPTIMEOUT)
  {

    uint64_t ext_wakeup_pin_1_mask = 0;
    ext_wakeup_pin_1_mask |= (1ULL << WAKEUPPIN);
    esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask, ESP_EXT1_WAKEUP_ANY_HIGH);

    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);

    wasAsleep = true;

    esp_light_sleep_start();

    delay(500);
  }
}
Thanks,

ESP_igrr
Posts: 1656
Joined: Tue Dec 01, 2015 8:37 am

Re: Is it possible to use the RTC to track how long the chip was sleeping

Postby ESP_igrr » Thu Jan 24, 2019 4:23 am

You can call esp_timer_get_time or gettimeofday functions. Time reported by these function will advance correctly (up to the precision of the RTC clock) while the chip is in light sleep.

Thomas1
Posts: 11
Joined: Wed Jan 23, 2019 7:40 pm

Re: Is it possible to use the RTC to track how long the chip was sleeping

Postby Thomas1 » Thu Jan 24, 2019 5:56 pm

ESP_igrr wrote:
Thu Jan 24, 2019 4:23 am
You can call esp_timer_get_time or gettimeofday functions. Time reported by these function will advance correctly (up to the precision of the RTC clock) while the chip is in light sleep.
Thank you!

hellraise007
Posts: 13
Joined: Tue Aug 06, 2019 6:18 am

Re: Is it possible to use the RTC to track how long the chip was sleeping

Postby hellraise007 » Thu Aug 08, 2019 10:01 am

ESP_igrr wrote:
Thu Jan 24, 2019 4:23 am
You can call esp_timer_get_time or gettimeofday functions. Time reported by these function will advance correctly (up to the precision of the RTC clock) while the chip is in light sleep.
Hi, bumping this thread again because i need to know if there is any way gettimeofday() can be made to work using the arduino framework. Or if there is any other way to track time period in deep sleep mode. esp_timer_get_time resets on every wake.

Also is there any docs explaining light sleep working in arduino?

Thank you

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

Re: Is it possible to use the RTC to track how long the chip was sleeping

Postby boarchuz » Fri Aug 09, 2019 4:55 pm

With standard gettimeofday implementation:

Code: Select all

#include <sys/time.h>

RTC_DATA_ATTR timeval sleepTime;

void setup() {
  if(esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER)
  {
    timeval timeNow, timeDiff;
    gettimeofday(&timeNow, NULL);
    timersub(&timeNow,&sleepTime,&timeDiff);
    printf("Now: %"PRIu64"ms, Duration: %"PRIu64"ms\n", (timeNow.tv_sec * (uint64_t)1000) + (timeNow.tv_usec / 1000), (timeDiff.tv_sec * (uint64_t)1000) + (timeDiff.tv_usec / 1000));
    delay(2000);
  }
  gettimeofday(&sleepTime, NULL);
  printf("Sleeping...\n");
  esp_sleep_enable_timer_wakeup(5 * 1000000);
  esp_deep_sleep_start();
}
void loop(){}
With RTC functions:

Code: Select all

#include <soc/rtc.h>
extern "C" {
  #include <esp_clk.h>
}

RTC_DATA_ATTR uint64_t sleepTime;

void setup() {
  if(esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER)
  {
    uint64_t timeNow, timeDiff;
    timeNow = rtc_time_slowclk_to_us(rtc_time_get(), esp_clk_slowclk_cal_get());
    timeDiff = timeNow - sleepTime;
    printf("Now: %"PRIu64"ms, Duration: %"PRIu64"ms\n", timeNow / 1000, timeDiff / 1000);
    delay(2000);
  }
  sleepTime = rtc_time_slowclk_to_us(rtc_time_get(), esp_clk_slowclk_cal_get());
  printf("Sleeping...\n");
  esp_sleep_enable_timer_wakeup(5 * 1000000);
  esp_deep_sleep_start();
}
void loop(){}

Who is online

Users browsing this forum: No registered users and 12 guests