raiden00pl commented on code in PR #11203: URL: https://github.com/apache/nuttx/pull/11203#discussion_r1396127194
########## arch/arm/src/nrf91/nrf91_modem_gnss.c: ########## @@ -0,0 +1,734 @@ +/**************************************************************************** + * arch/arm/src/nrf91/nrf91_modem_gnss.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <nuttx/kthread.h> + +#include <debug.h> +#include <string.h> +#include <time.h> + +#include <nuttx/sensors/gps.h> +#include <nuttx/sensors/sensor.h> + +#include "nrf_modem.h" +#include "nrf_modem_gnss.h" + +#include "nrf91_modem_at.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_SENSORS_GPS +# error nRF91 GNSS driver needs CONFIG_SENSORS_GPS +#endif + +/* NMEA and AGPS not supported yet */ + +#undef NRF91_GNSS_NMEA +#undef NRF91_GNSS_AGPS + +/* Some default values */ + +#define NRF91_GNSS_USE_CASE NRF_MODEM_GNSS_USE_CASE_MULTIPLE_HOT_START +#define NRF91_GNSS_POWER_MODE NRF_MODEM_GNSS_PSM_DISABLED +#ifdef NRF91_GNSS_NMEA +# define NRF91_GNSS_NMEA_MASK (NRF_MODEM_GNSS_NMEA_GGA_MASK | \ + NRF_MODEM_GNSS_NMEA_GLL_MASK | \ + NRF_MODEM_GNSS_NMEA_GSA_MASK | \ + NRF_MODEM_GNSS_NMEA_GSV_MASK | \ + NRF_MODEM_GNSS_NMEA_RMC_MASK) +#else +# define NRF91_GNSS_NMEA_MASK 0 +#endif + +/* Shorten some names */ + +#define NRF_GNSS_PVT_FRAME_LEN (sizeof(struct nrf_modem_gnss_pvt_data_frame)) +#define NRF_GNSS_NMEA_FRAME_LEN (sizeof(struct nrf_modem_gnss_nmea_data_frame)) +#define NRF_GNSS_AGPS_FRAME_LEN (sizeof(struct nrf_modem_gnss_agps_data_frame)) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct nrf91_gnss_s +{ + /* lower must be first element */ + + struct gps_lowerhalf_s lower; + bool running; + bool singlefix; + int notime_cntr; + sem_t rx_sem; + + /* PVT support */ + + bool pvt_evt; + struct nrf_modem_gnss_pvt_data_frame pvt; + +#ifdef NRF91_GNSS_NMEA + /* NMEA support */ + + bool nmea_evt; + struct nrf_modem_gnss_nmea_data_frame nmea; +#endif + +#ifdef NRF91_GNSS_AGPS + /* AGPS support */ + + bool agps_evt; + struct nrf_modem_gnss_agps_data_frame agps; +#endif +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* GPS operations */ + +static int nrf91_gnss_activate(struct gps_lowerhalf_s *lower, + struct file *filep, bool enable); +static int nrf91_gnss_set_interval(struct gps_lowerhalf_s *lower, + struct file *filep, + unsigned long *period_us); +static int nrf91_gnss_control(struct gps_lowerhalf_s *lower, + struct file *filep, + int cmd, unsigned long arg); +static ssize_t nrf91_gnss_inject_data(struct gps_lowerhalf_s *lower, + struct file *filep, + const void *buffer, size_t buflen); + +/* Helpers */ + +static int nrf91_gnss_configure(void); +static bool nrf91_gnss_isactive(int cfun); +static int nrf91_gnss_enable(struct nrf91_gnss_s *priv, bool enable); +#ifdef CONFIG_NRF91_MODEM_GNSS_BOOST_PRIO +static void nrf91_gnss_boost_prio(struct nrf91_gnss_s *priv); +#endif +static void nrf91_gnss_pvt_event(struct nrf91_gnss_s *priv); +static void nrf91_gnss_event_handler(int event); +static int nrf91_gnss_thread(int argc, char** argv); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +struct gps_ops_s g_nrf91_gnss_ops = +{ + .activate = nrf91_gnss_activate, + .set_interval = nrf91_gnss_set_interval, + .control = nrf91_gnss_control, + .inject_data = nrf91_gnss_inject_data, +}; + +struct nrf91_gnss_s g_nrf91_gnss; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nrf91_gnss_configure + ****************************************************************************/ + +static int nrf91_gnss_configure(void) +{ + int ret = 0; + + /* Set NMEA strings */ + + ret = nrf_modem_gnss_nmea_mask_set(NRF91_GNSS_NMEA_MASK); + if (ret < 0) + { + goto errout; + } + + /* Set targeted start and runtime performance */ + + ret = nrf_modem_gnss_use_case_set(NRF91_GNSS_USE_CASE); + if (ret < 0) + { + goto errout; + } + + /* Set the used power saving mode */ + + ret = nrf_modem_gnss_power_mode_set(NRF91_GNSS_POWER_MODE); + if (ret < 0) + { + goto errout; + } + +errout: + return ret; +} + +/**************************************************************************** + * Name: nrf91_gnss_isactive + ****************************************************************************/ + +static bool nrf91_gnss_isactive(int cfun) +{ + return (cfun == NRF91_MODEM_FUNC_FULL || + cfun == NRF91_MODEM_FUNC_RXONLY || + cfun == NRF91_MODEM_FUNC_DEACTIVATE_LTE || + cfun == NRF91_MODEM_FUNC_ACTIVATE_GNSS); +} + +/**************************************************************************** + * Name: nrf91_gnss_enable + ****************************************************************************/ + +static int nrf91_gnss_enable(struct nrf91_gnss_s *priv, bool enable) +{ + int ret = OK; + int cfun = 0; + + if (enable) + { + /* Get modem functional mode */ + + ret = nrf_modem_at_scanf("AT+CFUN?", "+CFUN: %d", &cfun); + if (ret < 0) + { + snerr("nrf_modem_at_scanf failed %d", ret); + goto errout; + } + + /* GNSS must be active */ + + if (!nrf91_gnss_isactive(cfun)) + { + snerr("GNSS is not activated!"); + ret = -EACCES; + goto errout; + } + + /* Configure GNSS modem */ + + ret = nrf91_gnss_configure(); + if (ret < 0) + { + snerr("nrf91_gnss_configure failed %d\n", ret); + return ret; + } + + ret = nrf_modem_gnss_start(); + if (ret < 0) + { + snerr("nrf_modem_gnss_start failed %d", ret); + goto errout; + } + + priv->running = true; + } + else + { + ret = nrf_modem_gnss_stop(); + if (ret < 0) + { + snerr("nrf_modem_gnss_stop failed %d", ret); + goto errout; + } + + priv->running = false; + } + +errout: + return ret; +} + +/**************************************************************************** + * Name: nrf91_gnss_activate + ****************************************************************************/ + +static int nrf91_gnss_activate(struct gps_lowerhalf_s *lower, + struct file *filep, bool enable) +{ + struct nrf91_gnss_s *priv = (struct nrf91_gnss_s *)lower; + + return nrf91_gnss_enable(priv, enable); +} + +/**************************************************************************** + * Name: nrf91_gnss_set_interval + ****************************************************************************/ + +static int nrf91_gnss_set_interval(struct gps_lowerhalf_s *lower, + struct file *filep, + unsigned long *period_us) +{ + struct nrf91_gnss_s *priv = (struct nrf91_gnss_s *)lower; + uint16_t fix_interval = 0; + int ret = OK; + bool running = priv->running; + + /* GNSS must be disabled when fix interval change */ + + if (running == true) + { + nrf91_gnss_enable(priv, false); + } + + /* Fix interval in seconds */ + + fix_interval = (*period_us) / 1000000; + + /* Handle GNSS mode */ + + if (fix_interval == 1) + { + /* Continuous navigation with 1 Hz rate */ + + fix_interval = 1; Review Comment: some leftovers, removed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org