<?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>Last breath SAVE after power is cut off</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/97511/last-breath-save-after-power-is-cut-off</link><description>Hi, 
 I have a device built on NRF52840. I use Softdevice S340 since I use both BLE and ANT+. I&amp;#39;m on SDK 17.0.2. 
 My problem is - I want to save the currect state of my device to flash, just before it goes dead after power is turned off. The construction</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 09 Mar 2023 20:41:51 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/97511/last-breath-save-after-power-is-cut-off" /><item><title>RE: Last breath SAVE after power is cut off</title><link>https://devzone.nordicsemi.com/thread/414496?ContentTypeID=1</link><pubDate>Thu, 09 Mar 2023 20:41:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:06d476f8-b021-4b71-81c4-56e6f1bf5dab</guid><dc:creator>dragilla</dc:creator><description>&lt;p&gt;Ok. I managed to fix this. Thank you very much for your help.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The problem was - I was writing 8-bit words instead of 32-bit words. Now It all works just fine.&lt;/p&gt;
&lt;p&gt;This is what I ended up doing:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void EMERGENCY_SAVE_TIMER_event_handler(nrf_timer_event_t event_type, void* p_context)
{
		NRF_LOG_DEBUG(&amp;quot;Emergency CONFIG SAVE initiated&amp;quot;);
		nrf_sdh_suspend();	

		NRF_MWU-&amp;gt;PREGION[0].SUBS &amp;amp;= ~(MWU_PREGION_SUBS_SR30_Include &amp;lt;&amp;lt; MWU_PREGION_SUBS_SR30_Pos);
		__DSB(); 

	
		// Enable the NVMC write
		NRF_NVMC-&amp;gt;CONFIG = 1; 
        // Wait for the NVMC to be ready
        while (NRF_NVMC-&amp;gt;READY == 0);
		
		uint32_t value;
		for (int i = 0; i &amp;lt; sizeof(data_to_write80); i += 4) 
		{
				value = (data_to_write80[i+3] &amp;lt;&amp;lt; 24) | 
				        (data_to_write80[i+2] &amp;lt;&amp;lt; 16) | 
				        (data_to_write80[i+1] &amp;lt;&amp;lt;  8) | 
				        (data_to_write80[i]);
				*(volatile uint32_t *)(config_addr + i) = value;
				while (NRF_NVMC-&amp;gt;READY == 0);
		}				
		NRF_LOG_DEBUG(&amp;quot;Emergency CONFIG SAVE complete&amp;quot;);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I think we can close this thread. Once again - Thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Last breath SAVE after power is cut off</title><link>https://devzone.nordicsemi.com/thread/414464?ContentTypeID=1</link><pubDate>Thu, 09 Mar 2023 16:48:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f4284cf-b061-483b-83d8-d5ccae4b790e</guid><dc:creator>dragilla</dc:creator><description>&lt;p&gt;Thank you for your answer. Yes - nothing else should happen. Or even if anything happens - it does&amp;#39;t matter.&lt;/p&gt;
&lt;p&gt;I tried sisabling the protection of the NVMC as you mentioned. Then&amp;nbsp; I also call&amp;nbsp;nrf_sdh_suspend() which disables interrupts.&lt;/p&gt;
&lt;p&gt;Then I do:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;	// Enable the NVMC
    NRF_NVMC-&amp;gt;CONFIG = NVMC_CONFIG_WEN_Wen;

    // Wait for the NVMC to be ready
    while (NRF_NVMC-&amp;gt;READY == NVMC_READY_READY_Busy) {}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And finally I call this function:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void my_write_data_to_flash(uint8_t *data, uint32_t length, uint32_t address)
{
    // Enable writing to flash memory
    *((volatile uint32_t *)(NRF_NVMC_BASE + NRF_NVMC_CONFIG_OFFSET)) = 1;
    // Write the data to the flash memory
    for (uint32_t i = 0; i &amp;lt; length; i++) {
        *((volatile uint8_t *)(address + i)) = data[i];
        // Wait for the write to complete
        while (*((volatile uint32_t *)(NRF_NVMC_BASE + NRF_NVMC_READY_OFFSET)) == 0) {}
    }
    
    // Disable writing to flash memory
    *((volatile uint32_t *)(NRF_NVMC_BASE + NRF_NVMC_CONFIG_OFFSET)) = 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;It halts at&amp;nbsp;*((volatile uint8_t *)(address + i)) = data[i];&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I also tried wrapping the call in&amp;nbsp;CRITICAL_REGION_ENTER() and&amp;nbsp;CRITICAL_REGION_EXIT();&lt;/p&gt;
&lt;p&gt;No go.&lt;/p&gt;
&lt;p&gt;Anythin else I could try?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;PS: these are the defines I used:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;// Define the addresses of the registers that control the flash memory
#define NRF_NVMC_READY_OFFSET       (0x400UL)
#define NRF_NVMC_CONFIG_OFFSET      (0x504UL)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;EDIT: I think it might be an issue with the address. I guess it is not the same address that I use for nrf_fstorage_write.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m having difficulties calculating the right value. Can you please help?&lt;/p&gt;
&lt;p&gt;For now, the address I use for nrf_strorage_write is&amp;nbsp;0x000AB000&lt;/p&gt;
&lt;p&gt;And my fstorage is defined as follows:&amp;nbsp;&lt;pre class="ui-code" data-mode="text"&gt;NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
{
    /* Set a handler for fstorage events. */
    .evt_handler = fstorage_evt_handler,

    /* These below are the boundaries of the flash space assigned to this instance of fstorage.
     * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
     * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
     * last page of flash available to write data. */
    .start_addr = 0x4B000, // current end of app is 0x4A8F3
    .end_addr   = 0xE0000, // bootloader start (0xE0000)
};&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;EDIT2: I checked in the NRF Connect Programmer app and it seems this IS in fact a physical address. I can clearly see 80 byes (my saved data size) at the location&amp;nbsp;&lt;span&gt;0x000AB000.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;So the problem is most likely NOT the address.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Last breath SAVE after power is cut off</title><link>https://devzone.nordicsemi.com/thread/414365?ContentTypeID=1</link><pubDate>Thu, 09 Mar 2023 12:26:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:133f8930-743c-4194-b0b3-58c4377f2cbe</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user=""]1. Can I make the 15ms timer with same or higher priority as the SoftDevice? My experiments / reading showed that this is sadly impossible[/quote]
&lt;p&gt;Technically you may be able to use a high priority interrupt if the interrupt routine is very short, but having interrupts of the highest priority (0) is not supported and may cause problems for the SoftDevice (certainly if the ISR is not very short). Do you need that level of accuracy, though? What if you use&amp;nbsp;APP_IRQ_PRIORITY_HIGHEST, which is 2 on a nRF52 series device? That means it will be lower priority than the most time critical SoftDevice operations, but higher than lower priority.&amp;nbsp;Based on the use case you describe it does not seem to me that this is extremely timing critical, and I would expect that to be ok?&lt;/p&gt;
&lt;p&gt;For point 2-5 I think that can be done in a simpler way. As this is a special case, where the device is shutting off,&amp;nbsp;and nothing else should happen(?), you could simply disable all interrupts, and then access the NVMC directly via register access. You could also disable just the MWU protection of the NVMC, as suggested in &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/53248/disable-softdevice-sandboxing-in-fault-handler"&gt;this thread&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>