Changeset - 50cc79d449a4
[Not reviewed]
default
0 4 0
Ethan Zonca - 9 years ago 2016-10-08 23:40:53
ez@ethanzonca.com
GPS comms with ubx interface now functional!
4 files changed with 124 insertions and 52 deletions:
0 comments (0 inline, 0 general)
inc/gpio.h
Show inline comments
 
#ifndef __gpio_H
 
#define __gpio_H
 
 
#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
 
 
#define GPS_NEN_Pin GPIO_PIN_0
 
#define GPS_NEN_GPIO_Port GPIOF
 
#define GPS_NOTEN GPS_NEN_GPIO_Port , GPS_NEN_Pin
 
 
#define VBATT_SENSE_Pin GPIO_PIN_6
 
#define VBATT_SENSE_GPIO_Port GPIOA
 
 
#define LED_BLUE_Pin GPIO_PIN_0
 
#define LED_BLUE_GPIO_Port GPIOB
 
#define LED_BLUE LED_BLUE_GPIO_Port , LED_BLUE_Pin
 
 
#define TCXO_EN_Pin GPIO_PIN_8
 
#define TCXO_EN_GPIO_Port GPIOA
 
#define TCXO_EN TCXO_EN_GPIO_Port  , TCXO_EN_Pin
 
 
 
void gpio_init(void);
 
void led_blink(uint8_t n);
src/gpio.c
Show inline comments
 
#include "gpio.h"
 
 
void gpio_init(void)
 
{
 
 
  GPIO_InitTypeDef GPIO_InitStruct;
 
 
  /* GPIO Ports Clock Enable */
 
  __GPIOF_CLK_ENABLE();
 
  __GPIOA_CLK_ENABLE();
 
  __GPIOB_CLK_ENABLE();
 
 
 
  // Oscillator enable pin
 
  GPIO_InitStruct.Pin = OSC_EN_Pin;
 
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 
  HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
 
  HAL_GPIO_WritePin(GPIOF, OSC_EN_Pin, 0); // disable
 
 
 
  // GPS enable pin
 
  GPIO_InitStruct.Pin = GPS_NEN_Pin;
 
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 
  HAL_GPIO_Init(GPS_NEN_GPIO_Port, &GPIO_InitStruct);
 
 
  HAL_GPIO_WritePin(GPS_NEN_GPIO_Port, GPS_NEN_Pin, 1); // disable
 
 
  /*Configure GPIO pins : PA0 PA2 PA3 PA4 
 
                           PA5 PA7 PA15 */
 
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4 
 
                          |GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_15;
 
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 
//  /*Configure GPIO pin : PA1 */
 
//  GPIO_InitStruct.Pin = GPIO_PIN_1;
 
//  GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
 
//  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
//  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 
  /*Configure GPIO pin : PtPin */
 
  GPIO_InitStruct.Pin = LED_BLUE_Pin;
 
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 
  HAL_GPIO_Init(LED_BLUE_GPIO_Port, &GPIO_InitStruct);
 
 
  /*Configure GPIO pins : PB1 PB3 PB4 PB5 */
 
  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
 
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
 
  /*Configure GPIO pin : PtPin */
 
  GPIO_InitStruct.Pin = TCXO_EN_Pin;
 
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 
  GPIO_InitStruct.Pull = GPIO_NOPULL;
 
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
 
  HAL_GPIO_Init(TCXO_EN_GPIO_Port, &GPIO_InitStruct);
 
  HAL_GPIO_WritePin(TCXO_EN_GPIO_Port, TCXO_EN_Pin, 0); // disable
 
 
}
 
 
void led_blink(uint8_t n)
 
{
 
    for(int i = 0; i < n; i++)
 
    {
 
        HAL_GPIO_WritePin(LED_BLUE, 1);
 
        HAL_Delay(100);
 
        HAL_GPIO_WritePin(LED_BLUE, 0);
 
        HAL_Delay(100);
 
    }
 
}
src/gps.c
Show inline comments
 
