Changeset - d4a53aacce1c
[Not reviewed]
default
0 1 0
Ethan Zonca - 7 years ago 2019-09-02 20:09:15
ez@ethanzonca.com
Add support for RTC-based full unix timestamp with subsecond support
1 file changed with 23 insertions and 35 deletions:
src/main.c
23
35
0 comments (0 inline, 0 general)
src/main.c
Show inline comments
 
@@ -31,12 +31,13 @@ enum _state
 
    SYSTEM_GPSACQ, // RTC interrupted
 
    SYSTEM_WSPRTX, // Wait for timeslot and actually transmit the message
 
};
 
 
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;
 
 
 
int main(void)
 
{
 
@@ -113,13 +114,12 @@ int main(void)
 
        }
 
 
 
 
        switch(state)
 
        {
 
 
            // Idling: sleep and wait for RTC timeslot trigger
 
            case SYSTEM_IDLE:
 
            {
 
            	// Don't blink with the usual method
 
                blink_rate = BLINK_DISABLE;
 
 
@@ -129,47 +129,16 @@ int main(void)
 
                	HAL_GPIO_WritePin(LED_BLUE, 1);
 
                	HAL_Delay(20);
 
                	HAL_GPIO_WritePin(LED_BLUE, 0);
 
                	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;
 
 
 
            // Attempt to acquire GPS fix
 
            case SYSTEM_GPSACQ:
 
@@ -283,12 +252,31 @@ int main(void)
 
		#endif
 
 
    }
 
}
 
 
 
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)
 
{
 
    HAL_GPIO_WritePin(LED_BLUE, 1);
 
	statled_ontime = HAL_GetTick();
 
}
0 comments (0 inline, 0 general)