<?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>Write to FLASH inside BLE event</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/19761/write-to-flash-inside-ble-event</link><description>Hello,
i have read if not all posts about writing to FLASH while using BLE. But could not find the desirable method. 
 What i would like to do is to write to flash from characteristic write as it comes.
The problem is that sd_flash_write is asynchronous</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 17 Feb 2017 12:55:26 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/19761/write-to-flash-inside-ble-event" /><item><title>RE: Write to FLASH inside BLE event</title><link>https://devzone.nordicsemi.com/thread/76837?ContentTypeID=1</link><pubDate>Fri, 17 Feb 2017 12:55:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c5cd6ff-8d13-4ffd-80c1-08950c188309</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;If you are sending lots of byte to the device, I would suggest you follow what we do in our DFU protocol where the DFU master after sending say 200 bytes will stop and wait for a notification from the nRF5 before it keep sending the next 200 bytes.
Then you just need a 200 bytes buffer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write to FLASH inside BLE event</title><link>https://devzone.nordicsemi.com/thread/76840?ContentTypeID=1</link><pubDate>Fri, 17 Feb 2017 12:16:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f3d1856-67ae-4d37-b154-44e8e92c0626</guid><dc:creator>schef</dc:creator><description>&lt;p&gt;Thanks for the comment. For example when i want to transfer 50kb of data over BLE. The good thing is that you can&amp;#39;t send more the 20 bytes over BLE at once so that means that it will block the CPU for max 5 words which is a very short time i think.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write to FLASH inside BLE event</title><link>https://devzone.nordicsemi.com/thread/76839?ContentTypeID=1</link><pubDate>Fri, 17 Feb 2017 12:11:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1f0cd90-46de-48c9-b156-0fe5eb83ca6e</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Well, stay for a long time in a interrupt handler may not the very best solution. CPU will be occupied and any other interrupt handler except for APP HIGH will not be executed. Note that there is a chance that the flash write will not be successful. For example when the softdevice return NRF_EVT_FLASH_OPERATION_ERROR   then you may end up in a infinite loop.&lt;/p&gt;
&lt;p&gt;I still don&amp;#39;t know what type of data buffer that you have and which event you have to handle the flash writing. If it&amp;#39;s not BLE event, but application event (interrupt from peripheral ) then you can set it to APP LOW priority and you should be able to wait for the event instead of writing and waiting word by word.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write to FLASH inside BLE event</title><link>https://devzone.nordicsemi.com/thread/76843?ContentTypeID=1</link><pubDate>Fri, 17 Feb 2017 08:44:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dff59477-81fd-4da9-99cf-88da565f22bf</guid><dc:creator>schef</dc:creator><description>&lt;p&gt;I think i have found a solution. I write a word for word in memory so i can check if is written with this simple snippet:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sd_flash_write(address, &amp;amp;word, 1);
while (*address != word) {
 nrf_delay_ms(1);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For now that i have tested i think it needed around 2ms for one word or write operation.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write to FLASH inside BLE event</title><link>https://devzone.nordicsemi.com/thread/76842?ContentTypeID=1</link><pubDate>Fri, 17 Feb 2017 08:36:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:57fe55cf-afa1-498f-a96c-060593985599</guid><dc:creator>Hung Bui</dc:creator><description>&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Sorry I didn&amp;#39;t get you at the beginning. Yes, you are correct. This is due to the nature that flash operation will block all CPU activity, so we need to let the softdevice control when to execute flash. And yes, then you will need to wait for NRF_EVT_FLASH_OPERATION_SUCCESS inside sys_evt_handler. And if you put a loop inside the interrupt handler to wait, you will not receive the NRF_EVT_FLASH_OPERATION_SUCCESS event because you still inside the interrupt handler with same priority level.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The local variable with large data you need to store, does it have to be in the BLE event handler ? If it&amp;#39;s in the APP Low event handler then it&amp;#39;s possible to wait for the event there because APP Low has lower priority than the BLE event.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write to FLASH inside BLE event</title><link>https://devzone.nordicsemi.com/thread/76841?ContentTypeID=1</link><pubDate>Fri, 17 Feb 2017 07:45:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:48891fe3-fc2e-4ae9-9d63-7ea1b9dad2a9</guid><dc:creator>schef</dc:creator><description>&lt;ul&gt;
&lt;li&gt;Writen data may get corrupted because of a local variable that you want to write at the time of writing will no longer exist. Which means you have to use a buffer which is not suitable for large data transfer.&lt;/li&gt;
&lt;li&gt;I beleve that the event does not come because of a interrupt priority issue.&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write to FLASH inside BLE event</title><link>https://devzone.nordicsemi.com/thread/76838?ContentTypeID=1</link><pubDate>Thu, 16 Feb 2017 14:26:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9f63c668-4adb-4c30-a8d1-c716ccb0fbe1</guid><dc:creator>Hung Bui</dc:creator><description>&lt;p&gt;Hi Schef,&lt;/p&gt;
&lt;p&gt;Could you explain a little bit more on:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;why &amp;quot;writen data may get corrupted&amp;quot; ? Couldn&amp;#39;t you store data in another variable until it&amp;#39;s finished writing?&lt;/li&gt;
&lt;li&gt;But as some other post mentioned if you call the flash command inside the ble event you don&amp;#39;t get the response. =&amp;gt;Not sure if it&amp;#39;s correct. Only when you call the API in a APP_HIGH level then hardfault may occur.&lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>