ublox.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // Created by SQ5RWU on 2016-12-27.
  3. //
  4. #ifndef RS41HUP_UBLOX_H
  5. #define RS41HUP_UBLOX_H
  6. #include <stdint.h>
  7. typedef struct
  8. {
  9. int32_t lat_raw;
  10. int32_t lon_raw;
  11. int32_t alt_raw;
  12. int32_t speed_raw;
  13. uint8_t sats_raw;
  14. uint8_t seconds;
  15. uint8_t minutes;
  16. uint8_t hours;
  17. uint16_t year;
  18. uint8_t month;
  19. uint8_t day;
  20. uint8_t fix;
  21. uint16_t ok_packets;
  22. uint16_t bad_packets;
  23. } GPSEntry;
  24. typedef struct __attribute__((packed))
  25. {
  26. uint8_t sc1; // 0xB5
  27. uint8_t sc2; // 0x62
  28. uint8_t messageClass;
  29. uint8_t messageId;
  30. uint16_t payloadSize;
  31. } uBloxHeader;
  32. typedef struct
  33. {
  34. uint8_t ck_a;
  35. uint8_t ck_b;
  36. } uBloxChecksum;
  37. typedef struct
  38. {
  39. uint32_t iTOW; // GPS time of week of the navigation epoch. [- ms]
  40. uint16_t year; // Year (UTC) [- y]
  41. uint8_t month; // Month, range 1..12 (UTC) [- month]
  42. uint8_t day; // Day of month, range 1..31 (UTC) [- d]
  43. uint8_t hour; // Hour of day, range 0..23 (UTC) [- h]
  44. uint8_t min; // Minute of hour, range 0..59 (UTC) [- min]
  45. uint8_t sec; // Seconds of minute, range 0..60 (UTC) [- s]
  46. uint8_t valid; // Validity flags (see graphic below) [- -]
  47. uint32_t tAcc; // Time accuracy estimate (UTC) [- ns]
  48. int32_t nano; // Fraction of second, range -1e9 .. 1e9 (UTC) [- ns]
  49. uint8_t fixType; // GNSSfix Type: [- -]
  50. uint8_t flags; // Fix status flags (see graphic below) [- -]
  51. uint8_t flags2; // Additional flags (see graphic below) [- -]
  52. uint8_t numSV; // Number of satellites used in Nav Solution [- -]
  53. int32_t lon; // Longitude [1e-7 deg]
  54. int32_t lat; // Latitude [1e-7 deg]
  55. int32_t height; // Height above ellipsoid [- mm]
  56. int32_t hMSL; // Height above mean sea level [- mm]
  57. uint32_t hAcc; // Horizontal accuracy estimate [- mm]
  58. uint32_t vAcc; // Vertical accuracy estimate [- mm]
  59. int32_t velN; // NED north velocity [- mm/s]
  60. int32_t velE; // NED east velocity [- mm/s]
  61. int32_t velD; // NED down velocity [- mm/s]
  62. int32_t gSpeed; // Ground Speed (2-D) [- mm/s]
  63. int32_t headMot; // Heading of motion (2-D) [1e-5 deg]
  64. uint32_t sAcc; // Speed accuracy estimate [- mm/s]
  65. uint32_t headAcc; // Heading accuracy estimate (both motion and vehicle) [1e-5 deg]
  66. uint16_t pDOP; // Position DOP [0.01 -]
  67. uint8_t reserved1[6]; // Reserved [- -]
  68. int32_t headVeh; // Heading of vehicle (2-D) [1e-5 deg]
  69. uint8_t reserved2[4]; // Reserved [- -]
  70. } uBloxNAVPVTPayload;
  71. typedef struct
  72. {
  73. uint32_t iTOW; // GPS Millisecond Time of Week [- ms]
  74. int32_t lon; // Longitude [1e-7 deg]
  75. int32_t lat; // Latitude [1e-7 deg]
  76. int32_t height; // Height above Ellipsoid [- mm]
  77. int32_t hMSL; // Height above mean sea level [- mm]
  78. uint32_t hAcc; // Horizontal Accuracy Estimate [- mm]
  79. uint32_t vAcc; // Vertical Accuracy Estimate [- mm]
  80. } uBloxNAVPOSLLHPayload;
  81. typedef struct
  82. {
  83. uint32_t iTOW; // GPS Millisecond Time of Week [- ms]
  84. int32_t fTOW; // Fractional Nanoseconds remainder of rounded ms above, range -500000 .. 500000 [- ns]
  85. int16_t week; // GPS week (GPS time) [- -]
  86. uint8_t gpsFix; // GPSfix Type, range 0..5 0x00 = No Fix 0x01 = Dead Reckoning only 0x02 = 2D-Fix 0x03 = 3D-Fix 0x04 = GPS + dead reckoning combined 0x05 = Time only fix 0x06..0xff: reserved [- -]
  87. uint8_t flags; // Fix Status Flags (see graphic below) [- -]
  88. int32_t ecefX; // ECEF X coordinate [- cm]
  89. int32_t ecefY; // ECEF Y coordinate [- cm]
  90. int32_t ecefZ; // ECEF Z coordinate [- cm]
  91. uint32_t pAcc; // 3D Position Accuracy Estimate [- cm]
  92. int32_t ecefVX; // ECEF X velocity [- cm/s]
  93. int32_t ecefVY; // ECEF Y velocity [- cm/s]
  94. int32_t ecefVZ; // ECEF Z velocity [- cm/s]
  95. uint32_t sAcc; // Speed Accuracy Estimate [- cm/s]
  96. uint16_t pDOP; // Position DOP [0.01 -]
  97. uint8_t reserved1; // Reserved [- -]
  98. uint8_t numSV; // Number of SVs used in Nav Solution [- -]
  99. uint32_t reserved2; // Reserved [- -]
  100. } uBloxNAVSOLPayload;
  101. typedef struct
  102. {
  103. uint32_t iTOW; // GPS Millisecond Time of Week [- ms]
  104. uint32_t tAcc; // Time Accuracy Estimate [- ns]
  105. int32_t nano; // Nanoseconds of second, range -1e9 .. 1e9 (UTC) [- ns]
  106. uint16_t year; // Year, range 1999..2099 (UTC) [- y]
  107. uint8_t month; // Month, range 1..12 (UTC) [- month]
  108. uint8_t day; // Day of Month, range 1..31 (UTC) [- d]
  109. uint8_t hour; // Hour of Day, range 0..23 (UTC) [- h]
  110. uint8_t min; // Minute of Hour, range 0..59 (UTC) [- min]
  111. uint8_t sec; // Seconds of Minute, range 0..59 (UTC) [- s]
  112. uint8_t valid; // Validity Flags (see graphic below) [- -]
  113. } uBloxNAVTIMEUTCPayload;
  114. typedef struct
  115. {
  116. uint8_t portID; // Port Identifier Number (see Serial [- -]
  117. uint8_t reserved1; // Reserved [- -]
  118. uint16_t txReady; // TX ready PIN configuration [- -]
  119. uint32_t mode; // A bit mask describing the UART mode [- -]
  120. uint32_t baudRate; // Baud rate in bits/second [- Bits/s]
  121. uint16_t inProtoMask; // A mask describing which input protocols are active. [- -]
  122. uint16_t outProtoMask;// A mask describing which output protocols are active. [- -]
  123. uint16_t flags; // Flags bit mask (see graphic below) [- -]
  124. uint8_t reserved2[2]; // Reserved [- -]
  125. } uBloxCFGPRTPayload;
  126. typedef struct
  127. {
  128. uint8_t clsID; // Message Class [- -]
  129. uint8_t msgID; // Message Identifier [- -]
  130. uint8_t ck_a;
  131. uint8_t ck_b;
  132. } uBloxACKACKayload;
  133. typedef struct
  134. {
  135. uint8_t msgClass; // Message Class [- -]
  136. uint8_t msgID; // Message Identifier [- -]
  137. uint8_t rate; // Send rate on current Port [- -]
  138. } uBloxCFGMSGPayload;
  139. typedef struct
  140. {
  141. uint16_t navBbrMask; // BBR Sections to clear. The following Special Sets apply:
  142. // - 0x0000 Hotstart
  143. // - 0x0001 Warmstart
  144. // - 0xFFFF Coldstart [- -]
  145. uint8_t resetMode; // Reset Type
  146. // - 0x00 - Hardware reset (Watchdog) immediately
  147. // - 0x01 - Controlled Software reset
  148. // - 0x02 - Controlled Software reset (GPS only)
  149. // - 0x04 - Hardware reset (Watchdog) after shutdown (>=FW6.0)
  150. // - 0x08 - Controlled GPS stop
  151. // - 0x09 - Controlled GPS start [- -]
  152. // - 0x09 - Controlled GPS start [- -]
  153. uint8_t reserved1; // Reserved [- -]
  154. } uBloxCFGRSTPayload;
  155. typedef struct
  156. {
  157. uint16_t mask; // Parameters Bitmask. Only the masked parameters will be applied. (see graphic below) [- -]
  158. uint8_t dynModel; // Dynamic Platform model:
  159. // - 0 Portable - 2 Stationary - 3 Pedestrian - 4 Automotive - 5 Sea
  160. // - 6 Airborne with <1g Acceleration - 7 Airborne with <2g Acceleration - 8 Airborne with <4g Acceleration
  161. uint8_t fixMode; // Position Fixing Mode. - 1: 2D only - 2: 3D only - 3: Auto 2D/3D [- -]
  162. int32_t fixedAlt; // Fixed altitude (mean sea level) for 2D fix mode. [0.01 m]
  163. uint32_t fixedAltVar; // Fixed altitude variance for 2D mode. [0.0001 m^2]
  164. int8_t minElev; // Minimum Elevation for a GNSS satellite to be used in NAV [- deg]
  165. uint8_t drLimit; // Maximum time to perform dead reckoning (linear extrapolation) in case of GPS signal loss [- s]
  166. uint16_t pDop; // Position DOP Mask to use [0.1 -]
  167. uint16_t tDop; // Time DOP Mask to use [0.1 -]
  168. uint16_t pAcc; // Position Accuracy Mask [- m]
  169. uint16_t tAcc; // Time Accuracy Mask [- m]
  170. uint8_t staticHoldThresh;// Static hold threshold [- cm/s]
  171. uint8_t dgpsTimeOut; // DGPS timeout, firmware 7 and newer only [- s]
  172. uint32_t reserved2; // Always set to zero [- -]
  173. uint32_t reserved3; // Always set to zero [- -]
  174. uint32_t reserved4; // Always set to zero [- -]
  175. } uBloxCFGNAV5Payload;
  176. typedef struct
  177. {
  178. uint8_t reserved1; // Always set to 8 [- -]
  179. uint8_t lpMode; // Low Power Mode 0: Max. performance mode 1: Power Save Mode (>= FW 6.00 only)
  180. // - 2-3: reserved 4: Eco mode 5-255: reserved
  181. } uBloxCFGRXMPayload;
  182. typedef union
  183. {
  184. uBloxNAVPVTPayload navpvt;
  185. uBloxCFGPRTPayload cfgprt;
  186. uBloxCFGMSGPayload cfgmsg;
  187. uBloxCFGNAV5Payload cfgnav5;
  188. uBloxNAVPOSLLHPayload navposllh;
  189. uBloxNAVSOLPayload navsol;
  190. uBloxNAVTIMEUTCPayload navtimeutc;
  191. uBloxACKACKayload ackack;
  192. uBloxCFGRSTPayload cfgrst;
  193. uBloxCFGRXMPayload cfgrxm;
  194. } ubloxPacketData;
  195. typedef struct __attribute__((packed))
  196. {
  197. uBloxHeader header;
  198. ubloxPacketData data;
  199. } uBloxPacket;
  200. void ublox_init (void);
  201. void send_ublox (uint8_t msgClass, uint8_t msgId, uint8_t *payload, uint16_t payloadSize);
  202. void send_ublox_packet (uBloxPacket * packet);
  203. void ublox_get_last_data (GPSEntry * gpsEntry);
  204. uBloxChecksum ublox_calc_checksum (const uint8_t msgClass, const uint8_t msgId, const uint8_t *message, uint16_t size);
  205. void ublox_handle_incoming_byte (uint8_t data);
  206. void ublox_handle_packet (uBloxPacket *pkt);
  207. uint8_t ublox_wait_for_ack (void);
  208. #endif //RS41HUP_UBLOX_H