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;