@@ -18,178 +18,243 @@ typedef struct _gps_data
 

	
 
    int32_t latitude;
 
    int32_t longitude;
 
    int32_t altitude;
 

	
 
    uint8_t hour;
 
    uint8_t minute;
 
    uint8_t second;
 

	
 
} gps_data_t;
 

	
 
gps_data_t position;
 

	
 
// Private methods
 
static void gps_ubx_checksum(uint8_t* data, uint8_t len, uint8_t* cka, uint8_t* ckb);
 
static uint8_t _gps_verify_checksum(uint8_t* data, uint8_t len);
 

	
 

	
 

	
 
void gps_init()
 
{
 
    // Initialize serial port
 
   // done in poweron uart_init();
 

	
 

	
 
    // 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};
 

	
 
    //gps_sendubx(disable_glonass, 20);
 
    HAL_UART_Transmit(uart_gethandle(), disable_glonass, 20, 100);
 
	gps_poweron();
 
	HAL_Delay(500);
 

	
 
    // Enable power saving
 
    uint8_t enable_powersave[10] = {0xB5, 0x62, 0x06, 0x11, 0x02, 0x00, 0x08, 0x01, 0x22, 0x92};
 
    //gps_sendubx(enable_powersave, 10);
 
    HAL_UART_Transmit(uart_gethandle(), enable_powersave, 10, 100);
 

	
 

	
 
    // Set dynamic model 6 (<1g airborne platform)
 
    uint8_t airborne_model[] = { 0xB5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0xDC };
 
    //gps_sendubx(airborne_model, sizeof(airborne_model)/sizeof(uint8_t));
 
    HAL_UART_Transmit(uart_gethandle(), airborne_model, sizeof(airborne_model)/sizeof(uint8_t), 100);
 

	
 
//	// uart1 ubx only
 
//
 
//	uint8_t setUBXuart1 = {0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x80, 0x25,
 
//	0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9A, 0x79};
 
//	HAL_UART_Transmit(uart_gethandle(), setUBXuart1, sizeof(setUBXuart1)/sizeof(uint8_t), 100);
 
//	HAL_Delay(100);
 
//
 
//
 
//	// uart0 ubx only
 
//	uint8_t setUBXuart0 = {0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x80, 0x25,
 
//	0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x65};
 
//	HAL_UART_Transmit(uart_gethandle(), setUBXuart0, sizeof(setUBXuart0)/sizeof(uint8_t), 100);
 
//	HAL_Delay(100);
 
//
 
//
 
//	// uart1 ubx only
 
//
 
//	uint8_t setUBXuart2 = {0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x02, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x80, 0x25,
 
//	0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9B, 0x8D};
 
//	HAL_UART_Transmit(uart_gethandle(), setUBXuart2, sizeof(setUBXuart2)/sizeof(uint8_t), 100);
 
//	HAL_Delay(100);
 

	
 
   // Disable messages
 
   uint8_t setGLL[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x2B};
 
   HAL_UART_Transmit(uart_gethandle(), setGLL, sizeof(setGLL)/sizeof(uint8_t), 100);
 
   HAL_Delay(1);
 
	uint8_t setGGA[] = {0XB5, 0X62, 0X06, 0X01, 0X08, 0X00, 0XF0, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0XFF, 0X23};
 
	HAL_UART_Transmit(uart_gethandle(), setGGA, sizeof(setGGA)/sizeof(uint8_t), 100);
 
	HAL_Delay(100);
 

	
 
	uint8_t ackbuffer[10];
 
	for(uint8_t i=0; i<10; i++)
 
		ackbuffer[i] = 0xaa;
 
	HAL_UART_Receive(uart_gethandle(), ackbuffer, 10, 100);
 

	
 
   uint8_t setGSA[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x32};
 
	uint8_t setZDA[] = {0XB5, 0X62, 0X06, 0X01, 0X08, 0X00, 0XF0, 0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X07, 0X5B};
 
	HAL_UART_Transmit(uart_gethandle(), setZDA, sizeof(setZDA)/sizeof(uint8_t), 100);
 
	HAL_Delay(100);
 

	
 
	uint8_t setGLL[] = {0XB5, 0X62, 0X06, 0X01, 0X08, 0X00, 0XF0, 0X01, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X2A};
 
	HAL_UART_Transmit(uart_gethandle(), setGLL, sizeof(setGLL)/sizeof(uint8_t), 100);
 
	HAL_Delay(100);
 

	
 
	uint8_t setGSA[] = {0XB5, 0X62, 0X06, 0X01, 0X08, 0X00, 0XF0, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X01, 0X31};
 
   HAL_UART_Transmit(uart_gethandle(), setGSA, sizeof(setGSA)/sizeof(uint8_t), 100);
 
   HAL_Delay(1);
 
	HAL_Delay(100);
 

	
 
   uint8_t setGSV[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x39};
 
	uint8_t setGSV[] = {0XB5, 0X62, 0X06, 0X01, 0X08, 0X00, 0XF0, 0X03, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X02, 0X38};
 
   HAL_UART_Transmit(uart_gethandle(), setGSV, sizeof(setGSV)/sizeof(uint8_t), 100);
 
   HAL_Delay(1);
 
	HAL_Delay(100);
 

	
 
   uint8_t setRMC[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x40};
 
	uint8_t setRMC[] = {0XB5, 0X62, 0X06, 0X01, 0X08, 0X00, 0XF0, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X03, 0X3F};
 
   HAL_UART_Transmit(uart_gethandle(), setRMC, sizeof(setRMC)/sizeof(uint8_t), 100);
 
   HAL_Delay(1);
 
	HAL_Delay(100);
 

	
 
	uint8_t setVTG[] = {0XB5, 0X62, 0X06, 0X01, 0X08, 0X00, 0XF0, 0X05, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X46};
 
	HAL_UART_Transmit(uart_gethandle(), setVTG, sizeof(setRMC)/sizeof(uint8_t), 100);
 
	HAL_Delay(100);
 

	
 

	
 
   uint8_t setVTG[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x46};
 
   HAL_UART_Transmit(uart_gethandle(), setVTG, sizeof(setRMC)/sizeof(uint8_t), 100);
 
   HAL_Delay(1);
 
//    // 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};
 
//
 
//    //gps_sendubx(disable_glonass, 20);
 
//    volatile HAL_StatusTypeDef res = HAL_UART_Transmit(uart_gethandle(), disable_glonass, 20, 100);
 
//
 
//    // Enable power saving
 
//    uint8_t enable_powersave[10] = {0xB5, 0x62, 0x06, 0x11, 0x02, 0x00, 0x08, 0x01, 0x22, 0x92};
 
//    //gps_sendubx(enable_powersave, 10);
 
//    res = HAL_UART_Transmit(uart_gethandle(), enable_powersave, 10, 100);
 
//
 
//
 
//    // Set dynamic model 6 (<1g airborne platform)
 
//    uint8_t airborne_model[] = { 0xB5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0xDC };
 
//    //gps_sendubx(airborne_model, sizeof(airborne_model)/sizeof(uint8_t));
 
//    res = HAL_UART_Transmit(uart_gethandle(), airborne_model, sizeof(airborne_model)/sizeof(uint8_t), 100);
 

	
 

	
 

	
 

	
 
}
 

	
 

	
 
