17
17
https://github.com/sparkfun/SparkFun_Ublox_Arduino_Library
18
18
19
19
Development environment specifics:
20
- Arduino IDE 1.8.5
20
+ NCS v1.0.3 release - this port
21
+
22
+ This port was made by Vid Rajtmajer <vid@irnas.eu>, www.irnas.eu
21
23
22
24
SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
23
25
The MIT License (MIT)
39
41
*/
40
42
41
43
#include " SparkFun_Ublox_Zephyr_Library.h"
42
-
43
44
#include < drivers/gpio.h>
44
45
45
46
SFE_UBLOX_GPS::SFE_UBLOX_GPS (void )
@@ -49,18 +50,24 @@ SFE_UBLOX_GPS::SFE_UBLOX_GPS(void)
49
50
moduleQueried.versionNumber = false ;
50
51
}
51
52
52
- bool SFE_UBLOX_GPS::init_gpio_pins (struct device &gpio_dev) {
53
+ // Get i2c device to the class and configure checksumFailurePin
54
+ bool SFE_UBLOX_GPS::init_gpio_pins (struct device &gpio_dev)
55
+ {
53
56
_gpio_dev = &gpio_dev;
54
-
55
57
if (checksumFailurePin >= 0 )
56
58
{
57
- gpio_pin_configure (_gpio_dev, (uint8_t )checksumFailurePin, GPIO_OUTPUT);
58
- gpio_pin_set (_gpio_dev, (uint8_t )checksumFailurePin, HIGH);
59
+ int err;
60
+ err = gpio_pin_configure (_gpio_dev, (uint8_t )checksumFailurePin, GPIO_OUTPUT);
61
+ err = gpio_pin_set (_gpio_dev, (uint8_t )checksumFailurePin, HIGH);
62
+ if (err)
63
+ {
64
+ return false ;
65
+ }
59
66
}
60
67
return true ;
61
68
}
62
69
63
- // Initialize the Serial port
70
+ // Initialize the I2C port
64
71
bool SFE_UBLOX_GPS::begin (struct device &i2c_dev, uint8_t deviceAddress)
65
72
{
66
73
commType = COMM_TYPE_I2C;
@@ -79,8 +86,8 @@ bool SFE_UBLOX_GPS::begin(struct device &i2c_dev, uint8_t deviceAddress)
79
86
return (isConnected ());
80
87
}
81
88
82
- // Initialize the Serial port
83
- /* // TODO
89
+ // Initialize the Serial port - function not ported
90
+ /*
84
91
bool SFE_UBLOX_GPS::begin(Stream &serialPort)
85
92
{
86
93
commType = COMM_TYPE_SERIAL;
@@ -213,37 +220,28 @@ void SFE_UBLOX_GPS::hardReset()
213
220
sendCommand (&packetCfg, 0 ); // don't expect ACK
214
221
}
215
222
216
- int SFE_UBLOX_GPS::transferWriteI2C (u8_t *buffer, u32_t num_bytes, bool stop) {
223
+ // Write data to I2C, Arguments: buffer - data to write, num_bytes - buffer length,
224
+ // stop - if true send STOP, if false send RESTART after message
225
+ int SFE_UBLOX_GPS::transferWriteI2C (u8_t *buffer, u32_t num_bytes, bool stop)
226
+ {
217
227
struct i2c_msg msgs[1 ];
218
- /* Send the address to write to */
219
- // msgs[0].buf = 0x42;
220
- // msgs[0].len = 1;
221
- // msgs[0].flags = I2C_MSG_WRITE;
222
-
223
- // printk("transferWriteI2C: %x, num_bytes: %d\n", buffer[0], num_bytes);
224
-
225
- /* Data to be written, and STOP or RESTART after this. */
228
+ // Data to be written and STOP or RESTART after this.
226
229
msgs[0 ].buf = buffer;
227
230
msgs[0 ].len = num_bytes;
228
231
if (stop) {
229
232
msgs[0 ].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
230
- // printk("transferWriteI2C - I2C_MSG_STOP\n");
231
233
}
232
234
else {
233
235
msgs[0 ].flags = I2C_MSG_WRITE | I2C_MSG_RESTART;
234
- // printk("transferWriteI2C - I2C_MSG_RESTART\n");
235
236
}
236
237
return i2c_transfer (_i2cPort, &msgs[0 ], 1 , _gpsI2Caddress);
237
238
}
238
239
239
- int SFE_UBLOX_GPS::transferReadI2C (u8_t *buffer, u32_t num_bytes) {
240
+ // Read data from I2C, Arguments: buffer - store read data, num_bytes - buffer length
241
+ int SFE_UBLOX_GPS::transferReadI2C (u8_t *buffer, u32_t num_bytes)
242
+ {
240
243
struct i2c_msg msgs[1 ];
241
- /* Send the address to write to */
242
- // msgs[0].buf = (u8_t)_gpsI2Caddress;
243
- // msgs[0].len = 2U;
244
- // msgs[0].flags = I2C_MSG_READ;
245
-
246
- /* Data to be read, and STOP or RESTART after this. */
244
+ // Data to be read and STOP after this
247
245
msgs[0 ].buf = buffer;
248
246
msgs[0 ].len = num_bytes;
249
247
msgs[0 ].flags = I2C_MSG_READ | I2C_MSG_STOP;
@@ -341,42 +339,29 @@ bool SFE_UBLOX_GPS::checkUbloxI2C(ubxPacket *incomingUBX, uint8_t requestedClass
341
339
{
342
340
if (k_uptime_get_32 () - lastCheck >= i2cPollingWait)
343
341
{
344
- int err;
345
- // Get the number of bytes available from the module
346
342
uint16_t bytesAvailable = 0 ;
347
- /*
348
- _i2cPort->beginTransmission(_gpsI2Caddress); // TODO
349
- _i2cPort->write(0xFD); //0xFD (MSB) and 0xFE (LSB) are the registers that contain number of bytes available
350
- if (_i2cPort->endTransmission(false) != 0) //Send a restart command. Do not release bus.
351
- return (false); //Sensor did not ACK
352
- */
343
+ int err;
353
344
345
+ // Get the number of bytes available from the module
346
+ // 0xFD (MSB) and 0xFE (LSB) are the registers that contain number of bytes available
354
347
u8_t data_buffer[1 ];
355
348
data_buffer[0 ] = 0xFD ;
356
- // err = i2c_write(_i2cPort, data_buffer, 1, _gpsI2Caddress);
357
349
err = transferWriteI2C (data_buffer, 1 , true );
358
350
if (err) {
359
351
return (false );
360
352
}
361
353
362
- // _i2cPort->requestFrom((uint8_t)_gpsI2Caddress, (uint8_t)2); // TODO
363
- // if (_i2cPort->available())
364
354
bool data_read_ok = true ;
365
355
u8_t read_buffer[2 ];
366
- // err = i2c_read(_i2cPort, read_buffer, 2, _gpsI2Caddress);
367
356
err = transferReadI2C (data_buffer, 2 );
368
357
if (err) {
369
358
data_read_ok = false ;
370
359
}
371
360
372
361
if (data_read_ok)
373
362
{
374
- // uint8_t msb = _i2cPort->read(); // TODO
375
- // uint8_t lsb = _i2cPort->read();
376
363
uint8_t msb = read_buffer[0 ];
377
364
uint8_t lsb = read_buffer[1 ];
378
- // printk("checkUbloxI2C, data_read_ok, MSB: 0x%x LSB: 0x%x\n", msb, lsb);
379
-
380
365
if (lsb == 0xFF )
381
366
{
382
367
// I believe this is a Ublox bug. Device should never present an 0xFF.
@@ -443,15 +428,9 @@ bool SFE_UBLOX_GPS::checkUbloxI2C(ubxPacket *incomingUBX, uint8_t requestedClass
443
428
444
429
while (bytesAvailable)
445
430
{
446
- /*
447
- _i2cPort->beginTransmission(_gpsI2Caddress); // TODO
448
- _i2cPort->write(0xFF); //0xFF is the register to read data from
449
- if (_i2cPort->endTransmission(false) != 0) //Send a restart command. Do not release bus.
450
- return (false); //Sensor did not ACK
451
- */
452
431
u8_t data_buffer[1 ];
453
- data_buffer[0 ] = 0xFF ;
454
- err = transferWriteI2C (data_buffer, 1 , false );
432
+ data_buffer[0 ] = 0xFF ; // 0xFF is the register to read data from
433
+ err = transferWriteI2C (data_buffer, 1 , false ); // Send a restart command. Do not release bus.
455
434
if (err) {
456
435
return (false );
457
436
}
@@ -462,8 +441,6 @@ bool SFE_UBLOX_GPS::checkUbloxI2C(ubxPacket *incomingUBX, uint8_t requestedClass
462
441
bytesToRead = I2C_BUFFER_LENGTH;
463
442
464
443
TRY_AGAIN:
465
- // _i2cPort->requestFrom((uint8_t)_gpsI2Caddress, (uint8_t)bytesToRead); // TODO
466
- // if (_i2cPort->available())
467
444
bool data_read_ok = true ;
468
445
u8_t read_buffer[(uint8_t )bytesToRead];
469
446
err = transferReadI2C (read_buffer, (uint8_t )bytesToRead);
@@ -475,9 +452,7 @@ bool SFE_UBLOX_GPS::checkUbloxI2C(ubxPacket *incomingUBX, uint8_t requestedClass
475
452
{
476
453
for (uint16_t x = 0 ; x < bytesToRead; x++)
477
454
{
478
- // uint8_t incoming = _i2cPort->read(); //Grab the actual character // TODO
479
- // i2c_read(_i2cPort, read_buffer, 1, _gpsI2Caddress); //Grab the actual character
480
- uint8_t incoming = read_buffer[x];
455
+ uint8_t incoming = read_buffer[x]; // Grab the actual character
481
456
482
457
// Check to see if the first read is 0x7F. If it is, the module is not ready
483
458
// to respond. Stop, wait, and try again
@@ -1028,7 +1003,7 @@ void SFE_UBLOX_GPS::processUBXpacket(ubxPacket *msg)
1028
1003
highResModuleQueried.verticalAccuracy = true ;
1029
1004
moduleQueried.gpsiTOW = true ; // this can arrive via HPPOS too.
1030
1005
1031
- if (_printDebug == true ) // TODO check float prints
1006
+ if (_printDebug == true )
1032
1007
{
1033
1008
printk (" Sec: %f" , ((float )extractLong (4 )) / 1000 .0f );
1034
1009
printk (" \n " );
@@ -1115,40 +1090,21 @@ sfe_ublox_status_e SFE_UBLOX_GPS::sendI2cCommand(ubxPacket *outgoingUBX, uint16_
1115
1090
{
1116
1091
int err;
1117
1092
// Point at 0xFF data register
1118
- /* // TODO
1119
- _i2cPort->beginTransmission((uint8_t)_gpsI2Caddress); //There is no register to write to, we just begin writing data bytes
1120
- _i2cPort->write(0xFF);
1121
- if (_i2cPort->endTransmission() != 0) //Don't release bus
1122
- return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); //Sensor did not ACK
1123
- */
1124
1093
u8_t small_buffer[1 ];
1125
1094
small_buffer[0 ] = 0xFF ;
1126
- // err = i2c_write(_i2cPort, small_buffer, 1, _gpsI2Caddress);
1127
- err = transferWriteI2C (small_buffer, 1 );
1095
+ err = transferWriteI2C (small_buffer, 1 ); // There is no register to write to, we just begin writing data bytes
1128
1096
if (err) {
1129
1097
return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); // Sensor did not ACK
1130
1098
}
1131
1099
1132
1100
// Write header bytes
1133
- /* // TODO
1134
- _i2cPort->beginTransmission((uint8_t)_gpsI2Caddress); //There is no register to write to, we just begin writing data bytes
1135
- _i2cPort->write(UBX_SYNCH_1); //μ - oh ublox, you're funny. I will call you micro-blox from now on.
1136
- _i2cPort->write(UBX_SYNCH_2); //b
1137
- _i2cPort->write(outgoingUBX->cls);
1138
- _i2cPort->write(outgoingUBX->id);
1139
- _i2cPort->write(outgoingUBX->len & 0xFF); //LSB
1140
- _i2cPort->write(outgoingUBX->len >> 8); //MSB
1141
- if (_i2cPort->endTransmission(false) != 0) //Do not release bus
1142
- return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); //Sensor did not ACK
1143
- */
1144
1101
u8_t header_buffer[6 ]; // There is no register to write to, we just begin writing data bytes
1145
1102
header_buffer[0 ] = UBX_SYNCH_1; // μ - oh ublox, you're funny. I will call you micro-blox from now on.
1146
1103
header_buffer[1 ] = UBX_SYNCH_2; // b
1147
1104
header_buffer[2 ] = outgoingUBX->cls ;
1148
1105
header_buffer[3 ] = outgoingUBX->id ;
1149
1106
header_buffer[4 ] = outgoingUBX->len & 0xFF ; // LSB
1150
1107
header_buffer[5 ] = outgoingUBX->len >> 8 ; // MSB
1151
- // err = i2c_write(_i2cPort, header_buffer, 6, _gpsI2Caddress);
1152
1108
1153
1109
err = transferWriteI2C (header_buffer, 6 , false );
1154
1110
k_msleep (1 ); // added this millisecond sleep to not progress too fast
@@ -1168,22 +1124,12 @@ sfe_ublox_status_e SFE_UBLOX_GPS::sendI2cCommand(ubxPacket *outgoingUBX, uint16_
1168
1124
uint8_t len = bytesToSend;
1169
1125
if (len > I2C_BUFFER_LENGTH)
1170
1126
len = I2C_BUFFER_LENGTH;
1171
- /* // TODO
1172
- _i2cPort->beginTransmission((uint8_t)_gpsI2Caddress);
1173
- //_i2cPort->write(outgoingUBX->payload, len); //Write a portion of the payload to the bus
1174
-
1175
- for (uint16_t x = 0; x < len; x++)
1176
- _i2cPort->write(outgoingUBX->payload[startSpot + x]); //Write a portion of the payload to the bus
1177
1127
1178
- if (_i2cPort->endTransmission(false) != 0) //Don't release bus
1179
- return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); //Sensor did not ACK
1180
- */
1181
1128
u8_t data_buffer[len];
1182
1129
for (uint16_t x = 0 ; x < len; x++) {
1183
1130
data_buffer[x] = outgoingUBX->payload [startSpot + x];
1184
1131
}
1185
- // err = i2c_write(_i2cPort, data_buffer, len, _gpsI2Caddress);
1186
- err = transferWriteI2C (data_buffer, len, false );
1132
+ err = transferWriteI2C (data_buffer, len, false ); // Write a portion of the payload to the bus
1187
1133
if (err) {
1188
1134
return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); // Sensor did not ACK
1189
1135
}
@@ -1194,11 +1140,7 @@ sfe_ublox_status_e SFE_UBLOX_GPS::sendI2cCommand(ubxPacket *outgoingUBX, uint16_
1194
1140
}
1195
1141
1196
1142
// Write checksum
1197
- // _i2cPort->beginTransmission((uint8_t)_gpsI2Caddress); // TODO
1198
1143
if (bytesToSend == 1 ) {
1199
- // _i2cPort->write(outgoingUBX->payload, 1);
1200
-
1201
- // i2c_write(_i2cPort, outgoingUBX->payload, 1, _gpsI2Caddress);
1202
1144
u8_t checksum_buffer[3 ];
1203
1145
uint8_t payload = *outgoingUBX->payload ;
1204
1146
checksum_buffer[0 ] = payload;
@@ -1212,10 +1154,6 @@ sfe_ublox_status_e SFE_UBLOX_GPS::sendI2cCommand(ubxPacket *outgoingUBX, uint16_
1212
1154
checksum_buffer[1 ] = outgoingUBX->checksumB ;
1213
1155
err = transferWriteI2C (checksum_buffer, 2 , true );
1214
1156
}
1215
- // _i2cPort->write(outgoingUBX->checksumA); // TODO
1216
- // _i2cPort->write(outgoingUBX->checksumB);
1217
-
1218
- // err = i2c_write(_i2cPort, checksum_buffer, 3, _gpsI2Caddress);
1219
1157
if (err) {
1220
1158
return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); // Sensor did not ACK
1221
1159
}
@@ -1253,20 +1191,12 @@ bool SFE_UBLOX_GPS::isConnected(uint16_t maxWait)
1253
1191
{
1254
1192
if (commType == COMM_TYPE_I2C)
1255
1193
{
1256
- /* // TODO
1257
- _i2cPort->beginTransmission((uint8_t)_gpsI2Caddress);
1258
- if (_i2cPort->endTransmission() != 0)
1259
- return false; //Sensor did not ack
1260
- */
1261
-
1262
1194
u8_t buff[1 ];
1263
1195
buff[0 ] = 0x00 ;
1264
- // int err = i2c_write(_i2cPort, buff, 1, _gpsI2Caddress);
1265
1196
int err = transferWriteI2C (buff, 1 );
1266
1197
if (err) {
1267
1198
return false ; // Sensor did not ack
1268
1199
}
1269
-
1270
1200
}
1271
1201
1272
1202
// Query navigation rate to see whether we get a meaningful response
@@ -1276,7 +1206,7 @@ bool SFE_UBLOX_GPS::isConnected(uint16_t maxWait)
1276
1206
packetCfg.startingSpot = 0 ;
1277
1207
1278
1208
// return (sendCommand(&packetCfg, maxWait) == SFE_UBLOX_STATUS_DATA_RECEIVED); // We are polling the RATE so we expect data and an ACK
1279
- return (sendCommand (&packetCfg, maxWait) == SFE_UBLOX_STATUS_DATA_SENT); // data send is returned not data received - TODO library bug?
1209
+ return (sendCommand (&packetCfg, maxWait) == SFE_UBLOX_STATUS_DATA_SENT); // data send is returned, not data received - TODO library bug?
1280
1210
}
1281
1211
1282
1212
// Given a message, calc and store the two byte "8-Bit Fletcher" checksum over the entirety of the message
0 commit comments