From eaad9555a1f38a5aea883e3339d087c474e77581 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 9 Feb 2022 15:27:56 +0000 Subject: [PATCH] #839 Time (relative) in deep sleep --- xs/platforms/esp/xsHost.c | 12 ++++++++++-- xs/platforms/esp/xsHost.h | 7 ++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/xs/platforms/esp/xsHost.c b/xs/platforms/esp/xsHost.c index 2158177ab2..589b4b9195 100644 --- a/xs/platforms/esp/xsHost.c +++ b/xs/platforms/esp/xsHost.c @@ -642,6 +642,10 @@ void modGetTimeOfDay(struct modTimeVal *tv, struct modTimeZone *tz) } } +#if ESP32 +int64_t RTC_SLOW_ATTR __microsecondsOffset = 0; +#endif + void modSetTime(uint32_t seconds) { #if !ESP32 @@ -664,7 +668,9 @@ void modSetTime(uint32_t seconds) tv.tv_sec = seconds; tv.tv_usec = 0; + uint64_t usBefore = kMicroseconds64(); settimeofday(&tv, NULL); //@@ implementation doesn't use timezone yet.... + __microsecondsOffset += kMicroseconds64() - usBefore; #endif } @@ -867,9 +873,11 @@ void IRAM_ATTR timer_group0_isr(void *para) #if ESP32 -uint32_t modMilliseconds(void) +uint64_t kMicroseconds64(void) { - return xTaskGetTickCount(); + struct timeval tv_now; + gettimeofday(&tv_now, NULL); + return (uint64_t) tv_now.tv_sec * 1000000L + (uint64_t) tv_now.tv_usec - __microsecondsOffset; } #endif diff --git a/xs/platforms/esp/xsHost.h b/xs/platforms/esp/xsHost.h index 014556e8af..7894f1481f 100644 --- a/xs/platforms/esp/xsHost.h +++ b/xs/platforms/esp/xsHost.h @@ -153,9 +153,10 @@ extern uint8_t ESP_setBaud(int baud); */ #if ESP32 - extern uint32_t modMilliseconds(void); - #define modMicroseconds() (uint32_t)(esp_timer_get_time()) - + extern uint64_t kMicroseconds64(void); + #define modMilliseconds() ((uint32_t) (kMicroseconds64() / 1000L)) + #define modMicroseconds() ((uint32_t) kMicroseconds64()) + #define modDelayMilliseconds(ms) vTaskDelay(ms) #define modDelayMicroseconds(us) vTaskDelay(((us) + 500) / 1000)