<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How do I get GPS data from the modem in a non-blocking manner?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/67831/how-do-i-get-gps-data-from-the-modem-in-a-non-blocking-manner</link><description>I am debugging the GPS function and referring to the sample code of the official GPS. As for the code of reading GPS data, I have a question that is the way of obtaining GPS data. Why we can&amp;#39;t use the non-blocking interrupt mode? Now it is read through</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 05 Nov 2020 15:03:12 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/67831/how-do-i-get-gps-data-from-the-modem-in-a-non-blocking-manner" /><item><title>RE: How do I get GPS data from the modem in a non-blocking manner?</title><link>https://devzone.nordicsemi.com/thread/278645?ContentTypeID=1</link><pubDate>Thu, 05 Nov 2020 15:03:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:30492270-dc11-4ddd-9184-1893fe4d360c</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;The problem is probably the %XSYSTEMMODE, %XCOEX0 and %XMAGPIO commands.&lt;/p&gt;
&lt;p&gt;You only need to set it once, where you enable both LTE and GPS (if you want to switch between LTE-M and NB-IoT you must use %XSYSTEMMODE again).&lt;/p&gt;
&lt;p&gt;You can then start and stop the LTE using +CFUN=21 and 20. The GPS is controlled by gnss_ctrl (or socket options directly).&lt;/p&gt;
&lt;p&gt;There is not really a need for enabling and disabling the GNSS stack (+CFUN=31 and 30). You should also not need to change %XCOEX0 and %XMAGPIO while the application is running. But I am not very familiar with antennas, etc., so there might be some legitimate reasons why you want to do that. It is not necessary when using a 91DK or Thingy91 though.&lt;/p&gt;
&lt;p&gt;The reason you get the error when you send the commands I listed on top is because those commands can only be sent when the modem is offline (+CFUN=4 or 0). As the modem is running when you try to re-enable LTE, the commands will return an error. But, as explained above, you should not need to re-send those commands in the first place.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I get GPS data from the modem in a non-blocking manner?</title><link>https://devzone.nordicsemi.com/thread/278536?ContentTypeID=1</link><pubDate>Thu, 05 Nov 2020 07:54:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f59612f1-b5a1-4fd3-9283-6c76ed60ff6d</guid><dc:creator>duxinglang</dc:creator><description>&lt;p&gt;&lt;span&gt;Here&amp;#39;s another problem. When I turn off the GPS and turn it back on a short time later, it prompts an error:&amp;ldquo;Failed to initialize modem&amp;rdquo;，Below are the steps I took to turn off GPS by referring to turning on GPS. Are these actions correct for turning off GPS?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define AT_XSYSTEMMODE_GPSON      &amp;quot;AT\%XSYSTEMMODE=0,1,1,0&amp;quot;
#define AT_XSYSTEMMODE_GPSOFF     &amp;quot;AT\%XSYSTEMMODE=0,1,0,0&amp;quot;
#define AT_ACTIVATE_GPS     &amp;quot;AT+CFUN=31&amp;quot;
#define AT_DEACTIVATE_GPS	&amp;quot;AT+CFUN=30&amp;quot;
#define AT_ACTIVATE_LTE     &amp;quot;AT+CFUN=21&amp;quot;
#define AT_DEACTIVATE_LTE   &amp;quot;AT+CFUN=20&amp;quot;

#ifdef CONFIG_BOARD_NRF9160_PCA10090NS
#define AT_MAGPIO			&amp;quot;AT\%XMAGPIO=1,0,0,1,1,1574,1577&amp;quot;
#define AT_MAGPIO_CANCEL	&amp;quot;AT%XMAGPIO&amp;quot;
#define AT_COEX0			&amp;quot;AT\%XCOEX0=1,1,1570,1580&amp;quot;
#define AT_COEX0_CANCEL		&amp;quot;AT\%XCOEX0&amp;quot;
#endif

