<?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 in flash + re-init problem</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/39952/write-in-flash-re-init-problem</link><description>Hello, 
 
 I am currently running into an issue with nrf51822. I am trying to write in flash a flag which defines the device I use. Indeeed, the device on which the code will run can have 3 modes &amp;amp; a user can modify this mode with a command sent through</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 31 Oct 2018 14:55:44 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/39952/write-in-flash-re-init-problem" /><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155380?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 14:55:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a41525ad-715b-4a3c-ad32-63db822c19f2</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Yes, that could explain the behavior.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155366?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 14:26:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e63bae0b-5411-4484-bf75-817bcb8b1ea4</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;Oh gosh, I think I just found the mistake, I interverted to letters in memory address (0x33AC0 =&amp;gt; 0x33CA0)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155361?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 14:14:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:22930873-7972-4aa6-a903-22c8468d3cc2</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Did you pass the address itself to&amp;nbsp;sd_flash_page_erase? Note that this function take the&amp;nbsp;&lt;strong&gt;&lt;em&gt;page_number&lt;/em&gt;&lt;/strong&gt; as parameter. The &lt;a href="http://infocenter.nordicsemi.com/pdf/nRF51822_PS_v3.3.pdf#page=22"&gt;page size in nRF51822 is 1024 bytes&lt;/a&gt;. Have a look at &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/18177/relation-between-addresses-and-pages-in-flash/70190#70190"&gt;this answer&lt;/a&gt;&amp;nbsp;for conversion from address to page.&lt;/p&gt;
&lt;p&gt;Please always remember to check the error codes from function calls. It looks like you are not doing this on call to&amp;nbsp;sd_flash_page_erase in your code posted before. Most likely this function will report an error if you pass an invalid address.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155357?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 14:09:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d0011118-a5a7-40a3-b15c-58d975f8d969</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;Maybe I can post here the init function, main, UART command processing function &amp;amp; write in flash function it that can help&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155355?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 14:03:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:df6053a9-b39d-4ec6-b535-431696f76f57</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;I did, erase did not seem to work, still 00 00 00 00 at this memory location&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155353?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 13:58:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b126bd6e-39e3-4d0c-87b3-eb7063d46f6c</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Removing calls to advertising/scan start should be sufficient.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155352?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 13:54:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:08991b44-3092-4aad-b4ec-9b8a3eeb83af</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;What do I need to do to achieve that ? Remove all inits but keep only&amp;nbsp;softdevice_sys_evt_handler_set(sys_evt_handler) + timer_init + SOFTDEVICE_HANDLER_INIT ?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155350?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 13:46:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:392bdb49-176c-4ea0-af9c-bad2bc052e6a</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Yes, this is correct. Can you try the code that you use for erasing with all BLE activity deactivated (no advertising/scanning/connection, but softdevice still enabled)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155348?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 13:43:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a6f5d663-f934-45e6-b99a-0395ce5a3163</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;Seems that the flash is never erased, I still have 00 00 00 00 at this address, when I erase I should get FF FF FF FF if I am right&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155325?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 12:39:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3dcf26ca-5231-4421-8a30-5d0b0fb80516</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Put an infinite while loop (while(1)) after the erase and check the flash with nrfjprog.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155322?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 12:33:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f72d770a-1249-4477-bba6-9e4c8b0c1357</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;I just tried to put a breakpoint before erasing flash, I do not know why but Keil does not let me put a breakpoint there, so I think the erase is not done, maybe that could be the problem, I cannot erase =&amp;gt; I cannot write again in flash. But I do not know why I cannot put a breakpoint there or why this part of code cannot be run&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155319?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 12:28:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8447345d-c547-41bd-ab29-d0ea7127ed1e</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;It could, but then you should not receive a SUCCESS event. Did you try reading the flash area after erase operation but before write operation?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155312?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 12:14:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8abcf247-74d2-4d6f-97b7-0cb4467313b1</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;Yes, I do the exact same thing when I erase a page, I wait for&amp;nbsp;&lt;span&gt;NRF_EVT_FLASH_OPERATION_SUCCESS, then I try to write again, waiting for the same event to be triggered, I was suspecting BLE activity could maybe interfere when I try to write in flash, could it be that ?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155309?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 12:08:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:422ba777-fdc4-4c99-94cd-30f840359cb6</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Are you waiting for the SUCCESS even when erasing as well? Note that &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.nrf52832.ps.v1.1/nvmc.html?cp=2_1_0_10_7#unique_1022076144"&gt;erasing a page can take a long time&lt;/a&gt;, especially if there are &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.sds/dita/softdevices/s130/flash_mem_api/flash_mem_api.html?cp=2_3_1_0_7"&gt;BLE activity going on at the same time&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155285?ContentTypeID=1</link><pubDate>Wed, 31 Oct 2018 10:24:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3d97e0e2-325c-4505-8227-a1a71d5ff24d</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;I have already kinda&lt;span&gt;&amp;nbsp;&lt;/span&gt;done&lt;span&gt;&amp;nbsp;&lt;/span&gt;that with Keil &amp;amp; &amp;quot;Memory&amp;quot; section while in debug mode and the value did not change. I tried it with nrfjprog but got same results, when I erase &amp;amp; reflash all (including SoftDevice), if I read at this address, I have FF FF FF FF which is normal. Then I run the program once and set the default value at this memory location which is 00 00 00 00, that works.&lt;/p&gt;
&lt;p&gt;After that, I cannot change the value anymore, I try to erase it first then write the new value but that does not seems to work.&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/87827.Capture.PNG" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155205?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 15:59:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:25cf2584-36b7-4737-b664-4d7e00be3372</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Can you read the memory location you are writing before and after writing it? You can do this using nrfjrpog with the command&amp;nbsp;&lt;span&gt;--memrd &amp;lt;addr&amp;gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155170?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 14:34:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc64beb9-29f8-4fbb-af4c-b99c474746ab</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;Nothing really happens, the status code is ok (NRF_SUCCESS), after the write has been done I restart the device with NVIC_SystemReset, but value in flash is not modified, therefore I start again with the exact same mode.&lt;/p&gt;
&lt;p&gt;So no particular error code, the address location is 0x00033AC0 which corresponds to the beginning of a page (page size is 1024)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155162?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 14:25:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b0f05dcd-1e8c-40ac-87c9-581c9ddaef2e</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;What happens when you try to write flash again? Do you get any error codes etc.? What address is the location you are trying to write?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155157?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 14:16:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:05618ea7-2332-4db1-9f17-54ab4236fdf5</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;I used nRFgo Studio to erase all and flasing again SoftDevice, then Keil for the application.&lt;/p&gt;
&lt;p&gt;When I try to write in flash, I start by erasing the corresponding page first&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;flashBusy = true;
sd_flash_page_erase(DEVICE_TYPE_PAGE);
    
