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
 
@@ -49,25 +49,25 @@ int main(void)
 
    rtc_init();
 
    adc_init();
 
    wspr_init();
 
 
    uint32_t led_timer = HAL_GetTick();
 
 
    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;
 
    uint64_t idle_blink_last = 0;
 
 
    uint8_t packet_type = 0;
 
 
    // Transmit pilot tone to test TX on bootup
 
    HAL_Delay(1000);
 
@@ -83,24 +83,29 @@ int main(void)
 
    {
 
    	// Every 10 minutes, wake up and try to wspr
 
    	if(state == SYSTEM_IDLE && (HAL_GetTick() - last_wspr_tx_time > 60000 * 10))
 
    	{
 
    		state = SYSTEM_GPSACQ;
 
    	}
 
 
        // 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
 
            		nextwspr_time = HAL_GetTick() + (60000 - (gps_getdata()->second * 1000));
 
                    nextwspr_time_valid = 1;
 
 
            	}
 
            	// If even minute
 
            	else
 
@@ -134,30 +139,26 @@ int main(void)
 
 
                // Go into stop mode for ~1s or so
 
                __sleep_enter_stop();
 
 
                // 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);
 
 
                //blink_rate = BLINK_FAST;
 
                blink_rate = BLINK_DISABLE;
 
 
                if(!gps_ison())
 
                {
 
                	fix_acq_starttime = HAL_GetTick();
 
                    gps_poweron(); // power on and initialize GPS module
 
                }
 
 
                // TODO: Move GPS processing into here from above!
 
 
 
                // If 3d fix with a decent enough precision
 
                if( ((gps_getdata()->fixtype == 2) || (gps_getdata()->fixtype == 3)) && gps_getdata()->pdop < 10 && nextwspr_time_valid == 1)
 
@@ -175,34 +176,40 @@ int main(void)
 
                    adc_start();
 
                }
 
                // If no decent fix in 3 minutes
 
                else if(HAL_GetTick() - fix_acq_starttime > 60000 * 3)
 
                {
 
                	// Flash error code and go to idle, try again next time
 
                	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)
 
            		{
 
            			volatile double latitude_flt = (double)gps_getdata()->latitude / 10000000.0;
 
            			volatile double longitude_flt = (double)gps_getdata()->longitude / 10000000.0;
 
            			volatile uint8_t grid_locator[7];
 
 
            			__calc_gridloc(grid_locator, latitude_flt, longitude_flt);
 
 
@@ -215,33 +222,36 @@ int main(void)
 
            		}
 
            		else
 
            		{
 
            			// Window was missed, go back to idle, and try again after time delay
 
						last_wspr_tx_time = HAL_GetTick();
 
            			state = SYSTEM_IDLE;
 
                        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))
 
			{
 
				ledpulse();
 
				led_timer = HAL_GetTick();
 
			}
 
 
			if((blink_rate != BLINK_DISABLE) && (statled_ontime && HAL_GetTick() - statled_ontime > 10))
 
@@ -249,28 +259,28 @@ int main(void)
 
				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
 
	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
 
	HAL_IncTickBy(timedelta);
 
	HAL_ResumeTick();
 
 
	// We have woken up! Clear wakeup flag
 
	__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
 
}
0 comments (0 inline, 0 general)