<?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>calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/46471/calling-fds_write-within-a-ble_evt_handler-gets-the-sd-or-app-stuck</link><description>I&amp;#39;m modifying the DFU example app in SDK 15.3, for the nRF52840. 
 attempting to call fds_record_update or fds_record_write within the context of app/main.c ble_evt_handler(), gets the system stuck. 
 Calling it elsewhere, for example from main(), works</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 23 Apr 2019 11:42:47 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/46471/calling-fds_write-within-a-ble_evt_handler-gets-the-sd-or-app-stuck" /><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/183097?ContentTypeID=1</link><pubDate>Tue, 23 Apr 2019 11:42:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:85394eaf-3f2d-43db-9a89-72838eafa98c</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;And the problem with handling the write to flash in the event handler is that, if the write returns an error for one reason or another, there&amp;#39;s no way to retry later but to wait for another BLE event.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/183096?ContentTypeID=1</link><pubDate>Tue, 23 Apr 2019 11:40:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a26bc255-3b31-46bc-8317-bad2c038bb9b</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;There&amp;#39;s the&amp;nbsp;&lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.3.0/lib_scheduler.html"&gt;scheduler&lt;/a&gt;&amp;nbsp;library. I don&amp;#39;t know exactly which examples showcase its use though. It&amp;#39;s pretty easy to use.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/183094?ContentTypeID=1</link><pubDate>Tue, 23 Apr 2019 11:37:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9ea71862-2bc9-4047-86a9-3a5ed526dd6f</guid><dc:creator>kobyatom</dc:creator><description>&lt;p&gt;Thanks for helping so far. do Nordic have an example project of events/msgs getting sent to and handled in the main loop, maybe even inside a message-queue?&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/183093?ContentTypeID=1</link><pubDate>Tue, 23 Apr 2019 11:35:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ac24a23f-e396-48c3-807e-54e1cbdfd0a4</guid><dc:creator>kobyatom</dc:creator><description>&lt;p&gt;the proper way for me was to call fds_write from the ble_event, and skip waiting for it to complete. I add protection that returns err and doesn&amp;#39;t call fds_write, in case the WRITE hasn&amp;#39;t completed (fds_event_handler not called).&lt;/p&gt;
&lt;p&gt;My FDS writing occur due to sporadic BLE write events, so there is no chance of any of them getting rejected anyway.&lt;/p&gt;
&lt;p&gt;FDS writing from the ble_evt_handler context now works fine.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/183091?ContentTypeID=1</link><pubDate>Tue, 23 Apr 2019 11:31:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:44557e77-5255-43be-9118-30acdbcf6e57</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;Mm good question, I couldn&amp;#39;t find anything about it. Maybe the events are also generated by the softdevice since the FDS uses the SD as a backend.&lt;/p&gt;
&lt;p&gt;We should probably wait for an answer from Nordic for this. In any case, the proper&amp;nbsp;way to do it is to remove the write (and the while loop) from the event handler and write to flash later from the main context.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/183082?ContentTypeID=1</link><pubDate>Tue, 23 Apr 2019 10:46:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:94a0f740-d784-4582-926b-d4716f4b9427</guid><dc:creator>kobyatom</dc:creator><description>&lt;p&gt;how do I change the interrupt priority of the FDS module?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/183046?ContentTypeID=1</link><pubDate>Tue, 23 Apr 2019 08:54:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0ffb0933-342d-417c-a567-4f4c4d0dd1fb</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;Well, what&amp;#39;s the benefit of having non-blocking APIs if you&amp;#39;re wrapping them in while loops and waiting for the operation to finish? You&amp;#39;re effectively converting the non-blocking code into blocking code.&lt;/p&gt;
&lt;p&gt;Also, I&amp;#39;m not sure if I understood correctly, but I think you have those while loops in the ble_evt_handler. As I said, if the interrupt priority of the FDS module is lower than the BLE priority, the FDS event won&amp;#39;t ever preempt the BLE event, and therefore the flag won&amp;#39;t ever be set. You would just be stuck in a while loop in the BLE event handler forever, and since you&amp;#39;re already in the event handler, no other events can be processed.&lt;/p&gt;
&lt;p&gt;Lastly, you don&amp;#39;t know how long the write operation is going to take. That&amp;#39;s up to the softdevice to decide and it depends on how busy it is.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/183015?ContentTypeID=1</link><pubDate>Tue, 23 Apr 2019 08:09:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2b92d892-a97b-4fa6-b7a6-89ba7de910b3</guid><dc:creator>kobyatom</dc:creator><description>&lt;ol&gt;
&lt;li&gt;fds_record_write is async and non-blocking. It causes a callback to be called when the write is complete&lt;/li&gt;
&lt;li&gt;after calling fds_record_write, I&amp;nbsp;wait in a sleep-loop (not busy-loop) using&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;    while (fds_write_in_progress)
    {
        nrf_pwr_mgmt_run();
    }
    
    // or
    
    while(fds_write_in_progress)
    {
    #ifdef SOFTDEVICE_PRESENT
        (void) sd_app_evt_wait();
    #else
        __WFE();
    #endif
    }&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If there is only 1 task in the&amp;nbsp;SoftDevice, which is responsible for handling ble_evt_handler and the fds event handler, then it will indeed block, since it will not get the chance to respond to the fds event.&lt;br /&gt;But, since my write operation is short (several bytes, which take a few microseconds which I believe the SD can spare), and I would like to keep my code simple, I hope there is a way to create a 2nd thread, one that will be coupled with the fds events and other non-BLE hardware events (ADC, I2C, and such).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: calling fds_write within a ble_evt_handler gets the SD or app stuck</title><link>https://devzone.nordicsemi.com/thread/182940?ContentTypeID=1</link><pubDate>Mon, 22 Apr 2019 14:43:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d07be828-14ce-4979-8377-fa646a3d5ec1</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;It sounds to me like you have a problem with your code. Executing blocking code in event handlers is not a good idea, you need to use flags or to schedule function calls to be executed in the main context.&lt;/p&gt;
&lt;p&gt;My guess is that the interrupt priority of the FDS module is lower than the BLE priority in your code.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>