<?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>Peripheral crashes when Central powers off</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/112310/peripheral-crashes-when-central-powers-off</link><description>Hi, 
 My fw running on nRF52840 (ncs2.4.0, zephyr) develops from HRS sample application. It acts as a peripheral. 
 In this project, I have a dedicated thread to receiving data from a queue, then notify to the connected Central 
 
 `notify_user_data(</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 26 Jun 2024 16:17:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/112310/peripheral-crashes-when-central-powers-off" /><item><title>RE: Peripheral crashes when Central powers off</title><link>https://devzone.nordicsemi.com/thread/490939?ContentTypeID=1</link><pubDate>Wed, 26 Jun 2024 16:17:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2fc85d0d-7b52-4075-a87f-30b31a11e891</guid><dc:creator>quan1328</dc:creator><description>&lt;p&gt;I just got this resolve. Thanks, Vidar.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Peripheral crashes when Central powers off</title><link>https://devzone.nordicsemi.com/thread/490838?ContentTypeID=1</link><pubDate>Wed, 26 Jun 2024 10:30:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:11b9def2-1234-4ff1-91e5-3f81ff1eb549</guid><dc:creator>quan1328</dc:creator><description>&lt;p&gt;Hi Vidar,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks for the link in your last reply, which&amp;nbsp;hints me about the bug in `func_thread()`&lt;/p&gt;
&lt;p&gt;When the connected central disconnects, BLE stack generates an IRQ, which calls to my on_disconnected() callback that was set up in the other module of my project?&lt;/p&gt;
&lt;p&gt;In this link:&amp;nbsp;&lt;a id="" href="https://docs.zephyrproject.org/latest/kernel/services/data_passing/message_queues.html"&gt;https://docs.zephyrproject.org/latest/kernel/services/data_passing/message_queues.html&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;span&gt;However, this can increase interrupt latency as interrupts are locked while a data item is written or read.&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;span&gt;&lt;strong&gt;While&lt;/strong&gt; the queue is read by `k_msgq_get()`, the Interrupt comes in, thus, the thread crashes.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;What do you think about this crash flow?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;By the way, I summarize my code below:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;typedef struct esp_msg
{
	short len;
	uint8_t buf[512];
} esp_msg_t;

void func_thread1()
{
    // stack size: 4096
    // prio: 1
    
    while(1)
    {
        esp_msg_t msg;
		msg.len = 125 * 2 + 4;
		memcpy(msg.buf, ecg_send_pkt, msg.len);
		k_msgq_put(&amp;amp;uart_msgq, &amp;amp;msg, K_NO_WAIT);
		
		...
		// sleep
    }
}

void func_thread2()
{
    // stack size: 16024, increased from 4096
    // prio:  2
    
    esp_msg_t msg;

    while (1)
    {
        int status = k_msgq_get(&amp;amp;uart_msgq, &amp;amp;msg, K_MSEC(500));
		if (status != 0)
		{
			// k_yield(); no need due to K_MSEC(500)
			continue;
		}
		
		notify_user_data(msg.buf, msg.len);
    }
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;In `func_thread2()` I changed from `k_msgq_get(&amp;amp;uart_msgq, &amp;amp;msg, k_is_in_isr() ? K_NO_WAIT : K_FOREVER) == 0` to `&lt;/span&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;k_msgq_get&lt;/span&gt;&lt;span&gt;(&amp;amp;&lt;/span&gt;&lt;span&gt;uart_msgq&lt;/span&gt;&lt;span&gt;, &amp;amp;&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;K_MSEC&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;500&lt;/span&gt;&lt;span&gt;))` because I don&amp;#39;t think it&amp;#39;s needed. However, it still crashes.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Somehow,&amp;nbsp;k_msgq_get() blocks the system when ISR comes in. That&amp;#39;s why it crashes?&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;How should I solve this?&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Regards&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Peripheral crashes when Central powers off</title><link>https://devzone.nordicsemi.com/thread/489998?ContentTypeID=1</link><pubDate>Fri, 21 Jun 2024 13:53:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:82a90db2-6f98-4391-8e15-469a2557348c</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;It looks like the assert in the &amp;quot;ios&amp;quot; case is raised here because the connection object pointer is NULL:&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/52292768176a4372eaeb682d191dccd5c6064b66/subsys/bluetooth/host/gatt.c#L2090"&gt;https://github.com/nrfconnect/sdk-zephyr/blob/52292768176a4372eaeb682d191dccd5c6064b66/subsys/bluetooth/host/gatt.c#L2090&lt;/a&gt;, which&amp;nbsp;maybe makes sense given the connection has been terminated. However, I&amp;#39;m not sure what could be causing the busfault.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I recommend you build the project with CONFIG_RESET_ON_FATAL_ERROR=n, then try to debug your project in VS code. After the fault exception has been raised, you can&amp;nbsp;click on the last stack frame for the &amp;#39;uart_pro_task&amp;#39; thread to see where the program was running prior to the crash.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&amp;nbsp;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>