Littlefs file system on external SPI flash memory with non-secure build

In my project, I am planning to use LTE modem and an external flash memory with a file system. I have problems to run code for the non-secure build type. A similar problem occurs when trying to use the shell/fs example (developer.nordicsemi.com/.../README.html) on the nrf9160dk_nrf9160_ns target. Building for the secure nrf9160dk_nrf9160 target works well.

Here is my "prj.conf":

CONFIG_FLASH=y
CONFIG_LOG=y
#CONFIG_FS_LOG_LEVEL_DBG=y
CONFIG_HEAP_MEM_POOL_SIZE=16384
CONFIG_SHELL=y
CONFIG_SHELL_LOG_LEVEL_INF=y
CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_SHELL=y
CONFIG_FILE_SYSTEM_LITTLEFS=y
CONFIG_FLASH_MAP=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_SPI=y
CONFIG_SPI_NOR=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096

and here is my "app.overlay":

/delete-node/ &storage_partition;

&mx25r64  {
  // spi-max-frequency = <1000000>;
  partitions {
    compatible = "fixed-partitions";
    #address-cells = <1>;
    #size-cells = <1>;

    storage_partition: partition@0 {
      label = "storage";
      reg = <0x00000000 0x00800000>;
    };
  };
};

For nrf9160dk_nrf9160 target there is usual shell:

uart:~$ *** Booting Zephyr OS build v3.0.99-ncs1  ***

uart:~$ fs mount littlefs /lfs
[00:00:24.930,999] <inf> littlefs: LittleFS version 2.4, disk version 2.0
[00:00:24.932,769] <inf> littlefs: FS at MX25R64:0x0 is 2048 0x1000-byte blocks with 512 cycle
[00:00:24.932,800] <inf> littlefs: sizes: rd 16 ; pr 16 ; ca 64 ; la 32
[00:00:24.935,363] <inf> littlefs: /lfs mounted
uart:~$ fs cd /lfs
uart:~$ fs ls
file.bin
uart:~$ fs read file.bin
File size: 9
00000000  11 22 33 44 55 BA DC 0F FE                       ."3DU....
uart:~$ fs statvfs /lfs
bsize 16, frsize 4096, blocks 2048, bfree 2046
uart:~$

But for nrf9160dk_nrf9160_ns target nothing happens. I can run with the debugger and see that everything stops somewhere here:

