<?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>Call to bt_nus_send creates data access violation</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/92328/call-to-bt_nus_send-creates-data-access-violation</link><description>Hello, I&amp;#39;m currently using the nRF Connect SDK toolchain with BM832 modules (nrf52832). After updating to version 2.1.0 I&amp;#39;ve noticed my bluetooth applications hang whenever the applications try sending data to connected peers. I&amp;#39;ve isolated the problem</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 28 Sep 2022 17:19:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/92328/call-to-bt_nus_send-creates-data-access-violation" /><item><title>RE: Call to bt_nus_send creates data access violation</title><link>https://devzone.nordicsemi.com/thread/388420?ContentTypeID=1</link><pubDate>Wed, 28 Sep 2022 17:19:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9da4247-0178-470c-a477-af0c66e77cdd</guid><dc:creator>miket.create</dc:creator><description>&lt;p&gt;Modifying prj.conf, I was able to confirm that the stack size was the issue with the application. However, in my modified code above, I believe the call to bt_nus_send() was happening on the main thread, rather than the separate ble_write_thread as defined in the original example.&lt;/p&gt;
&lt;p&gt;So my application was still crashing when increasing the value of CONFIG_BT_NUS_THREAD_STACK_SIZE, but when I added CONFIG_MAIN_STACK_SIZE to prj.conf the application ran as expected, and messages were sent to the connected peer.&lt;/p&gt;
&lt;p&gt;Thank you again for your response, this was still very helpful and informative!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to bt_nus_send creates data access violation</title><link>https://devzone.nordicsemi.com/thread/388331?ContentTypeID=1</link><pubDate>Wed, 28 Sep 2022 11:39:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e344ba51-58e8-49d0-b3b9-4e5dfdd8a66a</guid><dc:creator>Maria Gilje</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;The short answer is that the stack size for ble_write_thread needs to be increased. You can check the stack usage with the &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.1.0/zephyr/services/debugging/thread-analyzer.html"&gt;Thread analyzer&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Now to the more detailed answer.&lt;/p&gt;
[quote user=""]00&amp;gt; [00:02:50.019,897] &amp;lt;err&amp;gt; os: &amp;gt;&amp;gt;&amp;gt; ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0[/quote]
&lt;p&gt;This log message shows the the reason for your error is a stack overflow. The stack size is one of the parameters used to define a thread, e.g. in the unmodified peripheral_uart sample, ble_write_thread is defined with&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;K_THREAD_DEFINE(ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL,
		NULL, PRIORITY, 0, 0);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;STACKSIZE from above is defined to be CONFIG_BT_NUS_THREAD_STACK_SIZE, which is set in the sample&amp;#39;s Kconfig or prj_minimal.conf, depending on which configuration file you are building with. The default value 1024 is set in Kconfig, while the value set in prj_minimal.conf is 512. You can increase the stack size by adding a line in your prj.conf where you set CONFIG_BT_NUS_THREAD_STACK_SIZE to a larger value.&lt;/p&gt;
&lt;p&gt;The new value for the stack size should not be excessively large. You can find a suitable value for the stack size by first setting it higher than you think you will need. Then you can use the &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.1.0/zephyr/services/debugging/thread-analyzer.html"&gt;Thread analyzer&lt;/a&gt; to see how much stack your threads actually use. The Thread analyzer will give a report at set intervals or when the application calls the print or run functions for Thread analyzer.&lt;/p&gt;
[quote user=""]The same code runs as expected on an earlier version of nRF Connect (1.9.1)[/quote][quote user="miket.create"]I noticed that the modified example also works for nRF Connect versions 2.0.0 and 2.0.2, and the data access violation also occurs for 2.1.0-rc1 and rc2.[/quote]
&lt;p&gt;Now, the reason for versions 1.9.1 and 2.0.x working with the modification while 2.1.0 does not likely boils down to changes in the sample which results in a slight increase of stack use for 2.1.0 compared to earlier versions. The extra increase of stack used from your modification will then result in the overflow you experienced. &lt;/p&gt;
&lt;p&gt;I hope you find this helpful, and don&amp;#39;t hesitate to ask if anything is lacking or unclear.&lt;/p&gt;
&lt;p&gt;Kind Regards,&lt;/p&gt;
&lt;p&gt;Maria&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to bt_nus_send creates data access violation</title><link>https://devzone.nordicsemi.com/thread/388226?ContentTypeID=1</link><pubDate>Tue, 27 Sep 2022 20:23:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f5e631f-f4a8-4702-8bda-c3a376258a88</guid><dc:creator>robmacl</dc:creator><description>&lt;p&gt;Additional context for this error.&amp;nbsp; I randomly hit on a workaround for this problem which worked in the older nrfconnect.&amp;nbsp; I noticed that our code worked when programmed on a NRF52dk board, but not on our target board (I derived the board definition from NRF52dk).&amp;nbsp; I read the dev board memory using the programmer and wrote it into our target.&amp;nbsp; Now it worked!&amp;nbsp; Even when I wrote new code using &amp;quot;west flash&amp;quot;.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Also, the nrf52dk stopped working with our bluetooth code when I did &amp;quot;nrfjprog --recover&amp;quot;, but started working again when I wrote the saved flash image back to the board.&lt;/p&gt;
&lt;p&gt;To me it seems very likely that we are missing some config flag which causes something to be programmed.&amp;nbsp; Could this have to do with the flash data partition used by the bluetooth code?&amp;nbsp; I copied the memory map from NRF52dk device tree without knowing what that is doing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to bt_nus_send creates data access violation</title><link>https://devzone.nordicsemi.com/thread/388211?ContentTypeID=1</link><pubDate>Tue, 27 Sep 2022 17:21:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:82f099c2-3bac-42d5-9b49-1331f265510d</guid><dc:creator>miket.create</dc:creator><description>&lt;p&gt;Thank you for taking the time on this, Maria. It is much appreciated.&lt;/p&gt;
&lt;p&gt;Just a little bit of additional info from further testing: I noticed that the modified example also works for nRF Connect versions 2.0.0 and 2.0.2, and the data access violation also occurs for 2.1.0-rc1 and rc2.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Call to bt_nus_send creates data access violation</title><link>https://devzone.nordicsemi.com/thread/388192?ContentTypeID=1</link><pubDate>Tue, 27 Sep 2022 15:11:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:45ff0a16-832e-4880-b379-9292e3d7d424</guid><dc:creator>Maria Gilje</dc:creator><description>&lt;p&gt;Hello, &lt;/p&gt;
&lt;p&gt;I am looking into your case, but I need some time to be able to answer you thoroughly. Thank you for your patience.&lt;/p&gt;
&lt;p&gt;Kind Regards,&lt;/p&gt;
&lt;p&gt;Maria&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>