I'm looking for a timer on the ESP32-C3 that persists through sleep phases. The system time does and it is fed by one of two clocks as described here: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-reference/system/system_time.html
I set the system time to the 17.5 MHz clock in Menuconfig, and I am aware that there will be some drift that occurs over time. However, in testing I found that there is a randomly variable offset between -10 and 100 microseconds that occurs when transitioning in and out of deep sleep
Does anyone know if it is possible to keep that from happening? Perhaps there's a setting I missed that might allow the system time to continue running during any interrupts or flash access. I also know that other ESP32 variants have more deep sleep functionality. Is it possible to run a separate timer from the ULP that will not be affected by sleep transitions?
Minimum code. PPS is coming from a DS3231:
#include "esp_sleep.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "rom/ets_sys.h"
#include "soc/periph_defs.h"
#include <sys/time.h>
#define PIN_PPS 5
#define CPU_CORE 1
QueueHandle_t binsem;
gptimer_handle_t gptimer = NULL;
RTC_DATA_ATTR uint64_t time1 = 0ULL;
RTC_DATA_ATTR uint64_t time2 = 0ULL;
RTC_DATA_ATTR uint64_t tally = 0ULL;
uint64_t readTOD() {
struct timeval now;
gettimeofday(&now, NULL);
uint64_t time_us = (uint64_t)now.tv_sec * 1000000ULL + (uint64_t)now.tv_usec;
return time_us;
}
void IRAM_ATTR ppsInterrupt(void* params) {
uint64_t now = readTOD();
time2 = time1;
time1 = now;
xSemaphoreGiveFromISR(binsem, pdFALSE);
}
void setupPPSPin(void* params) {
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_POSEDGE;
io_conf.pin_bit_mask = 1 << PIN_PPS;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = 1;
io_conf.pull_down_en = 0;
ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_IRAM));
ESP_ERROR_CHECK(gpio_config(&io_conf));
ESP_ERROR_CHECK(gpio_isr_handler_add(PIN_PPS, ppsInterrupt, NULL));
ESP_ERROR_CHECK(gpio_intr_enable(PIN_PPS));
vTaskDelete(NULL);
}
void app_main() {
binsem = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore(
setupPPSPin, /* Task function. */
"Int Task", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
NULL, /* Task handle to keep track of created task */
CPU_CORE); /* pin task to core 1 */
uint64_t ticksPerSecond = time1-time2;
if(ticksPerSecond != 0 && tally == 0) tally = time2;
int i=0;
for(i=0;i<4;i++) {
xSemaphoreTake(binsem, portMAX_DELAY);
tally+=ticksPerSecond;
printf("PPS %i: %llu - %llu \t= %lli\n", i, time1, time2, (int64_t)time1-(int64_t)time2);
// printf("PPS %i: %llu - %llu \t= %lli\n", i, time1, tally, (int64_t)time1-(int64_t)tally);
}
esp_sleep_enable_timer_wakeup(1ULL);
esp_deep_sleep_start();
}
Output shows the first two PPS interrupt triggers after wake up with random variability and any subsequent PPS triggers with a consistent timing. Observation over time does show a small but substantial cumulative drift over the inherent drift of the timer otherwise
PPS 0: 16452136656003456033 - 16452136656002456002 = 1000031
PPS 1: 16452136656004455944 - 16452136656003456033 = 999911
PPS 2: 16452136656005455916 - 16452136656004455944 = 999972
PPS 3: 16452136656006455888 - 16452136656005455916 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656007455953 - 16452136656006455888 = 1000065
PPS 1: 16452136656008455864 - 16452136656007455953 = 999911
PPS 2: 16452136656009455836 - 16452136656008455864 = 999972
PPS 3: 16452136656010455808 - 16452136656009455836 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656011455788 - 16452136656010455808 = 999980
PPS 1: 16452136656012455699 - 16452136656011455788 = 999911
PPS 2: 16452136656013455671 - 16452136656012455699 = 999972
PPS 3: 16452136656014455643 - 16452136656013455671 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656015455673 - 16452136656014455643 = 1000030
PPS 1: 16452136656016455584 - 16452136656015455673 = 999911
PPS 2: 16452136656017455556 - 16452136656016455584 = 999972
PPS 3: 16452136656018455528 - 16452136656017455556 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656019455553 - 16452136656018455528 = 1000025
PPS 1: 16452136656020455464 - 16452136656019455553 = 999911
PPS 2: 16452136656021455436 - 16452136656020455464 = 999972
PPS 3: 16452136656022455408 - 16452136656021455436 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656023455434 - 16452136656022455408 = 1000026
PPS 1: 16452136656024455345 - 16452136656023455434 = 999911
PPS 2: 16452136656025455317 - 16452136656024455345 = 999972
PPS 3: 16452136656026455289 - 16452136656025455317 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656027455351 - 16452136656026455289 = 1000062
PPS 1: 16452136656028455262 - 16452136656027455351 = 999911
PPS 2: 16452136656029455234 - 16452136656028455262 = 999972
PPS 3: 16452136656030455206 - 16452136656029455234 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656031455278 - 16452136656030455206 = 1000072
PPS 1: 16452136656032455190 - 16452136656031455278 = 999912
PPS 2: 16452136656033455162 - 16452136656032455190 = 999972
PPS 3: 16452136656034455134 - 16452136656033455162 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656035455173 - 16452136656034455134 = 1000039
PPS 1: 16452136656036455084 - 16452136656035455173 = 999911
PPS 2: 16452136656037455056 - 16452136656036455084 = 999972
PPS 3: 16452136656038455028 - 16452136656037455056 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656039454997 - 16452136656038455028 = 999969
PPS 1: 16452136656040454909 - 16452136656039454997 = 999912
PPS 2: 16452136656041454881 - 16452136656040454909 = 999972
PPS 3: 16452136656042454853 - 16452136656041454881 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656043454897 - 16452136656042454853 = 1000044
PPS 1: 16452136656044454809 - 16452136656043454897 = 999912
PPS 2: 16452136656045454781 - 16452136656044454809 = 999972
PPS 3: 16452136656046454752 - 16452136656045454781 = 999971
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656047454776 - 16452136656046454752 = 1000024
PPS 1: 16452136656048454687 - 16452136656047454776 = 999911
PPS 2: 16452136656049454659 - 16452136656048454687 = 999972
PPS 3: 16452136656050454631 - 16452136656049454659 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656051454644 - 16452136656050454631 = 1000013
PPS 1: 16452136656052454556 - 16452136656051454644 = 999912
PPS 2: 16452136656053454527 - 16452136656052454556 = 999971
PPS 3: 16452136656054454499 - 16452136656053454527 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656055454494 - 16452136656054454499 = 999995
PPS 1: 16452136656056454406 - 16452136656055454494 = 999912
PPS 2: 16452136656057454378 - 16452136656056454406 = 999972
PPS 3: 16452136656058454349 - 16452136656057454378 = 999971
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656059454378 - 16452136656058454349 = 1000029
PPS 1: 16452136656060454289 - 16452136656059454378 = 999911
PPS 2: 16452136656061454261 - 16452136656060454289 = 999972
PPS 3: 16452136656062454233 - 16452136656061454261 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656063454225 - 16452136656062454233 = 999992
PPS 1: 16452136656064454136 - 16452136656063454225 = 999911
PPS 2: 16452136656065454108 - 16452136656064454136 = 999972
PPS 3: 16452136656066454080 - 16452136656065454108 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 . Connected!
PPS 1: 16452136656068454080 - 16452136656067454168 = 999912
PPS 2: 16452136656069454051 - 16452136656068454080 = 999971
PPS 3: 16452136656070454023 - 16452136656069454051 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656071454020 - 16452136656070454023 = 999997
PPS 1: 16452136656072453931 - 16452136656071454020 = 999911
PPS 2: 16452136656073453903 - 16452136656072453931 = 999972
PPS 3: 16452136656074453875 - 16452136656073453903 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656075453897 - 16452136656074453875 = 1000022
PPS 1: 16452136656076453809 - 16452136656075453897 = 999912
PPS 2: 16452136656077453781 - 16452136656076453809 = 999972
PPS 3: 16452136656078453752 - 16452136656077453781 = 999971
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 . Connected!
PPS 1: 16452136656080453677 - 16452136656079453766 = 999911
PPS 2: 16452136656081453649 - 16452136656080453677 = 999972
PPS 3: 16452136656082453621 - 16452136656081453649 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656083453587 - 16452136656082453621 = 999966
PPS 1: 16452136656084453499 - 16452136656083453587 = 999912
PPS 2: 16452136656085453470 - 16452136656084453499 = 999971
PPS 3: 16452136656086453442 - 16452136656085453470 = 999972
Disconnected (read failed: [Errno 6] Device not configured)
Reconnecting to /dev/cu.usbmodem1101 Connected!
PPS 0: 16452136656087453460 - 16452136656086453442 = 1000018
PPS 1: 16452136656088453372 - 16452136656087453460 = 999912
PPS 2: 16452136656089453343 - 16452136656088453372 = 999971
PPS 3: 16452136656090453315 - 16452136656089453343 = 999972