static const char at_commands_activate_gps[][31]  = 
{
	AT_XSYSTEMMODE_GPSON,
#ifdef CONFIG_BOARD_NRF9160_PCA10090NS
	AT_MAGPIO,
	AT_COEX0,
#endif
	AT_ACTIVATE_GPS
};
static const char at_commands_deactivate_gps[][31]  = 
{
	AT_DEACTIVATE_GPS,
#ifdef CONFIG_BOARD_NRF9160_PCA10090NS
	AT_COEX0_CANCEL,
	AT_MAGPIO_CANCEL,
#endif
	AT_XSYSTEMMODE_GPSOFF
};

static int setup_modem(void)
{
	int i;

	for(i = 0; i &amp;lt; ARRAY_SIZE(at_commands_activate_gps); i++)
	{
		if(at_cmd_write(at_commands_activate_gps[i], NULL, 0, NULL) != 0)
		{
			return -1;
		}
	}

	return 0;
}

static int reset_modem(void)
{
	int i;

	for(i = 0; i &amp;lt; ARRAY_SIZE(at_commands_deactivate_gps); i++)
	{
		if(at_cmd_write(at_commands_deactivate_gps[i], NULL, 0, NULL) != 0)
		{
			return -1;
		}
	}

	return 0;
}

static int init_app(void)
{
	int retval;

	if(setup_modem() != 0)
	{
		printk(&amp;quot;Failed to initialize modem\n&amp;quot;);
	#ifdef SHOW_LOG_IN_SCREEN	
		show_infor(&amp;quot;Failed to initialize modem&amp;quot;);
	#endif	
		return -1;
	}

	retval = gnss_ctrl(GNSS_INIT_AND_START);

	return retval;
}

void gps_off(void)
{
	gnss_ctrl(GNSS_STOP);

	if(reset_modem() != 0)
	{
		printk(&amp;quot;Failed to reset modem\n&amp;quot;);
	#ifdef SHOW_LOG_IN_SCREEN	
		show_infor(&amp;quot;Failed to reset modem&amp;quot;);
	#endif	
	}	

	got_first_fix = false;
}

