ESP32 Light sleep and time accuracy

User avatar
eugeniusz.gienek
Posts: 1
Joined: Fri Jan 20, 2023 12:36 pm

ESP32 Light sleep and time accuracy

Postby eugeniusz.gienek » Fri Jan 20, 2023 12:48 pm

I am trying to make power saving for my ESP32 smart watch based on ESP32 - Lillygo TTGo 1.54 e-ink. The idea is that it shows the time, then goes to light sleep for almost a minute, gets back from sleep and draws new time. The problem is the time accuracy - for some reason when I am using this approach (without light sleep no issues with time btw) the time shifts forwards - about a minute every couple of hours. Btw I have limited the cpu frequency to 10 MHz - don't know if that can somehow affect the end result. Any ideas how to fix the time inaccuracy? I've seen the similar post about that but with deep sleep (without resolution) - this one is related to light sleep and I leave the XTAL oscillator on - that doesn't help for some reason...

The sleep function looks like that:
  1. void lightSleep(int tm) {
  2.   bool is_in_light_sleep=false;
  3.   if(tm <= 0) tm = DEFAULT_LIGHT_SLEEP_TIME_S * uS_TO_S_FACTOR;
  4.   else tm *= uS_TO_S_FACTOR;
  5.   esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH,   ESP_PD_OPTION_ON);//!< RTC IO, sensors and ULP co-processor
  6.   esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,         ESP_PD_OPTION_ON);//!< XTAL oscillator
  7.   esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
  8.   for(gpio_num_t button_pin : buttons){
  9.     if(gpio_wakeup_enable(button_pin, GPIO_INTR_LOW_LEVEL) != ESP_OK) {
  10.       Serial.print("Error setting up the wakeup pin '");
  11.       Serial.print(button_pin);
  12.       Serial.println("'!");
  13.     }
  14.   }
  15.   //https://github.com/espressif/arduino-esp32/issues/5107
  16.   PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_GPIO3);
  17.   if(gpio_wakeup_enable(GPIO_NUM_3, GPIO_INTR_LOW_LEVEL) != ESP_OK) {
  18.     Serial.println("Error setting UART via GPIO pin '3' wakeup!");
  19.     is_in_light_sleep = false;
  20.     return;
  21.   }
  22.   is_in_light_sleep = true;
  23.   if(esp_sleep_enable_gpio_wakeup() != ESP_OK) {
  24.     Serial.println("Error setting gpio wakeup!");
  25.     is_in_light_sleep = false;
  26.     return;
  27.   }
  28.   if(tm > 0)
  29.     if(esp_sleep_enable_timer_wakeup(tm) != ESP_OK) {
  30.       Serial.println("Error setting timer wakeup!");
  31.       is_in_light_sleep = false;
  32.       return;
  33.     }
  34.   Serial.println("Starting light sleep.");
  35.   Serial.print("Sleep time set to ");
  36.   Serial.print(round(tm/uS_TO_S_FACTOR));
  37.   Serial.println(" seconds.");
  38.   Serial.flush();
  39.   delay(100); // https://rntlab.com/question/how-to-use-esp32-light-sleep/
  40.   esp_err_t t = esp_light_sleep_start();
  41.   if(t != ESP_OK) {
  42.     Serial.println("Sleep mode NOT enabled due to an error!");
  43.   }
  44.   Serial.println("Back from light sleep!");
  45.   PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_U0RXD); // https://github.com/espressif/arduino-esp32/issues/5107
  46.   if(is_in_light_sleep) {
  47.     esp_sleep_wakeup_cause_t wakeup_reason;
  48.     wakeup_reason = esp_sleep_get_wakeup_cause();
  49.     switch(wakeup_reason)
  50.     {
  51.       case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
  52.       case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
  53.       case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
  54.       case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
  55.       case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
  56.       case ESP_SLEEP_WAKEUP_GPIO : Serial.println("Wakeup caused by GPIO interruption"); break;
  57.       case ESP_SLEEP_WAKEUP_UART : Serial.println("Wakeup caused by UART"); break;
  58.       //case ESP_SLEEP_WAKEUP_BT : Serial.println("Wakeup caused by Bluetooth"); break;
  59.       //case ESP_SLEEP_WAKEUP_WIFI : Serial.println("Wakeup caused by WiFi"); break;
  60.       default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  61.     }
  62.     is_in_light_sleep = false;
  63.   }
  64. }

Who is online

Users browsing this forum: No registered users and 115 guests