[SW AL] LittleFS on external flash with custom partition

Our project is running on a nRF9160 using the SDK v2.1.0.

We want to store some data on an external SPI NOR flash installed on the device.

We defined a partition located in the external flash in our board file

&spi3 {
	compatible = "nordic,nrf-spim";
	status = "okay";
	cs-gpios = <&gpio0 21 GPIO_ACTIVE_LOW>;
	miso-pull-up;
	pinctrl-0 = <&spi3_default>;
	pinctrl-1 = <&spi3_sleep>;
	pinctrl-names = "default", "sleep";

		w25q16jv: w25q16jv@0 {
		compatible = "jedec,spi-nor";
		label = "W25Q16JV";
		reg = <0>;
		spi-max-frequency = <1000000>;
		reset-gpios = <&gpio0 25 GPIO_ACTIVE_LOW>;
		size = <0x1000000>;
		has-dpd;
		t-enter-dpd = <4000>;
		t-exit-dpd = <25000>;
		jedec-id = [ef 40 15];
	};
};

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

		ext_flash_partition: partition@0 {
			label = "external-flash";
			reg = <0x00000000 0x00200000>;
		};
	};
};

We would like to use littlefs on the partition we defined, getting automatically the size and offset from the board file and have the possibility of having multiple partitions in the flash.

For now I'm trying to load the flash area defined in the board file using the following code

FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(external_flash);

static struct fs_mount_t lfs_storage_mnt = {
    .type = FS_LITTLEFS,
    .fs_data = &external_flash,
    .storage_dev = (void*)FLASH_AREA_ID(external_flash),
    .mnt_point = STORAGE_ROOT_PATH,
};

struct fs_mount_t* storage_mp = &lfs_storage_mnt;

Calling fs_mount() i get the following error

[00:00:26.657,745] <wrn> RECORDS_STORAGE: Flash area not found!
[00:00:26.657,775] <inf> littlefs: LittleFS version 2.4, disk version 2.0
[00:00:26.671,569] <err> littlefs: can't open flash area 0
[00:00:26.671,600] <err> fs: fs mount error (-19)
[00:00:26.683,135] <err> RECORDS_STORAGE: Error remounting file system
[00:00:26.683,288] <err> RECORD_CREATION: Record failed to store No such device

If I try to put "storage" as label of the partition in the external flash and use the same in the macro FLASH_AREA_ID() the code works but from the logs i see that it's actually using a partition that's not on the external flash

[00:00:17.553,039] <inf> littlefs: LittleFS version 2.4, disk version 2.0
[00:00:17.553,222] <inf> littlefs: FS at flash-controller@39000:0xf4000 is 6 0x1000-byte blocks with 512 cycle
[00:00:17.570,800] <inf> littlefs: sizes: rd 128 ; pr 128 ; ca 128 ; la 64
[00:00:17.572,052] <inf> littlefs: /lfs mounted

Reading the documentation and looking at the sample code using littlefs it seems that a partition to be used with littlefs needs to have label "storage" so we can't really figure out how to make this work if there is already a partition with such label (e.g. in the internal flash) and eventually how to use multiple partitions.

Thanks,

Alessio

Related