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
 
@@ -25,24 +25,25 @@
 
//  Z: temperature
 
//  KD8TDF XXYYZZ // could use alt callsign thing
 
 
enum _state
 
{
 
    SYSTEM_IDLE = 0, // awaiting RTC interrupt for wakeup TODO wake up before scheduled time to get fix?
 
    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)
 
{
 
    HAL_Init();
 
    HAL_Delay(1000); // startup delay before infinisleep
 
 
    sysclk_init();
 
    gpio_init();
 
    rtc_init();
 
@@ -107,75 +108,43 @@ int main(void)
 
            		// Wait until odd minute, one minute and some change away
 
            		nextwspr_time = HAL_GetTick() + 60000 + (60000 - (gps_getdata()->second * 1000));
 
                    nextwspr_time_valid = 1;
 
            	}
 
            }
 
            gps_polltimer = HAL_GetTick();
 
        }
 
 
 
 
        switch(state)
 
        {
 
 
            // Idling: sleep and wait for RTC timeslot trigger
 
            case SYSTEM_IDLE:
 
            {
 
            	// Don't blink with the usual method
 
                blink_rate = BLINK_DISABLE;
 
 
                // If we haven't blinked for a while, blink now
 
                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 = 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:
 
            {
 
                blink_rate = BLINK_FAST;
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
                HAL_PWR_EnterSLEEPMode(0, PWR_SLEEPENTRY_WFI);
 
@@ -277,24 +246,43 @@ int main(void)
 
 
			if((blink_rate != BLINK_DISABLE) && (statled_ontime && HAL_GetTick() - statled_ontime > 10))
 
			{
 
				HAL_GPIO_WritePin(LED_BLUE, 0);
 
				statled_ontime = 0;
 
			}
 
		#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();
 
}
 
 
static void __calc_gridloc(char *dst, double lat, double lon)
 
{
 
	int o1, o2, o3;
 
	int a1, a2, a3;
 
	double remainder;
0 comments (0 inline, 0 general)