USB Mass Storage sample not building with LittleFS on SDK 2.9 (nrf52840)

I'm trying to get the USB Mass Storage sample working on the nRF52840 using SDK 2.9. I followed the official guide here:
https://docs.nordicsemi.com/bundle/ncs-latest/page/zephyr/samples/subsys/usb/mass/README.html

My goal is to use LittleFS as the storage backend for the USB Mass Storage device. However, when I try to build the sample using the following command:

west build -b nrf52840dk/nrf52840 samples/subsys/usb/mass -- -DCONFIG_APP_MSC_STORAGE_FLASH_LITTLEFS=y

I get a build error. I also tried with SDK 2.7 just in case, but I ran into the same issue.


I came across a similar question on the forums:
https://devzone.nordicsemi.com/f/nordic-q-a/117539/fs_sample-doesn-t-work-in-my-nrf52840-dk
But in that case, the suggested workaround was to use RAM instead of flash for storage, which doesn't quite fit my use case.

Am I missing something in the configuration? Is LittleFS currently supported as a backend for the USB MSC sample on nRF52840?

Thanks in advance

Parents Reply
  • Thank you for your response. However, I’m not sure where exactly I should add the littlefs_storage node.

    I haven’t defined the littlefs_storage node in my configuration, and I could not find it in the available overlays in the sample I’m using.

    Could you please clarify whether I need to create a new Device Tree overlay or modify an existing one? If you could point me to the specific file or location where this should be added, it would be very helpful.

Children
  • You will need to use a static Partition Manager configuration file. The simplest way to do so is copy partitions.yml to the root of your project and rename it pm_static.yml.

    Then subsequent build will refer to that files for all the partitioning configurations.

    In that file, you add the affiliation key and value under the littlefs_storage node.

    For more information, please refer to https://docs.nordicsemi.com/bundle/ncs-2.9.1/page/nrf/scripts/partition_manager/partition_manager.html.

  • Hello,

    I created the pm_static.yml file with the following contents, as you suggested:

    littlefs_storage:
      affiliation:
      - disk
      address: 0xfa000
      end_address: 0x100000
      placement:
        before:
        - end
      region: flash_primary
      size: 0x6000
      cache-size: 0x1000

    It seems that the file is correctly detected during the build.

    -- Found partition manager static configuration : C:/ncs/v2.9.0-nRF54H20-1-rc2/zephyr/samples/subsys/usb/mass/pm_static.yml Partition 'littlefs_storage' is not included in the dynamic resolving since it is statically defined.

    However, the build fails with the following error:

    C:/ncs/v2.9.0-nRF54H20-1-rc2/zephyr/drivers/disk/flashdisk.c:519:54: error: 'PM_LITTLEFS_STORAGE_EXTRA_PARAM_disk_cache_siz' undeclared here (not in a function)
    519 | #define PM_FLASH_DISK_ENTRY_EXTRA_PARAM(name, param) PM_##name##_EXTRA_PARAM_disk_##param
    | ^~~
    C:/ncs/v2.9.0-nRF54H20-1-rc2/zephyr/drivers/disk/flashdisk.c:529:24: note: in expansion of macro 'PM_FLASH_DISK_ENTRY_EXTRA_PARAM'
    529 | PM_FLASH_DISK_ENTRY_EXTRA_PARAM(n, cache_size))
    | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    C:/ncs/v2.9.0-nRF54H20-1-rc2/zephyr/drivers/disk/flashdisk.c:531:69: note: in expansion of macro 'CACHE_SIZE'
    531 | static uint8_t __aligned(4) PM_FLASH_DISK_CACHE_VARIABLE(n)[CACHE_SIZE(n)];
    .....

    This looks like a missing or undefined macro.

    Shouldn’t this sample build out of the box without requiring extra configuration or changes like this?

    If additional setup is indeed required for littlefs_storage, could you clarify the correct steps or provide a working example?

  • I finally get my sample to build by changing the pm_static.yaml as follows:

    littlefs_storage:
      affiliation: disk
      extra_params: {
          disk_name: "littlefs",
          disk_cache_size: 4096,
          disk_sector_size: 512,
          disk_read_only: 0
        }
      address: 0xfa000
      end_address: 0x100000
      placement:
        before:
        - end
      region: flash_primary
      size: 0x6000

  • Hi Rodrigo,

    I am glad you figured out the extra params. My apology that I missed it initially.

    Rodrigo G said:
    Shouldn’t this sample build out of the box without requiring extra configuration or changes like this?

    So, there are two parts to this.

    Firstly, Partition Manager was created to address RAM and ROM partitioning needs in the early days of Zephyr, when Zephyr didn't have a native solution for those. It then soon got tightly entangled with the SDK.

    Secondly, both the USB samples and LittleFS samples come from the Zephyr upstream, so they are created for Zephyr in general and naturally do not have consideration for NCS. So, adaptation to work in NCS environment is necessary.
    You might find other Zephyr samples having issues working in NCS or with nRF boards too for this reason. Sometimes it is because of Partition Manager, sometimes it's because the samples are created specifically for another board vendor.

    Please feel free to close the case at your convenience then.

  • Hi Hieu,

    Thanks for your previous support.

    Despite all my efforts, I’m still unable to get the USB Mass Storage sample to work properly on my target. After a successful build and flash, I get the following output in the logs:

    *** Using Zephyr OS v3.7.99-706ab3d781e4 *** [00:00:00.265,777]
    <err> usb_msc: Storage init ERROR !!!! - Aborting USB init Area 1 at 0xfa000 on flash-controller@4001e000 for 24576 bytes [00:00:00.265,838] <inf> littlefs: LittleFS version 2.9, disk version 2.1 [00:00:00.266,052] <inf> littlefs: FS at flash-controller@4001e000:0xfa000 is 6 0x1000-byte blocks with 512 cycle [00:00:00.266,082] <inf> littlefs: sizes: rd 16 ; pr 16 ; ca 64 ; la 32 Mount /lfs: 0 /lfs: bsize = 16 ; frsize = 4096 ; blocks = 6 ; bfree = 4 /lfs opendir: 0 End of files [00:00:00.317,291] <inf> main: The device is put in USB mass storage mode.

    On the host side (Ubuntu), I see that the device is detected but unmountable:

    usb-storage 1-1:1.0: USB Mass Storage device detected scsi 1:0:0:0: Direct-Access Zephyr FlashDisk 0.00 PQ: 0 ANSI: 4 sd 1:0:0:0: [sda] Media removed, stopped polling

    From the log, LittleFS seems to be mounting fine, and the device appears as /lfs. However, the usb_msc: Storage init ERROR message is confusing—especially since it later proceeds to "put the device in USB mass storage mode."

    Should I open a new ticket for this new issue?  

Related