1 /*
2 * Copyright (c) 2023 Trackunit Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdio.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/gnss.h>
10 #include <zephyr/logging/log.h>
11
12 #define GNSS_MODEM DEVICE_DT_GET(DT_ALIAS(gnss))
13
14 LOG_MODULE_REGISTER(gnss_sample, CONFIG_GNSS_LOG_LEVEL);
15
gnss_data_cb(const struct device * dev,const struct gnss_data * data)16 static void gnss_data_cb(const struct device *dev, const struct gnss_data *data)
17 {
18 uint64_t timepulse_ns;
19 k_ticks_t timepulse;
20
21 if (data->info.fix_status != GNSS_FIX_STATUS_NO_FIX) {
22 if (gnss_get_latest_timepulse(dev, &timepulse) == 0) {
23 timepulse_ns = k_ticks_to_ns_near64(timepulse);
24 printf("Got a fix @ %lld ns\n", timepulse_ns);
25 } else {
26 printf("Got a fix!\n");
27 }
28 }
29 }
30 GNSS_DATA_CALLBACK_DEFINE(GNSS_MODEM, gnss_data_cb);
31
32 #if CONFIG_GNSS_SATELLITES
gnss_satellites_cb(const struct device * dev,const struct gnss_satellite * satellites,uint16_t size)33 static void gnss_satellites_cb(const struct device *dev, const struct gnss_satellite *satellites,
34 uint16_t size)
35 {
36 unsigned int tracked_count = 0;
37
38 for (unsigned int i = 0; i != size; ++i) {
39 tracked_count += satellites[i].is_tracked;
40 }
41 printf("%u satellite%s reported (of which %u tracked)!\n",
42 size, size > 1 ? "s" : "", tracked_count);
43 }
44 #endif
45 GNSS_SATELLITES_CALLBACK_DEFINE(GNSS_MODEM, gnss_satellites_cb);
46
47 #define GNSS_SYSTEMS_PRINTF(define, supported, enabled) \
48 printf("\t%20s: Supported: %3s Enabled: %3s\n", \
49 STRINGIFY(define), (supported & define) ? "Yes" : "No", \
50 (enabled & define) ? "Yes" : "No");
51
main(void)52 int main(void)
53 {
54 gnss_systems_t supported, enabled;
55 uint32_t fix_interval;
56 int rc;
57
58 rc = gnss_get_supported_systems(GNSS_MODEM, &supported);
59 if (rc < 0) {
60 printf("Failed to query supported systems (%d)\n", rc);
61 return rc;
62 }
63 rc = gnss_get_enabled_systems(GNSS_MODEM, &enabled);
64 if (rc < 0) {
65 printf("Failed to query enabled systems (%d)\n", rc);
66 return rc;
67 }
68 printf("GNSS Systems:\n");
69 GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_GPS, supported, enabled);
70 GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_GLONASS, supported, enabled);
71 GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_GALILEO, supported, enabled);
72 GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_BEIDOU, supported, enabled);
73 GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_QZSS, supported, enabled);
74 GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_IRNSS, supported, enabled);
75 GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_SBAS, supported, enabled);
76 GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_IMES, supported, enabled);
77
78 rc = gnss_get_fix_rate(GNSS_MODEM, &fix_interval);
79 if (rc < 0) {
80 printf("Failed to query fix rate (%d)\n", rc);
81 return rc;
82 }
83 printf("Fix rate = %d ms\n", fix_interval);
84
85 return 0;
86 }
87