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

Erasing UICR register in application code

Hello Everyone,

I am using nrf51822 with S130. After completely erasing the chip, with the below mentioned code snippet in application code:

NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos;

while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}

*(uint32_t *)0x10001080 = hexData;

NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos;

while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}

I am able to write data in the UICR register. I tried subsequent writing to UICR using the same code in the application code, however, UICR data doesn't change. Based on further research I came to know that the entire chip needs to be erased for writing new data. However, I want to just erase UICR to rewrite new data to the UICR. Is there any way to erase the UICR registers in application code to rewrite new data?

Thanks

  • Can I use particular area of flash memory to write data? So that even if application code is updated, the particular reserved area will not be erased.

  • You should make sure your application does not overwrite the flash area where the data is stored. In order to overwrite written data, you will have to perform an erase. If your application, or you do not erase the flash, the data will not be overwritten. The best approach is to place the data in the upper region of flash, and reduce the flash size settings in your project to avoid that the application grows into this area.

  • Thank you for reply, Jorgen. I am using nrf51822 with 256KB flash and 32KB ram with SDK11, S130 (108kb I guess), with a dfu bootloader (16kb). I have to store only two data (each 4kb). Which addresses will be the best suitable addresses to write the two data?

  • Please have a look at the Memory layout and Preserving application data sections in the SDK 11 BLE DFU Bootloader documentation.

  • static void flash_page_erase(uint32_t * page_address) { // 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)page_address;
    
        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_Een << NVMC_CONFIG_WEN_Pos);
    
        while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
        {
            // Do nothing.
        }
    }
    
    static void flash_word_write(uint32_t * address, uint32_t value)
    {
        // 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.
        }
    
        *address = value;
    
        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.
        }
    }
    

    I am using above function to erase and write data to location 0x0003B000 (1 page below bootloader). But device isn't advertising. When I disable erasing, the device is advertising. Does that means I am erasing wrong location?

Related