diff options
| author | Michal Hanus <hanus.michal@divelit.cz> | 2025-04-23 13:43:14 +0200 |
|---|---|---|
| committer | Michal Hanus <hanus.michal@divelit.cz> | 2025-04-23 13:43:14 +0200 |
| commit | d1c04e8b36bc206eb520f7b79183d5c015855252 (patch) | |
| tree | 469857024814e16b8ed27c52c5b8689358455f33 /gnss/src/main.c | |
| parent | 851d92974af6cefa00de1379dad8b8076c2dd107 (diff) | |
| parent | 0dee9f28ef79d54ec06f460b5e1deb10a8efd303 (diff) | |
Merge remote-tracking branch 'refs/remotes/xyz/master'
Diffstat (limited to 'gnss/src/main.c')
| -rw-r--r-- | gnss/src/main.c | 344 |
1 files changed, 344 insertions, 0 deletions
diff --git a/gnss/src/main.c b/gnss/src/main.c new file mode 100644 index 0000000..29264e5 --- /dev/null +++ b/gnss/src/main.c @@ -0,0 +1,344 @@ +#if 1 +/* + * Copyright (c) 2024 Conexio Technologies, Inc + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include <stdio.h> +#include <zephyr/kernel.h> + +#include <zephyr/logging/log.h> +#include <modem/nrf_modem_lib.h> +#include <modem/lte_lc.h> +#include <dk_buttons_and_leds.h> + +/* Include the header file for the GNSS interface */ +#include <nrf_modem_gnss.h> + +/* Define the PVT data frame variable */ +static struct nrf_modem_gnss_pvt_data_frame pvt_data; + +/* Declare helper variables to find the TTFF */ +static int64_t gnss_start_time; +static bool first_fix = false; + +static K_SEM_DEFINE(lte_connected, 0, 1); + +LOG_MODULE_REGISTER(GNSS_sample, LOG_LEVEL_INF); + + +static int modem_configure(void) +{ + int err; + + LOG_INF("Initializing modem library"); + + err = nrf_modem_lib_init(); + if (err) { + LOG_ERR("Failed to initialize the modem library, error: %d", err); + return err; + } + + err = lte_lc_init(); + if (err) { + LOG_ERR("Failed to initialize LTE Link Controller, error: %d", err); + return err; + } + + return 0; +} + +/* Define a function to log fix data in a readable format */ +static void print_fix_data(struct nrf_modem_gnss_pvt_data_frame *pvt_data) +{ + LOG_INF("Latitude: %.06f", pvt_data->latitude); + LOG_INF("Longitude: %.06f", pvt_data->longitude); + LOG_INF("Altitude: %.01f m", pvt_data->altitude); + LOG_INF("Time (UTC): %02u:%02u:%02u.%03u", + pvt_data->datetime.hour, + pvt_data->datetime.minute, + pvt_data->datetime.seconds, + pvt_data->datetime.ms); +} + + +static void gnss_event_handler(int event) +{ + int err; + + switch (event) { + /* On a PVT event, confirm if PVT data is a valid fix */ + case NRF_MODEM_GNSS_EVT_PVT: + LOG_INF("Searching..."); + /* Print satellite information */ + int num_satellites = 0; + for (int i = 0; i < 12 ; i++) { + if (pvt_data.sv[i].signal != 0) { + LOG_INF("sv: %d, cn0: %d", pvt_data.sv[i].sv, pvt_data.sv[i].cn0); + num_satellites++; + } + } + LOG_INF("Number of current satellites: %d", num_satellites); + err = nrf_modem_gnss_read(&pvt_data, sizeof(pvt_data), NRF_MODEM_GNSS_DATA_PVT); + if (err) { + LOG_ERR("nrf_modem_gnss_read failed, err %d", err); + return; + } + if (pvt_data.flags & NRF_MODEM_GNSS_PVT_FLAG_FIX_VALID) { + dk_set_led_on(DK_LED1); + print_fix_data(&pvt_data); + /* Print the time to first fix */ + if (!first_fix) { + LOG_INF("Time to first fix: %2.1lld s", (k_uptime_get() - gnss_start_time)/1000); + first_fix = true; + } + return; + } + break; + /* Log when the GNSS sleeps and wakes up */ + case NRF_MODEM_GNSS_EVT_PERIODIC_WAKEUP: + LOG_INF("GNSS has woken up"); + break; + case NRF_MODEM_GNSS_EVT_SLEEP_AFTER_FIX: + LOG_INF("GNSS enter sleep after fix"); + break; + default: + break; + } +} + +int main(void) +{ + int err; + + LOG_INF("Stratus GNSS sample started"); + + if (dk_leds_init() != 0) { + LOG_ERR("Failed to initialize the LEDs Library"); + } + + err = modem_configure(); + if (err) { + LOG_ERR("Failed to configure the modem"); + return 0; + } + + /* Activate only the GNSS stack */ + if (lte_lc_func_mode_set(LTE_LC_FUNC_MODE_ACTIVATE_GNSS) != 0) { + LOG_ERR("Failed to activate GNSS functional mode"); + return 0; + } + + /* Register the GNSS event handler */ + if (nrf_modem_gnss_event_handler_set(gnss_event_handler) != 0) { + LOG_ERR("Failed to set GNSS event handler"); + return 0; + } + + /* Set the GNSS fix interval and GNSS fix retry period */ + if (nrf_modem_gnss_fix_interval_set(CONFIG_GNSS_PERIODIC_INTERVAL) != 0) { + LOG_ERR("Failed to set GNSS fix interval"); + return 0; + } + + if (nrf_modem_gnss_fix_retry_set(CONFIG_GNSS_PERIODIC_TIMEOUT) != 0) { + LOG_ERR("Failed to set GNSS fix retry"); + return 0; + } + + /* Start the GNSS receiver */ + LOG_INF("Starting GNSS"); + if (nrf_modem_gnss_start() != 0) { + LOG_ERR("Failed to start GNSS"); + return 0; + } + + /* Log the current system uptime */ + gnss_start_time = k_uptime_get(); + + return 0; +} +#elif 0 +/* */ +/* Copyright (c) 2020 Nordic Semiconductor ASA */ +/* */ +/* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ +#include <zephyr/kernel.h> +#include <zephyr/sys/printk.h> +#include <zephyr/drivers/i2c.h> + +/* STEP 8 - Define the addresses of relevant registers */ +#define CTRLMEAS 0xF4 +#define CALIB00 0x88 +#define ID 0xD0 +#define TEMPMSB 0xFA + +#define CHIP_ID 0x61 +#define SENSOR_CONFIG_VALUE 0x93 + +/* STEP 6 - Get the node identifier of the sensor */ +#define I2C_NODE DT_NODELABEL(mysensor) + +/* Data structure to store BME280 data */ +struct bme280_data { + /* Compensation parameters */ + uint16_t dig_t1; + int16_t dig_t2; + int16_t dig_t3; +} bmedata; + +/* Read sensor calibration data and stores these into sensor data */ +void bme_calibrationdata(const struct i2c_dt_spec *spec, struct bme280_data *sensor_data_ptr) +{ + + /* Step 10 - Put calibration function code */ + uint8_t values[6]; + + int ret = i2c_burst_read_dt(spec, CALIB00, values, 6); + + if (ret != 0) { + printk("Failed to read register %x \n", CALIB00); + return; + } + + sensor_data_ptr->dig_t1 = ((uint16_t)values[1]) << 8 | values[0]; + sensor_data_ptr->dig_t2 = ((uint16_t)values[3]) << 8 | values[2]; + sensor_data_ptr->dig_t3 = ((uint16_t)values[5]) << 8 | values[4]; +} + +/* Compensate current temperature using previously stored sensor calibration data */ +static int32_t bme280_compensate_temp(struct bme280_data *data, int32_t adc_temp) +{ + int32_t var1, var2; + + var1 = (((adc_temp >> 3) - ((int32_t)data->dig_t1 << 1)) * ((int32_t)data->dig_t2)) >> 11; + + var2 = (((((adc_temp >> 4) - ((int32_t)data->dig_t1)) * + ((adc_temp >> 4) - ((int32_t)data->dig_t1))) >> + 12) * + ((int32_t)data->dig_t3)) >> + 14; + + return ((var1 + var2) * 5 + 128) >> 8; +} + +int main(void) +{ + + /* STEP 7 - Retrieve the API-specific device structure and make sure that the device is + * ready to use */ + static const struct i2c_dt_spec dev_i2c = I2C_DT_SPEC_GET(I2C_NODE); + + if (!device_is_ready(dev_i2c.bus)) { + printk("I2C bus %s is not ready!\n", dev_i2c.bus->name); + return -1; + } + + /* STEP 9 - Verify it is proper device by reading device id */ + uint8_t id = 0; + uint8_t regs[] = {ID}; + + int ret = i2c_write_read_dt(&dev_i2c, regs, 1, &id, 1); + + if (ret != 0) { + printk("Failed to read register %x \n", regs[0]); + return -1; + } + + if (id != CHIP_ID) { + printk("Invalid chip id! %x \n", id); + return -1; + } + + bme_calibrationdata(&dev_i2c, &bmedata); + + /* STEP 11 - Setup the sensor by writing the value 0x93 to the Configuration register */ + uint8_t sensor_config[] = {CTRLMEAS, SENSOR_CONFIG_VALUE}; + + ret = i2c_write_dt(&dev_i2c, sensor_config, 2); + + if (ret != 0) { + printk("Failed to write register %x \n", sensor_config[0]); + return -1; + } + + while (1) { + + /* STEP 12 - Read the temperature from the sensor */ + uint8_t temp_val[3] = {0}; + + int ret = i2c_burst_read_dt(&dev_i2c, TEMPMSB, temp_val, 3); + + if (ret != 0) { + printk("Failed to read register %x \n", TEMPMSB); + k_msleep(1000); + continue; + } + + /* STEP 12.1 - Put the data read from registers into actual order (see datasheet) */ + int32_t adc_temp = + (temp_val[0] << 12) | (temp_val[1] << 4) | ((temp_val[2] >> 4) & 0x0F); + + /* STEP 12.2 - Compensate temperature */ + int32_t comp_temp = bme280_compensate_temp(&bmedata, adc_temp); + + /* STEP 12.3 - Convert temperature */ + float temperature = (float)comp_temp / 100.0f; + double fTemp = (double)temperature * 1.8 + 32; + + // Print reading to console + printk("Temperature in Celsius : %8.2f C\n", (double)temperature); + printk("Temperature in Fahrenheit : %.2f F\n", fTemp); + + k_msleep(1000); + } +} +#else + +/* + * Copyright (c) 2018 Bosch Sensortec GmbH + * + * SPDX-License-Identifier: Apache-2.0 + */ + + #include <zephyr/kernel.h> + #include <zephyr/device.h> + #include <zephyr/drivers/sensor.h> + #include <stdio.h> + + int main(void) + { + const struct device *const dev = DEVICE_DT_GET_ONE(bosch_bme680); + struct sensor_value temp, press, humidity, gas_res; + + if (!device_is_ready(dev)) { + printk("sensor: device not ready.\n"); + return 0; + } + + printf("Device %p name is %s\n", dev, dev->name); + + #ifndef CONFIG_COVERAGE + while (1) { + #else + for (int i = 0; i < 5; i++) { + #endif + k_sleep(K_MSEC(3000)); + + sensor_sample_fetch(dev); + sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &temp); + sensor_channel_get(dev, SENSOR_CHAN_PRESS, &press); + sensor_channel_get(dev, SENSOR_CHAN_HUMIDITY, &humidity); + sensor_channel_get(dev, SENSOR_CHAN_GAS_RES, &gas_res); + + printf("Temp: %d.%02d degC; Press: %d.%02d kPa; RH: %d.%02d %%; G: %d.%02d Ohm\n", + temp.val1, temp.val2, press.val1, press.val2, + humidity.val1, humidity.val2, gas_res.val1, + gas_res.val2); + } + return 0; + } + + +#endif |
