ublox(8039).h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * ublox.h
  3. *
  4. * Created on: Oct 28, 2017
  5. * Author: Milosz Iskrzynski SQ6NTI
  6. */
  7. #ifndef UBLOX_H_
  8. #define UBLOX_H_
  9. #include <stdint.h>
  10. #include "stm32l4xx_hal.h"
  11. /* UBX sync bytes */
  12. #define UBX_SYNC1 0xB5
  13. #define UBX_SYNC2 0x62
  14. /* UBX NAV Message Class */
  15. #define UBX_NAV 0x01
  16. #define UBX_NAV_POSLLH 0x02
  17. #define UBX_NAV_STATUS 0x03
  18. #define UBX_NAV_SOL 0x06
  19. #define UBX_NAV_TIMEUTC 0x21
  20. /* UBX RXM Message Class */
  21. #define UBX_RXM 0x02
  22. /* UBX INF Message Class */
  23. #define UBX_INF 0x04
  24. /* UBX ACK Message Class */
  25. #define UBX_ACK 0x05
  26. #define UBX_ACK_NAK 0x00
  27. #define UBX_ACK_ACK 0x01
  28. /* UBX CFG Message Class */
  29. #define UBX_CFG 0x06
  30. #define UBX_CFG_PRT 0x00
  31. #define UBX_CFG_MSG 0x01
  32. #define UBX_CFG_RST 0x04
  33. #define UBX_CFG_RXM 0x11
  34. #define UBX_CFG_NAV5 0x24
  35. /* Other UBX Message Classes */
  36. #define UBX_MON 0x0A
  37. #define UBX_AID 0x0B
  38. #define UBX_TIM 0x0D
  39. #define UBX_ESF 0x10
  40. /* NMEA Standard Message Class */
  41. #define NMEA_STD 0xF0
  42. #define NMEA_STD_GGA 0x00
  43. #define NMEA_STD_GLL 0x01
  44. #define NMEA_STD_GSA 0x02
  45. #define NMEA_STD_GSV 0x03
  46. #define NMEA_STD_RMC 0x04
  47. #define NMEA_STD_VTG 0x05
  48. /* NMEA Proprietary Message Class */
  49. #define NMEA_PROP 0xF0
  50. /* UBX header */
  51. typedef struct __attribute__((packed)) {
  52. uint8_t syncChar1; // 0xB5
  53. uint8_t syncChar2; // 0x62
  54. uint8_t messageClass;
  55. uint8_t messageId;
  56. uint16_t payloadLength;
  57. } ubxHeader;
  58. /* UBX ACK-ACK message (0x05 0x01) */
  59. typedef struct {
  60. uint8_t msgClass;
  61. uint8_t msgID;
  62. } ubxAckAck;
  63. /* UBX ACK-NAK message (0x05 0x00) */
  64. typedef struct {
  65. uint8_t msgClass;
  66. uint8_t msgID;
  67. } ubxAckNak;
  68. /* UBX CFG-MSG message (0x06 0x01) */
  69. typedef struct {
  70. uint8_t msgClass;
  71. uint8_t msgID;
  72. uint8_t rate;
  73. } ubxCfgMsg;
  74. /* UBX CFG-NAV5 message (0x06 0x24) */
  75. typedef struct {
  76. uint16_t mask; // Parameters Bitmask 0b0000000011111111 to set all params)
  77. uint8_t dynModel; // Dynamic Platform model: 0 Portable, 2 Stationary, 3 Pedestrian, 4 Automotive, 5 Sea, 6 Airborne with <1g Acceleration, 7 Airborne with <2g Acceleration, 8 Airborne with <4g Acceleration
  78. uint8_t fixMode; // Position Fixing Mode. - 1: 2D only - 2: 3D only - 3: Auto 2D/3D
  79. int32_t fixedAlt; // Fixed altitude (mean sea level) for 2D fix mode. [0.01 m]
  80. uint32_t fixedAltVar; // Fixed altitude variance for 2D mode. [0.0001 m^2]
  81. int8_t minElev; // Minimum Elevation for a GNSS satellite to be used in NAV [deg]
  82. uint8_t drLimit; // Maximum time to perform dead reckoning (linear extrapolation) in case of GPS signal loss [s]
  83. uint16_t pDop; // Position DOP Mask to use [0.1]
  84. uint16_t tDop; // Time DOP Mask to use [0.1]
  85. uint16_t pAcc; // Position Accuracy Mask [m]
  86. uint16_t tAcc; // Time Accuracy Mask [m]
  87. uint8_t staticHoldThresh; // Static hold threshold [cm/s]
  88. uint8_t dgpsTimeOut; // DGPS timeout, firmware 7 and newer only [s]
  89. uint32_t reserved2; // Always set to zero
  90. uint32_t reserved3; // Always set to zero
  91. uint32_t reserved4; // Always set to zero
  92. } ubxCfgNav5;
  93. /* UBX CFG-PRT message (0x06 0x00) */
  94. typedef struct {
  95. uint8_t portID; // Port Identifier Number (= 1 or 2 for UART ports)
  96. uint8_t reserved1; // Reserved
  97. uint16_t txReady; // TX ready PIN configuration
  98. uint32_t mode; // A bit mask describing the UART mode
  99. uint32_t baudRate; // Baud rate in bits/second [Bits/s]
  100. uint16_t inProtoMask; // A mask describing which input protocols are active
  101. uint16_t outProtoMask; // A mask describing which output protocols are active
  102. uint8_t reserved2[2]; // Reserved
  103. } ubxCfgPrt;
  104. /* UBX CFG-RST message (0x06 0x04) */
  105. typedef struct {
  106. uint16_t navBbrMask; // BBR Sections to clear. (Special: 0x0000 Hotstart, 0x0001 Warmstart, 0xFFFF Coldstart)
  107. uint8_t resetMode; // Reset Type
  108. uint8_t reserved1; // Reserved
  109. } ubxCfgRst;
  110. /* UBX CFG-RXM message (0x06 0x11) */
  111. typedef struct {
  112. uint8_t reserved1; // Always set to 8
  113. uint8_t lpMode; // Low Power Mode (0: max performance, 1: power save, 4: eco)
  114. } ubxCfgRxm;
  115. /* UBX NAV-POSLLH message (0x01 0x02) */
  116. typedef struct {
  117. uint32_t iTOW; // GPS Millisecond Time of Week [ms]
  118. int32_t lon; // Longitude [1e-7 deg]
  119. int32_t lat; // Latitude [1e-7 deg]
  120. int32_t height; // Height above Ellipsoid [mm]
  121. int32_t hMSL; // Height above mean sea level [mm]
  122. uint32_t hAcc; // Horizontal Accuracy Estimate [mm]
  123. uint32_t vAcc; // Vertical Accuracy Estimate [mm]
  124. } ubxNavPosllh;
  125. /* UBX NAV-SOL message (0x01 0x06) */
  126. typedef struct {
  127. uint32_t iTOW; // GPS Millisecond Time of Week [ms]
  128. int32_t fTOW; // Fractional Nanoseconds remainder of rounded ms above, range -500000 .. 500000 [ns]
  129. int16_t week; // GPS week (GPS time)
  130. uint8_t gpsFix; // GPSfix Type (0x00 No Fix, 01 Dead Reckoning only, 02 2D-Fix, 03 3D-Fix, 04 GPS + dead reckoning combined, 05 Time only fix)
  131. uint8_t flags; // Fix Status Flags
  132. int32_t ecefX; // ECEF X coordinate [cm]
  133. int32_t ecefY; // ECEF Y coordinate [cm]
  134. int32_t ecefZ; // ECEF Z coordinate [cm]
  135. uint32_t pAcc; // 3D Position Accuracy Estimate [cm]
  136. int32_t ecefVX; // ECEF X velocity [cm/s]
  137. int32_t ecefVY; // ECEF Y velocity [cm/s]
  138. int32_t ecefVZ; // ECEF Z velocity [cm/s]
  139. uint32_t sAcc; // Speed Accuracy Estimate [cm/s]
  140. uint16_t pDOP; // Position DOP [0.01]
  141. uint8_t reserved1; // Reserved
  142. uint8_t numSV; // Number of SVs used in Nav Solution
  143. uint32_t reserved2; // Reserved
  144. } ubxNavSol;
  145. /* UBX NAV-STATUS message (0x01 0x03) */
  146. typedef struct {
  147. uint32_t iTOW; // GPS Millisecond Time of Week [ms]
  148. uint8_t gpsFix; // GPSfix Type (0x00 No Fix, 01 Dead Reckoning only, 02 2D-Fix, 03 3D-Fix, 04 GPS + dead reckoning combined, 05 Time only fix)
  149. uint8_t flags; // Fix Status Flags
  150. uint8_t fixStat; // Fix Status Information
  151. uint8_t flags2; // Further information about navigation output
  152. uint32_t ttff; // Time to first fix (millisecond time tag)
  153. uint32_t msss; // Milliseconds since Startup / Reset
  154. } ubxNavStatus;
  155. /* UBX NAV-TIMEUTC message (0x01 0x021) */
  156. typedef struct {
  157. uint32_t iTOW; // GPS Millisecond Time of Week [ms]
  158. uint32_t tAcc; // Time Accuracy Estimate [ns]
  159. int32_t nano; // Nanoseconds of second, range -1e9 .. 1e9 (UTC) [ns]
  160. uint16_t year; // Year, range 1999..2099 (UTC) [y]
  161. uint8_t month; // Month, range 1..12 (UTC) [month]
  162. uint8_t day; // Day of Month, range 1..31 (UTC) [d]
  163. uint8_t hour; // Hour of Day, range 0..23 (UTC) [h]
  164. uint8_t min; // Minute of Hour, range 0..59 (UTC) [min]
  165. uint8_t sec; // Seconds of Minute, range 0..59 (UTC) [s]
  166. uint8_t valid; // Validity Flags
  167. } ubxNavTimeutc;
  168. /* UBX Message */
  169. typedef union {
  170. ubxAckAck ackack;
  171. ubxAckNak acknak;
  172. ubxCfgMsg cfgmsg;
  173. ubxCfgNav5 cfgnav5;
  174. ubxCfgPrt cfgprt;
  175. ubxCfgRst cfgrst;
  176. ubxCfgRxm cfgrxm;
  177. ubxNavPosllh navposllh;
  178. ubxNavStatus navstatus;
  179. ubxNavSol navsol;
  180. ubxNavTimeutc navtimeutc;
  181. } ubxMessage;
  182. /* UBX checksum */
  183. typedef struct {
  184. uint8_t ck_a;
  185. uint8_t ck_b;
  186. } ubxChecksum;
  187. /* UBX packet */
  188. typedef struct __attribute__((packed)) {
  189. ubxHeader header;
  190. ubxMessage message;
  191. ubxChecksum checksum;
  192. } ubxPacket;
  193. /* Ublox GPS current data */
  194. typedef struct {
  195. int32_t lat;
  196. int32_t lon;
  197. int32_t alt;
  198. int32_t speed;
  199. uint8_t sats;
  200. uint16_t year;
  201. uint8_t month;
  202. uint8_t day;
  203. uint8_t hour;
  204. uint8_t minute;
  205. uint8_t second;
  206. uint8_t fix;
  207. } ubxGPSData;
  208. void ubxInit(UART_HandleTypeDef *uart);
  209. void ubxRxByte(uint8_t data);
  210. void ubxProcessPacket(const ubxPacket *packet);
  211. int ubxResponseWait(int timeout);
  212. void ubxSendPacket(uint8_t messageClass, uint8_t messageId, uint16_t payloadLength, ubxMessage message);
  213. void ubxTxPacket(ubxPacket packet);
  214. uint8_t *serializeUbxPacket(ubxPacket *ubxPacket);
  215. ubxChecksum ubxCalcChecksum(const ubxPacket *packet);
  216. ubxGPSData ubxLastGPSData(void);
  217. extern void UART_ReInit(UART_HandleTypeDef*, uint32_t);
  218. extern void UART_TxStart(UART_HandleTypeDef*);
  219. extern int UART_TxFinished(UART_HandleTypeDef*);
  220. #endif /* UBLOX_H_ */