Writing to UICR Registers on NRF52840

I’ve been trying to find a way to write to UICR registers during program operation on the nrf52840. I’ve noted in the nrf52840 Product Specification, UICR registers are writeable in the same way as normal ROM, but when I attempt to use the FLASHIAP device, the memory address is out of range. I’ve tried bypassing the flashiap device and writing using the flash_program_page command, but it seems this is an issue all the way down to nrf_fstorage.c where addr_is_within_bounds will return false. Is there a physical hardware limitation of writing to the UICR registers, or are these all just preventative checks put in place that can be removed? Are there any other ways to store data in the customer registers during program runtime?

The UICR is a small memory region and is usually intended for things like factory-installed configuration information that doesn’t change much over the course of the product’s lifespan.

It would be somewhat dangerous (and pretty pointless) to expose this small region of memory as a general storage area using the FlashIAP BlockDevice API.

That being said, it should be entirely possible to program at runtime if you wish to do so. You will probably have to write your own custom code to do so however.

You should go back and read through sections related to the UICR. That document details what steps are required to erase the UICR and then program it with new data. There is a special register that locks normal write access to the UICR so you’ll have to set that up properly first.

I would look at the NVMC registers section (4.3.9 in the product spec). Registers of particular interest to you will be:

  • CONFIG (WEN bits, must enable erase, THEN write access, do not enable both at same time)
  • ERASEUICR (to erase the UICR before programming)

Carefully read section 4.3.1 (Writing to Flash). Your writes will need to be word-aligned and you can only set 1 bits to 0, otherwise you’ll need to erase the UICR again.

If you’re using the softdevice with Mbed-OS for some reason, you should note that some of the UICR registers are used to configure the soft device, bootloader, MBR, and possibly other things included in the Nordic stack… If you’re using the Cordio Bluetooth Stack (default in current Mbed-OS release) you don’t have to worry about this.

See this post for possible caveats and some problems stemming from the MWU (Memory Watch Unit) peripheral. I’m not sure if Mbed enables this features on the nRF52840 but if you run into problems that’s something else to check.

Hope this help!

Thank you for the information.