diff --git a/inc/gpio.h b/inc/gpio.h --- a/inc/gpio.h +++ b/inc/gpio.h @@ -4,6 +4,14 @@ #include "stm32f0xx_hal.h" +enum _blinkrate +{ + BLINK_FAST = 50, + BLINK_MED = 250, + BLINK_SLOW = 500 +} + + #define OSC_EN_Pin GPIO_PIN_1 #define OSC_EN_GPIO_Port GPIOF #define OSC_NOTEN OSC_EN_GPIO_Port , OSC_EN_Pin diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,19 @@ #include "gps.h" +// We have access to the 1PPS pin of the gps... could have trim routine for internal oscillator based on this when we have a fix +// Probabl wake up 1 minute early -- 0.45min possible +/- on wakeup time with 15min sync intervals + + +enum _state +{ + SYSTEM_POWERUP_SYNC = 0, // on powerup, get a GPS fix and set the RTC + SYSTEM_IDLE, // 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 +} + + int main(void) { HAL_Init(); @@ -21,80 +34,89 @@ int main(void) adc_init(); i2c_init(); gps_init(); - - //jtencode_init(); - - HAL_Delay(300); - - //gps_poweroff(); - - // Disable ICs - HAL_GPIO_WritePin(OSC_NOTEN, 1); - HAL_GPIO_WritePin(TCXO_EN, 0); + wspr_init(); uint32_t led_timer = HAL_GetTick(); - uint32_t last_gps = HAL_GetTick(); - uint32_t last_wspr = HAL_GetTick(); //0xfffff; // start immediately. - led_blink(4); - uint8_t lastMinute = 0; - uint16_t blink_rate = 250; + uint16_t blink_rate = BLINK_FAST; + uint8_t state = SYSTEM_GPSACQ; - gps_poweron(); - HAL_Delay(500); - gps_update_position(); while (1) { - // TODO: Trigger this when RTC thinks its time to go - if(HAL_GetTick() - last_wspr > 500) + switch(state) { - switch(gps_getstate()) + + // Idling: sleep and wait for RTC timeslot trigger + case SYSTEM_IDLE: + { + blink_rate = BLINK_SLOW; + // Wait for RTC wakeup interrupt + wfi(); + //enter_sleep(); + + // Somehow go to another state when we get an interrupt + state = SYSTEM_GPSACQ; + } break; + + + // Attempt to acquire GPS fix + case SYSTEM_GPSACQ: { - case GPS_STATE_ACQUIRING: - blink_rate = 250; - break; - case GPS_STATE_FRESHFIX: - blink_rate = 50; - break; - case GPS_STATE_STALEFIX: - case GPS_STATE_NOFIX: - gps_acquirefix(); - blink_rate = 500; - break; - } + blink_rate = BLINK_FAST; + + // TODO: probably don't power on all the time, just on state transition + gps_poweron(); + HAL_Delay(500); + gps_update_position(); + + if(fix_ok) + { + // Disable GPS module + gps_poweroff(); + + // TODO: Set RTC from GPS time + + // TODO: Set RTC for countdown to next transmission timeslot! - uint8_t hour, minute, second; - gps_update_time(&hour, &minute, &second); + // TODO: Set wspr countdown timer for this transmission! + state = SYSTEM_WSPRTX; + } + else if(fix_timeout) + { + // Flash error code and go to idle probably? or just try forever? + } + + } break; - // EMZ TODO: this needs to trigger off of RTC minute, not GPS minute - // If last minute was odd and this minute is even (transition) -// if(lastMinute%2 == 1 && minute%2 == 0) -// { -// // Wait until the first second of the minute -// HAL_Delay(1000); -// wspr_transmit(); -// } + + // Wait for wspr timeslot and start transmitting + case SYSTEM_WSPRTX: + { + // Wait for wspr countdown timer to expire and go to tx + if(timeout_expired) + { + wspr_transmit(); + } - lastMinute = minute; - last_wspr = HAL_GetTick(); + // Schedule next wakeup (maybe 2mins prior ot timeslot if no osc trim) + // Next wakeup should enter SYSTEM_GPSACQ state... + + state = SYSTEM_IDLE; + + } break; + } + if(HAL_GetTick() - led_timer > blink_rate) { HAL_GPIO_TogglePin(LED_BLUE); led_timer = HAL_GetTick(); } - if(HAL_GetTick() - last_gps > 10) - { - //gps_process(); - gps_update_position(); - last_gps = HAL_GetTick(); - } - //enter_sleep(); } } diff --git a/src/wspr.c b/src/wspr.c --- a/src/wspr.c +++ b/src/wspr.c @@ -43,6 +43,9 @@ void wspr_init(void) HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn); + // Turn off ICs + HAL_GPIO_WritePin(OSC_NOTEN, 1); + HAL_GPIO_WritePin(TCXO_EN, 0); } // Do anything needed to prepare for sleep