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

Storing Device Serial/Identifier

We'd like to be able to store a few items including mutable keys, and unique identifier in each device that is generated on initial boot of the product. I'm just starting out with research but I'm wondering if there is any documentation/guidance from Nordic regarding how/where to store this kind of information?

  • Take a look at the NVS driver that Zephyr provides.  There is a sample application over in zephyr/samples/subsys/nvs.

    The only issue I bumped into was the flash offset you pass into nvs_init().  The Zephyr sample uses DT_FLASH_AREA_STORAGE_OFFSET, which should work in most cases.  Howeverif you are using mcuboot for FOTA updating, that value needs to be PM_MCUBOOT_STORAGE_ADDRESS.

  • I'm revisiting this now. I am able to read/write NVS at the PM_MCUBOOT_STORAGE_ADDRESS however I'm not sure how to initialize that area of flash. I'd like to include as part of our programming script writing the initial values to PM_MCUBOOT_STORAGE_ADDRESS (0xFC000) but when I try to write a binary there I get an error. I assume the area is protected.

    What is the proper way to do this?

  • I'm not sure when that area of flash is or isn't protected.  I would expect the JTAG programmer should be able to write there whenever it wants.

    In our application, our initial/default values are the same for every unit we program, so we actually hard-coded them into special NVS-reader functions.  A factory reset erases the NVS space on our devices, and the reader functions notice that there is no stored value and return the hard-coded initial/default value instead.

    Are you using unique or changing initial NVS values for different devices?

  • Thanks for the details. Unfortunately yes, we need to configure the system for things like serial number, battery type, operating mode, etc.

  • I am trying to use PM_MCUBOOT_STORAGE_ADDRESS in our project instead of DT_FLASH_AREA_STORAGE_OFFSET so that we can retain NVS storage after a FOTA update.  However, when building, I get the following error:

    'PM_MCUBOOT_STORAGE_ADDRESS' undeclared (first use in this function); did you mean 'PWM_0_BASE_ADDRESS'?

    I am setting CONFIG_BOOTLOADER_MCUBOOT=y in the prj.conf, and we are able to do a FOTA.

    Here is the init code... this works fine when using DT_FLASH_AREA_STORAGE_OFFSET.

    int tk_nv_init(void){
      int err = 0;
      struct flash_pages_info info;
        /* define the nvs file system by settings with:
       *  sector_size equal to the pagesize,
       *  3 sectors
       *  starting at DT_FLASH_AREA_STORAGE_OFFSET
       */
      fs.offset = PM_MCUBOOT_STORAGE_ADDRESS; // was DT_FLASH_AREA_STORAGE_OFFSET (should be PM_MCUBOOT_STORAGE_ADDRESS)
      err = flash_get_page_info_by_offs(device_get_binding(DT_FLASH_DEV_NAME), fs.offset, &info); // previous NVS space
      if (err) {
        printk("Unable to get page info");
        return err;
      }
      fs.sector_size = info.size;
      fs.sector_count = 3U;

      err = nvs_init(&fs, DT_FLASH_DEV_NAME);   // previous NVS space
      if (err) {
        printk("Flash Init failed\n");
        return err;
      }

      TK_NV_INCR_M(REBOOT_CNT_ID);  // Increment the reboot counter

      return 0;
    }
Related