updates
[inav.git] / src / main / io / gps_ublox.h
blob8ec9be16d09834b01899ef3b55a529dc20d054ec
1 /*
2 * This file is part of INAV
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
20 #include <stdint.h>
21 #include <stdbool.h>
23 #include "common/time.h"
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
29 #define GPS_CFG_CMD_TIMEOUT_MS 500
30 #define GPS_VERSION_RETRY_TIMES 3
31 #define MAX_UBLOX_PAYLOAD_SIZE 256
32 #define UBLOX_BUFFER_SIZE MAX_UBLOX_PAYLOAD_SIZE
33 #define UBLOX_SBAS_MESSAGE_LENGTH 16
34 #define GPS_CAPA_INTERVAL 5000
36 #define UBX_DYNMODEL_PEDESTRIAN 3
37 #define UBX_DYNMODEL_AUTOMOVITE 4
38 #define UBX_DYNMODEL_AIR_1G 6
39 #define UBX_DYNMODEL_AIR_2G 7
40 #define UBX_DYNMODEL_AIR_4G 8
42 #define UBX_FIXMODE_2D_ONLY 1
43 #define UBX_FIXMODE_3D_ONLY 2
44 #define UBX_FIXMODE_AUTO 3
46 #define UBX_VALID_GPS_DATE(valid) (valid & 1 << 0)
47 #define UBX_VALID_GPS_TIME(valid) (valid & 1 << 1)
48 #define UBX_VALID_GPS_DATE_TIME(valid) (UBX_VALID_GPS_DATE(valid) && UBX_VALID_GPS_TIME(valid))
50 #define UBX_HW_VERSION_UNKNOWN 0
51 #define UBX_HW_VERSION_UBLOX5 500
52 #define UBX_HW_VERSION_UBLOX6 600
53 #define UBX_HW_VERSION_UBLOX7 700
54 #define UBX_HW_VERSION_UBLOX8 800
55 #define UBX_HW_VERSION_UBLOX9 900
56 #define UBX_HW_VERSION_UBLOX10 1000
59 #define UBLOX_CFG_SIGNAL_SBAS_ENA 0x10310020 // U1
60 #define UBLOX_CFG_SIGNAL_SBAS_L1CA_ENA 0x10310005 // U1
62 #define UBLOX_CFG_SIGNAL_GAL_ENA 0x10310021 // U1
63 #define UBLOX_CFG_SIGNAL_GAL_E1_ENA 0x10310007 // U1
65 #define UBLOX_CFG_SIGNAL_BDS_ENA 0x10310022 // U1
66 #define UBLOX_CFG_SIGNAL_BDS_B1_ENA 0x1031000d // U1
67 #define UBLOX_CFG_SIGNAL_BDS_B1C_ENA 0x1031000f // U1 default off
69 #define UBLOX_CFG_QZSS_ENA 0x10310024 // U1
70 #define UBLOX_CFG_QZSS_L1CA_ENA 0x10310012 // U1
71 #define UBLOX_CFG_QZSS_L1S_ENA 0x10310014 // U1
73 #define UBLOX_CFG_GLO_ENA 0x10310025 // U1 default off - may conflict with other constelations
74 #define UBLOX_CFG_GLO_L1_ENA 0x10310018 // U1 default off
76 #define UBLOX_CFG_SBAS_PRNSCANMASK 0x50360006 // 0 = auto // X8
77 #define UBLOX_SBAS_ALL 0x0000000000000000 //Enable search for all SBAS PRNs
78 #define UBLOX_SBAS_PRN120 0x0000000000000001 //Enable search for SBAS PRN120
79 #define UBLOX_SBAS_PRN121 0x0000000000000002 //Enable search for SBAS PRN121
80 #define UBLOX_SBAS_PRN122 0x0000000000000004 //Enable search for SBAS PRN122
81 #define UBLOX_SBAS_PRN123 0x0000000000000008 //Enable search for SBAS PRN123
82 #define UBLOX_SBAS_PRN124 0x0000000000000010 //Enable search for SBAS PRN124
83 #define UBLOX_SBAS_PRN125 0x0000000000000020 //Enable search for SBAS PRN125
84 #define UBLOX_SBAS_PRN126 0x0000000000000040 //Enable search for SBAS PRN126
85 #define UBLOX_SBAS_PRN127 0x0000000000000080 //Enable search for SBAS PRN127
86 #define UBLOX_SBAS_PRN128 0x0000000000000100 //Enable search for SBAS PRN128
87 #define UBLOX_SBAS_PRN129 0x0000000000000200 //Enable search for SBAS PRN129
88 #define UBLOX_SBAS_PRN130 0x0000000000000400 //Enable search for SBAS PRN130
89 #define UBLOX_SBAS_PRN131 0x0000000000000800 //Enable search for SBAS PRN131
90 #define UBLOX_SBAS_PRN132 0x0000000000001000 //Enable search for SBAS PRN132
91 #define UBLOX_SBAS_PRN133 0x0000000000002000 //Enable search for SBAS PRN133
92 #define UBLOX_SBAS_PRN134 0x0000000000004000 //Enable search for SBAS PRN134
93 #define UBLOX_SBAS_PRN135 0x0000000000008000 //Enable search for SBAS PRN135
94 #define UBLOX_SBAS_PRN136 0x0000000000010000 //Enable search for SBAS PRN136
95 #define UBLOX_SBAS_PRN137 0x0000000000020000 //Enable search for SBAS PRN137
96 #define UBLOX_SBAS_PRN138 0x0000000000040000 //Enable search for SBAS PRN138
97 #define UBLOX_SBAS_PRN139 0x0000000000080000 //Enable search for SBAS PRN139
98 #define UBLOX_SBAS_PRN140 0x0000000000100000 //Enable search for SBAS PRN140
99 #define UBLOX_SBAS_PRN141 0x0000000000200000 //Enable search for SBAS PRN141
100 #define UBLOX_SBAS_PRN142 0x0000000000400000 //Enable search for SBAS PRN142
101 #define UBLOX_SBAS_PRN143 0x0000000000800000 //Enable search for SBAS PRN143
102 #define UBLOX_SBAS_PRN144 0x0000000001000000 //Enable search for SBAS PRN144
103 #define UBLOX_SBAS_PRN145 0x0000000002000000 //Enable search for SBAS PRN145
104 #define UBLOX_SBAS_PRN146 0x0000000004000000 //Enable search for SBAS PRN146
105 #define UBLOX_SBAS_PRN147 0x0000000008000000 //Enable search for SBAS PRN147
106 #define UBLOX_SBAS_PRN148 0x0000000010000000 //Enable search for SBAS PRN148
107 #define UBLOX_SBAS_PRN149 0x0000000020000000 //Enable search for SBAS PRN149
108 #define UBLOX_SBAS_PRN150 0x0000000040000000 //Enable search for SBAS PRN150
109 #define UBLOX_SBAS_PRN151 0x0000000080000000 //Enable search for SBAS PRN151
110 #define UBLOX_SBAS_PRN152 0x0000000100000000 //Enable search for SBAS PRN152
111 #define UBLOX_SBAS_PRN153 0x0000000200000000 //Enable search for SBAS PRN153
112 #define UBLOX_SBAS_PRN154 0x0000000400000000 //Enable search for SBAS PRN154
113 #define UBLOX_SBAS_PRN155 0x0000000800000000 //Enable search for SBAS PRN155
114 #define UBLOX_SBAS_PRN156 0x0000001000000000 //Enable search for SBAS PRN156
115 #define UBLOX_SBAS_PRN157 0x0000002000000000 //Enable search for SBAS PRN157
116 #define UBLOX_SBAS_PRN158 0x0000004000000000 //Enable search for SBAS PRN158
118 // payload types
119 typedef struct {
120 uint8_t mode;
121 uint8_t usage;
122 uint8_t maxSBAS;
123 uint8_t scanmode2;
124 uint32_t scanmode1;
125 } ubx_sbas;
127 typedef struct {
128 uint8_t msg_class;
129 uint8_t id;
130 uint8_t rate;
131 } ubx_msg;
133 typedef struct {
134 uint16_t meas;
135 uint16_t nav;
136 uint16_t time;
137 } ubx_rate;
139 typedef struct {
140 uint8_t gnssId;
141 uint8_t resTrkCh;
142 uint8_t maxTrkCh;
143 uint8_t reserved1;
144 // flags
145 uint8_t enabled;
146 uint8_t undefined0;
147 uint8_t sigCfgMask;
148 uint8_t undefined1;
149 } ubx_gnss_element_t;
151 typedef struct {
152 uint8_t msgVer;
153 uint8_t numTrkChHw;
154 uint8_t numTrkChUse;
155 uint8_t numConfigBlocks;
156 ubx_gnss_element_t config[0];
157 } ubx_gnss_msg_t;
159 typedef struct {
160 uint8_t version;
161 uint8_t layers;
162 uint8_t reserved;
163 } __attribute__((packed)) ubx_config_data_header_v0_t;
165 typedef struct {
166 uint8_t version;
167 uint8_t layers;
168 uint8_t transaction;
169 uint8_t reserved;
170 } __attribute__((packed)) ubx_config_data_header_v1_t;
173 #define MAX_GNSS 7
174 #define MAX_GNSS_SIZE_BYTES (sizeof(ubx_gnss_msg_t) + sizeof(ubx_gnss_element_t)*MAX_GNSS)
176 typedef union {
177 uint8_t bytes[MAX_GNSS_SIZE_BYTES]; // placeholder
178 ubx_sbas sbas;
179 ubx_msg msg;
180 ubx_rate rate;
181 ubx_gnss_msg_t gnss;
182 } ubx_payload;
184 // UBX support
185 typedef struct {
186 uint8_t preamble1;
187 uint8_t preamble2;
188 uint8_t msg_class;
189 uint8_t msg_id;
190 uint16_t length;
191 } ubx_header;
193 typedef struct {
194 uint32_t key;
195 uint8_t value;
196 } __attribute__((packed)) ubx_config_data8_payload_t;
199 #define MAX_CONFIG_SET_VAL_VALUES 32
201 typedef struct {
202 ubx_header header;
203 ubx_config_data_header_v1_t configHeader;
204 union {
205 ubx_config_data8_payload_t payload[0];
206 uint8_t buffer[(MAX_CONFIG_SET_VAL_VALUES * sizeof(ubx_config_data8_payload_t)) + 2]; // 12 key/value pairs + 2 checksum bytes
207 } data;
208 } __attribute__((packed)) ubx_config_data8_t;
210 typedef struct {
211 ubx_header header;
212 ubx_payload payload;
213 } __attribute__((packed)) ubx_message;
215 typedef struct {
216 char swVersion[30]; // Zero-terminated Software Version String
217 char hwVersion[10]; // Zero-terminated Hardware Version String
218 } ubx_mon_ver;
220 typedef struct {
221 uint32_t time; // GPS msToW
222 int32_t longitude;
223 int32_t latitude;
224 int32_t altitude_ellipsoid;
225 int32_t altitude_msl;
226 uint32_t horizontal_accuracy;
227 uint32_t vertical_accuracy;
228 } ubx_nav_posllh;
230 typedef struct {
231 uint32_t time; // GPS msToW
232 uint8_t fix_type;
233 uint8_t fix_status;
234 uint8_t differential_status;
235 uint8_t res;
236 uint32_t time_to_first_fix;
237 uint32_t uptime; // milliseconds
238 } ubx_nav_status;
240 typedef struct {
241 uint32_t time;
242 int32_t time_nsec;
243 int16_t week;
244 uint8_t fix_type;
245 uint8_t fix_status;
246 int32_t ecef_x;
247 int32_t ecef_y;
248 int32_t ecef_z;
249 uint32_t position_accuracy_3d;
250 int32_t ecef_x_velocity;
251 int32_t ecef_y_velocity;
252 int32_t ecef_z_velocity;
253 uint32_t speed_accuracy;
254 uint16_t position_DOP;
255 uint8_t res;
256 uint8_t satellites;
257 uint32_t res2;
258 } ubx_nav_solution;
260 typedef struct {
261 uint32_t time; // GPS msToW
262 int32_t ned_north;
263 int32_t ned_east;
264 int32_t ned_down;
265 uint32_t speed_3d;
266 uint32_t speed_2d;
267 int32_t heading_2d;
268 uint32_t speed_accuracy;
269 uint32_t heading_accuracy;
270 } ubx_nav_velned;
272 typedef struct {
273 uint8_t chn; // Channel number, 255 for SVx not assigned to channel
274 uint8_t svid; // Satellite ID
275 uint8_t flags; // Bitmask
276 uint8_t quality; // Bitfield
277 uint8_t cno; // Carrier to Noise Ratio (Signal Strength) // dbHz, 0-55.
278 uint8_t elev; // Elevation in integer degrees
279 int16_t azim; // Azimuth in integer degrees
280 int32_t prRes; // Pseudo range residual in centimetres
281 } ubx_nav_svinfo_channel;
283 typedef struct {
284 uint32_t time; // GPS Millisecond time of week
285 uint8_t numCh; // Number of channels
286 uint8_t globalFlags; // Bitmask, Chip hardware generation 0:Antaris, 1:u-blox 5, 2:u-blox 6
287 uint16_t reserved2; // Reserved
288 ubx_nav_svinfo_channel channel[16]; // 16 satellites * 12 byte
289 } ubx_nav_svinfo;
291 typedef struct {
292 uint32_t time; // GPS msToW
293 uint32_t tAcc;
294 int32_t nano;
295 uint16_t year;
296 uint8_t month;
297 uint8_t day;
298 uint8_t hour;
299 uint8_t min;
300 uint8_t sec;
301 uint8_t valid;
302 } ubx_nav_timeutc;
304 typedef struct {
305 uint32_t time; // GPS msToW
306 uint16_t year;
307 uint8_t month;
308 uint8_t day;
309 uint8_t hour;
310 uint8_t min;
311 uint8_t sec;
312 uint8_t valid;
313 uint32_t tAcc;
314 int32_t nano;
315 uint8_t fix_type;
316 uint8_t fix_status;
317 uint8_t reserved1;
318 uint8_t satellites;
319 int32_t longitude;
320 int32_t latitude;
321 int32_t altitude_ellipsoid;
322 int32_t altitude_msl;
323 uint32_t horizontal_accuracy;
324 uint32_t vertical_accuracy;
325 int32_t ned_north;
326 int32_t ned_east;
327 int32_t ned_down;
328 int32_t speed_2d;
329 int32_t heading_2d;
330 uint32_t speed_accuracy;
331 uint32_t heading_accuracy;
332 uint16_t position_DOP;
333 uint16_t reserved2;
334 uint16_t reserved3;
335 } ubx_nav_pvt;
337 #define UBX_MON_GNSS_GPS_MASK (1 << 0)
338 #define UBX_MON_GNSS_GLONASS_MASK (1 << 1)
339 #define UBX_MON_GNSS_BEIDOU_MASK (1 << 2)
340 #define UBX_MON_GNSS_GALILEO_MASK (1 << 3)
342 typedef struct {
343 uint8_t version;
344 uint8_t supported; // bitfield for GNSS types: 0:GPS, 1:Glonass, 2:Beidou, 3:Galileo
345 uint8_t defaultGnss; // bitfield for GNSS types: 0:GPS, 1:Glonass, 2:Beidou, 3:Galileo
346 uint8_t enabled; // bitfield for GNSS types: 0:GPS, 1:Glonass, 2:Beidou, 3:Galileo
347 uint8_t maxConcurrent;
348 uint8_t reserverd1;
349 uint8_t reserverd2;
350 uint8_t reserverd3;
351 } ubx_mon_gnss;
353 typedef struct {
354 uint8_t msg_class;
355 uint8_t msg;
356 } ubx_ack_ack;
358 typedef enum {
359 UBX_ACK_WAITING = 0,
360 UBX_ACK_GOT_ACK = 1,
361 UBX_ACK_GOT_NAK = 2
362 } ubx_ack_state_t;
364 typedef enum {
365 PREAMBLE1 = 0xB5,
366 PREAMBLE2 = 0x62,
367 CLASS_NAV = 0x01,
368 CLASS_ACK = 0x05,
369 CLASS_CFG = 0x06,
370 CLASS_MON = 0x0A,
371 MSG_CLASS_UBX = 0x01,
372 MSG_CLASS_NMEA = 0xF0,
373 MSG_VER = 0x04,
374 MSG_ACK_NACK = 0x00,
375 MSG_ACK_ACK = 0x01,
376 MSG_NMEA_GGA = 0x0,
377 MSG_NMEA_GLL = 0x1,
378 MSG_NMEA_GSA = 0x2,
379 MSG_NMEA_GSV = 0x3,
380 MSG_NMEA_RMC = 0x4,
381 MSG_NMEA_VGS = 0x5,
382 MSG_POSLLH = 0x2,
383 MSG_STATUS = 0x3,
384 MSG_SOL = 0x6,
385 MSG_PVT = 0x7,
386 MSG_VELNED = 0x12,
387 MSG_TIMEUTC = 0x21,
388 MSG_SVINFO = 0x30,
389 MSG_NAV_SAT = 0x35,
390 MSG_NAV_SIG = 0x35,
391 MSG_CFG_PRT = 0x00,
392 MSG_CFG_RATE = 0x08,
393 MSG_CFG_SET_RATE = 0x01,
394 MSG_CFG_NAV_SETTINGS = 0x24,
395 MSG_CFG_SBAS = 0x16,
396 MSG_CFG_GNSS = 0x3e,
397 MSG_MON_GNSS = 0x28
398 } ubx_protocol_bytes_t;
400 typedef enum {
401 FIX_NONE = 0,
402 FIX_DEAD_RECKONING = 1,
403 FIX_2D = 2,
404 FIX_3D = 3,
405 FIX_GPS_DEAD_RECKONING = 4,
406 FIX_TIME = 5
407 } ubs_nav_fix_type_t;
409 typedef enum {
410 NAV_STATUS_FIX_VALID = 1
411 } ubx_nav_status_bits_t;
413 uint8_t gpsUbloxMaxGnss(void);
414 timeMs_t gpsUbloxCapLastUpdate(void);
416 bool gpsUbloxHasGalileo(void);
417 bool gpsUbloxHasBeidou(void);
418 bool gpsUbloxHasGlonass(void);
420 bool gpsUbloxGalileoDefault(void);
421 bool gpsUbloxBeidouDefault(void);
422 bool gpsUbloxGlonassDefault(void);
424 bool gpsUbloxGalileoEnabled(void);
425 bool gpsUbloxBeidouEnabled(void);
426 bool gpsUbloxGlonassEnabled(void);
429 #ifdef __cplusplus
431 #endif