|
| 1 | +/* |
| 2 | + This is an interface that connects the CPP Ublox library with the main C code. |
| 3 | + Added to make it possible to run Ublox lib on Zephyr (NCS) |
| 4 | +
|
| 5 | + This port was made by Vid Rajtmajer <[email protected]>, www.irnas.eu |
| 6 | +*/ |
| 7 | +#include "SparkFun_Ublox_Zephyr_Interface.h" |
| 8 | + |
| 9 | +#include <errno.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <string.h> |
| 12 | +#include <time.h> |
| 13 | + |
| 14 | +#include "SparkFun_Ublox_Zephyr_Library.h" |
| 15 | + |
| 16 | + |
| 17 | +SFE_UBLOX_GPS myGPS; // driver class instance |
| 18 | +long lastTime = 0; // Simple local timer. Limits amount if I2C traffic to Ublox module. |
| 19 | + |
| 20 | +// init GPIO checksumFailurePin and load GPIO device pointer to the driver |
| 21 | +uint8_t set_gpio_dev(struct device *gpio_dev, uint8_t enable_debug) |
| 22 | +{ |
| 23 | + if (myGPS.init_gpio_pins(*gpio_dev) == false) |
| 24 | + { |
| 25 | + return -EIO; |
| 26 | + } |
| 27 | + // turn on debugging if enable_debug is set |
| 28 | + if (enable_debug) |
| 29 | + { |
| 30 | + myGPS.enableDebugging(); |
| 31 | + } |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +// initialize I2C and check if GPS device respons |
| 36 | +uint8_t gps_begin(struct device *i2c_dev) |
| 37 | +{ |
| 38 | + if (myGPS.begin(*i2c_dev) == false) |
| 39 | + { |
| 40 | + return -EIO; |
| 41 | + } |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +// This will pipe all NMEA sentences to UART so we can see them |
| 46 | +void pipe_nmea_sentences(void) |
| 47 | +{ |
| 48 | + myGPS.setNMEAOutputPort(); |
| 49 | +} |
| 50 | + |
| 51 | +// Check for available bytes from the device |
| 52 | +void check_ublox(void) |
| 53 | +{ |
| 54 | + myGPS.checkUblox(); |
| 55 | +} |
| 56 | + |
| 57 | +// Get position information when requested, also display number of satellites used in the fix |
| 58 | +int get_position(void) |
| 59 | +{ |
| 60 | + //Query module only every second. Doing it more often will just cause I2C traffic. |
| 61 | + //The module only responds when a new position is available, print it to console |
| 62 | + if (k_uptime_get_32() - lastTime > 1000) |
| 63 | + { |
| 64 | + lastTime = k_uptime_get_32(); //Update the timer |
| 65 | + |
| 66 | + long latitude = myGPS.getLatitude(); |
| 67 | + long longitude = myGPS.getLongitude(); |
| 68 | + long altitude = myGPS.getAltitude(); |
| 69 | + uint8_t SIV = myGPS.getSIV(); |
| 70 | + |
| 71 | + printk("Position: Lat: %ld, Lon: %ld, Alt: %ld, SIV: %d", latitude, longitude, altitude, SIV); |
| 72 | + return 0; |
| 73 | + } |
| 74 | + return -EBUSY; |
| 75 | +} |
| 76 | + |
| 77 | +// Get date and time information when requested, check if they are valid and print info to console, it returns UNIX time |
| 78 | +void get_datetime(void) |
| 79 | +{ |
| 80 | + int year = myGPS.getYear(); |
| 81 | + int month = myGPS.getMonth(); |
| 82 | + int day = myGPS.getDay(); |
| 83 | + int hour = myGPS.getHour(); |
| 84 | + int minute = myGPS.getMinute(); |
| 85 | + int second = myGPS.getSecond(); |
| 86 | + |
| 87 | + printk("DateTime: %d-%d-%d %d:%d:%d\n", year, month, day, hour, minute, second); |
| 88 | + |
| 89 | + printk("Time is "); |
| 90 | + if (myGPS.getTimeValid() == false) |
| 91 | + { |
| 92 | + printk("not "); |
| 93 | + } |
| 94 | + printk("valid. Date is "); |
| 95 | + if (myGPS.getDateValid() == false) |
| 96 | + { |
| 97 | + printk("not "); |
| 98 | + } |
| 99 | + printk("valid.\n"); |
| 100 | +} |
0 commit comments