void gps_update_position()
 
{
 
    // Request a NAV-POSLLH message from the GPS
 
    uint8_t request[8] = {0xB5, 0x62, 0x01, 0x02, 0x00, 0x00, 0x03,
 
        0x0A};
 
    uint8_t request[8] = {0xB5, 0x62, 0x01, 0x02, 0x00, 0x00, 0x03, 0x0A};
 

	
 
    volatile uint8_t check_a = 0;
 
    volatile uint8_t check_b = 0;
 
    for(uint8_t i = 2; i<6; i++)
 
    {
 
    	check_a += request[i];
 
    	check_b += check_a;
 
    }
 

	
 

	
 
    //_gps_send_msg(request, 8);
 
    uint8_t flushed = uart_gethandle()->Instance->RDR;
 
    HAL_UART_Transmit(uart_gethandle(), request, 8, 100);
 
    volatile HAL_StatusTypeDef txres = HAL_UART_Transmit(uart_gethandle(), request, 8, 100);
 

	
 
    uint8_t buf[36];
 
    HAL_UART_Receive(uart_gethandle(), buf, 36, 100);
 
    for(uint8_t i =0; i<36; i++)
 
    	buf[i] = 0xaa;
 
    volatile HAL_StatusTypeDef result = HAL_UART_Receive(uart_gethandle(), buf, 36, 1000);
 

	
 
    //for(uint8_t i = 0; i < 36; i++)
 
    //    buf[i] = _gps_get_byte();
 

	
 
//    // Verify the sync and header bits
 
//    if( buf[0] != 0xB5 || buf[1] != 0x62 )
 
//        led_set(LED_RED, 1);
 
//    if( buf[2] != 0x01 || buf[3] != 0x02 )
 
//        led_set(LED_RED, 1);
 

	
 
    // 4 bytes of longitude (1e-7)
 
    position.longitude = (int32_t)buf[10] | (int32_t)buf[11] << 8 |
 
        (int32_t)buf[12] << 16 | (int32_t)buf[13] << 24;
 

	
 
    // 4 bytes of latitude (1e-7)
 
    position.latitude = (int32_t)buf[14] | (int32_t)buf[15] << 8 |
 
        (int32_t)buf[16] << 16 | (int32_t)buf[17] << 24;
 

	
 
    // 4 bytes of altitude above MSL (mm)
 
    position.altitude = (int32_t)buf[22] | (int32_t)buf[23] << 8 |
 
        (int32_t)buf[24] << 16 | (int32_t)buf[25] << 24;
 

	
 
    if( !_gps_verify_checksum(&buf[2], 32) )
 
        led_blink(2);
 
}
 

	
 

	
 
