Added TimeTrim and Kept RTC 8M during sleep
This commit is contained in:
1 parent
0702065dd2
commit
a44137662e
1 file changed
+50
@@ -66,6 +66,50 @@ public:
|
||||
return P_LORA_DIO_1; // default for SX1262
|
||||
}
|
||||
|
||||
// 27 mins drift in 28 days and 40 mins = 669 ppm
|
||||
const int64_t MICROSECONDS_PER_SECOND_DRIFT = 669;
|
||||
const int64_t DRIFT_THRESHOLD_US = 60000000LL; // 1 minute in microseconds
|
||||
|
||||
void applyTimeTrim() {
|
||||
static int64_t last_trim_us = 0;
|
||||
static int64_t accumulated_drift_us = 0;
|
||||
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
int64_t now_us = (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec;
|
||||
|
||||
if (last_trim_us == 0) {
|
||||
last_trim_us = now_us;
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate elapsed time
|
||||
int64_t elapsed_us = now_us - last_trim_us;
|
||||
|
||||
// Add this interval's drift to accumulated drift
|
||||
accumulated_drift_us += (elapsed_us * MICROSECONDS_PER_SECOND_DRIFT) / 1000000LL;
|
||||
|
||||
// Only trim when accumulated drift exceeds threshold
|
||||
if (accumulated_drift_us >= DRIFT_THRESHOLD_US) {
|
||||
// Calculate full seconds to trim
|
||||
uint32_t seconds_to_trim = (uint32_t)(accumulated_drift_us / 1000000LL);
|
||||
|
||||
// Set back the trimmed time to RTC
|
||||
tv.tv_sec -= (time_t)seconds_to_trim;
|
||||
settimeofday(&tv, NULL);
|
||||
|
||||
// Keep the fractional remainder (anything less than a full second)
|
||||
accumulated_drift_us %= 1000000LL;
|
||||
|
||||
// Save for next trim
|
||||
gettimeofday(&tv, NULL);
|
||||
last_trim_us = (int64_t)tv.tv_sec * 1000000LL + tv.tv_usec;
|
||||
} else {
|
||||
// Skip trimming
|
||||
last_trim_us = now_us;
|
||||
}
|
||||
}
|
||||
|
||||
void sleep(uint32_t secs) override {
|
||||
// Skip if not allow to sleep
|
||||
if (inhibit_sleep) {
|
||||
@@ -85,6 +129,9 @@ public:
|
||||
}
|
||||
#endif
|
||||
|
||||
// Keep RTC 8M during sleep
|
||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC8M, ESP_PD_OPTION_ON);
|
||||
|
||||
// Set GPIO wakeup
|
||||
gpio_num_t wakeupPin = (gpio_num_t)getIRQGpio();
|
||||
|
||||
@@ -116,6 +163,9 @@ public:
|
||||
|
||||
// Enable CPU interrupt servicing
|
||||
portEXIT_CRITICAL(&sleepMux);
|
||||
|
||||
// Apply the software trim to correct for RC drift
|
||||
applyTimeTrim();
|
||||
}
|
||||
|
||||
uint8_t getStartupReason() const override { return startup_reason; }
|
||||
|
||||
Reference in new issue
Block a user