This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Non-Volatile memory permanent storage problem

Hi ,

Here is my code used to write in to non volatile memory.

#include "nrf.h" #include <stdint.h> #include <stdbool.h> #include "nrf_nvmc.h"

static void flash_page_erase(uint32_t * p_page) { // Turn on flash erase enable and wait until the NVMC is ready. NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos); while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. }

// Erase page.
NRF_NVMC->ERASEPAGE = (uint32_t)p_page;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
    // Do nothing.
}

// Turn off flash erase enable and wait until the NVMC is ready.
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
    // Do nothing
}

}

static void flash_word_write(uint32_t * p_address, uint32_t value) { // If radio is active, wait for it to become inactive. while (m_radio_active) { // Do nothing (just wait for radio to become inactive). (void) sd_app_event_wait(); }

// Turn on flash write enable and wait until the NVMC is ready.
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
    // Do nothing.
}

*p_address = value;
// Wait flash write to finish
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
    // Do nothing.
}
// Turn off flash write enable and wait until the NVMC is ready.
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
    // Do nothing
}

}

int main(void) { uint32_t write=0; uint32_t check = 0;

write	= 0x73A34;

flash_page_erase((uint32_t *)0x28400);

    flash_word_write((uint32_t *)0x28400, write);

check = *(uint32_t *)0x28400;

}

when i am testing this code in debug mode i can able to erase, read, write.

But the problem we are facing is when i do power reset the data is getting erased (like volatile memory).

i can see in debug mode in memory window that values are getting removed after power-reset

Kindly suggest me, where i am wrong.

Regards Balaji

Parents
  • I'm not able to replicate your problem, using code like this:

    
    #define VALUE_WRITTEN_PIN_NO 10
    #define VALUE_EXISTS_PIN_NO 11
    
    static void flash_test(void)
    {
        uint32_t err_code;
        uint32_t page_number = NRF_FICR->CODESIZE-10;
        uint32_t *p_page_base_address = (uint32_t *) (page_number * NRF_FICR->CODEPAGESIZE);
        uint32_t desired_value = 0x12345678;
        
        nrf_gpio_cfg_output(VALUE_EXISTS_PIN_NO);
        nrf_gpio_cfg_output(VALUE_WRITTEN_PIN_NO);
        
        if (*p_page_base_address != desired_value)
        {
            err_code = ble_flash_page_erase(page_number);
            APP_ERROR_CHECK(err_code);
    
            err_code = ble_flash_word_write(p_page_base_address, desired_value);
            APP_ERROR_CHECK(err_code);
            
            nrf_gpio_pin_set(VALUE_WRITTEN_PIN_NO);
        }
        else 
        {
            nrf_gpio_pin_set(VALUE_EXISTS_PIN_NO);
        }
    }
    
    

    This works as expected, on first run after a flash erase, the VALUE_WRITTEN_PIN_NO is lit, while on subsequent runs, VALUE_EXISTS_PIN_NO is lit.

    I would however strongly recommend you to upgrade to softdevice 6.0.0 and SDK 5.0.0 and use the new functionality there, pstorage and/or the flash API of the softdevice itself.

  • sorry, previously we tested it in the wrong way, now it is working fine with above code, thanks for your reply Morten.

Reply Children
No Data
Related