void gps_update_time(uint8_t* hour, uint8_t* minute, uint8_t* second)
 
{
 
    // Send a NAV-TIMEUTC message to the receiver
 
    uint8_t request[8] = {0xB5, 0x62, 0x01, 0x21, 0x00, 0x00, 0x22, 0x67};
 

	
 
    volatile uint8_t check_a = 0;
 
    volatile uint8_t check_b = 0;
 
    for(uint8_t i = 2; i<6; i++)
 
    {
 
    	check_a += request[i];
 
    	check_b += check_a;
 
    }
 

	
 

	
 
    uint8_t flushed = uart_gethandle()->Instance->RDR;
 
    volatile HAL_StatusTypeDef res = HAL_UART_Transmit(uart_gethandle(), request, 8, 100);
 

	
 
    // Get the message back from the GPS
 
    uint8_t buf[28];
 
    res = HAL_UART_Receive(uart_gethandle(), buf, 28, 500);
 

	
 
//    // Verify the sync and header bits
 
//    if( buf[0] != 0xB5 || buf[1] != 0x62 )
 
//        led_set(LED_RED, 1);
 
//    if( buf[2] != 0x01 || buf[3] != 0x21 )
 
//        led_set(LED_RED, 1);
 

	
 
    *hour = buf[22];
 
    *minute = buf[23];
 
    *second = buf[24];
 

	
 
//    if( !_gps_verify_checksum(&buf[2], 24) ) led_set(LED_RED, 1);
 
}
 

	
 
/**
 
 * Check the navigation status to determine the quality of the
 
 * fix currently held by the receiver with a NAV-STATUS message.
 
 */
 
void gps_check_lock(uint8_t* lock, uint8_t* sats)
 
