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

  • 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.

  • USB MSC is a dumb interface that only cares about raw blocks and lets the host deal with any filesystem issues. Consequently you can't really use the data with a host connected over USB since it could cache stuff.

    No host OS that I am aware of knows how to handle littlefs device out-of-the-box. Are use using something unusual? Windows for example would just ask the user to format the device with FAT(12).

    USB MSC should be able to use the flash device that contains littlefs as a backend when configured correctly (see other post about PM), but the NRF MCU must not use anything stored there with USB connected.

  • 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.

Related