void gps_on(void)
{
	u8_t tmpbuf[128] = {0};
	nrf_gnss_data_frame_t gps_data;
	u8_t		      cnt = 0;

#ifdef CONFIG_SUPL_CLIENT_LIB
	static struct supl_api supl_api = {
		.read       = supl_read,
		.write      = supl_write,
		.handler    = inject_agps_type,
		.logger     = supl_logger,
		.counter_ms = k_uptime_get
	};
#endif

	printk(&amp;quot;Staring GPS application\n&amp;quot;);
#ifdef SHOW_LOG_IN_SCREEN
	show_infor(&amp;quot;Staring GPS application&amp;quot;);
#endif

	if(init_app() != 0)
	{
		return;
	}

#ifdef CONFIG_SUPL_CLIENT_LIB
	int rc = supl_init(&amp;amp;supl_api);

	if (rc != 0)
	{
		return;
	}
#endif

	printk(&amp;quot;Getting GPS data...\n&amp;quot;);
#ifdef SHOW_LOG_IN_SCREEN
	show_infor(&amp;quot;Getting GPS data...&amp;quot;);
#endif

	while(1)
	{
		do
		{
			/* Loop until we don&amp;#39;t have more
			 * data to read
			 */
		}while(process_gps_data(&amp;amp;gps_data) &amp;gt; 0);

		if(!got_first_fix)
		{
			cnt++;
			printk(&amp;quot;\033[1;1H&amp;quot;);
			printk(&amp;quot;\033[2J&amp;quot;);
			print_satellite_stats(&amp;amp;gps_data);
			printk(&amp;quot;\nScanning [%c] &amp;quot;,
					update_indicator[cnt%4]);
		#ifdef SHOW_LOG_IN_SCREEN
			sprintf(tmpbuf, &amp;quot;Scanning [%c] &amp;quot;, update_indicator[cnt%4]);
			show_infor(tmpbuf);
		#endif	
		}

		if(((k_uptime_get() - fix_timestamp) &amp;gt;= 1) &amp;amp;&amp;amp; (got_first_fix))
		{
			printk(&amp;quot;\033[1;1H&amp;quot;);
			printk(&amp;quot;\033[2J&amp;quot;);

			print_satellite_stats(&amp;amp;gps_data);

			printk(&amp;quot;---------------------------------\n&amp;quot;);
			print_pvt_data(&amp;amp;last_fix);
			printk(&amp;quot;\n&amp;quot;);
			print_nmea_data();
			printk(&amp;quot;---------------------------------&amp;quot;);

			update_terminal = false;

			if(((k_uptime_get() - fix_timestamp) &amp;gt;= 5) &amp;amp;&amp;amp; (APP_wait_gps))
			{
				u8_t str_gps[20] = {0};
				u32_t tmp1;
				double tmp2;

				if(k_timer_remaining_get(&amp;amp;app_wait_gps_timer) &amp;gt; 0)
					k_timer_stop(&amp;amp;app_wait_gps_timer);

				APP_wait_gps = false;
				gps_off();

				//UTC date&amp;amp;time
				//year
				str_gps[0] = last_fix.pvt.datetime.year&amp;gt;&amp;gt;8;
				str_gps[1] = (u8_t)(last_fix.pvt.datetime.year&amp;amp;0x00FF);
				//month
				str_gps[2] = last_fix.pvt.datetime.month;
				//day
				str_gps[3] = last_fix.pvt.datetime.day;
				//hour
				str_gps[4] = last_fix.pvt.datetime.hour;
				//minute
				str_gps[5] = last_fix.pvt.datetime.minute;
				//seconds
				str_gps[6] = last_fix.pvt.datetime.seconds;

				//longitude
				str_gps[7] = &amp;#39;E&amp;#39;;
				if(last_fix.pvt.longitude &amp;lt; 0)
				{
					str_gps[7] = &amp;#39;W&amp;#39;;
					last_fix.pvt.longitude = -last_fix.pvt.longitude;
				}

				tmp1 = (u32_t)(last_fix.pvt.longitude);	//经度整数部分
				tmp2 = last_fix.pvt.longitude - tmp1;	//经度小数部分
				
				str_gps[8] = tmp1;//整数部分
				tmp1 = (u32_t)(tmp2*1000000);
				str_gps[9] = (u8_t)(tmp1/10000);
				tmp1 = tmp1%10000;
				str_gps[10] = (u8_t)(tmp1/100);
				tmp1 = tmp1%100;
				str_gps[11] = (u8_t)(tmp1);
				
				//latitude
				str_gps[12] = &amp;#39;N&amp;#39;;
				if(last_fix.pvt.latitude &amp;lt; 0)
				{
					str_gps[12] = &amp;#39;S&amp;#39;;
					last_fix.pvt.latitude = -last_fix.pvt.latitude;
				}

				tmp1 = (u32_t)(last_fix.pvt.latitude);	//经度整数部分
				tmp2 = last_fix.pvt.latitude - tmp1;	//经度小数部分
				
				str_gps[13] = tmp1;//整数部分
				tmp1 = (u32_t)(tmp2*1000000);
				str_gps[14] = (u8_t)(tmp1/10000);
				tmp1 = tmp1%10000;
				str_gps[15] = (u8_t)(tmp1/100);
				tmp1 = tmp1%100;
				str_gps[16] = (u8_t)(tmp1);

				APP_get_location_data_reply(str_gps, 17);

				return;
			}
		}

		print_nmea_data();
		k_sleep(K_MSEC(500));
	}

	return;
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I get GPS data from the modem in a non-blocking manner?</title><link>https://devzone.nordicsemi.com/thread/277875?ContentTypeID=1</link><pubDate>Fri, 30 Oct 2020 15:54:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fa1d6c41-8791-435b-8fdf-e1973ccbbc93</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The GPS will output data continuously while it is running, and this data must be read out, if not you risk filling up the buffer between the modem and the application.&lt;/p&gt;
&lt;p&gt;But, you can read this data in a separate thread, thus giving the main thread (and any other threads) the opportunity to do other things.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Didrik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>