My question is: what additional settings are required for the example to work in non-secure mode?

  • Hi,

    In case it helps, here is a report of what I do to make this work.

    Hardware: nRF9160DK 1.0.0
    nRF Connect SDK: v2.0.2
    Sample: zephyr/samples/subsys/fs/littlefs
    Build target: nrf9160dk_nrf9160_ns

    I used the sample as is, except that I added "CONFIG_BUILD_WITH_TFM=n" to prj.conf.

    Flash with erase, and log right after flash:

    *** Booting Zephyr OS build v3.0.99-ncs1-1  ***
    Sample program to r/w files on littlefs
    Area 2 at 0xfa000 on NRF_FLASH_DRV_NAME for 24576 bytes
    I: LittleFS version 2.4, disk version 2.0
    I: FS at NRF_FLASH_DRV_NAME:0xfa000 is 6 0x1000-byte blocks with 512 cycle
    I: sizes: rd 16 ; pr 16 ; ca 64 ; la 32
    E: WEST_TOPDIR/modules/fs/littlefs/lfs.c:1077: Corrupted dir pair at {0x0, 0x1}
    W: can't mount (LFS -84); formatting
    I: /lfs mounted
    /lfs mount: 0
    /lfs: bsize = 16 ; frsize = 4096 ; blocks = 6 ; bfree = 4
    
    Listing dir /lfs ...
    /lfs/boot_count read count:0 (bytes: 0)
    /lfs/boot_count write new boot count 1: [wr:1]
    I: Test file: /lfs/pattern.bin not found, create one!
    ------ FILE: /lfs/pattern.bin ------
    01 55 55 55 55 55 55 55 02 55 55 55 55 55 55 55
    03 55 55 55 55 55 55 55 04 55 55 55 55 55 55 55
    05 55 55 55 55 55 55 55 06 55 55 55 55 55 55 55
    07 55 55 55 55 55 55 55 08 55 55 55 55 55 55 55
    09 55 55 55 55 55 55 55 0a 55 55 55 55 55 55 55
    0b 55 55 55 55 55 55 55 0c 55 55 55 55 55 55 55
    0d 55 55 55 55 55 55 55 0e 55 55 55 55 55 55 55
    0f 55 55 55 55 55 55 55 10 55 55 55 55 55 55 55
    11 55 55 55 55 55 55 55 12 55 55 55 55 55 55 55
    13 55 55 55 55 55 55 55 14 55 55 55 55 55 55 55
    15 55 55 55 55 55 55 55 16 55 55 55 55 55 55 55
    17 55 55 55 55 55 55 55 18 55 55 55 55 55 55 55
    19 55 55 55 55 55 55 55 1a 55 55 55 55 55 55 55
    1b 55 55 55 55 55 55 55 1c 55 55 55 55 55 55 55
    1d 55 55 55 55 55 55 55 1e 55 55 55 55 55 55 55
    1f 55 55 55 55 55 55 55 20 55 55 55 55 55 55 55
    21 55 55 55 55 55 55 55 22 55 55 55 55 55 55 55
    23 55 55 55 55 55 55 55 24 55 55 55 55 55 55 55
    25 55 55 55 55 55 55 55 26 55 55 55 55 55 55 55
    27 55 55 55 55 55 55 55 28 55 55 55 55 55 55 55
    29 55 55 55 55 55 55 55 2a 55 55 55 55 55 55 55
    2b 55 55 55 55 55 55 55 2c 55 55 55 55 55 55 55
    2d 55 55 55 55 55 55 55 2e 55 55 55 55 55 55 55
    2f 55 55 55 55 55 55 55 30 55 55 55 55 55 55 55
    31 55 55 55 55 55 55 55 32 55 55 55 55 55 55 55
    33 55 55 55 55 55 55 55 34 55 55 55 55 55 55 55
    35 55 55 55 55 55 55 55 36 55 55 55 55 55 55 55
    37 55 55 55 55 55 55 55 38 55 55 55 55 55 55 55
    39 55 55 55 55 55 55 55 3a 55 55 55 55 55 55 55
    3b 55 55 55 55 55 55 55 3c 55 55 55 55 55 55 55
    3d 55 55 55 55 55 55 55 3e 55 55 55 55 55 55 55
    3f 55 55 55 55 55 55 55 40 55 55 55 55 55 55 55
    
    41 55 55 55 55 55 55 55 42 55 55 55 55 55 55 55
    43 55 55 55 55 55 55 55 44 55 55 55 55 55 55 55
    45 55 aa 
    I: /lfs unmounted
    /lfs unmount: 0
    

    Then I press the reset button on the nRF9160DK:

    *** Booting Zephyr OS build v3.0.99-ncs1-1  ***
    Sample program to r/w files on littlefs
    Area 2 at 0xfa000 on NRF_FLASH_DRV_NAME for 24576 bytes
    I: LittleFS version 2.4, disk version 2.0
    I: FS at NRF_FLASH_DRV_NAME:0xfa000 is 6 0x1000-byte blocks with 512 cycle
    I: sizes: rd 16 ; pr 16 ; ca 64 ; la 32
    I: /lfs mounted
    /lfs mount: 0
    /lfs: bsize = 16 ; frsize = 4096 ; blocks = 6 ; bfree = 3
    
    Listing dir /lfs ...
    [FILE] boot_count (size = 1)
    [FILE] pattern.bin (size = 547)
    /lfs/boot_count read count:1 (bytes: 1)
    /lfs/boot_count write new boot count 2: [wr:1]
    ------ FILE: /lfs/pattern.bin ------
    02 55 55 55 55 55 55 55 03 55 55 55 55 55 55 55
    04 55 55 55 55 55 55 55 05 55 55 55 55 55 55 55
    06 55 55 55 55 55 55 55 07 55 55 55 55 55 55 55
    08 55 55 55 55 55 55 55 09 55 55 55 55 55 55 55
    0a 55 55 55 55 55 55 55 0b 55 55 55 55 55 55 55
    0c 55 55 55 55 55 55 55 0d 55 55 55 55 55 55 55
    0e 55 55 55 55 55 55 55 0f 55 55 55 55 55 55 55
    10 55 55 55 55 55 55 55 11 55 55 55 55 55 55 55
    12 55 55 55 55 55 55 55 13 55 55 55 55 55 55 55
    14 55 55 55 55 55 55 55 15 55 55 55 55 55 55 55
    16 55 55 55 55 55 55 55 17 55 55 55 55 55 55 55
    18 55 55 55 55 55 55 55 19 55 55 55 55 55 55 55
    1a 55 55 55 55 55 55 55 1b 55 55 55 55 55 55 55
    1c 55 55 55 55 55 55 55 1d 55 55 55 55 55 55 55
    1e 55 55 55 55 55 55 55 1f 55 55 55 55 55 55 55
    20 55 55 55 55 55 55 55 21 55 55 55 55 55 55 55
    22 55 55 55 55 55 55 55 23 55 55 55 55 55 55 55
    24 55 55 55 55 55 55 55 25 55 55 55 55 55 55 55
    26 55 55 55 55 55 55 55 27 55 55 55 55 55 55 55
    28 55 55 55 55 55 55 55 29 55 55 55 55 55 55 55
    2a 55 55 55 55 55 55 55 2b 55 55 55 55 55 55 55
    2c 55 55 55 55 55 55 55 2d 55 55 55 55 55 55 55
    2e 55 55 55 55 55 55 55 2f 55 55 55 55 55 55 55
    30 55 55 55 55 55 55 55 31 55 55 55 55 55 55 55
    32 55 55 55 55 55 55 55 33 55 55 55 55 55 55 55
    34 55 55 55 55 55 55 55 35 55 55 55 55 55 55 55
    36 55 55 55 55 55 55 55 37 55 55 55 55 55 55 55
    38 55 55 55 55 55 55 55 39 55 55 55 55 55 55 55
    3a 55 55 55 55 55 55 55 3b 55 55 55 55 55 55 55
    3c 55 55 55 55 55 55 55 3d 55 55 55 55 55 55 55
    3e 55 55 55 55 55 55 55 3f 55 55 55 55 55 55 55
    40 55 55 55 55 55 55 55 41 55 55 55 55 55 55 55
    
    42 55 55 55 55 55 55 55 43 55 55 55 55 55 55 55
    44 55 55 55 55 55 55 55 45 55 55 55 55 55 55 55
    46 55 ab 
    I: /lfs unmounted
    /lfs unmount: 0
    

    Is this what you are trying to do?

    Regards,
    Sigurd Hellesvik

  • This example works as expected - I can replicate it too. But I am interested in external flash memory. In my first post there was log line:

    [00:00:24.932,769] <inf> littlefs: FS at MX25R64:0x0 is 2048 0x1000-byte blocks with 512 cycle

    so, external flash drive MX25R64 was ready and correctly working. In SPM non-secure mode I have next log line:

    [00:00:15.988,159] <inf> littlefs: FS at NRF_FLASH_DRV_NAME:0xfa000 is 6 0x1000-byte blocks with 512 cycle

    so, external flash drive MX25R64 is invisible and LittleFS is working with internal flash memory

  • Right, external flash.

    Yea, it seems that there are some configurations needed for this to work with external flash.
    I did some testing with that today, but has not figured it out quite yet.

    I will continue looking into it tomorrow.

    Regards,
    Sigurd Hellesvik

  • Hi,

    When you build for "_ns", the build will add a second partition for either TF-M or SPM.
    A "side effect" of this is that the Partition Manager is used for the partitioning.

    When building for nrf9160dk_nrf9160, your DeviceTree overlay file (app.overlay) partitions littlefs_storage to be in external flash.
    However, if you build for nrf9160dk_nrf9160_ns, the partition manager will handle the configuration of the littlefs_storage instead.

    The correct solution to get littlefs_storage into the external flash would be to have a Kconfig option in the nRF Connect SDK to tell the Partition Manager to use external flash for littlefs.
    This option does not exist in the nRF Connect SDK v2.0.2.

    However, it has been added in the main branch. I suggest you add the changes found in commit f9434e39 to your version of the nRF Connect SDK, and then set CONFIG_PM_PARTITION_REGION_LITTLEFS_EXTERNAL for your project.

    Regards,
    Sigurd Hellesvik

  • I finally managed to run the example with an external SPI flash memory. Thank you for your support. But I have to describe the difficulties and present the files for those who need to repeat the example.

    First, the git checkout to the main branch completely destroyed my build system (I work on Windows 10 and Visual Studio Code). So I just fixed manually files in <path_to_ncs>/ncs/v2.0.2/nrf/subsys/partition_manager/...

    Secondly, it was not possible to build the example for TF-M. Only for SPM non-secure build and secure build.

    Here is my prj.conf:

    CONFIG_LOG=y
    CONFIG_HEAP_MEM_POOL_SIZE=16384
    
    CONFIG_SHELL=y
    CONFIG_SHELL_LOG_LEVEL_INF=y
    
    CONFIG_FILE_SYSTEM=y
    CONFIG_FILE_SYSTEM_SHELL=y
    CONFIG_FILE_SYSTEM_LITTLEFS=y
    
    CONFIG_FLASH=y
    CONFIG_FLASH_MAP=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    
    CONFIG_SPI=y
    CONFIG_SPI_NOR=y
    CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
    CONFIG_SPI_NRFX_RAM_BUFFER_SIZE=32
    
    # only works with SPM
    CONFIG_BUILD_WITH_TFM=n
    
    # need sdk-nrf fix:
    # https://github.com/nrfconnect/sdk-nrf/commit/f9434e393c1feb10a18af838ec43ac8b0248821a
    CONFIG_PM_PARTITION_REGION_LITTLEFS_EXTERNAL=y
    CONFIG_PM_PARTITION_SIZE_LITTLEFS=0x800000

    Here is my app.overlay:

    /delete-node/ &storage_partition;
    
    / {
    	chosen {
    		nordic,pm-ext-flash = &mx25r64;
    	};
    };
    
    &mx25r64  {
      partitions {
        compatible = "fixed-partitions";
        #address-cells = <1>;
        #size-cells = <1>;
    
        storage_partition: partition@0 {
          label = "storage";
          reg = <0x00000000 0x00800000>;
        };
      };
    };
    

Related