<?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>Device freezes when jumping between main and Bluetooth write thread</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/96635/device-freezes-when-jumping-between-main-and-bluetooth-write-thread</link><description>Hello! Apologies if my ticket subject is a little unclear or if the terminology isn&amp;#39;t correct. I&amp;#39;m currently trying to investigate an issue that causes my device to get stuck/freeze at some point between main() and a Bluetooth thread that writes using</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 14 Feb 2023 19:56:46 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/96635/device-freezes-when-jumping-between-main-and-bluetooth-write-thread" /><item><title>RE: Device freezes when jumping between main and Bluetooth write thread</title><link>https://devzone.nordicsemi.com/thread/409951?ContentTypeID=1</link><pubDate>Tue, 14 Feb 2023 19:56:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:55a3f267-7b0b-4f6a-bee5-dbd55509dd95</guid><dc:creator>jksu</dc:creator><description>&lt;p&gt;Hello Kenneth! Thanks for the quick reply, I&amp;#39;ve done as you advised and&amp;nbsp;fixed the blocking behavior within that thread. I also discovered that I had areas in my main() that were blocking as well, which I corrected with the&amp;nbsp;&lt;strong&gt;else&lt;/strong&gt; sleep solution you recommended.&lt;br /&gt;&lt;br /&gt;Once I corrected those, the issue went away! Thanks for figuring it out!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Device freezes when jumping between main and Bluetooth write thread</title><link>https://devzone.nordicsemi.com/thread/409789?ContentTypeID=1</link><pubDate>Tue, 14 Feb 2023 10:01:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bd62d076-b46d-46f8-80fd-2530b94a3364</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Looking at the following you shared this thread is blocking if the if() statement is not true, so I added an else:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/* Function for writing messages from peripheral to central (mobile device, or other) */
void ble_write_thread(void)
{
	/* Don&amp;#39;t go any further until BLE is initialized */
	k_sem_take(&amp;amp;ble_init_ok, K_FOREVER);

	//int i = 0;
	for (;;)
	{
		if ((current_conn != 0) &amp;amp;&amp;amp; (databuf != 0))
		{
			bt_nus_send(NULL, databuf, 10);
			printk(&amp;quot;\nMessage Sent!&amp;quot;);
			k_msleep(100); //this is where the thread priority is passed back to main()
			printk(&amp;quot;Returned to ble_write_thread\n&amp;quot;);
		}
		else
		{
		    k_msleep(100); //nothing to do
		}
	}
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Likely there are better ways to do this, but at least it avoid it to be blocking.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Device freezes when jumping between main and Bluetooth write thread</title><link>https://devzone.nordicsemi.com/thread/409709?ContentTypeID=1</link><pubDate>Mon, 13 Feb 2023 21:27:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7db31d1b-456d-4832-8b45-3fc7603f7904</guid><dc:creator>jksu</dc:creator><description>&lt;p&gt;Just an updated comment to include additional observed behavior&lt;br /&gt;&lt;br /&gt;This error seems to have a higher chance to occur if I let the code run idly through main for a few minutes without sending a message. &lt;br /&gt;&lt;br /&gt;So if I a) tile device to send a message, b) return to main thread, c) wait for 5-10 minutes, d) tilt device to send a message, this has a very high chance to get stuck before returning to main&lt;br /&gt;&lt;br /&gt;Edit: I managed to replicate the issue while in Debug mode, and when the code got stuck, I clicked Pause, and ended up in &lt;strong&gt;cpu_idle.S&lt;/strong&gt;, line 105, cpsie i&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;	/*
	 * Wait for all memory transactions to complete before entering low
	 * power state.
	 */
	dsb

	/* Enter low power state */
	wfi

	/*
	 * Clear PRIMASK and flush instruction buffer to immediately service
	 * the wake-up interrupt.
	 */
	cpsie	i
	isb

	bx	lr&lt;/pre&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>