-
Notifications
You must be signed in to change notification settings - Fork 0
/
UbxGpsI2C.cpp
470 lines (346 loc) · 11.8 KB
/
UbxGpsI2C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
MIT License
Copyright (c) 2024 Pavel Slama
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "UbxGpsI2C.h"
UbxGpsI2C::UbxGpsI2C(int8_t address) : _i2c(nullptr), _i2c_addr(address) {
MBED_STATIC_ASSERT((UBX_HEADER_LEN + sizeof(cfg_prt_t) + UBX_CHECKSUM_LEN) < sizeof(_tx_buffer),
"TX buffer size is too small");
}
UbxGpsI2C::UbxGpsI2C(PinName sda, PinName scl, int8_t address, uint32_t frequency) : _i2c_addr(address) {
MBED_STATIC_ASSERT((UBX_HEADER_LEN + sizeof(cfg_prt_t) + UBX_CHECKSUM_LEN) < sizeof(_tx_buffer),
"TX buffer size is too small");
_i2c = new (_i2c_obj) I2C(sda, scl);
_i2c->frequency(frequency);
}
UbxGpsI2C::~UbxGpsI2C() {}
bool UbxGpsI2C::init(Callback<void()> cb, I2C *i2c_obj) {
ubx_info("Setting up ublox GPS");
if (i2c_obj != nullptr) {
_i2c = i2c_obj;
}
MBED_ASSERT(_i2c);
_cb = cb;
// get protocol version
char ver[6] = {0};
if (!protocol_version(ver)) {
return false;
}
float version = strtof(ver, nullptr);
if (version > 23.01) {
_new_cfg = true;
}
if (!_new_cfg) {
cfg_prt_t cfg_prt = {0};
cfg_prt.mode = UBX_DEFAULT_ADDRESS;
cfg_prt.inProtoMask = 1; // input only UBX
cfg_prt.outProtoMask = 1; // output only UBX
cfg_prt.flags = 0b10; // enable extendedTxTimeout
return send_ack(UBX_CFG, UBX_CFG_PRT, &cfg_prt, sizeof(cfg_prt_t));
}
// TODO new cfg
return false;
}
bool UbxGpsI2C::poll() {
int available = bytes_available();
if (available < 0 || !_mutex.trylock()) {
ubx_error("Invalid");
return false;
}
_rx_buffer_len = available;
_mutex.unlock(); // in case we succeeded
if (available > 0) {
return _i2c->transfer(_i2c_addr, nullptr, 0, _rx_buffer, available, callback(this, &UbxGpsI2C::rx_cb),
I2C_EVENT_ALL) == 0;
}
return true;
};
bool UbxGpsI2C::auto_send(UbxClassId class_id, char id, uint8_t rate, Callback<void()> cb) {
ubx_info("Autosend request, class: %02X id: %02X", class_id, id);
if (rate > 0) {
this->oob(class_id, id, cb);
} else {
this->remove_oob(class_id, id);
}
if (!_new_cfg) {
cfg_msg_t cfg_msg = {.classId = class_id, .id = id, .rate = rate};
return send_ack(UBX_CFG, UBX_CFG_MSG, &cfg_msg, sizeof(cfg_msg_t));
}
// TODO new cfg
return false;
}
void UbxGpsI2C::process() {
_mutex.lock();
this->parse(_rx_buffer, _rx_buffer_len);
_mutex.unlock();
}
bool UbxGpsI2C::set_output_rate(milliseconds ms, uint16_t cycles) {
ubx_info("Setting output rate");
if (ms < 50ms || cycles > 127 || ms > 65535ms) {
ubx_error("Wrong parameter");
return false;
}
if (!_new_cfg) {
cfg_rate_t cfg_rate = {
.measRate = (uint16_t)duration_cast<milliseconds>(ms).count(),
.navRate = cycles,
.timeRef = 0 // UTC
};
return send_ack(UBX_CFG, UBX_CFG_RATE, &cfg_rate, sizeof(cfg_rate));
}
// TODO new cfg
return false;
}
bool UbxGpsI2C::set_odometer(bool enable, OdoCfgProfile profile, uint8_t velocity_filter) {
ubx_info("Setting odometer");
if (!_new_cfg) {
if (!send(UBX_CFG, UBX_CFG_ODO)) {
return false;
}
if (!get(UBX_CFG, UBX_CFG_ODO)) {
return false;
}
cfg_odo_t cfg_odo;
memcpy(&cfg_odo, _tx_buffer + 2, sizeof(cfg_odo_t));
cfg_odo.odoCfg &= ~0b111;
cfg_odo.odoCfg |= (uint8_t)profile;
if (velocity_filter > 0) {
cfg_odo.flags |= 0b100;
} else {
cfg_odo.flags &= ~0b100;
}
cfg_odo.velLpGain = velocity_filter;
if (enable) {
cfg_odo.flags |= 0b1;
} else {
cfg_odo.flags &= ~0b1;
}
return send_ack(UBX_CFG, UBX_CFG_ODO, &cfg_odo, sizeof(cfg_odo_t));
}
// TODO new cfg
return false;
}
bool UbxGpsI2C::set_power_mode(PowerSetupValue mode, uint16_t period, uint16_t on_time) {
ubx_info("Setting power mode");
if (!_new_cfg) {
if (!send(UBX_CFG, UBX_CFG_PMS)) {
return false;
}
if (!get(UBX_CFG, UBX_CFG_PMS)) {
return false;
}
cfg_pms_t cfg_pms;
memcpy(&cfg_pms, _tx_buffer + 2, sizeof(cfg_pms_t));
cfg_pms.powerSetupValue = (uint8_t)mode;
cfg_pms.period = (mode == PSV_INTERVAL) ? period : 0;
cfg_pms.onTime = (mode == PSV_INTERVAL) ? on_time : 0;
return send_ack(UBX_CFG, UBX_CFG_PMS, &cfg_pms, sizeof(cfg_pms_t));
}
// TODO new cfg
return false;
}
bool UbxGpsI2C::set_psm(bool low_power) {
ubx_info("Setting low power");
if (!_new_cfg) {
if (!send(UBX_CFG, UBX_CFG_RXM)) {
return false;
}
if (!get(UBX_CFG, UBX_CFG_RXM)) {
return false;
}
cfg_rxm_t cfg_rxm;
memcpy(&cfg_rxm, _tx_buffer + 2, sizeof(cfg_rxm_t));
cfg_rxm.lpMode = low_power;
return send_ack(UBX_CFG, UBX_CFG_RXM, &cfg_rxm, sizeof(cfg_rxm_t));
}
// TODO new cfg
return false;
}
bool UbxGpsI2C::reset(ResetMode mode, uint16_t bbr_mask) {
ubx_info("Resetting");
if (!_new_cfg) {
cfg_rst_t cfg_rst = {.navBbrMask = bbr_mask, .resetMode = mode, .reserved1 = 0};
return send(UBX_CFG, UBX_CFG_RST, &cfg_rst, sizeof(cfg_rst_t));
}
// TODO new cfg
return false;
}
bool UbxGpsI2C::reset_odometer() {
ubx_info("Resetting odometer");
return send_ack(UBX_NAV, UBX_NAV_RESETODO);
}
bool UbxGpsI2C::permanent_configuration(PermanentConfig type, uint32_t mask, uint8_t device_mask) {
ubx_info("Setting permanent configuration");
if (!_new_cfg) {
cfg_cfg_t cfg_cfg = {.clearMask = 0, .saveMask = 0, .loadMask = 0, .deviceMask = device_mask};
switch (type) {
case Clear:
cfg_cfg.clearMask = mask;
break;
case Save:
cfg_cfg.saveMask = mask;
break;
case Load:
cfg_cfg.loadMask = mask;
break;
}
return send_ack(UBX_CFG, UBX_CFG_CFG, &cfg_cfg, sizeof(cfg_cfg_t));
}
// TODO new cfg
return false;
}
bool UbxGpsI2C::set_dynamic_model(DynamicModel model) {
ubx_info("Setting low power");
if (!_new_cfg) {
if (!send(UBX_CFG, UBX_CFG_NAV5)) {
return false;
}
if (!get(UBX_CFG, UBX_CFG_NAV5)) {
return false;
}
cfg_nav5_t cfg_nav5;
memcpy(&cfg_nav5, _tx_buffer + 2, sizeof(cfg_nav5_t));
cfg_nav5.dynModel = model;
return send_ack(UBX_CFG, UBX_CFG_NAV5, &cfg_nav5, sizeof(cfg_nav5_t));
}
// TODO new cfg
return false;
}
bool UbxGpsI2C::wakeup() {
_tx_buffer[0] = 0xFF;
int32_t ack = _i2c->write(_i2c_addr, _tx_buffer, 1);
return (ack == 0);
}
bool UbxGpsI2C::protocol_version(char *version) {
ubx_info("Getting procotol version");
if (!send(UBX_MON, UBX_MON_VER)) {
return false;
}
if (!get(UBX_MON, UBX_MON_VER)) {
return false;
}
ubx_info("Protocol version: %.5s", _tx_buffer + 2);
if (version != nullptr) {
memcpy(version, _tx_buffer + 2, 5);
}
return true;
}
int UbxGpsI2C::bytes_available() {
_tx_buffer[0] = 0xFD;
int32_t ack = _i2c->write(UBX_DEFAULT_ADDRESS, _tx_buffer, 1, true);
if (ack != 0) {
return -1;
}
ack = _i2c->read(UBX_DEFAULT_ADDRESS, _tx_buffer, 2);
if (ack != 0) {
return -1;
}
uint16_t bytes_available = (uint16_t)_tx_buffer[0] << 8 | _tx_buffer[1];
ubx_debug("Bytes available: %u", bytes_available);
return min(bytes_available, (uint16_t)sizeof(_rx_buffer));
}
bool UbxGpsI2C::read_sync(int length) {
bool ok = false;
_mutex.lock();
int ack = _i2c->read(UBX_DEFAULT_ADDRESS, _rx_buffer, length);
ubx_debug("Read: %s", tr_array((uint8_t *)_rx_buffer, length));
if (ack == 0) {
ok = true;
this->parse(_rx_buffer, length);
}
_mutex.unlock();
return ok;
}
bool UbxGpsI2C::send(UbxClassId class_id, char id, const void *payload, uint16_t payload_len) {
if ((uint16_t)(UBX_HEADER_LEN + payload_len + UBX_CHECKSUM_LEN) > sizeof(_tx_buffer)) {
ubx_error("TX buffer overflow");
return false;
}
uint16_t len = this->buildMessage(_tx_buffer, class_id, id, payload, payload_len);
ubx_debug("Sending[%u]: %s", len, tr_array((uint8_t *)_tx_buffer, len));
return _i2c->write(_i2c_addr, _tx_buffer, len) == 0;
}
bool UbxGpsI2C::send_ack(UbxClassId class_id, char id, const void *payload, uint16_t payload_len) {
if (!send(class_id, id, payload, payload_len)) {
return false;
}
if (!get(UBX_ACK, UBX_ACK_ACK)) {
return false;
}
bool ok =
_tx_buffer[0] == UBX_ACK && _tx_buffer[1] == UBX_ACK_ACK && _tx_buffer[2] == class_id && _tx_buffer[3] == id;
ubx_debug("ACK OK: %u", ok);
return ok;
}
void UbxGpsI2C::rx_cb(int event) { // ISR
if (event > I2C_EVENT_ERROR_NO_SLAVE) {
_cb();
}
}
void UbxGpsI2C::done_cb() {
_tx_buffer[0] = msg.classId;
_tx_buffer[1] = msg.id;
if (msg.classId == UBX_MON && msg.id == UBX_MON_VER) {
const char protver[] = "PROTVER=";
const uint16_t protver_len = strlen(protver);
for (uint16_t i = 0; i < msg.length - protver_len + 1; ++i) {
if (memcmp(msg.data.get() + i, protver, protver_len) == 0) {
memcpy(_tx_buffer + 2, msg.data.get() + i + protver_len, 5);
_done_cb_called = true;
}
}
} else {
memcpy(_tx_buffer + 2, msg.data.get(), min(msg.length, (uint16_t)sizeof(_tx_buffer)));
_done_cb_called = true;
}
}
bool UbxGpsI2C::get(UbxClassId class_id, char id) {
bool ok = false;
uint8_t try_counter = 0;
this->oob(class_id, id, callback(this, &UbxGpsI2C::done_cb));
while (1) {
int len = bytes_available();
if (len == 0) {
try_counter++;
if (try_counter >= MBED_CONF_UBXGPS_REPEAT_COUNT) {
ubx_warning("This was last try");
break;
} else {
ubx_debug("Data not yet ready, will wait");
ThisThread::sleep_for(milliseconds{MBED_CONF_UBXGPS_REPEAT_DELAY});
}
} else if (len == -1) {
ubx_error("Invalid");
break;
} else {
bool status = read_sync(len);
if (!status) {
ubx_error("Read error");
break;
}
if (_done_cb_called) {
_done_cb_called = false;
ok = true;
ubx_info("Internal message");
break;
}
}
}
this->remove_oob(class_id, id);
return ok;
}