This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Example code for using Non volatile memory

I want to store a block of data in nRF51x22. I tried the following:

void HalStoreNVMBlock(uint32_t size, uint8_t* pBuf) { nrf_nvmc_page_erase(NRF_UICR_BASE); nrf_nvmc_write_bytes(NRF_UICR_BASE, pBuf, size); }

but after this code was executed the first time, I could not reprogram the device from Keil. I had to erase all using nRFgo Studio. Is there a code example for doing this?

  • When using a softdevice, the base address of the application is stored in UICR, and if you delete the entire UICR register, the softdevice will therefore no longer know where or how it can start the application.

    If you just want to store a little configuration data, you can use the upper bytes of the UICR register, but there is no convenient way to erase this part without erasing the rest of UICR.

    If you need to store and remove more data, I'd therefore recommend you to just use a normal flash page for this. There have been quite some discussion on how to do such flash writes in this question, so it might be worth having a look at. I believe that should be more or less sufficient to get going, but please let me know if anything is still unclear.

  • Hi Ole Thanks’ very much for your quick answer. Basically I only need to store 8 bytes, so using a full flash page for that seems like overkill. I have tried reading out the first 1024 bytes of UICR (address 0x10001000 - 0x10001400) and they are all 0xff. I then tried writing 0x55 to offset 0x80 (Reserved for customer) and it worked fine. I can’t see the application start address (BOOTLOADERADDR from nrf51.h) as it seems to be 0xffffffff. If I could see the data I’m not allowed to erase, I could save it before erasing, and then write it back after the erase. Is that possible?

    Thanks,

    Klaus Karkov

  • If you just need to store 8 bytes, you should be able to just write them to the correct part of UICR without erasing anything first. This can also be done with nrfjprog or similar, during production.

    When reading UICR, you should see that CLENR0 register at offset 0 (full address 0x10001000) is 0x14000 and that the FWID register at offset 0x10 also have a non-0xFF value, if you have programmed the S110 softdevice.

    I would strongly recommend you to not do an erase of UICR during run-time in your application, since this could potentially give you a bricked device if an external reset/power-loss happens just after erasing. If you need to change the value, I'd recommend you to either just cycle through UICR, and stop when it's full, or just use a regular flash page, even though the size needed is small.

  • Thank you very much for your answer. I will go for the full flash page solution, because it’s unknown how many times the user will change the NV setup parameters. I consider this case successfully closed.

  • Good! I'd be happy if you could accept one answer, by clicking the accept button below it, to mark this discussion as solved. :-)

Related