{
 
    // Construct the request to the GPS
 
    uint8_t request[8] = {0xB5, 0x62, 0x01, 0x06, 0x00, 0x00,
 
        0x07, 0x16};
 
    uint8_t request[8] = {0xB5, 0x62, 0x01, 0x06, 0x00, 0x00, 0x07, 0x16};
 
    uint8_t flushed = uart_gethandle()->Instance->RDR;
 
    HAL_UART_Transmit(uart_gethandle(), request, 8, 100);
 

	
 

	
 

	
 
    // Get the message back from the GPS
 
    uint8_t buf[60];
 
    HAL_UART_Receive(uart_gethandle(), buf, 60, 100);
 
    for(uint8_t i=0; i<60; i++)
 
    	buf[i] = 0xaa;
 
    volatile HAL_StatusTypeDef res = HAL_UART_Receive(uart_gethandle(), buf, 60, 3000);
 

	
 
    // Verify the sync and header bits
 
//    if( buf[0] != 0xB5 || buf[1] != 0x62 )
 
//        led_set(LED_RED, 1);
 
//    if( buf[2] != 0x01 || buf[3] != 0x06 )
 
//        led_set(LED_RED, 1);
 

	
 
    // Check 60 bytes minus SYNC and CHECKSUM (4 bytes)
 
//    if( !_gps_verify_checksum(&buf[2], 56) ) led_set(LED_RED, 1);
 
    if( !_gps_verify_checksum(&buf[2], 56) )
 
        led_blink(2);
 

	
 

	
 
    // Return the value if GPSfixOK is set in 'flags'
 
    if( buf[17] & 0x01 )
 
        *lock = buf[16];
 
    else
 
        *lock = 0;
 

	
 
    *sats = buf[53];
 
}
 

	
 
/**
 
 * Verify that the uBlox 6 GPS receiver is set to the <1g airborne
 
 * navigaion mode.
 
 */
 
uint8_t gps_check_nav(void)
 
{
 
    uint8_t request[8] = {0xB5, 0x62, 0x06, 0x24, 0x00, 0x00,
 
        0x2A, 0x84};
 
    uint8_t flushed = uart_gethandle()->Instance->RDR;
 
    HAL_UART_Transmit(uart_gethandle(), request, 8, 100);
 

	
 
    // Get the message back from the GPS
 
    uint8_t buf[44];
 
    HAL_UART_Receive(uart_gethandle(), buf, 44, 100);
src/main.c
Show inline comments
 
//
 
// WSPRHAB: Minimal high-altitude balloon tracker with WSPR telemetry
 
//
 
 
#include "stm32f0xx_hal.h"
 
#include "adc.h"
 
#include "system.h"
 
#include "i2c.h"
 
#include "uart.h"
 
#include "gpio.h"
 
#include "wspr.h"
 
#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();
 
 
    sysclk_init();
 
    gpio_init();
 
    adc_init();
 
    i2c_init();
 
    gps_init();
 
    wspr_init();
 
 
    uint32_t led_timer = HAL_GetTick();
 
 
    led_blink(4);
 
 
    uint16_t blink_rate = BLINK_FAST;
 
    uint8_t state = SYSTEM_GPSACQ;
 
 
 
    while (1)
 
    {
 
        switch(state)
 
        {
 
 
            // Idling: sleep and wait for RTC timeslot trigger
 
            case SYSTEM_IDLE:
 
            {
 
                blink_rate = BLINK_SLOW;
 
                // Wait for RTC wakeup interrupt
 
                wfi();
 
                //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:
 
            {
 
                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();
 
//                gps_poweron();
 
//                HAL_Delay(100);
 
                HAL_Delay(1000);
 
//                gps_update_position();
 
                
 
                uint8_t fix_ok = 0;
 
                uint8_t numsats = 0;
 
                gps_check_lock(&fix_ok, &numsats);
 
 
                if(fix_ok)
 
                {
 
                    // Disable GPS module
 
                    gps_poweroff();
 
 
                    // TODO: Set RTC from GPS time
 
 
                    // TODO: Set RTC for countdown to next transmission timeslot!
 
 
                    // 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?
 
                }
 
//                else if(fix_timeout)
 
 //               {
 
  //                  // Flash error code and go to idle probably? or just try forever?
 
   //             }
 
 
            } break;
 
 
            
 
            // 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();
 
                }
 
//                if(timeout_expired)
 
//                {
 
//                    wspr_transmit();
 
//                }
 
 
                // 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();
 
        }
 
 
    }
 
}
 
 
 
 
0 comments (0 inline, 0 general)