Merged in corvusvcorax/librepilot/LP-490_insgps13state_mag_fixes (pull request #398)
[librepilot.git] / flight / modules / GPS / ubx_autoconfig.c
blobb1dee67ad206c05aad09acfa80c542573911f1dc
1 /**
2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
4 * @{
5 * @addtogroup GPSModule GPS Module
6 * @brief Support code for UBX AutoConfig
7 * @{
9 * @file ubx_autoconfig.c
10 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015-2016.
11 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
12 * @brief Support code for UBX AutoConfig
13 * @see The GNU Public License (GPL) Version 3
15 *****************************************************************************/
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <openpilot.h>
32 #include "hwsettings.h"
33 #include "gpssettings.h"
35 #include "inc/ubx_autoconfig.h"
36 #include <pios_mem.h>
38 // private type definitions
40 typedef enum {
41 INIT_STEP_DISABLED = 0,
42 INIT_STEP_START,
43 INIT_STEP_SEND_MON_VER,
44 INIT_STEP_WAIT_MON_VER_ACK,
45 INIT_STEP_RESET_GPS,
46 INIT_STEP_REVO_9600_BAUD,
47 INIT_STEP_GPS_BAUD,
48 INIT_STEP_REVO_BAUD,
49 INIT_STEP_ENABLE_SENTENCES,
50 INIT_STEP_ENABLE_SENTENCES_WAIT_ACK,
51 INIT_STEP_CONFIGURE,
52 INIT_STEP_CONFIGURE_WAIT_ACK,
53 INIT_STEP_SAVE,
54 INIT_STEP_SAVE_WAIT_ACK,
55 INIT_STEP_PRE_DONE,
56 INIT_STEP_DONE,
57 INIT_STEP_PRE_ERROR,
58 INIT_STEP_ERROR
59 } initSteps_t;
61 typedef struct {
62 initSteps_t currentStep; // Current configuration "fsm" status
63 initSteps_t currentStepSave; // Current configuration "fsm" status
64 uint32_t lastStepTimestampRaw; // timestamp of last operation
65 uint32_t lastConnectedRaw; // timestamp of last time gps was connected
66 struct {
67 union {
68 struct {
69 UBXSentPacket_t working_packet; // outbound "buffer"
70 // bufferPaddingForPiosBugAt2400Baud must exist for baud rate change to work at 2400 or 4800
71 // failure mode otherwise:
72 // - send message with baud rate change
73 // - wait 1 second (even at 2400, the baud rate change command
74 // - should clear even an initially full 31 byte PIOS buffer much more quickly)
75 // - change Revo port baud rate
76 // sometimes fails (much worse for lowest baud rates)
77 // FIXME: remove this and retest when someone has time
78 uint8_t bufferPaddingForPiosBugAt2400Baud[2]; // must be at least 2 for 2400 to work, probably 1 for 4800 and 0 for 9600+
79 } __attribute__((packed));
80 GPSSettingsData gpsSettings;
81 } __attribute__((packed));
82 } __attribute__((packed));
83 volatile ubx_autoconfig_settings_t currentSettings;
84 int8_t lastConfigSent; // index of last configuration string sent
85 struct UBX_ACK_ACK requiredAck; // Class and id of the message we are waiting for an ACK from GPS
86 uint8_t retryCount;
87 } status_t;
89 ubx_cfg_msg_t msg_config_ubx6[] = {
90 // messages to disable
91 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_CLOCK, .rate = 0 },
92 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_POSECEF, .rate = 0 },
93 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_SBAS, .rate = 0 },
94 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_TIMEGPS, .rate = 0 },
95 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_VELECEF, .rate = 0 },
97 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_HW, .rate = 0 },
98 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_HW2, .rate = 0 },
99 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_IO, .rate = 0 },
100 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_MSGPP, .rate = 0 },
101 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_RXBUFF, .rate = 0 },
102 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_RXR, .rate = 0 },
103 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_TXBUF, .rate = 0 },
105 { .msgClass = UBX_CLASS_RXM, .msgID = UBX_ID_RXM_SVSI, .rate = 0 },
107 // message to enable
108 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_POSLLH, .rate = 1 },
109 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_DOP, .rate = 1 },
110 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_SOL, .rate = 1 },
111 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_STATUS, .rate = 1 },
112 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_VELNED, .rate = 1 },
113 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_TIMEUTC, .rate = 1 },
114 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_SVINFO, .rate = 10 },
117 ubx_cfg_msg_t msg_config_ubx7[] = {
118 // messages to disable
119 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_AOPSTATUS, .rate = 0 },
120 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_CLOCK, .rate = 0 },
121 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_DGPS, .rate = 0 },
122 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_POSECEF, .rate = 0 },
123 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_SBAS, .rate = 0 },
124 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_TIMEGPS, .rate = 0 },
125 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_VELECEF, .rate = 0 },
126 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_SOL, .rate = 0 },
127 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_STATUS, .rate = 0 },
128 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_VELNED, .rate = 0 },
129 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_TIMEUTC, .rate = 0 },
130 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_POSLLH, .rate = 0 },
132 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_HW, .rate = 0 },
133 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_HW2, .rate = 0 },
134 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_IO, .rate = 0 },
135 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_MSGPP, .rate = 0 },
136 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_RXBUFF, .rate = 0 },
137 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_RXR, .rate = 0 },
138 { .msgClass = UBX_CLASS_MON, .msgID = UBX_ID_MON_TXBUF, .rate = 0 },
140 { .msgClass = UBX_CLASS_RXM, .msgID = UBX_ID_RXM_SVSI, .rate = 0 },
142 // message to enable
143 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_PVT, .rate = 1 },
144 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_DOP, .rate = 1 },
145 { .msgClass = UBX_CLASS_NAV, .msgID = UBX_ID_NAV_SVINFO, .rate = 10 },
148 // private defines
150 #define LAST_CONFIG_SENT_START (-1)
151 #define LAST_CONFIG_SENT_COMPLETED (-2)
152 // always reset the stored GPS configuration, even when doing autoconfig.nostore
153 // that is required to do a 100% correct configuration
154 // but is unexpected because it changes the stored configuration when doing autoconfig.nostore
155 // note that a reset is always done with autoconfig.store
156 // #define ALWAYS_RESET
158 // we can enable this when we know how to make the Flight Controller save an object to permanent storage
159 // also see comment about simple edit in gpssettings.xml
160 #define AUTOBAUD_CONFIGURE_STORE_AND_DISABLE
161 // Alessio Morale May 20 3:16 AM
162 // @Cliff you should update the ObjectPersistence uavo passing the object id and the desired operation.
164 // private variables
166 // enable the autoconfiguration system
167 static volatile bool enabled = false;
168 static volatile bool current_step_touched = false;
169 // both the pointer and what it points to are volatile. Yuk.
170 static volatile status_t *volatile status = 0;
171 static uint8_t hwsettings_baud;
172 static uint8_t baud_to_try_index = 255;
174 // functions
176 static void append_checksum(UBXSentPacket_t *packet)
178 uint8_t i;
179 uint8_t ck_a = 0;
180 uint8_t ck_b = 0;
181 uint16_t len = packet->message.header.len + sizeof(UBXSentHeader_t);
183 for (i = 2; i < len; i++) {
184 ck_a += packet->buffer[i];
185 ck_b += ck_a;
188 packet->buffer[len] = ck_a;
189 packet->buffer[len + 1] = ck_b;
194 * prepare a packet to be sent, fill the header and appends the checksum.
195 * return the total packet lenght comprising header and checksum
197 static uint16_t prepare_packet(UBXSentPacket_t *packet, uint8_t classID, uint8_t messageID, uint16_t len)
199 memset((uint8_t *)status->working_packet.buffer + len + sizeof(UBXSentHeader_t) + 2, 0, sizeof(status->bufferPaddingForPiosBugAt2400Baud));
200 packet->message.header.prolog[0] = UBX_SYNC1;
201 packet->message.header.prolog[1] = UBX_SYNC2;
202 packet->message.header.class = classID;
203 packet->message.header.id = messageID;
204 packet->message.header.len = len;
205 append_checksum(packet);
207 status->requiredAck.clsID = classID;
208 status->requiredAck.msgID = messageID;
210 return len + sizeof(UBXSentHeader_t) + 2 + sizeof(status->bufferPaddingForPiosBugAt2400Baud); // payload + header + checksum + extra bytes
214 static void build_request(UBXSentPacket_t *packet, uint8_t classID, uint8_t messageID, uint16_t *bytes_to_send)
216 *bytes_to_send = prepare_packet(packet, classID, messageID, 0);
220 static void set_current_step_if_untouched(initSteps_t new_steps)
222 // assume this one byte initSteps_t is atomic
223 // take care of some concurrency issues
225 if (!current_step_touched) {
226 status->currentStep = new_steps;
228 if (current_step_touched) {
229 status->currentStep = status->currentStepSave;
234 void gps_ubx_reset_sensor_type()
236 static uint8_t mutex; // = 0
238 // is this needed?
239 // what happens if two tasks / threads try to do an XyzSet() at the same time?
240 if (__sync_fetch_and_add(&mutex, 1) == 0) {
241 ubxHwVersion = -1;
242 baud_to_try_index -= 1; // undo postincrement and start with the one that was most recently successful
243 ubxSensorType = GPSPOSITIONSENSOR_SENSORTYPE_UNKNOWN;
244 GPSPositionSensorSensorTypeSet(&ubxSensorType);
245 // make the sensor type / autobaud code time out immediately to send the request immediately
246 status->lastStepTimestampRaw += 0x8000000UL;
248 --mutex;
252 static void config_reset(uint16_t *bytes_to_send)
254 memset((uint8_t *)status->working_packet.buffer, 0, sizeof(UBXSentHeader_t) + sizeof(ubx_cfg_cfg_t));
255 // mask LSB=ioPort|msgConf|infMsg|navConf|rxmConf|||||rinvConf|antConf|....|= MSB
256 // ioPort=1, msgConf=2, infMsg=4, navConf=8, tpConf=0x10, sfdrConf=0x100, rinvConf=0x200, antConf=0x400
257 // first: reset (permanent settings to default) all but rinv = e.g. owner name
258 status->working_packet.message.payload.cfg_cfg.clearMask = UBX_CFG_CFG_OP_RESET_SETTINGS;
259 // then: don't store any current settings to permanent
260 status->working_packet.message.payload.cfg_cfg.saveMask = UBX_CFG_CFG_SETTINGS_NONE;
261 // lastly: load (immediately start to use) all but rinv = e.g. owner name
262 status->working_packet.message.payload.cfg_cfg.loadMask = UBX_CFG_CFG_OP_RESET_SETTINGS;
263 // all devices
264 status->working_packet.message.payload.cfg_cfg.deviceMask = UBX_CFG_CFG_DEVICE_ALL;
266 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_CFG, sizeof(ubx_cfg_cfg_t));
270 // set the GPS baud rate to the user specified baud rate
271 // because we may have started up with 9600 baud (for a GPS with no permanent settings)
272 static void config_gps_baud(uint16_t *bytes_to_send)
274 memset((uint8_t *)status->working_packet.buffer, 0, sizeof(UBXSentHeader_t) + sizeof(ubx_cfg_prt_t));
275 status->working_packet.message.payload.cfg_prt.mode = UBX_CFG_PRT_MODE_DEFAULT; // 8databits, 1stopbit, noparity, and non-zero reserved
276 status->working_packet.message.payload.cfg_prt.portID = 1; // 1 = UART1, 2 = UART2
277 // for protocol masks, bit 0 is UBX enable, bit 1 is NMEA enable
278 status->working_packet.message.payload.cfg_prt.inProtoMask = 1; // 1 = UBX only (bit 0)
279 // disable current UBX messages for low baud rates
280 status->working_packet.message.payload.cfg_prt.outProtoMask = 1;
281 // Ask GPS to change it's speed
282 status->working_packet.message.payload.cfg_prt.baudRate = hwsettings_gpsspeed_enum_to_baud(hwsettings_baud);
283 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_PRT, sizeof(ubx_cfg_prt_t));
287 static void config_rate(uint16_t *bytes_to_send)
289 memset((uint8_t *)status->working_packet.buffer, 0, sizeof(UBXSentHeader_t) + sizeof(ubx_cfg_rate_t));
290 // if rate is less than 1 uses the highest rate for current hardware
291 uint16_t rate = status->currentSettings.navRate > 0 ? status->currentSettings.navRate : 99;
292 if (ubxHwVersion < UBX_HW_VERSION_7 && rate > UBX_MAX_RATE) {
293 rate = UBX_MAX_RATE;
294 } else if (ubxHwVersion < UBX_HW_VERSION_8 && rate > UBX_MAX_RATE_VER7) {
295 rate = UBX_MAX_RATE_VER7;
296 } else if (ubxHwVersion >= UBX_HW_VERSION_8 && rate > UBX_MAX_RATE_VER8) {
297 rate = UBX_MAX_RATE_VER8;
299 uint16_t period = 1000 / rate;
300 status->working_packet.message.payload.cfg_rate.measRate = period;
301 status->working_packet.message.payload.cfg_rate.navRate = 1; // must be set to 1
302 status->working_packet.message.payload.cfg_rate.timeRef = 1; // 0 = UTC Time, 1 = GPS Time
304 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_RATE, sizeof(ubx_cfg_rate_t));
308 static void config_nav(uint16_t *bytes_to_send)
310 memset((uint8_t *)status->working_packet.buffer, 0, sizeof(UBXSentHeader_t) + sizeof(ubx_cfg_nav5_t));
311 status->working_packet.message.payload.cfg_nav5.dynModel = status->currentSettings.dynamicModel;
312 status->working_packet.message.payload.cfg_nav5.fixMode = UBX_CFG_NAV5_FIXMODE_3D_ONLY;
313 // mask LSB=dyn|minEl|posFixMode|drLim|posMask|statisticHoldMask|dgpsMask|......|reservedBit0 = MSB
314 status->working_packet.message.payload.cfg_nav5.mask = UBX_CFG_NAV5_DYNMODEL + UBX_CFG_NAV5_FIXMODE;
316 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_NAV5, sizeof(ubx_cfg_nav5_t));
319 static void config_navx(uint16_t *bytes_to_send)
321 memset((uint8_t *)status->working_packet.buffer, 0, sizeof(UBXSentHeader_t) + sizeof(ubx_cfg_navx5_t));
322 status->working_packet.message.payload.cfg_navx5.useAOP = status->currentSettings.AssistNowAutonomous;
323 status->working_packet.message.payload.cfg_navx5.mask1 = UBX_CFG_NAVX5_AOP;
325 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_NAVX5, sizeof(ubx_cfg_navx5_t));
329 static void config_sbas(uint16_t *bytes_to_send)
331 memset((uint8_t *)status->working_packet.buffer, 0, sizeof(UBXSentHeader_t) + sizeof(ubx_cfg_sbas_t));
332 status->working_packet.message.payload.cfg_sbas.maxSBAS =
333 status->currentSettings.SBASChannelsUsed < 4 ? status->currentSettings.SBASChannelsUsed : 3;
334 status->working_packet.message.payload.cfg_sbas.usage =
335 (status->currentSettings.SBASCorrection ? UBX_CFG_SBAS_USAGE_DIFFCORR : 0) |
336 (status->currentSettings.SBASIntegrity ? UBX_CFG_SBAS_USAGE_INTEGRITY : 0) |
337 (status->currentSettings.SBASRanging ? UBX_CFG_SBAS_USAGE_RANGE : 0);
338 // If sbas is used for anything then set mode as enabled
339 status->working_packet.message.payload.cfg_sbas.mode =
340 status->working_packet.message.payload.cfg_sbas.usage != 0 ? UBX_CFG_SBAS_MODE_ENABLED : 0;
341 status->working_packet.message.payload.cfg_sbas.scanmode1 =
342 status->currentSettings.SBASSats == UBX_SBAS_SATS_WAAS ? UBX_CFG_SBAS_SCANMODE1_WAAS :
343 status->currentSettings.SBASSats == UBX_SBAS_SATS_EGNOS ? UBX_CFG_SBAS_SCANMODE1_EGNOS :
344 status->currentSettings.SBASSats == UBX_SBAS_SATS_MSAS ? UBX_CFG_SBAS_SCANMODE1_MSAS :
345 status->currentSettings.SBASSats == UBX_SBAS_SATS_GAGAN ? UBX_CFG_SBAS_SCANMODE1_GAGAN :
346 status->currentSettings.SBASSats == UBX_SBAS_SATS_SDCM ? UBX_CFG_SBAS_SCANMODE1_SDCM : UBX_SBAS_SATS_AUTOSCAN;
347 status->working_packet.message.payload.cfg_sbas.scanmode2 =
348 UBX_CFG_SBAS_SCANMODE2;
350 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_SBAS, sizeof(ubx_cfg_sbas_t));
354 static void config_gnss(uint16_t *bytes_to_send)
356 memset((uint8_t *)status->working_packet.buffer, 0, sizeof(UBXSentHeader_t) + sizeof(ubx_cfg_gnss_t));
357 status->working_packet.message.payload.cfg_gnss.numConfigBlocks = UBX_GNSS_ID_MAX;
358 status->working_packet.message.payload.cfg_gnss.numTrkChHw = (ubxHwVersion > UBX_HW_VERSION_7) ? UBX_CFG_GNSS_NUMCH_VER8 : UBX_CFG_GNSS_NUMCH_VER7;
359 status->working_packet.message.payload.cfg_gnss.numTrkChUse = status->working_packet.message.payload.cfg_gnss.numTrkChHw;
361 for (int32_t i = 0; i < UBX_GNSS_ID_MAX; i++) {
362 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].gnssId = i;
363 switch (i) {
364 case UBX_GNSS_ID_GPS:
365 if (status->currentSettings.enableGPS) {
366 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].flags = UBX_CFG_GNSS_FLAGS_ENABLED | UBX_CFG_GNSS_FLAGS_GPS_L1CA;
367 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].maxTrkCh = 16;
368 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].resTrkCh = 8;
370 break;
371 case UBX_GNSS_ID_QZSS:
372 if (status->currentSettings.enableGPS) {
373 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].flags = UBX_CFG_GNSS_FLAGS_ENABLED | UBX_CFG_GNSS_FLAGS_QZSS_L1CA;
374 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].maxTrkCh = 3;
375 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].resTrkCh = 0;
377 break;
378 case UBX_GNSS_ID_SBAS:
379 if (status->currentSettings.SBASCorrection || status->currentSettings.SBASIntegrity || status->currentSettings.SBASRanging) {
380 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].flags = UBX_CFG_GNSS_FLAGS_ENABLED | UBX_CFG_GNSS_FLAGS_SBAS_L1CA;
381 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].maxTrkCh = status->currentSettings.SBASChannelsUsed < 4 ? status->currentSettings.SBASChannelsUsed : 3;
382 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].resTrkCh = 1;
384 break;
385 case UBX_GNSS_ID_GLONASS:
386 if (status->currentSettings.enableGLONASS) {
387 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].flags = UBX_CFG_GNSS_FLAGS_ENABLED | UBX_CFG_GNSS_FLAGS_GLONASS_L1OF;
388 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].maxTrkCh = 14;
389 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].resTrkCh = 8;
391 break;
392 case UBX_GNSS_ID_BEIDOU:
393 if (status->currentSettings.enableBeiDou) {
394 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].flags = UBX_CFG_GNSS_FLAGS_ENABLED | UBX_CFG_GNSS_FLAGS_BEIDOU_B1I;
395 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].maxTrkCh = 14;
396 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].resTrkCh = 8;
398 break;
399 case UBX_GNSS_ID_GALILEO:
400 if (status->currentSettings.enableGalileo) {
401 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].flags = UBX_CFG_GNSS_FLAGS_ENABLED | UBX_CFG_GNSS_FLAGS_GALILEO_E1;
402 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].maxTrkCh = 10;
403 status->working_packet.message.payload.cfg_gnss.cfgBlocks[i].resTrkCh = 8;
405 break;
406 default:
407 break;
411 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_GNSS, sizeof(ubx_cfg_gnss_t));
415 static void config_save(uint16_t *bytes_to_send)
417 memset((uint8_t *)status->working_packet.buffer, 0, sizeof(UBXSentHeader_t) + sizeof(ubx_cfg_cfg_t));
418 // mask LSB=ioPort|msgConf|infMsg|navConf|rxmConf|||||rinvConf|antConf|....|= MSB
419 // ioPort=1, msgConf=2, infMsg=4, navConf=8, tpConf=0x10, sfdrConf=0x100, rinvConf=0x200, antConf=0x400
420 status->working_packet.message.payload.cfg_cfg.saveMask = UBX_CFG_CFG_OP_STORE_SETTINGS; // a list of settings we just set
421 status->working_packet.message.payload.cfg_cfg.clearMask = UBX_CFG_CFG_OP_CLEAR_SETTINGS; // everything else gets factory default
422 status->working_packet.message.payload.cfg_cfg.deviceMask = UBX_CFG_CFG_DEVICE_ALL;
424 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_CFG, sizeof(ubx_cfg_cfg_t));
427 static void configure(uint16_t *bytes_to_send)
429 switch (status->lastConfigSent) {
430 case LAST_CONFIG_SENT_START:
431 // increase message rates to 5 fixes per second
432 config_rate(bytes_to_send);
433 break;
435 case LAST_CONFIG_SENT_START + 1:
436 config_nav(bytes_to_send);
437 break;
439 case LAST_CONFIG_SENT_START + 2:
440 if (ubxHwVersion > UBX_HW_VERSION_5) {
441 config_navx(bytes_to_send);
442 break;
443 } else {
444 // Skip and fall through to next step
445 status->lastConfigSent++;
448 case LAST_CONFIG_SENT_START + 3:
449 if (status->currentSettings.enableGLONASS || status->currentSettings.enableGPS) {
450 config_gnss(bytes_to_send);
451 break;
452 } else {
453 // Skip and fall through to next step
454 status->lastConfigSent++;
456 // in the else case we must fall through because we must send something each time because successful send is tested externally
458 case LAST_CONFIG_SENT_START + 4:
459 config_sbas(bytes_to_send);
460 break;
462 default:
463 status->lastConfigSent = LAST_CONFIG_SENT_COMPLETED;
464 break;
469 static void enable_sentences(__attribute__((unused)) uint16_t *bytes_to_send)
471 int8_t msg = status->lastConfigSent + 1;
472 uint8_t msg_count = (ubxHwVersion >= UBX_HW_VERSION_7) ?
473 NELEMENTS(msg_config_ubx7) : NELEMENTS(msg_config_ubx6);
474 ubx_cfg_msg_t *msg_config = (ubxHwVersion >= UBX_HW_VERSION_7) ?
475 &msg_config_ubx7[0] : &msg_config_ubx6[0];
477 if (msg >= 0 && msg < msg_count) {
478 status->working_packet.message.payload.cfg_msg = msg_config[msg];
479 *bytes_to_send = prepare_packet((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_CFG, UBX_ID_CFG_MSG, sizeof(ubx_cfg_msg_t));
480 } else {
481 status->lastConfigSent = LAST_CONFIG_SENT_COMPLETED;
486 #if defined(AUTOBAUD_CONFIGURE_STORE_AND_DISABLE)
487 // permanently store our version of GPSSettings.UbxAutoConfig
488 // we use this to disable after AbConfigStoreAndDisable is complete
489 static void setGpsSettings()
491 // trying to do this as perfectly as possible we must realize that they may have pressed Send on some fields
492 // and so those fields are not stored permanently
493 // if we write the memory copy to flash, we will have made those permanent
495 // we could save off the uavo memory copy to a local buffer with a standard GPSSettingsGet()
496 // load from flash to uavo memory with a UAVObjLoad()
497 // update our one setting in uavo memory with a standard GPSSettingsUbxAutoConfigSet()
498 // save from uavo memory to flash with a UAVObjSave()
499 // modify our saved off copy to have our new setting in it too
500 // and finally copy the local buffer back out to uavo memory
502 // that would do it as correctly as possible, but it doesn't work
503 // so we do it the way autotune.c does it
505 #if 0
506 // get the "in memory" version to a local buffer
507 GPSSettingsGet((void *)&status->gpsSettings);
508 // load the permanent version into memory
509 UAVObjLoad(GPSSettingsHandle(), 0);
510 #endif
511 // change the in memory version of the field we want to change
512 GPSSettingsUbxAutoConfigSet((GPSSettingsUbxAutoConfigOptions *)&status->currentSettings.UbxAutoConfig);
513 // save the in memory version to permanent
514 UAVObjSave(GPSSettingsHandle(), 0);
515 #if 0
516 // copy the setting into the struct we will use to Set()
517 status->gpsSettings.UbxAutoConfig = status->currentSettings.UbxAutoConfig;
518 // try casting it correctly and it says:
519 // expected 'struct GPSSettingsData *' but argument is of type 'struct GPSSettingsData *'
520 // probably a volatile or align issue
521 GPSSettingsSet((void *)&status->gpsSettings); // set the "in memory" version back into use
522 #endif
524 #endif /* if defined(AUTOBAUD_CONFIGURE_STORE_AND_DISABLE) */
527 // 9600 baud and lower are not usable, and are best left at factory default
528 // if the user selects 9600
529 void gps_ubx_autoconfig_run(char * *buffer, uint16_t *bytes_to_send)
531 *bytes_to_send = 0;
532 *buffer = (char *)status->working_packet.buffer;
533 current_step_touched = false;
535 // autoconfig struct not yet allocated
536 if (!status) {
537 return;
540 // get UBX version whether autobaud / autoconfig is enabled or not
541 // this allows the user to manually try some baud rates and visibly see when it works
542 // it also is how the autobaud code determines when the baud rate is correct
543 // ubxHwVersion is a global set externally by the caller of this function
544 // it is set when the GPS responds to a MON_VER message
545 if (ubxHwVersion <= 0) {
546 // at low baud rates and high data rates the ubx gps simply must drop some outgoing data
547 // this isn't really an error
548 // and when a lot of data is being dropped, the MON VER reply often gets dropped
549 // on the other hand, uBlox documents that some versions discard data that is over 1 second old
550 // implying a 1 second send buffer and that it could be over 1 second before a reply is received
551 // later uBlox versions dropped this 1 second constraint and drop data when the send buffer is full
552 // and that could be even longer than 1 second
553 // send this more quickly and it will get a reply more quickly if a fixed percentage of replies are being dropped
555 // wait for the normal reply timeout before sending it over and over
556 if (PIOS_DELAY_DiffuS(status->lastStepTimestampRaw) < UBX_PARSER_TIMEOUT) {
557 return;
560 // at this point we have already waited for the MON_VER reply to time out (except the first time where it times out without being sent)
561 // and the fact we are here says that ubxHwVersion has not been set (it is set externally)
562 // so this try at this baud rate has failed
563 // if we get here
564 // select the next baud rate, skipping ahead if new baud rate is HwSettings.GPSSpeed
565 // set Revo baud rate to current++ value (immediate change so we can send right after that) and send the MON_VER request
566 // baud rate search order are most likely matches first
568 // if AutoBaud or higher, do AutoBaud
569 if (status->currentSettings.UbxAutoConfig >= GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUD) {
570 uint8_t baud_to_try;
571 static uint8_t baud_array[] = {
572 HWSETTINGS_GPSSPEED_57600,
573 HWSETTINGS_GPSSPEED_9600,
574 HWSETTINGS_GPSSPEED_115200,
575 HWSETTINGS_GPSSPEED_38400,
576 HWSETTINGS_GPSSPEED_19200,
577 HWSETTINGS_GPSSPEED_230400,
578 HWSETTINGS_GPSSPEED_4800,
579 HWSETTINGS_GPSSPEED_2400
582 // first try HwSettings.GPSSpeed and then
583 // get the next baud rate to try from the table, but skip over the value of HwSettings.GPSSpeed
584 do {
585 // index is inited to be out of bounds, which is interpreted as "currently defined baud rate" (= HwSettings.GPSSpeed)
586 if (baud_to_try_index >= sizeof(baud_array) / sizeof(baud_array[0])) {
587 HwSettingsGPSSpeedGet(&hwsettings_baud);
588 baud_to_try = hwsettings_baud;
589 baud_to_try_index = 0;
590 break;
591 } else {
592 baud_to_try = baud_array[baud_to_try_index++];
594 // skip HwSettings.GPSSpeed when you run across it in the list
595 } while (baud_to_try == hwsettings_baud);
596 // set the FC (Revo) baud rate
597 gps_set_fc_baud_from_arg(baud_to_try);
600 // this code is executed even if ubxautoconfig is disabled
601 // it detects the "sensor type" = type of GPS
602 // the user can use this to manually determine if the baud rate is correct
603 build_request((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_MON, UBX_ID_MON_VER, bytes_to_send);
604 // keep timeouts running properly, we (will have) just sent a packet that generates a reply
605 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
606 return;
609 if (!enabled) {
610 // keep resetting the timeouts here if we are not actually going to run the configure code
611 // not really necessary, but it keeps the timer from wrapping every 50 seconds
612 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
613 return; // autoconfig not enabled
616 ////////
617 // FSM
618 ////////
619 switch (status->currentStep) {
620 // if here, we have verified that the baud rates are in sync sometime in the past
621 case INIT_STEP_START:
622 // we should look for the GPS version again (user may plug in a different GPS and then do autoconfig again)
623 // zero retries for the next state that needs it (INIT_STEP_SAVE)
624 set_current_step_if_untouched(INIT_STEP_SEND_MON_VER);
625 // fall through to next state
626 // we can do that if we choose because we haven't sent any data in this state
627 // break;
629 case INIT_STEP_SEND_MON_VER:
630 build_request((UBXSentPacket_t *)&status->working_packet, UBX_CLASS_MON, UBX_ID_MON_VER, bytes_to_send);
631 // keep timeouts running properly, we (will have) just sent a packet that generates a reply
632 set_current_step_if_untouched(INIT_STEP_WAIT_MON_VER_ACK);
633 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
634 break;
636 case INIT_STEP_WAIT_MON_VER_ACK:
637 // wait for previous step
638 // extra wait time might well be unnecessary but we want to make sure
639 // that we don't stop waiting too soon
640 if (PIOS_DELAY_DiffuS(status->lastStepTimestampRaw) < UBX_UNVERIFIED_STEP_WAIT_TIME) {
641 return;
643 // Continue with next configuration option
644 set_current_step_if_untouched(INIT_STEP_RESET_GPS);
645 // fall through to next state
646 // we can do that if we choose because we haven't sent any data in this state
647 // break;
649 // if here, we have just verified that the baud rates are in sync (again)
650 case INIT_STEP_RESET_GPS:
651 // make sure we don't change the baud rate too soon and garble the packet being sent
652 // even after pios says the buffer is empty, the serial port buffer still has data in it
653 // and changing the baud will screw it up
654 // when the GPS is configured to send a lot of data, but has a low baud rate
655 // it has way too many messages to send and has to drop most of them
657 // Retrieve desired GPS baud rate once for use throughout this module
658 HwSettingsGPSSpeedGet(&hwsettings_baud);
659 #if !defined(ALWAYS_RESET)
660 // ALWAYS_RESET is undefined because it causes stored settings to change even with autoconfig.nostore
661 // but with it off, some settings may be enabled that should really be disabled (but aren't) after autoconfig.nostore
662 // if user requests a low baud rate then we just reset and avoid adding navigation sentences
663 // because low GPS baud and high OP data rate doesn't play nice
664 // if user requests that settings be saved, we will reset here too
665 // that makes sure that all strange settings are reset to factory default
666 // else these strange settings may persist because we don't reset all settings by table
667 if (status->currentSettings.UbxAutoConfig == GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDCONFIGUREANDSTORE
668 #if defined(AUTOBAUD_CONFIGURE_STORE_AND_DISABLE)
669 || status->currentSettings.UbxAutoConfig == GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDCONFIGURESTOREANDDISABLE
670 #endif
672 #endif
674 // reset all GPS parameters to factory default (configure low rate NMEA for low baud rates)
675 // this is not usable by OP code for either baud rate or types of messages sent
676 // but it starts up very quickly for use with autoconfig-nostore (which sets a high baud and enables all the necessary messages)
677 config_reset(bytes_to_send);
678 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
680 // else allow it enter the next state immmediately by not setting status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
681 set_current_step_if_untouched(INIT_STEP_REVO_9600_BAUD);
682 break;
684 // GPS was just reset, so GPS is running 9600 baud, and Revo is running whatever baud it was before
685 case INIT_STEP_REVO_9600_BAUD:
686 #if !defined(ALWAYS_RESET)
687 // if user requests a low baud rate then we just reset and leave it set to NMEA
688 // because low baud and high OP data rate doesn't play nice
689 // if user requests that settings be saved, we will reset here too
690 // that makes sure that all strange settings are reset to factory default
691 // else these strange settings may persist because we don't reset all settings by hand
692 if (status->currentSettings.UbxAutoConfig == GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDCONFIGUREANDSTORE
693 #if defined(AUTOBAUD_CONFIGURE_STORE_AND_DISABLE)
694 || status->currentSettings.UbxAutoConfig == GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDCONFIGURESTOREANDDISABLE
695 #endif
697 #endif
699 // wait for previous step
700 // extra wait time might well be unnecessary but we want to make very sure
701 // that we don't stop waiting too soon as that could leave us at an unknown baud rate
702 // (i.e. set or not set) if the the transmit buffer was full and we were running at a low baud rate
703 if (PIOS_DELAY_DiffuS(status->lastStepTimestampRaw) < UBX_UNVERIFIED_STEP_WAIT_TIME) {
704 return;
706 // set the Revo GPS port to 9600 baud to match the reset to factory default that has already been done
707 gps_set_fc_baud_from_arg(HWSETTINGS_GPSSPEED_9600);
709 // at most, we just set Revo baud and that doesn't send any data
710 // fall through to next state
711 // we can do that if we choose because we haven't sent any data in this state
712 // set_current_step_if_untouched(INIT_STEP_GPS_BAUD);
713 // allow it enter the next state immmediately by not setting status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
714 // break;
716 // Revo and GPS are both at 9600 baud
717 case INIT_STEP_GPS_BAUD:
718 // https://www.u-blox.com/images/downloads/Product_Docs/u-bloxM8_ReceiverDescriptionProtocolSpec_%28UBX-13003221%29_Public.pdf
719 // It is possible to change the current communications port settings using a UBX-CFG-CFG message. This could
720 // affect baud rate and other transmission parameters. Because there may be messages queued for transmission
721 // there may be uncertainty about which protocol applies to such messages. In addition a message currently in
722 // transmission may be corrupted by a protocol change. Host data reception parameters may have to be changed to
723 // be able to receive future messages, including the acknowledge message associated with the UBX-CFG-CFG message.
725 // so the message that changes the baud rate will send it's acknowledgement back at the new baud rate; this is not good.
726 // if your message was corrupted, you didn't change the baud rate and you have to guess; try pinging at both baud rates.
727 // also, you would have to change the baud rate instantly after the last byte of the sentence was sent,
728 // and you would have to poll the port in real time for that, and there may be messages ahead of the baud rate change.
730 // so we ignore the ack from this. it has proven to be reliable (with the addition of two dummy bytes after the packet)
732 // set the GPS internal baud rate to the user configured value
733 config_gps_baud(bytes_to_send);
734 set_current_step_if_untouched(INIT_STEP_REVO_BAUD);
735 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
736 break;
738 // GPS is at final baud and Revo is at old baud (old is 9600 or initial detected baud)
739 case INIT_STEP_REVO_BAUD:
740 // wait for previous step
741 if (PIOS_DELAY_DiffuS(status->lastStepTimestampRaw) < UBX_UNVERIFIED_STEP_WAIT_TIME) {
742 return;
744 // set the Revo GPS port baud rate to the (same) user configured value
745 gps_set_fc_baud_from_arg(hwsettings_baud);
746 status->lastConfigSent = LAST_CONFIG_SENT_START;
747 // zero the retries for the first "enable sentence"
748 status->retryCount = 0;
749 // skip enabling UBX sentences for low baud rates
750 // low baud rates are not usable, and higher data rates just makes it harder for this code to change the configuration
751 if (hwsettings_baud <= HWSETTINGS_GPSSPEED_9600) {
752 set_current_step_if_untouched(INIT_STEP_SAVE);
753 } else {
754 set_current_step_if_untouched(INIT_STEP_ENABLE_SENTENCES);
756 // allow it enter the next state immmediately by not setting status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
757 break;
759 case INIT_STEP_ENABLE_SENTENCES:
760 case INIT_STEP_CONFIGURE:
762 bool step_configure = (status->currentStep == INIT_STEP_CONFIGURE);
763 if (step_configure) {
764 configure(bytes_to_send);
765 } else {
766 enable_sentences(bytes_to_send);
769 // for some branches, allow it enter the next state immmediately by not setting status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
770 if (status->lastConfigSent == LAST_CONFIG_SENT_COMPLETED) {
771 if (step_configure) {
772 // zero retries for the next state that needs it (INIT_STEP_SAVE)
773 status->retryCount = 0;
774 set_current_step_if_untouched(INIT_STEP_SAVE);
775 } else {
776 // finished enabling sentences, now configure() needs to start at the beginning
777 status->lastConfigSent = LAST_CONFIG_SENT_START;
778 set_current_step_if_untouched(INIT_STEP_CONFIGURE);
780 } else {
781 set_current_step_if_untouched(step_configure ? INIT_STEP_CONFIGURE_WAIT_ACK : INIT_STEP_ENABLE_SENTENCES_WAIT_ACK);
782 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
784 break;
787 case INIT_STEP_ENABLE_SENTENCES_WAIT_ACK:
788 case INIT_STEP_CONFIGURE_WAIT_ACK: // Wait for an ack from GPS
790 bool step_configure = (status->currentStep == INIT_STEP_CONFIGURE_WAIT_ACK);
791 if (ubxLastAck.clsID == status->requiredAck.clsID && ubxLastAck.msgID == status->requiredAck.msgID) {
792 // Continue with next configuration option
793 // start retries over for the next setting to be sent
794 status->retryCount = 0;
795 status->lastConfigSent++;
796 } else if (PIOS_DELAY_DiffuS(status->lastStepTimestampRaw) < UBX_REPLY_TIMEOUT &&
797 (ubxLastNak.clsID != status->requiredAck.clsID || ubxLastNak.msgID != status->requiredAck.msgID)) {
798 // allow timeouts to count up by not setting status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
799 break;
800 } else {
801 // timeout or NAK, resend the message or abort
802 status->retryCount++;
803 if (status->retryCount > UBX_MAX_RETRIES) {
804 set_current_step_if_untouched(INIT_STEP_PRE_ERROR);
805 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
806 break;
809 // success or failure here, retries are handled elsewhere
810 if (step_configure) {
811 set_current_step_if_untouched(INIT_STEP_CONFIGURE);
812 } else {
813 set_current_step_if_untouched(INIT_STEP_ENABLE_SENTENCES);
815 break;
818 // all configurations have been made
819 case INIT_STEP_SAVE:
820 // now decide whether to save them permanently into the GPS
821 if (status->currentSettings.UbxAutoConfig == GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDCONFIGUREANDSTORE
822 #if defined(AUTOBAUD_CONFIGURE_STORE_AND_DISABLE)
823 || status->currentSettings.UbxAutoConfig == GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDCONFIGURESTOREANDDISABLE
824 #endif
826 config_save(bytes_to_send);
827 set_current_step_if_untouched(INIT_STEP_SAVE_WAIT_ACK);
828 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
829 } else {
830 set_current_step_if_untouched(INIT_STEP_PRE_DONE);
831 // allow it enter INIT_STEP_PRE_DONE immmediately by not setting status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
833 break;
835 // command to save configuration has already been issued
836 case INIT_STEP_SAVE_WAIT_ACK:
837 // save doesn't appear to respond, even in 24 seconds
838 // just delay a while, in case there it is busy with a flash write, etc.
839 if (PIOS_DELAY_DiffuS(status->lastStepTimestampRaw) < UBX_SAVE_WAIT_TIME) {
840 return;
842 // fall through to next state
843 // we can do that if we choose because we haven't sent any data in this state
844 set_current_step_if_untouched(INIT_STEP_PRE_DONE);
845 // break;
847 // the autoconfig has completed normally
848 case INIT_STEP_PRE_DONE:
849 #if defined(AUTOBAUD_CONFIGURE_STORE_AND_DISABLE)
850 // determine if we need to disable autoconfig via the autoconfig==AUTOBAUDCONFIGSTOREANDDISABLE setting
851 if (status->currentSettings.UbxAutoConfig == GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDCONFIGURESTOREANDDISABLE) {
852 enabled = false;
853 status->currentSettings.UbxAutoConfig = GPSSETTINGS_UBXAUTOCONFIG_DISABLED;
854 // like it says
855 setGpsSettings();
857 #endif
858 set_current_step_if_untouched(INIT_STEP_DONE);
859 break;
861 // an error, such as retries exhausted, has occurred
862 case INIT_STEP_PRE_ERROR:
863 // on error we should get the GPS version immediately
864 gps_ubx_reset_sensor_type();
865 set_current_step_if_untouched(INIT_STEP_ERROR);
866 break;
868 case INIT_STEP_DONE:
869 case INIT_STEP_ERROR:
870 case INIT_STEP_DISABLED:
871 break;
876 // (re)init the autoconfig stuff so that it will run if called
878 // this can be called from a different thread
879 // so everything it touches must be declared volatile
880 void gps_ubx_autoconfig_set(ubx_autoconfig_settings_t *config)
882 initSteps_t new_step;
884 enabled = false;
886 if (!status) {
887 status = (status_t *)pios_malloc(sizeof(status_t));
888 PIOS_Assert(status);
889 memset((status_t *)status, 0, sizeof(status_t));
892 // if caller used NULL, just use current settings to restart autoconfig process
893 if (config != NULL) {
894 status->currentSettings = *config;
896 if (status->currentSettings.UbxAutoConfig >= GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDANDCONFIGURE) {
897 new_step = INIT_STEP_START;
898 } else {
899 new_step = INIT_STEP_DISABLED;
901 status->lastStepTimestampRaw = PIOS_DELAY_GetRaw();
903 // assume this one byte initSteps_t is atomic
904 // take care of some but not all concurrency issues
906 status->currentStep = new_step;
907 status->currentStepSave = new_step;
908 current_step_touched = true;
909 status->currentStep = new_step;
910 status->currentStepSave = new_step;
912 // this forces the sensor type detection to occur outside the FSM
913 // and _can_ also engage the autobaud detection that is outside the FSM
914 // don't do it if FSM is enabled as FSM can change the baud itself
915 // (don't do it because the baud rates are already in sync)
916 gps_ubx_reset_sensor_type();
918 if (status->currentSettings.UbxAutoConfig >= GPSSETTINGS_UBXAUTOCONFIG_AUTOBAUDANDCONFIGURE) {
919 // enabled refers to autoconfigure
920 // note that sensor type (gps type) detection happens even if completely disabled
921 // also note that AutoBaud is less than AutoBaudAndConfigure
922 enabled = true;
927 int32_t ubx_autoconfig_get_status()
929 if (!status || !enabled) {
930 return UBX_AUTOCONFIG_STATUS_DISABLED;
932 switch (status->currentStep) {
933 case INIT_STEP_ERROR:
934 return UBX_AUTOCONFIG_STATUS_ERROR;
936 case INIT_STEP_DISABLED:
937 return UBX_AUTOCONFIG_STATUS_DISABLED;
939 case INIT_STEP_DONE:
940 return UBX_AUTOCONFIG_STATUS_DONE;
942 default:
943 break;
945 return UBX_AUTOCONFIG_STATUS_RUNNING;