Changeset - 876e5a9f77ab
[Not reviewed]
default
0 1 0
Ethan Zonca - 7 years ago 2019-09-02 21:44:09
ez@ethanzonca.com
Add stop mode to all modes of operation
1 file changed with 29 insertions and 19 deletions:
src/main.c
29
19
0 comments (0 inline, 0 general)
src/main.c
Show inline comments
 
@@ -55,13 +55,13 @@ int main(void)
 
    led_blink(4);
 
 
    uint16_t blink_rate = BLINK_FAST;
 
    uint8_t state = SYSTEM_GPSACQ;
 
 
    // DEBUG EMZ FIXME
 
    state = SYSTEM_IDLE;
 
//    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;
 
@@ -89,12 +89,17 @@ int main(void)
 
 
        // Update fix status every 2 seconds, only if the GPS is powered on
 
        if(HAL_GetTick() - gps_polltimer > 2000)
 
        {
 
            if(gps_ison())
 
            {
 
            	HAL_GPIO_WritePin(LED_BLUE, 1);
 
            	HAL_Delay(50);
 
            	HAL_GPIO_WritePin(LED_BLUE, 0);
 
 
 
            	gps_update_data();
 
 
            	// If odd minute
 
            	if(gps_getdata()->minute % 2)
 
            	{
 
            		// Wait until even minute plus one second, coming soon
 
@@ -140,18 +145,14 @@ int main(void)
 
            } 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);
 
 
                //blink_rate = BLINK_FAST;
 
                blink_rate = BLINK_DISABLE;
 
 
                if(!gps_ison())
 
                {
 
                	fix_acq_starttime = HAL_GetTick();
 
                    gps_poweron(); // power on and initialize GPS module
 
                }
 
@@ -181,22 +182,28 @@ int main(void)
 
                	led_blink(4);
 
                    gps_poweroff();
 
                    fix_acq_starttime = 0;
 
                    last_wspr_tx_time = HAL_GetTick(); // repeat acq/tx cycle after big time delay
 
                	state = SYSTEM_IDLE;
 
                }
 
                else
 
                {
 
                	// We're waiting for a GPS fix, might as well sleep and let the GPS get a fix.
 
            		// Enter stop mode for 1 second. Blink in the GPS code above.
 
                    __sleep_enter_stop();
 
                }
 
 
            } break;
 
 
 
            // Wait for wspr timeslot and start transmitting
 
            case SYSTEM_WSPRTX:
 
            {
 
            	blink_rate = BLINK_MED;
 
                // Wait for wspr countdown timer to expire and go to tx
 
//                if(timeout_expired)
 
//                {
 
            	//blink_rate = BLINK_MED;
 
                blink_rate = BLINK_DISABLE;
 
 
 
            	// If we're after the minute but not more than 2s after the minute, start tx
 
            	if(HAL_GetTick() >= nextwspr_time)
 
            	{
 
            		if(HAL_GetTick() < nextwspr_time + 2000)
 
            		{
 
@@ -221,21 +228,24 @@ int main(void)
 
                        adc_stop();
 
            		}
 
                    nextwspr_time_valid = 0; // invalidate wspr time
 
                }
 
            	else
 
            	{
 
                    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);
 
                	HAL_GPIO_WritePin(LED_BLUE, 1);
 
                	HAL_Delay(50);
 
                	HAL_GPIO_WritePin(LED_BLUE, 0);
 
                	HAL_Delay(50);
 
                	HAL_GPIO_WritePin(LED_BLUE, 1);
 
                	HAL_Delay(50);
 
                	HAL_GPIO_WritePin(LED_BLUE, 0);
 
 
            		// Enter stop mode for 1 second
 
                    __sleep_enter_stop();
 
            	}
 
 
                // Schedule next wakeup (maybe 2mins prior to timeslot if no osc trim)
 
                // Next wakeup should enter SYSTEM_GPSACQ state...
 
 
            } break;
 
 
        }
 
 
		#ifndef LED_DISABLE
 
			if((blink_rate != BLINK_DISABLE) && (HAL_GetTick() - led_timer > blink_rate))
 
@@ -255,16 +265,16 @@ int main(void)
 
}
 
 
 
static void __sleep_enter_stop(void)
 
{
 
	// Save ms unix timestamp before we enter sleep mode
 
	HAL_SuspendTick();
 
	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
0 comments (0 inline, 0 general)