ublox.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "ublox.h"
  2. //remember to enable uart interrupts, set 9600boaud and change HAL_UART_Receive_IT(&GPS_Uart_Inst,UBX_CFG_MSG,36); and extern UART_HandleTypeDef GPS_Uart_Inst; in ublox.h if using different uart
  3. //up untill ublox fixes, it returns 0 in all fields.
  4. volatile uint8_t GPS_data_buff[150]; // 136 exact length
  5. //remember to enable uart interrupts, set 9600boaud and change HAL_UART_Receive_IT(&GPS_Uart_Inst,UBX_CFG_MSG,36); and extern UART_HandleTypeDef GPS_Uart_Inst; in ublox.h if using different uart
  6. //up untill ublox fixes, it returns 0 in all fields.
  7. volatile uint8_t GPS_data_buff[150]; // 136 exact length
  8. void UBLOX_init()
  9. {
  10. HAL_Delay(1000);
  11. uint8_t UBX_CFG_MSG[] = {0xb5,0x62,0x06,0x01,0x08,0x00, 0xf0,0x00, 0x01,0x00,0x01,0x01,0x01,0x01, 0x04,0x33};
  12. uint8_t UBX_NAV_POSLLH[] = {0xb5,0x62,0x06,0x01,0x08,0x00, 0x01,0x02, 0x00,0x01,0x00,0x00,0x00,0x00, 0x13,0xbe};
  13. uint8_t UBX_NAV_PVT[] = {0xb5,0x62,0x06,0x01,0x08,0x00, 0x01,0x07, 0x00,0x01,0x00,0x00,0x00,0x00, 0x18,0xE1};
  14. // disable MNEMEA messages one by one by setting update rate to 0 in USART interface
  15. HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // GGA
  16. UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x3a;
  17. HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // GLL
  18. UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x41;
  19. HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // GSA
  20. UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x48;
  21. HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // GSV
  22. UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x4f;
  23. HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // RMC
  24. UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x56;
  25. HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // VTG
  26. HAL_UART_Transmit(&GPS_Uart_Inst,UBX_NAV_POSLLH,16,20); ///UBX-NAV-POSLLH
  27. HAL_UART_Transmit(&GPS_Uart_Inst,UBX_NAV_PVT,16,20); ///UBX-NAV-PVT
  28. // CONFIG
  29. HAL_UART_Transmit(&GPS_Uart_Inst,(uint8_t[]){0xb5,0x62,0x06,0x24,0x24,0x00,0xff,0xff,0x08,0x03,0x00,0x00,0x00,0x00,0x10,0x27,0x00,0x00,0x05,0x00,0xfa,0x00,0xfa,0x00,0x64,0x00,0x2c,0x01,0x00,0x3c,0x00,0x00,0x00,0x00,0xc8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x6c},44,55);
  30. }
  31. void UBLOX_get_data_from_buff(GPS_Data_t * GPS_Data_pointer)
  32. {
  33. GPS_Data_pointer->year = ((GPS_data_buff[5+6] << 8)|(GPS_data_buff[4+6]));
  34. GPS_Data_pointer->month = GPS_data_buff[6+6];
  35. GPS_Data_pointer->day = GPS_data_buff[7+6];
  36. GPS_Data_pointer->hour = GPS_data_buff[8+6];
  37. GPS_Data_pointer->min = GPS_data_buff[9+6];
  38. GPS_Data_pointer->sec = GPS_data_buff[10+6];
  39. GPS_Data_pointer->num_of_sat = GPS_data_buff[23+6];
  40. GPS_Data_pointer->longitude = ((GPS_data_buff[100+6+7] << 24)|(GPS_data_buff[100+6+6] << 16)|(GPS_data_buff[100+6+5] << 8)|(GPS_data_buff[100+6+4]));
  41. GPS_Data_pointer->latitude = ((GPS_data_buff[100+6+11] << 24)|(GPS_data_buff[100+6+10] << 16)|(GPS_data_buff[100+6+9] << 8)|(GPS_data_buff[100+6+8]));
  42. GPS_Data_pointer->height_sea_lvl = ((GPS_data_buff[100+6+19] << 24)|(GPS_data_buff[100+6+18] << 16)|(GPS_data_buff[100+6+17] << 8)|(GPS_data_buff[100+6+16]));
  43. printf("%d %d %d \r\n",GPS_Data_pointer->longitude,GPS_Data_pointer->latitude,GPS_Data_pointer->height_sea_lvl);
  44. }
  45. void UBLOX_receive(void)
  46. {
  47. //printf("dupa1\r\n");
  48. //HAL_UART_Receive(&GPS_Uart_Inst,GPS_data_buff,1,1);
  49. HAL_UART_Receive_DMA(&GPS_Uart_Inst,GPS_data_buff,136);
  50. }
  51. /*void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) // interupt on rising edge
  52. {
  53. if(GPIO_Pin == TIMEPULSE_Pin)
  54. {
  55. UBLOX_receive();
  56. printf("dupa\r\n");
  57. }
  58. }*/
  59. //https://www.u-blox.com/sites/default/files/products/documents/u-blox8-M8_ReceiverDescrProtSpec_%28UBX-13003221%29_Public.pdf#page=370&zoom=100,0,0
  60. //str 371 -> num of sat
  61. //time hh:mm:ss