// Wait end erase flash page
while(flashBusy)
{
    sd_app_evt_wait();
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I run this code before writing in flash, the flashBusy variable is reset by the sys_evt_handler callback on&amp;nbsp;&lt;span&gt;NRF_EVT_FLASH_OPERATION_SUCCESS&amp;nbsp;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;It is not planned to write oftenly to flash, but for now, I cannot change the default value, it is pretty weird because I had the behaviour I wanted once but since then I cannot reproduce it anymore, really do not know why&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155155?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 14:08:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:47631000-8164-485d-900b-d839287b37f1</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;You should be able to write the location in flash once after erasing the page. Did you run &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.tools/dita/tools/nrf5x_command_line_tools/nrf5x_nrfjprogexe_reference.html?cp=5_1_3_0"&gt;--eraseall command using nrfjprog&lt;/a&gt;? If you only erase the flash are used by softdevice and application, this will not erase the are where your application wrote to flash. If you need to update the information in flash, you need to change the location where it is stored, or &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.s132.api.v6.1.0/group___n_r_f___s_o_c___f_u_n_c_t_i_o_n_s.html#ga9c93dd94a138ad8b5ed3693ea38ffb3e"&gt;erase the flash page&lt;/a&gt; where the address is located. If you are planning to write often/much data to flash, I would recommend looking into &lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.2.0/lib_fds.html?cp=4_0_0_3_55"&gt;FDS&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155148?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 13:42:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:99715fb8-c077-4e3e-bfa5-da65d87b899e</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;Well in fact I am running into another issue which I do not understand at all, I managed to get the desired behaviour once, and now, nothing works at all, the program cannot write in flash at the specified address, I really do not know why.&lt;/p&gt;
&lt;p&gt;I tried to erase all in the device then re-flash the SoftDevice &amp;amp; application but it does not work anymore, I can write in flash the first time I run the program (I write at init the default mode) but after that, I cannot do it again it seems&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155135?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 13:18:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5008af19-84f2-4f22-be70-df5b232d35cf</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;I was at first trying to use it from interrupt context and figured out it was not working so I changed that and now&amp;nbsp; I trigger the Flash write in main context. I identified the problem I think, in my function which processes the UART command received, I sent again some debug information on same UART so I was trigerring a new interrupt which caused the write in flash to fail.&lt;/p&gt;
&lt;p&gt;Now I am able to write in flash &amp;amp; change the mode as I wanted, I just have another issue but I do not think it is related to this one, I guess it is due to bad behaviour in uart_event_handle function in different mode, I have to go check this out&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155129?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 13:02:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a81cbf57-007a-4e2b-ac86-83b830fab79d</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Most drivers/libraries have uninit functions. In most cases you should be able to reuse the initialized peripherals without re-initialization.&lt;/p&gt;
&lt;p&gt;Are you triggering the Flash write from interrupt context or main context? Have you tried debugging the application to see if it gets past the while loop where you call sd_flash_write?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155102?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 12:26:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f3b270f-c329-43ff-9d97-410e5d0aa30e</guid><dc:creator>NicoH</dc:creator><description>&lt;p&gt;Hi Jorgen,&lt;/p&gt;
&lt;p&gt;thank you for your answer, I figured out that I could not init the UART twice, same with BLE stack &amp;amp; other init functions, how to uninit those ?&lt;/p&gt;
&lt;p&gt;I also tried something else, instead of trying to re-init, I change the device mode &amp;amp; write the new mode in flash, reset the device &amp;amp; then init with the new mode but I have some issues trying to write in flash. Indeed, when the device receive the UART command to change its mode, a flag is set to 1 &amp;amp; in the main function, when this flag is set, I try to write in flash the new mode &amp;amp; then reset the device but writing to flash fails, the program&amp;nbsp;waits for&amp;nbsp;NRF_EVT_FLASH_OPERATION_SUCCESS which never happens.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I do not understand pretty well this behaviour, I&amp;nbsp;guess the sys_evt_handler callback is never called to trigger this event but I am not sure and i do not know why either.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Nicolas Hell.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Write in flash + re-init problem</title><link>https://devzone.nordicsemi.com/thread/155088?ContentTypeID=1</link><pubDate>Tue, 30 Oct 2018 11:56:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fafc5a66-80de-447b-9a86-025e090eebf2</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you have already initialized UART, you will get&amp;nbsp;&lt;span&gt;INVALID_STATE error if you try to init it again. You either need to uninit the old initialization before re-init, or use the old initialized UART interface.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;br /&gt;Jørgen&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>