Last breath SAVE after power is cut off

Hi,

I have a device built on NRF52840. I use Softdevice S340 since I use both BLE and ANT+. I'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 of my device allows it to work for couple of seconds after it is powered off. I noticed that if I initiate the save action from a connected BLE device it works: I cut the power, then I click save in the mobile app connected to my device and I can see in the log, that the save worked just fine.

However If I do it from my code (I have a mechanism that can detect if the power was cut off) the nrf_fstorage_write function keeps waiting and never executes. I guess it's waiting for the SoftDevice, since it's priority is higher by definition?

The power off detection mechanism is based on an interrupt detecting the external power supply phase going trhough "0" . I get an tnterrupt every 10ms (for 50Hz installations). So I created a timer that expires after 15 ms. I reset this timer in the event handler of the "0 detection". When the timer expires I know it's been no more that 15 ms since the power was cut off. I tested this mechanism and it works reliably.

My questions and ideas:

1. Can I make the 15ms timer with same or higher priority as the SoftDevice? My experiments / reading showed that this is sadly impossible

2. Can I somehow turn off / block / stop the Softdevice just before my nrf_fstorage_write (in the 15ms timer handler)? I don't care about waiting events, ongoing stuff, queues, anything. The power was cut off - I need to save! - sadly I was also unable to do so using different methods.

3. Can I wite to Flash using some other methos that would not interfere with the SD? I tried unloading the fstorage and saving using other methods, but mostly I just get 'SOFTDEVICE: INVALID MEMORY ACCESS'

4. Can I somehow trigger the BLE Characteristic that is used by the mobile app, but do it locally? Ideally without my device being connected to itself via Bluetooth. 

5. Or maybe there is some other method to be used to call a function from the context of the SoftDevice, i.e. with its priority?

Please consider, that when the power off event comes the device is more often that not connected to a mobile app, and possibly other BLE devices, connected to one or more ANT+ devices and/or scanning for ANT+/BLE devices to connect to. In other words - the SoftDevice is busy! 

Please advice

Thanks & regards!

  • Hi,

    1. Can I make the 15ms timer with same or higher priority as the SoftDevice? My experiments / reading showed that this is sadly impossible

    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 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. 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?

    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, 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 this thread.

  • Thank you for your answer. Yes - nothing else should happen. Or even if anything happens - it does't matter.

    I tried sisabling the protection of the NVMC as you mentioned. Then  I also call nrf_sdh_suspend() which disables interrupts.

    Then I do:

    	// Enable the NVMC
        NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
    
        // Wait for the NVMC to be ready
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {}

    And finally I call this function:

    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 < 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;
    }

    It halts at *((volatile uint8_t *)(address + i)) = data[i];

    I also tried wrapping the call in CRITICAL_REGION_ENTER() and CRITICAL_REGION_EXIT();

    No go.

    Anythin else I could try?

    PS: these are the defines I used:

    // Define the addresses of the registers that control the flash memory
    #define NRF_NVMC_READY_OFFSET       (0x400UL)
    #define NRF_NVMC_CONFIG_OFFSET      (0x504UL)

    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. 

    I'm having difficulties calculating the right value. Can you please help?

    For now, the address I use for nrf_strorage_write is 0x000AB000

    And my fstorage is defined as follows: 

    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)
    };

    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 0x000AB000.

    So the problem is most likely NOT the address.

  • Ok. I managed to fix this. Thank you very much for your help. 

    The problem was - I was writing 8-bit words instead of 32-bit words. Now It all works just fine.

    This is what I ended up doing:

    void EMERGENCY_SAVE_TIMER_event_handler(nrf_timer_event_t event_type, void* p_context)
    {
    		NRF_LOG_DEBUG("Emergency CONFIG SAVE initiated");
    		nrf_sdh_suspend();	
    
    		NRF_MWU->PREGION[0].SUBS &= ~(MWU_PREGION_SUBS_SR30_Include << MWU_PREGION_SUBS_SR30_Pos);
    		__DSB(); 
    
    	
    		// Enable the NVMC write
    		NRF_NVMC->CONFIG = 1; 
            // Wait for the NVMC to be ready
            while (NRF_NVMC->READY == 0);
    		
    		uint32_t value;
    		for (int i = 0; i < sizeof(data_to_write80); i += 4) 
    		{
    				value = (data_to_write80[i+3] << 24) | 
    				        (data_to_write80[i+2] << 16) | 
    				        (data_to_write80[i+1] <<  8) | 
    				        (data_to_write80[i]);
    				*(volatile uint32_t *)(config_addr + i) = value;
    				while (NRF_NVMC->READY == 0);
    		}				
    		NRF_LOG_DEBUG("Emergency CONFIG SAVE complete");
    }

    I think we can close this thread. Once again - Thanks!

Related