diff --git a/inc/gps.h b/inc/gps.h --- a/inc/gps.h +++ b/inc/gps.h @@ -29,22 +29,17 @@ #include -// Hardware config -/*#define GPS_USART USART1 -#define GPS_IRQ NVIC_USART1_IRQ - -#define GPS_TX_PORT GPIOB -#define GPS_TX_PIN GPIO6 -#define GPS_TX_AF GPIO_AF0 +// Duration before GPS fix is declared stale +#define GPS_STALEFIX_MS 60000 -#define GPS_RX_PORT GPIOB -#define GPS_RX_PIN GPIO7 -#define GPS_RX_AF GPIO_AF0 +enum gps_state +{ + GPS_STATE_ACQUIRING = 0, + GPS_STATE_FRESHFIX, + GPS_STATE_STALEFIX, + GPS_STATE_NOFIX +}; -#define GPS_ONOFF_PORT GPIOA -#define GPS_ONOFF_PIN GPIO1 -#define GPS_ONOFF GPS_ONOFF_PORT, GPS_ONOFF_PIN -*/ // Messages (REMOVEME?) #define GGA_MESSAGE #define RMC_MESSAGE @@ -63,9 +58,13 @@ char* get_gpsaltitude(void); char* get_speedKnots(void); char* get_course(void); char* get_hdop(void); +uint16_t get_hdop_int_tenths(void); char* get_sv(void); char* get_dayofmonth(void); uint8_t gps_hasfix(void); +void gps_process(void); +uint8_t gps_getstate(void); +void gps_acquirefix(void); void parse_gps_transmission(void); void XORbyteWithChecksum(uint8_t byte); diff --git a/inc/usart.h b/inc/usart.h --- a/inc/usart.h +++ b/inc/usart.h @@ -4,6 +4,7 @@ #include "stm32f0xx_hal.h" void uart_init(void); +void uart_deinit(void); UART_HandleTypeDef* uart_gethandle(void); DMA_HandleTypeDef* uart_get_txdma_handle(void); DMA_HandleTypeDef* uart_get_rxdma_handle(void); diff --git a/src/gps.c b/src/gps.c --- a/src/gps.c +++ b/src/gps.c @@ -3,6 +3,7 @@ #include "stm32f0xx_hal.h" #include "config.h" +#include "gpio.h" #include "usart.h" #include "gps.h" @@ -25,13 +26,19 @@ char tramsmissionType[7]; void gps_poweron(void) { // NOTE: pchannel - //gpio_clear(GPS_ONOFF); + HAL_GPIO_WritePin(GPS_NOTEN, 0); + uart_init(); + + // Begin DMA reception + HAL_UART_Receive_DMA(uart_gethandle(), nmeaBuffer, NMEABUFFER_SIZE); + } void gps_poweroff(void) { // NOTE: pchannel - //gpio_set(GPS_ONOFF); + uart_deinit(); + HAL_GPIO_WritePin(GPS_NOTEN, 1); } char timestamp[12]; //hhmmss.ss @@ -86,6 +93,18 @@ char* get_hdop() return hdop; } +uint16_t get_hdop_int_tenths(void) +{ + // If only one digit before decimal + if(hdop[1] == '.') + { + return (hdop[0]-0x30)*10 + (hdop[2]-0x30); + } + + // Return normal hdop + return (hdop[0]-0x30)*100 + (hdop[1]-0x30)*10 + (hdop[2]-0x30); +} + char altitude[10]; //xxxxxx.x char* get_gpsaltitude() { @@ -170,10 +189,8 @@ enum decodeState { void gps_init() { // Initialize serial port - uart_init(); + // done in poweron uart_init(); - // Begin DMA reception - HAL_UART_Receive_DMA(uart_gethandle(), nmeaBuffer, NMEABUFFER_SIZE); timestamp[0] = 0x00; latitude[0] = 0x00; @@ -184,9 +201,6 @@ void gps_init() course[0] = 0x00; dayofmonth[0] = 0x00; - gps_poweron(); - HAL_Delay(100); // Make sure GPS is awake and alive - // // Disable GLONASS mode // uint8_t disable_glonass[20] = {0xB5, 0x62, 0x06, 0x3E, 0x0C, 0x00, 0x00, 0x00, 0x20, 0x01, 0x06, 0x08, 0x0E, 0x00, 0x00, 0x00, 0x01, 0x01, 0x8F, 0xB2}; // @@ -228,8 +242,69 @@ static void setParserState(uint8_t state } +static uint8_t gps_acquiring = 0; +static uint32_t gps_lastfix_time = 0; -//// MKa GPS transmission parser START + + +void gps_process(void) +{ + // If we're trying to acquire a GPS fix + if(gps_acquiring) + { + // Process incoming bytes + parse_gps_transmission(); + + // If fix acquired + uint16_t hdop_int = get_hdop_int_tenths(); + if(hdop_int < 50 && hdop_int > 0) + { + // Set RTC to GPS time + + // Record time of fix (TODO: don't use ticks if sleeping... use RTC time) + gps_lastfix_time = HAL_GetTick(); + + // Turn off GPS module + gps_poweroff(); + + // Invalidate HDOP + hdop[0] = '9'; + + // Go to idle state + gps_acquiring = 0; + } + else + { + // if too much time has elapsed, set an error flag and go idle + //gps_acquiring = 0; + } + } +} + +void gps_acquirefix(void) +{ + gps_poweron(); + + // Wait for fix + gps_acquiring = 1; +} + + + +uint8_t gps_getstate(void) +{ + if(gps_acquiring) + return GPS_STATE_ACQUIRING; + else if(gps_lastfix_time == 0) + return GPS_STATE_NOFIX; + else if(HAL_GetTick() - gps_lastfix_time < GPS_STALEFIX_MS) + return GPS_STATE_FRESHFIX; + else + return GPS_STATE_STALEFIX; + +} + +// MKa GPS transmission parser START void parse_gps_transmission(void) { uint16_t nmeaBufferDataPosition = NMEABUFFER_SIZE - uart_get_rxdma_handle()->Instance->CNDTR; diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -85,6 +85,8 @@ void encode_wspr(void) } + + TIM_HandleTypeDef htim1; int main(void) @@ -101,8 +103,7 @@ int main(void) HAL_Delay(300); - // Turn GPS on - HAL_GPIO_WritePin(GPS_NOTEN, 0); + //gps_poweroff(); // Disable ICs HAL_GPIO_WritePin(OSC_NOTEN, 1); @@ -140,26 +141,39 @@ int main(void) uint8_t lastMinute = 0; uint16_t blink_rate = 250; + while (1) { + // TODO: Trigger this when RTC thinks its time to go if(HAL_GetTick() - last_wspr > 500) { - if(get_hdop()[0] == '9' && get_hdop()[1] == '9') - blink_rate = 250; - else - blink_rate = 100; - - volatile uint8_t minute = get_timestamp()[3] - 0x30; - - // If last minute was odd and this minute is even (transition) - if(lastMinute%2 == 1 && minute%2 == 0) + switch(gps_getstate()) { - // Wait until the first second of the minute - HAL_Delay(1000); - encode_wspr(); + 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; } - lastMinute = minute; + // EMZ TODO: this needs to trigger off of RTC minute, not GPS minute +// volatile uint8_t minute = get_timestamp()[3] - 0x30; +// +// // 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); +// encode_wspr(); +// } + +// lastMinute = minute; last_wspr = HAL_GetTick(); } @@ -170,7 +184,7 @@ int main(void) } if(HAL_GetTick() - last_gps > 10) { - parse_gps_transmission(); + gps_process(); last_gps = HAL_GetTick(); } diff --git a/src/usart.c b/src/usart.c --- a/src/usart.c +++ b/src/usart.c @@ -7,6 +7,7 @@ UART_HandleTypeDef huart1; DMA_HandleTypeDef hdma_usart1_rx; DMA_HandleTypeDef hdma_usart1_tx; +uint8_t uart_initted = 0; void uart_init(void) { @@ -73,6 +74,18 @@ void uart_init(void) //HAL_NVIC_EnableIRQ(USART1_IRQn); HAL_NVIC_DisableIRQ(USART1_IRQn); + uart_initted = 1; +} + +void uart_deinit(void) +{ + if(uart_initted == 1) + { + HAL_DMA_DeInit(&hdma_usart1_rx); + HAL_DMA_DeInit(&hdma_usart1_tx); + HAL_UART_DeInit(&huart1); + uart_initted = 0; + } } UART_HandleTypeDef* uart_gethandle(void)