bexus_gps.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "bexus_gps.h"
  2. #define PMTK_SET_NMEA_UPDATE_5HZ "$PMTK220,200*2C"
  3. #define PMTK_API_SET_FIX_CTL_5HZ "$PMTK300,200,0,0,0,0*2F"
  4. #define PMTK_SET_BAUD_57600 "$PMTK251,57600*2C"
  5. #define PMTK_SET_NMEA_OUTPUT_RMCGGA "$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28"
  6. const uint8_t ubxRate5Hz[] = {0x00,200,0x00,0x01,0x00,0x01};
  7. /*
  8. const char disableRMC[] = "PUBX,40,RMC,0,0,0,0,0,0";
  9. const char disableGLL[] = "PUBX,40,GLL,0,0,0,0,0,0";
  10. const char disableGSV[] = "PUBX,40,GSV,0,0,0,0,0,0";
  11. const char disableGSA[] = "PUBX,40,GSA,0,0,0,0,0,0";
  12. const char disableGGA[] = "PUBX,40,GGA,0,0,0,0,0,0";
  13. const char disableVTG[] = "PUBX,40,VTG,0,0,0,0,0,0";
  14. const char disableZDA[] = "PUBX,40,ZDA,0,0,0,0,0,0";
  15. const char enableRMC[] = "PUBX,40,RMC,0,1,0,0,0,0";
  16. const char enableGLL[] = "PUBX,40,GLL,0,1,0,0,0,0";
  17. const char enableGSV[] = "PUBX,40,GSV,0,1,0,0,0,0";
  18. const char enableGSA[] = "PUBX,40,GSA,0,1,0,0,0,0";
  19. const char enableGGA[] = "PUBX,40,GGA,0,1,0,0,0,0";
  20. const char enableVTG[] = "PUBX,40,VTG,0,1,0,0,0,0";
  21. const char enableZDA[] = "PUBX,40,ZDA,0,1,0,0,0,0";
  22. const char baud9600 [] = "PUBX,41,1,3,3,9600,0";
  23. const char baud38400 [] = "PUBX,41,1,3,3,38400,0";
  24. const char baud57600 [] = "PUBX,41,1,3,3,57600,0";
  25. */
  26. const char baud115200[] = "PUBX,41,1,3,3,115200,0";
  27. uint8_t aRxBuffer[1];
  28. volatile bool newData = false;
  29. uint32_t GPS_RX_ISR_FLAG = 1;
  30. unsigned long chars;
  31. unsigned short sentences, failed;
  32. float flat, flon;
  33. unsigned long age;
  34. uint32_t count = 0;
  35. int year;
  36. uint8_t month, day, hour, minute, second, hundredths;
  37. extern DMA_HandleTypeDef hdma_usart2_rx;
  38. uint8_t sendConfigToGps(uint8_t cls, uint8_t id, uint16_t len, uint8_t payload[]);
  39. void Gps_Init() {
  40. //HAL_UART_Transmit(&huart2, (uint8_t *)aRxBuffer, 1,0xFFFF);
  41. /*
  42. HAL_UART_Transmit(&huart2, (uint8_t*)PMTK_SET_NMEA_UPDATE_5HZ, sizeof(PMTK_SET_NMEA_UPDATE_5HZ), 100);
  43. HAL_Delay(50);
  44. HAL_UART_Transmit(&huart2, (uint8_t*)PMTK_API_SET_FIX_CTL_5HZ, sizeof(PMTK_API_SET_FIX_CTL_5HZ), 100);
  45. HAL_Delay(50);
  46. HAL_UART_Transmit(&huart2, (uint8_t*)PMTK_SET_NMEA_OUTPUT_RMCGGA, sizeof(PMTK_SET_NMEA_OUTPUT_RMCGGA), 100);
  47. HAL_Delay(50);
  48. HAL_UART_Transmit(&huart2, (uint8_t*)PMTK_SET_BAUD_57600, sizeof(PMTK_SET_BAUD_57600), 100);
  49. HAL_Delay(50);
  50. */
  51. //sendConfigToGps(6, 8, 6, (uint8_t*)ubxRate5Hz);
  52. // while(millis()<250);
  53. //HAL_Delay(250);
  54. //HAL_UART_Transmit(&huart2, (uint8_t*)baud115200, sizeof(baud115200), 100);
  55. HAL_UART_DeInit(&huart2);
  56. huart2.Init.BaudRate = 9600; // 57600
  57. HAL_UART_Init(&huart2);
  58. HAL_UART_MspInit(&huart2);
  59. //__HAL_UART_FLUSH_DRREGISTER(&huart2);
  60. HAL_UART_Receive_DMA(&huart2, aRxBuffer, 1);
  61. }
  62. int Gps_Receive() {
  63. count++;
  64. if (newData) {
  65. f_get_position(&flat, &flon, &age);
  66. Dataset_Master[buff_sensor_data_index].latitude = flat;
  67. Dataset_Master[buff_sensor_data_index].longitude = flon;
  68. Dataset_Master[buff_sensor_data_index].hdop = _hdop;
  69. Dataset_Master[buff_sensor_data_index].altitude = f_altitude();
  70. crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  71. Dataset_Master[buff_sensor_data_index].time =
  72. hundredths + (
  73. second + (
  74. minute + (
  75. hour * 24UL) * 60UL) * 60UL) * 1000UL;
  76. newData = false;
  77. }
  78. stats(&chars, &sentences, &failed);
  79. return HAL_OK;
  80. }
  81. uint8_t sendConfigToGps(uint8_t cls, uint8_t id, uint16_t len, uint8_t payload[]) {
  82. uint8_t cfg_msg[len + 8];
  83. cfg_msg[0] = 0xb5;
  84. cfg_msg[1] = 0x62;
  85. cfg_msg[2] = cls;
  86. cfg_msg[3] = id;
  87. cfg_msg[4] = len;
  88. cfg_msg[5] = len>>8;
  89. memcpy(&cfg_msg[6], payload, len);
  90. uint8_t CK_A = len+6;
  91. uint8_t CK_B = len+7;
  92. for(int i=2; i<CK_A; i++) {
  93. cfg_msg[CK_A] = cfg_msg[CK_A] + cfg_msg[i];
  94. cfg_msg[CK_B] = cfg_msg[CK_B] + cfg_msg[CK_A];
  95. }
  96. HAL_UART_Transmit(&huart2, cfg_msg, len+8, 5000);
  97. uint8_t ack[4];
  98. /*
  99. do {
  100. HAL_UART_Receive(&huart2, ack, 1, 5000);
  101. } while (ack[0] != 0xb5);
  102. */
  103. HAL_UART_Receive(&huart2, ack, 4, 5000); //ACK: B5 62 05 01 NAK: B5 62 05 00
  104. return ack[3];
  105. }