Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit ccb20f2

Browse files
committed
code cleanup
1 parent 1fe56ab commit ccb20f2

File tree

5 files changed

+129
-161
lines changed

5 files changed

+129
-161
lines changed

examples/Example_Zephyr/src/main.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
Open the serial monitor at 115200 baud to see the output
1313
I2C clock speed: 100 kHz
1414
15-
Ported to Zephyr by Vid Rajtmajer <vid@irnas.eu>, IRNAS d.o.o.
15+
Ported to Zephyr by Vid Rajtmajer <vid@irnas.eu>, www.irnas.eu
1616
*/
17-
18-
#include <zephyr/types.h>
19-
#include <zephyr.h>
2017
#include <device.h>
2118
#include <drivers/i2c.h>
19+
#include <errno.h>
20+
#include <logging/log.h>
21+
#include <zephyr.h>
22+
#include <zephyr/types.h>
2223

2324
#include "ublox_lib_interface.h"
2425

2526

2627
#define I2C_DEV "I2C_0"
2728

29+
LOG_MODULE_REGISTER(zephyr_main); // init logging
30+
2831
struct device *gpio_dev;
2932
struct device *i2c_dev;
3033
/* I2C pins used are defaults for I2C_0 on nrf52840
@@ -33,15 +36,15 @@ struct device *i2c_dev;
3336
*/
3437

3538
uint8_t init_gpio(void) {
36-
const char* const gpioName = "GPIO_0";
39+
const char* gpioName = "GPIO_0";
3740
gpio_dev = device_get_binding(gpioName);
3841
if (gpio_dev == NULL) {
39-
printk("Could not get %s device\n", gpioName);
40-
return -1;
42+
LOG_ERR("Could not get %s device", gpioName);
43+
return -EIO;
4144
}
4245
int err = set_gpio_dev(gpio_dev);
4346
if (err) {
44-
return -1;
47+
return -EIO;
4548
}
4649
return 0;
4750
}
@@ -50,28 +53,28 @@ uint8_t init_i2c(void) {
5053
i2c_dev = device_get_binding(I2C_DEV);
5154
if (!i2c_dev)
5255
{
53-
printk("I2C_0 error\n");
56+
LOG_ERR("I2C_0 error");
5457
return -1;
5558
}
5659
else
5760
{
58-
printk("I2C_0 Init OK\n");
61+
LOG_INF("I2C_0 Init OK");
5962
return 0;
6063
}
6164
}
6265

6366
uint8_t init_gps(void) {
6467
if (gps_begin(i2c_dev) != 0)
6568
{
66-
printk("Ublox GPS init error!\n");
69+
LOG_ERR("Ublox GPS init error!");
6770
return -1;
6871
}
6972
return 0;
7073
}
7174

7275

7376
void main(void) {
74-
printk("UBlox i2c test\n");
77+
LOG_INF("Ublox Zephyr example");
7578

7679
int err;
7780
err = init_gpio();

src/SparkFun_Ublox_Zephyr_Library.cpp

Lines changed: 35 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
https://github.com/sparkfun/SparkFun_Ublox_Arduino_Library
1818
1919
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
2123
2224
SparkFun code, firmware, and software is released under the MIT License(http://opensource.org/licenses/MIT).
2325
The MIT License (MIT)
@@ -39,7 +41,6 @@
3941
*/
4042

4143
#include "SparkFun_Ublox_Zephyr_Library.h"
42-
4344
#include <drivers/gpio.h>
4445

4546
SFE_UBLOX_GPS::SFE_UBLOX_GPS(void)
@@ -49,18 +50,24 @@ SFE_UBLOX_GPS::SFE_UBLOX_GPS(void)
4950
moduleQueried.versionNumber = false;
5051
}
5152

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+
{
5356
_gpio_dev = &gpio_dev;
54-
5557
if (checksumFailurePin >= 0)
5658
{
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+
}
5966
}
6067
return true;
6168
}
6269

63-
//Initialize the Serial port
70+
//Initialize the I2C port
6471
bool SFE_UBLOX_GPS::begin(struct device &i2c_dev, uint8_t deviceAddress)
6572
{
6673
commType = COMM_TYPE_I2C;
@@ -79,8 +86,8 @@ bool SFE_UBLOX_GPS::begin(struct device &i2c_dev, uint8_t deviceAddress)
7986
return (isConnected());
8087
}
8188

