# HG changeset patch # User Ethan Zonca # Date 2019-09-02 20:09:15 # Node ID d4a53aacce1cc1d6d1b5c5bae5c79dad0a60c25c # Parent 7f3f8f2d539f9b7293a7d6610b50640d207d9e9e Add support for RTC-based full unix timestamp with subsecond support diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,7 @@ enum _state static void __calc_gridloc(char *dst, double lat, double lon); static void ledpulse(void); +static void __sleep_enter_stop(void); uint32_t statled_ontime = 0; @@ -116,7 +117,6 @@ int main(void) switch(state) { - // Idling: sleep and wait for RTC timeslot trigger case SYSTEM_IDLE: { @@ -132,41 +132,10 @@ int main(void) idle_blink_last = rtc_timestamp(); } - - // Enter STOP mode for one second using RTC for wakeup - // 1s might not be accurate because we're entering STOP - // mode any time between 1s interrupt ticks... - - // TODO: Calculate full RTC timestamp before entering - // and after entering, find delta, this is the amount - // we use to increment SYSTICK - - - // Enter sleep mode: wait for interrupt - //HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI); - - uint64_t start = rtc_timestamp(); - - __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); - HAL_SuspendTick(); + // Go into stop mode for ~1s or so + __sleep_enter_stop(); - HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); - - // We probably stopped for a second - uint32_t timedelta = rtc_timestamp() - start; - HAL_IncTickBy(timedelta); - //HAL_IncTickBy(1000); // maybe check the RTC before and after this, increment tick by the delta? - HAL_ResumeTick(); - - // We have woken up! Clear wakeup flag - __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); - - // This is hopefully the only timer that needs to stay alive in idle mode - // last_wspr_tx_time += 0; // move this timer forward based on sleep length - - // HAL_ResumeTick(); - - // TODO: Eventually use GPS time to calibrate the RTC maybe + // TODO: Eventually use GPS time to calibrate the RTC maybe/trim RTC clock } break; @@ -286,6 +255,25 @@ int main(void) } +static void __sleep_enter_stop(void) +{ + // Save ms unix timestamp before we enter sleep mode + uint64_t start = rtc_timestamp(); + + __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); + HAL_SuspendTick(); + HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); + + // Calculate how long we were asleep + uint32_t timedelta = rtc_timestamp() - start; + + // Increment systick by this value to keep all timing happy + HAL_IncTickBy(timedelta); + HAL_ResumeTick(); + + // We have woken up! Clear wakeup flag + __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); +} static void ledpulse(void) {