Changeset - b113d7d76caf
[Not reviewed]
default
0 3 0
Ethan Zonca - 9 years ago 2016-10-09 14:19:08
ez@ethanzonca.com
Implementation of faster baud rate and new message
3 files changed with 64 insertions and 25 deletions:
0 comments (0 inline, 0 general)
src/gps.c
Show inline comments
 
@@ -31,25 +31,24 @@ 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();
 

	
 
	gps_poweron();
 
	HAL_Delay(500);
 

	
 
//	// 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};
 
@@ -203,54 +202,79 @@ void gps_update_time(uint8_t* hour, uint
 
    *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, 0x07, 0x00, 0x00, 0xFF, 0xFF};
 

	
 

	
 
    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;
 
    }
 
    request[6] = check_a;
 
    request[7] = check_b;
 

	
 
    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];
 
    for(uint8_t i=0; i<60; i++)
 
    uint8_t buf[100];
 
    for(uint8_t i=0; i<100; 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);
 
    volatile HAL_StatusTypeDef res = HAL_UART_Receive(uart_gethandle(), buf, 100, 3000);
 

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

	
 

	
 
    // Return the value if GPSfixOK is set in 'flags'
 
    if( buf[17] & 0x01 )
 
        *lock = buf[16];
 
    else
 
        *lock = 0;
 
    //volatile uint32_t gpstime_ms = (buf[6+0] << 24) | (buf[6+1] << 16) | buf[6+2] << 8) | (buf[6+3]);
 

	
 
    volatile uint8_t month = buf[6+6];
 
    volatile uint8_t day = buf[6+7];
 
    volatile uint8_t hour = buf[6+8];
 
    volatile uint8_t minute = buf[6+9];
 
    volatile uint8_t second = buf[6+10];
 
    volatile uint8_t valid = buf[6+11] & 0b1111;
 
    volatile uint8_t fixtype = buf[6+20];
 

	
 
    volatile uint8_t sats_in_solution = buf[6+23];
 

	
 
    *sats = buf[53];
 
    volatile uint32_t longitude = (buf[6+24] << 24) | (buf[6+25] << 16) | (buf[6+26] << 8) | (buf[6+27]);
 
    volatile uint32_t latitude = (buf[6+28] << 24) | (buf[6+29] << 16) | (buf[6+30] << 8) | (buf[6+31]);
 
    volatile uint32_t altitude_sealevel = (buf[6+36] << 24) | (buf[6+37] << 16) | (buf[6+38] << 8) | (buf[6+39]);
 
    volatile uint32_t groundspeed = (buf[6+60] << 24) | (buf[6+61] << 16) | (buf[6+62] << 8) | (buf[6+63]);
 
    volatile uint16_t pdop = (buf[6+76] << 8) | (buf[6+77]);
 
//
 
//    // Return the value if GPSfixOK is set in 'flags'
 
//    if( buf[17] & 0x01 )
 
//        *lock = buf[16];
 
//    else
 
//        *lock = 0;
 
    *lock = fixtype;
 

	
 
    *sats = sats_in_solution;
 
}
 

	
 
/**
 
 * 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);
src/main.c
Show inline comments
 
@@ -34,91 +34,97 @@ int main(void)
 
    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;
 
 
    uint32_t fixinfo_timer = 0;
 
    uint8_t fix_ok = 0;
 
    uint8_t numsats = 0;
 
 
    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();
 
                //enter_sleep();
 
 
                // Somehow go to another state when we get an interrupt
 
                state = SYSTEM_GPSACQ;
 
//                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(100);
 
                HAL_Delay(1000);
 
//                gps_update_position();
 
                
 
                uint8_t fix_ok = 0;
 
                uint8_t numsats = 0;
 
                // Update fix status every 2 seconds
 
                if(HAL_GetTick() - fixinfo_timer > 2000)
 
                {
 
                gps_check_lock(&fix_ok, &numsats);
 
                	fixinfo_timer = HAL_GetTick();
 
                }
 
 
                if(fix_ok)
 
                if(fix_ok > 0)
 
                {
 
                    // Disable GPS module
 
                    gps_poweroff();
 
                    //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?
 
   //             }
 
 
            } break;
 
 
            
 
            // Wait for wspr timeslot and start transmitting
 
            case SYSTEM_WSPRTX:
 
            {
 
            	blink_rate = BLINK_SLOW;
 
                // Wait for wspr countdown timer to expire and go to tx
 
//                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;
 
//                state = SYSTEM_IDLE;
 
 
            } break;
 
 
        }
 
 
 
        if(HAL_GetTick() - led_timer > blink_rate)
 
        {
 
            HAL_GPIO_TogglePin(LED_BLUE);
 
            led_timer = HAL_GetTick();
 
        }
 
src/uart.c
Show inline comments
 
@@ -30,24 +30,33 @@ void uart_init(void)
 
    huart1.Init.WordLength = UART_WORDLENGTH_8B;
 
    huart1.Init.StopBits = UART_STOPBITS_1;
 
    huart1.Init.Parity = UART_PARITY_NONE;
 
    huart1.Init.Mode = UART_MODE_TX_RX;
 
    huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 
    huart1.Init.OverSampling = UART_OVERSAMPLING_16;
 
    huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
 
    huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_RXOVERRUNDISABLE_INIT|UART_ADVFEATURE_DMADISABLEONERROR_INIT;
 
    huart1.AdvancedInit.OverrunDisable = UART_ADVFEATURE_OVERRUN_DISABLE;
 
    huart1.AdvancedInit.DMADisableonRxError = UART_ADVFEATURE_DMA_DISABLEONRXERROR;
 
    HAL_UART_Init(&huart1);
 
 
    HAL_Delay(100);
 
	uint8_t switch_baud[] = "$PUBX,41,1,0003,0001,115200,0*1E\r\n";
 
	HAL_UART_Transmit(uart_gethandle(), switch_baud, sizeof(switch_baud)/sizeof(uint8_t), 1000);
 
 
    HAL_UART_DeInit(&huart1);
 
    huart1.Init.BaudRate = 115200;
 
    HAL_UART_Init(&huart1);
 
 
 
//
 
//    __DMA1_CLK_ENABLE();
 
//
 
//  // Init UART DMA
 
//    hdma_usart1_rx.Instance = DMA1_Channel3;
 
//    hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
 
//    hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
 
//    hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
 
//    hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
 
//    hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
 
//    hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
 
//    hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
0 comments (0 inline, 0 general)