82-
//Initialize the Serial port
83-
/* // TODO
89+
//Initialize the Serial port - function not ported
90+
/*
8491
bool SFE_UBLOX_GPS::begin(Stream &serialPort)
8592
{
8693
commType = COMM_TYPE_SERIAL;
@@ -213,37 +220,28 @@ void SFE_UBLOX_GPS::hardReset()
213220
sendCommand(&packetCfg, 0); // don't expect ACK
214221
}
215222

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+
{
217227
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.
226229
msgs[0].buf = buffer;
227230
msgs[0].len = num_bytes;
228231
if (stop) {
229232
msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
230-
//printk("transferWriteI2C - I2C_MSG_STOP\n");
231233
}
232234
else {
233235
msgs[0].flags = I2C_MSG_WRITE | I2C_MSG_RESTART;
234-
//printk("transferWriteI2C - I2C_MSG_RESTART\n");
235236
}
236237
return i2c_transfer(_i2cPort, &msgs[0], 1, _gpsI2Caddress);
237238
}
238239

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+
{
240243
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
247245
msgs[0].buf = buffer;
248246
msgs[0].len = num_bytes;
249247
msgs[0].flags = I2C_MSG_READ | I2C_MSG_STOP;
@@ -341,42 +339,29 @@ bool SFE_UBLOX_GPS::checkUbloxI2C(ubxPacket *incomingUBX, uint8_t requestedClass
341339
{
342340
if (k_uptime_get_32() - lastCheck >= i2cPollingWait)
343341
{
344-
int err;
345-
//Get the number of bytes available from the module
346342
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;
353344

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
354347
u8_t data_buffer[1];
355348
data_buffer[0] = 0xFD;
356-
//err = i2c_write(_i2cPort, data_buffer, 1, _gpsI2Caddress);
357349
err = transferWriteI2C(data_buffer, 1, true);
358350
if (err) {
359351
return (false);
360352
}
361353

362-
//_i2cPort->requestFrom((uint8_t)_gpsI2Caddress, (uint8_t)2); // TODO
363-
//if (_i2cPort->available())
364354
bool data_read_ok = true;
365355
u8_t read_buffer[2];
366-
//err = i2c_read(_i2cPort, read_buffer, 2, _gpsI2Caddress);
367356
err = transferReadI2C(data_buffer, 2);
368357
if (err) {
369358
data_read_ok = false;
370359
}
371360

372361
if (data_read_ok)
373362
{
374-
//uint8_t msb = _i2cPort->read(); // TODO
375-
//uint8_t lsb = _i2cPort->read();
376363
uint8_t msb = read_buffer[0];
377364
uint8_t lsb = read_buffer[1];
378-
//printk("checkUbloxI2C, data_read_ok, MSB: 0x%x LSB: 0x%x\n", msb, lsb);
379-
380365
if (lsb == 0xFF)
381366
{
382367
//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
443428

444429
while (bytesAvailable)
445430
{
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-
*/
452431
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.
455434
if (err) {
456435
return (false);
457436
}
@@ -462,8 +441,6 @@ bool SFE_UBLOX_GPS::checkUbloxI2C(ubxPacket *incomingUBX, uint8_t requestedClass
462441
bytesToRead = I2C_BUFFER_LENGTH;
463442

464443
TRY_AGAIN:
465-
//_i2cPort->requestFrom((uint8_t)_gpsI2Caddress, (uint8_t)bytesToRead); // TODO
466-
//if (_i2cPort->available())
467444
bool data_read_ok = true;
468445
u8_t read_buffer[(uint8_t)bytesToRead];
469446
err = transferReadI2C(read_buffer, (uint8_t)bytesToRead);
@@ -475,9 +452,7 @@ bool SFE_UBLOX_GPS::checkUbloxI2C(ubxPacket *incomingUBX, uint8_t requestedClass
475452
{
476453
for (uint16_t x = 0; x < bytesToRead; x++)
477454
{
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
481456

482457
//Check to see if the first read is 0x7F. If it is, the module is not ready
483458
//to respond. Stop, wait, and try again
@@ -1028,7 +1003,7 @@ void SFE_UBLOX_GPS::processUBXpacket(ubxPacket *msg)
10281003
highResModuleQueried.verticalAccuracy = true;
10291004
moduleQueried.gpsiTOW = true; // this can arrive via HPPOS too.
10301005

1031-
if (_printDebug == true) // TODO check float prints
1006+
if (_printDebug == true)
10321007
{
10331008
printk("Sec: %f", ((float)extractLong(4)) / 1000.0f);
10341009
printk("\n");
@@ -1115,40 +1090,21 @@ sfe_ublox_status_e SFE_UBLOX_GPS::sendI2cCommand(ubxPacket *outgoingUBX, uint16_
11151090
{
11161091
int err;
11171092
//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-
*/
11241093
u8_t small_buffer[1];
11251094
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
11281096
if (err) {
11291097
return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); //Sensor did not ACK
11301098
}
11311099

11321100
//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-
*/
11441101
u8_t header_buffer[6]; //There is no register to write to, we just begin writing data bytes
11451102
header_buffer[0] = UBX_SYNCH_1; //μ - oh ublox, you're funny. I will call you micro-blox from now on.
11461103
header_buffer[1] = UBX_SYNCH_2; //b
11471104
header_buffer[2] = outgoingUBX->cls;
11481105
header_buffer[3] = outgoingUBX->id;
11491106
header_buffer[4] = outgoingUBX->len & 0xFF; //LSB
11501107
header_buffer[5] = outgoingUBX->len >> 8; //MSB
1151-
//err = i2c_write(_i2cPort, header_buffer, 6, _gpsI2Caddress);
11521108

11531109
err = transferWriteI2C(header_buffer, 6, false);
11541110
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_
11681124
uint8_t len = bytesToSend;
11691125
if (len > I2C_BUFFER_LENGTH)
11701126
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
11771127

1178-
if (_i2cPort->endTransmission(false) != 0) //Don't release bus
1179-
return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); //Sensor did not ACK
1180-
*/
11811128
u8_t data_buffer[len];
11821129
for (uint16_t x = 0; x < len; x++) {
11831130
data_buffer[x] = outgoingUBX->payload[startSpot + x];
11841131
}
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
11871133
if (err) {
11881134
return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); //Sensor did not ACK
11891135
}
@@ -1194,11 +1140,7 @@ sfe_ublox_status_e SFE_UBLOX_GPS::sendI2cCommand(ubxPacket *outgoingUBX, uint16_
11941140
}
11951141

11961142
//Write checksum
1197-
//_i2cPort->beginTransmission((uint8_t)_gpsI2Caddress); // TODO
11981143
if (bytesToSend == 1) {
1199-
//_i2cPort->write(outgoingUBX->payload, 1);
1200-
1201-
//i2c_write(_i2cPort, outgoingUBX->payload, 1, _gpsI2Caddress);
12021144
u8_t checksum_buffer[3];
12031145
uint8_t payload = *outgoingUBX->payload;
12041146
checksum_buffer[0] = payload;
@@ -1212,10 +1154,6 @@ sfe_ublox_status_e SFE_UBLOX_GPS::sendI2cCommand(ubxPacket *outgoingUBX, uint16_
12121154
checksum_buffer[1] = outgoingUBX->checksumB;
12131155
err = transferWriteI2C(checksum_buffer, 2, true);
12141156
}
1215-
//_i2cPort->write(outgoingUBX->checksumA); // TODO
1216-
//_i2cPort->write(outgoingUBX->checksumB);
1217-
1218-
//err = i2c_write(_i2cPort, checksum_buffer, 3, _gpsI2Caddress);
12191157
if (err) {
12201158
return (SFE_UBLOX_STATUS_I2C_COMM_FAILURE); //Sensor did not ACK
12211159
}
@@ -1253,20 +1191,12 @@ bool SFE_UBLOX_GPS::isConnected(uint16_t maxWait)
12531191
{
12541192
if (commType == COMM_TYPE_I2C)
12551193
{
1256-
/* // TODO
1257-
_i2cPort->beginTransmission((uint8_t)_gpsI2Caddress);
1258-
if (_i2cPort->endTransmission() != 0)
1259-
return false; //Sensor did not ack
1260-
*/
1261-
12621194
u8_t buff[1];
12631195
buff[0] = 0x00;
1264-
//int err = i2c_write(_i2cPort, buff, 1, _gpsI2Caddress);
12651196
int err = transferWriteI2C(buff, 1);
12661197
if (err) {
12671198
return false; //Sensor did not ack
12681199
}
1269-
12701200
}
12711201

12721202
// Query navigation rate to see whether we get a meaningful response
@@ -1276,7 +1206,7 @@ bool SFE_UBLOX_GPS::isConnected(uint16_t maxWait)
12761206
packetCfg.startingSpot = 0;
12771207

12781208
//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?
12801210
}
12811211

12821212
//Given a message, calc and store the two byte "8-Bit Fletcher" checksum over the entirety of the message

0 commit comments

Comments
 (0)