Using Partition Manager created partitions for FatFS

I have an issue with using partitions created by PM in external flash. I can create a partition in my prj.conf file and use that and everything works. But I need to put some data into the external NOR memory. PM creates the partitions correctly as far as I can see. But I have not found a way to make the FatFS driver correctly find those partitions. Basically I have the exact same issue as in this ticket:  How to reference PM generated partitions from device tree? 

I need to create a disk like this for FatFS driver to find.

/ {
	msc_disk0 {
		compatible = "zephyr,flash-disk";
		partition = <&external_flash>;
		disk-name = "NAND";
		cache-size = <4096>;
	};
};

If I have the partition in internal memory I can at least make this compile (the partition is tiny so it won't work for my application)

&flash0 {
	partitions {
		compatible = "fixed-partitions";
		#address-cells = <1>;
		#size-cells = <1>;

		boot_partition: partition@0 {
			label = "mcuboot";
			reg = <0x0 0xc000>;
		};
		slot0_partition: partition@c000 {
			label = "image-0";
			reg = <0xc000 0x72000>;
		};
		slot1_partition: partition@7e000 {
			label = "image-1";
			reg = <0x7e000 0x72000>;
		};
		scratch_partition: partition@f0000 {
			label = "image-scratch";
			reg = <0xf0000 0xa000>;
		};
		storage_partition: partition@fa000 {
			label = "storage";
			reg = <0xfa000 0x5f00>;
		};
		external_flash: partition@fff00 {
			label = "external_flash";
			reg = <0xfff00 0x100>;
		};
	};
};

The external memory itself is fine and I can read/write it with flash_area_read() and flash_area_write() functions. 

Is there a way to reference the PM created partitions from within the devicetree file of my main application?

  • Hi

    Yes, this should be possible. pm_config and the available defines are available in ".../build/zephyr/include/generated/pm_config.h". (PM_MCUBOOT_SECONDARY_ADDRESS Please check out this ticket where a solution to a similar issue is suggested both by a user IvanV and my colleague Sigurd.

    Is the second snippet (starting with &flash0) from your overlays or is it the build output? If it's the prior, the way to manually make partitions for the partition manager should be through pm_static.yml. Usually NCS makes these "automagically" so you won't have to do it manually though.

    Best regards,

    Simon

  • I will look at the ticket you referenced. But as far as I understand that means that much of the actual work done by the compiler based on DTS files will have to be manually done in code by hand. Like described here:

    If you look into partitions.yml file under build folder, cou can see that the nvs_storage start address is at 0x3f000. If you search that address in all files under build folder, you can find that the PM_NVS_STORAGE_ADDRESS macro defined in build\zephyr\include\generated\pm_config.h file is the start address of the NVS storage. With that, you can populate the fs.offset variable.

    To get the flash device binding, again found in the build\zephyr\include\generated\pm_config.h file, the PM_NVS_STORAGE_DEV_NAME macro includes NVS storage device name. With it, you can populate the fs.flash_device variable by calling device_get_binding(PM_NVS_STORAGE_DEV_NAME) function.

    What I have right now is basically the &flash0 part in the overlay and then the PM generated layout that I have to keep in sync manually . That is also not a good solution I think. 

    I will try to see if I can understand what user IvanV did and see if I can recreate that. It would make life so much simpler if you could include an example for something like this in the future. I think using DFU is pretty much standard with all BLE devices today and having all your examples fail to build with DFU doesn't seem like a good idea.

  • Hi again

    I may have been preemptive in my previous reply, and would like to reproduce and test this on my end if possible. What SDK version is this reproducible in for example, and are there any steps/changes necessary to do so on my end? If not, can you share some more information on what the error(s) you're seeing on your end, for example the full error log so we can get a better look of what exactly is going wrong.

    Best regards,

    Simon

  • Hi,

    I'm not sure what you are asking me to do now. I have a working state for now. It is just a very cumbersome way I had to set this up to work. I will try to explain my problem again.

    I want to have a device that enumerates as a USB MSD. It should use a partition in external memory for storage. I can set that up using devicetree overlays like this.

    &mx25r64 {
    	partitions {
    		compatible = "fixed-partitions";
    		#address-cells = <1>;
    		#size-cells = <1>;
    
    		external_flash: partition@0 {
    			label = "external_flash";
    			reg = <0x0 0x800000>;
    		};
    	};
    };
    
    / {
    	msc_disk0 {
    		compatible = "zephyr,flash-disk";
    		partition = <&external_flash>;
    		disk-name = "NAND";
    		cache-size = <4096>;
    	};
    };

    And that works exactly like in the USB MSD example. But if I now add DFU to my project that will force the use of Partition Manager and the partition defined in the DTS file is no longer used (correct me if I'm wrong here though). I can see in the partitions.yml file that partitions can still be created and used and using the pm_static.yml file I can still manually configure the partitions I want to use. While that was not easy to set up I think that also works now. Since the &mx25r64 part defining a partition is no longer used I would like to now remove this from the DTS file so that the only place I have to define the partitions would be in the pm_static.yml file. But when I do that msc_disk0 can't be defined because external_flash no longer defines a partition as far as the DTS file is concerned. What I have working now is I keep the partition definitions in bot files and that works so far but it doesn't seem like a good solution as any changes in partitioning have to be manually kept in sync in multiple files. 

    My question is - how can I still define msc_disk0 in my overlay/DTS file while the partitions are managed by the PM?

    Best regards,

    Tiit

  • Hi

    I have forwarded your question internally, as I'm not sure whether this is possible or not myself. One of the developers working specifically with the partition manager will take a look and I'll let you know what their input is. Thank you for your patience!

    Best regards,

    Simon

Related