diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -55,14 +55,16 @@ int main(void) uint16_t blink_rate = BLINK_FAST; uint8_t state = SYSTEM_GPSACQ; -//DEBUG: -// uint8_t state = SYSTEM_IDLE; + + // DEBUG EMZ FIXME + state = SYSTEM_IDLE; uint32_t gps_polltimer = 0; uint32_t fix_acq_starttime = 0; uint32_t nextwspr_time = 0; uint8_t nextwspr_time_valid = 0; uint32_t last_wspr_tx_time = 0; + uint64_t idle_blink_last = 0; uint8_t packet_type = 0; @@ -75,12 +77,9 @@ int main(void) // __DBGMCU_CLK_ENABLE() ; // (RCC->APB2ENR |= (RCC_APB2ENR_DBGMCUEN)) // HAL_EnableDBGStopMode(); // SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP); - uint32_t idle_blink_last = 0; while (1) { - // TODO: Disable GPIO port clocking when not needed! - // Every 10 minutes, wake up and try to wspr if(state == SYSTEM_IDLE && (HAL_GetTick() - last_wspr_tx_time > 60000 * 10)) { @@ -121,27 +120,42 @@ int main(void) // Idling: sleep and wait for RTC timeslot trigger case SYSTEM_IDLE: { - // Don't blink normally - blink_rate = 9999; //BLINK_SLOW; + // Don't blink with the usual method + blink_rate = BLINK_DISABLE; // If we haven't blinked for a while, blink now - if(HAL_GetTick() - idle_blink_last > 10 * 1000) + if(rtc_timestamp() - idle_blink_last > 10 * 1000) { HAL_GPIO_WritePin(LED_BLUE, 1); HAL_Delay(20); HAL_GPIO_WritePin(LED_BLUE, 0); - idle_blink_last = HAL_GetTick(); + 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(); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); // We probably stopped for a second - HAL_IncTickBy(1000); // maybe check the RTC before and after this, increment tick by the delta? + 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 @@ -271,6 +285,8 @@ int main(void) } } + + static void ledpulse(void) { HAL_GPIO_WritePin(LED_BLUE, 1);