Vfs with littlefs2 configuration of MTD

Hey everybody

I want to use littlefs2 to store and manipulate small text files.

I have seen that littlefs and littlefs2 can be used as pkgs in RIOT-OS. But they need a mtd configuration. In the test example they use the ram for that, but I would like to use the flash for it. I also have seen that there are a lot of predefined mtds for other boards than mine. For the project I use a nucleo f429zi. Can sombody help me with the mtd configuration? tell me a other way to use littlefs2? or do I oversee something?

Thanks a lot in advance.

You can use a macro in your board.c to create a mountpoint for littlefs2:

VFS_AUTO_MOUNT(littlefs2, VFS_MTD(my_mtd_dev), VFS_DEFAULT_NVM(0), 0);

This will use my_mtd_dev for littlefs and mount it as /nvm0 (that’s what VFS_DEFAULT_NVM(0) expands to, you can change that if you want a different mountpoint).

my_mtd_dev is your MTD device, for SPI connected NOR flash you’d have

static mtd_spi_nor_t my_mtd_dev = {
    .base = {
        .driver = &mtd_spi_nor_driver,
        .page_size = 256,
        .pages_per_sector = 16, // 4k sectors
    .params = &my_spi_nor_params,

my_spi_nor_params contains the configuration (pins, timings) for your flash, e.g. :

static const mtd_spi_nor_params_t _same54_nor_params = {
    .opcode = &mtd_spi_nor_opcode_default,
    .wait_chip_erase = 240 * US_PER_SEC, // check with data sheet
    .wait_64k_erase = 700 * US_PER_MS,
    .wait_sector_erase = 250 * US_PER_MS,
    .wait_chip_wake_up = 1 * US_PER_MS,
    .clk  = MHZ(54), // check with data sheet
    .flag = SPI_NOR_F_SECT_4K | SPI_NOR_F_SECT_64K, // check with data sheet
    .spi  = SPI_DEV(…), // insert your SPI device index
    .mode = SPI_MODE_0,
    .cs   = GPIO_PIN(…), // insert your pin
    .wp   = GPIO_PIN(…),
    .hold = GPIO_PIN(…),

To enable the fs automatically, add

ifneq (,$(filter mtd,$(USEMODULE)))
  USEMODULE += mtd_spi_nor

ifneq (,$(filter vfs_default,$(USEMODULE)))
  USEPKG += littlefs2
  USEMODULE += mtd

to your board’s Makefile.dep

To check if everything works correctly, you can run tests/sys/vfs_default

1 Like

Thanks for the fast answer @benpicco

I haven’t seen a board.c for the nucleo boards, so for the proper implementation should this be added to the nucleo-f429zi folder where only a periph_conf.h is, to the common/nulceo folder to as board_nucleo.c or to the common/nucleo144 folder as board.c ?

Thanks again

It doesn’t actually matter where the VFS_AUTO_MOUNT() macro is used, this is a Cross-File Array, you can also add it to your application or a separate mtd.c in the board folder.

1 Like

Thanks again for the quick answer @benpicco