Using Multiple NVS partitions and how to configure using partition manager

I would like to use multiple NVS partitions.

I have 2 reasons 2 use 2 or more NVS parttions.

1) we have internal flash and external flash and would like to store more long term backup stuff on external flash and rest on internal flash

2) Because of the nature of the data, I'm somewhat limited by the 2 byte id used by NVS. might be useful to have some data in 1 NVS partition and some other data in another partition using same 2 byte index. This is all solvable in anopther way: I agree, but I would like to know if I coulduse multiple partitions and how this can be achieved.

I have the impression from the way all nvs_* functions are defined (you always have to pass the pointer to the file system) it should be possible to have multiple file system/nvs partitions.

Until now we had o,ly 1 NVS partition which is being defined by setting CONFIG_PM_PARTITION_SIZE_NVS_STORAGE.

How should I setup the Partition Manager so that it defines multiple partitions: within internal flash and within external flash.

Kind regards,

Luc

Parents
  • Hi Luc,

    You can use create multiple partitions in a devicetree overlay. For example:

    /delete-node/ &storage_partition;
    
    &flash0 {
    	partitions {
    		storage_partition: partition@f4000 {
    			label = "storage";
    			reg = <0x000f4000 0x0000fa000>;
    		};
     	};
    };
    
    &mx25r64 {
    	partitions {
    		compatible = "fixed-partitions";
    		#address-cells = <1>;
    		#size-cells = <1>;
    
    		external_storage_partition: partition@0 {
    			label = "external-storage";
    			reg = <0x000000000 0x000001000>;
    		};
    	};
    };
    

    Then you can create NVS flash devices for each partition in your code, similar to what is done in Zephyr's NVS sample (zephyr/samples/subsys/nvs):

    static struct nvs_fs fs;
    static struct nvs_fs fs_ext;
    
    #define NVS_PARTITION		storage_partition
    #define NVS_PARTITION_DEVICE	FIXED_PARTITION_DEVICE(NVS_PARTITION)
    #define NVS_PARTITION_OFFSET	FIXED_PARTITION_OFFSET(NVS_PARTITION)
    
    #define NVS_EXT_PARTITION		external_storage_partition
    #define NVS_EXT_PARTITION_DEVICE	FIXED_PARTITION_DEVICE(NVS_EXT_PARTITION)
    #define NVS_EXT_PARTITION_OFFSET	FIXED_PARTITION_OFFSET(NVS_EXT_PARTITION)

    With this you can mount, write, read, etc. to the different partitions.

    Best regards,
    Marte

Reply
  • Hi Luc,

    You can use create multiple partitions in a devicetree overlay. For example:

    /delete-node/ &storage_partition;
    
    &flash0 {
    	partitions {
    		storage_partition: partition@f4000 {
    			label = "storage";
    			reg = <0x000f4000 0x0000fa000>;
    		};
     	};
    };
    
    &mx25r64 {
    	partitions {
    		compatible = "fixed-partitions";
    		#address-cells = <1>;
    		#size-cells = <1>;
    
    		external_storage_partition: partition@0 {
    			label = "external-storage";
    			reg = <0x000000000 0x000001000>;
    		};
    	};
    };
    

    Then you can create NVS flash devices for each partition in your code, similar to what is done in Zephyr's NVS sample (zephyr/samples/subsys/nvs):

    static struct nvs_fs fs;
    static struct nvs_fs fs_ext;
    
    #define NVS_PARTITION		storage_partition
    #define NVS_PARTITION_DEVICE	FIXED_PARTITION_DEVICE(NVS_PARTITION)
    #define NVS_PARTITION_OFFSET	FIXED_PARTITION_OFFSET(NVS_PARTITION)
    
    #define NVS_EXT_PARTITION		external_storage_partition
    #define NVS_EXT_PARTITION_DEVICE	FIXED_PARTITION_DEVICE(NVS_EXT_PARTITION)
    #define NVS_EXT_PARTITION_OFFSET	FIXED_PARTITION_OFFSET(NVS_EXT_PARTITION)

    With this you can mount, write, read, etc. to the different partitions.

    Best regards,
    Marte

Children
  • Hi Marte, thank you very much.

    But would this mean I need to disable the partition manager?

    DTS files is more how Zephyr native is doing this. Nordic seems to be using the partition manager to achieve this.

    Since we already defined all our other partitions using partition manager I'm wondering how this would work.

    Is this /delete-node/ a way to disable what is being set by the partition manager?

  • Hi,

    Yes, you would need to use devicetree for partitions instead of partition manager.
    You could also solve this by using partition manager instead. Enabling NVS will automatically generate a NVS partition. You can either have this default NVS partition as one of your NVS partitions and create separate partitions for the other partitions, or you can create all NVS partitions as sub partitions of the automatically generated NVS partition.
    For the latter, you should set CONFIG_PM_PARTITION_SIZE_NVS_STORAGE to the total size of your NVS partitions, and then use the span parameter. Here is an example:

    nvs_storage:
      address: 0xf4000
      end_address: 0x100000
      placement:
        before:
        - end
      region: flash_primary
      size: 0xc000
      span: [nvs_storage1, nvs_storage2]
    nvs_storage1:
      address: 0xf4000
      end_address: 0xfa000
      size: 0x6000
    nvs_storage2:
      address: 0xfa000
      end_address: 0x100000
      size: 0x6000

    Best regards,
    Marte

Related