ublox_device.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // This is a personal academic project. Dear PVS-Studio, please check it.
  2. // PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
  3. #pragma once
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. #include <furi.h>
  8. #define UBLOX_I2C_ADDRESS 0x42
  9. #define I2C_TIMEOUT_MS 20
  10. #define UBX_NAV_CLASS 0x01
  11. #define UBX_RXM_CLASS 0x02
  12. #define UBX_INF_CLASS 0x04
  13. #define UBX_ACK_CLASS 0x05
  14. #define UBX_CFG_CLASS 0x06
  15. #define UBX_UPD_CLASS 0x09
  16. #define UBX_MON_CLASS 0x0A
  17. #define UBX_AID_CLASS 0x0B
  18. #define UBX_TIM_CLASS 0x0D
  19. #define UBX_ESF_CLASS 0x10
  20. #define UBX_MGA_CLASS 0x13
  21. #define UBX_LOG_CLASS 0x21
  22. #define UBX_SEC_CLASS 0x27
  23. #define UBX_HNR_CLASS 0x28
  24. // The following are respective to a class.
  25. // ACK_CLASS
  26. #define UBX_ACK_ACK_MESSAGE 0x01
  27. // ACK and NAK have the same length
  28. #define UBX_ACK_ACK_MESSAGE_LENGTH (8 + 2)
  29. // NAV_CLASS
  30. #define UBX_NAV_PVT_MESSAGE 0x07
  31. #define UBX_NAV_PVT_MESSAGE_LENGTH (8 + 92)
  32. #define UBX_NAV_SAT_MESSAGE 0x35
  33. #define UBX_NAV_ODO_MESSAGE 0x09
  34. #define UBX_NAV_ODO_MESSAGE_LENGTH (8 + 20)
  35. #define UBX_NAV_RESETODO_MESSAGE 0x10
  36. #define UBX_NAV_TIMEUTC_MESSAGE 0x21
  37. #define UBX_NAV_TIMEUTC_MESSAGE_LENGTH (8 + 20)
  38. // CFG_CLASS
  39. #define UBX_CFG_PMS_MESSAGE 0x86
  40. #define UBX_CFG_PMS_MESSAGE_LENGTH (8 + 8)
  41. #define UBX_CFG_ODO_MESSAGE 0x1e
  42. #define UBX_CFG_ODO_MESSAGE_LENGTH (8 + 20)
  43. #define UBX_CFG_NAV5_MESSAGE 0x24
  44. #define UBX_CFG_NAV5_MESSAGE_LENGTH (8 + 36)
  45. /** A frame is a message sent to the GPS. This app supports u-blox 8 devices. */
  46. typedef struct UbloxFrame {
  47. uint8_t sync1; // always 0xb5, or ISO-8859.1 for 'µ'
  48. uint8_t sync2; // always 0x62, or ASCII for 'b'
  49. // class and id together indicate what kind of message is being sent.
  50. uint8_t class; // message class
  51. uint8_t id; // message id
  52. uint16_t len; // length of the payload only, 2 bytes, little-endian
  53. uint8_t* payload; // any number of bytes
  54. // 2 bytes of checksum
  55. uint8_t ck_a;
  56. uint8_t ck_b;
  57. // metadata
  58. bool valid;
  59. } UbloxFrame;
  60. typedef struct UbloxMessage {
  61. uint8_t* message;
  62. uint8_t length;
  63. } UbloxMessage;
  64. // Field names taken directly from u-blox protocol manual.
  65. typedef struct Ublox_NAV_PVT_Message {
  66. uint32_t iTOW;
  67. uint16_t year;
  68. uint8_t month;
  69. uint8_t day;
  70. uint8_t hour;
  71. uint8_t min;
  72. uint8_t sec;
  73. uint8_t valid;
  74. uint32_t tAcc;
  75. int32_t nano;
  76. uint8_t fixType;
  77. uint8_t flags;
  78. uint8_t flags2;
  79. uint8_t numSV;
  80. int32_t lon;
  81. int32_t lat;
  82. int32_t height;
  83. int32_t hMSL;
  84. uint32_t hAcc;
  85. uint32_t vAcc;
  86. int32_t velN;
  87. int32_t velE;
  88. int32_t velD;
  89. int32_t gSpeed;
  90. int32_t headMot;
  91. uint32_t sAcc;
  92. uint32_t headAcc;
  93. uint16_t pDOP;
  94. uint16_t flags3;
  95. uint8_t reserved1;
  96. uint8_t reserved2;
  97. uint8_t reserved3;
  98. uint8_t reserved4;
  99. int32_t headVeh;
  100. int16_t magDec;
  101. uint16_t magAcc;
  102. } Ublox_NAV_PVT_Message;
  103. typedef struct Ublox_NAV_ODO_Message {
  104. uint8_t version;
  105. uint8_t reserved1;
  106. uint8_t reserved2;
  107. uint8_t reserved3;
  108. uint32_t iTOW;
  109. uint32_t distance;
  110. uint32_t totalDistance;
  111. uint32_t distanceStd;
  112. } Ublox_NAV_ODO_Message;
  113. typedef struct Ublox_NAV_TIMEUTC_Message {
  114. uint32_t iTOW;
  115. uint32_t tAcc;
  116. int32_t nano;
  117. uint16_t year;
  118. uint8_t month;
  119. uint8_t day;
  120. uint8_t hour;
  121. uint8_t min;
  122. uint8_t sec;
  123. uint8_t valid;
  124. } Ublox_NAV_TIMEUTC_Message;
  125. /** For a given UbloxFrame, populate the sync bytes, calculate the
  126. * checksum bytes (storing them in `frame`), allocate a uint8_t array,
  127. * and fill it with the contents of the frame in an order ready to
  128. * send to the GPS. */
  129. UbloxMessage* ublox_frame_to_bytes(UbloxFrame* frame);
  130. /** For an array of uint8_ts, convert them to a frame. Returns NULL if
  131. something goes wrong, either with invalid data or malloc errors.*/
  132. UbloxFrame* ublox_bytes_to_frame(UbloxMessage* message);
  133. void ublox_message_free(UbloxMessage* message);
  134. void ublox_frame_free(UbloxFrame* frame);