Little File System (LittleFS) Initialization and Configuration#

The Little File System (LittleFS) is a lightweight file system designed for embedded systems with limited resources. This section explains how to initialize and configure LittleFS for Si91x devices by using Simplicity Studio and the WiSeConnect Software Development Kit (SDK).

Startup Sequence#

Before configuring LittleFS, review the startup sequence. This sequence ensures that the file system is properly integrated into your project and can be mounted successfully.

Step-by-step LittleFS Initialization and Configuration in Simplicity Studio#

This step-by-step guide shows how to set up LittleFS on Si91x devices by using Simplicity Studio and the WiSeConnect SDK.

Step 1. Create or Open a Project#

  1. Launch Simplicity Studio.

  2. Create a new project for your Si91x device, or open an existing one.

  3. For demonstration, select one of the LittleFS example projects available in the WiSeConnect SDK.

  4. Connect your Si91x radio board. Simplicity Studio automatically detects the board.

    Board auto detectionBoard auto detection

    Figure: Board detection in Simplicity Studio

  5. Search for a LittleFS example project in the Example Projects and Demos section.

    LittleFS example searchLittleFS example search

    Figure: Search for LittleFS example projects

  6. Click Create to add the project to your workspace. You can also open the project documentation to review usage details.

  7. After you create and configure the project (including the project name, location, and SDK source linking or copying), Simplicity Studio generates the following directory structure. Most examples follow this structure and open the readme.md file by default.

LittleFS exampleLittleFS example

  • autogen/: Auto-generated files, such as configuration headers and linker scripts.

  • config/: Platform-specific configuration headers.

  • resources/: Documentation images and resources.

  • simplicity_sdk_/: Gecko SDK platform layer and third-party libraries.

    • platform/: Hardware Abstraction Layer (HAL), Cortex Microcontroller Software Interface Standard (CMSIS) RTOS, and common libraries.

  • wiseconnect3_sdk_/: WiSeConnect SDK components and resources.

  • app.c: Main application source file.

  • app.h: Main application header file.

  • main.c: Application entry point.

  • readme.md: Example documentation and usage instructions.

  • sl_si91x_file_system_common_flash.slcp: Main project configuration file.

  • sl_si91x_file_system_common_flash.slps: Project set file for managing related solutions.

Step 2. Add the LittleFS Component#

You must add the LittleFS component to your project through the Project Configurator in Simplicity Studio.

To add the component:

  1. Open your project’s .slcp (Simplicity Studio Component Project) file.

  2. Go to the Software Components tab.

  3. Search for LittleFS or browse to WiSeConnect 3 SDK > Device > Si91x > MCU > Service.

LittleFS ComponentLittleFS Component

Step 3. Build, Flash, and Test#

Before building your project, update the connectivity firmware (also called Network Processor [NWP] firmware) to the latest version. This step prevents compatibility issues with the application binary. Download the latest firmware from the WiSeConnect GitHub repository and follow the upgrade instructions for SiWx91x connectivity firmware.

Then:

  1. Build your project in Simplicity Studio.

    LittleFS build projectLittleFS build project

  2. Flash the firmware to your Si91x device.

    LittleFS flash projectLittleFS flash project
    LittleFS program fileLittleFS program file

  3. Use a serial console or debugger to verify LittleFS operation. The following figure shows the console output for the LittleFS WiSeConnect example.

    LittleFS console outputLittleFS console output

API Initialization for LittleFS#

After you build and flash your project, you must initialize LittleFS by defining its configuration, declaring a control block, and mounting the file system.

Define the Configuration Structure#

Define and populate an lfs_config structure with details about the underlying storage device.

This includes:

  • read, prog (program/write), erase, and sync callbacks: Define how LittleFS interacts with the physical storage device (for example, reading and writing sectors, erasing blocks, flushing data).

  • read_size, prog_size, block_size: Specify the minimum read unit, program unit, and erase block size of the flash.

  • block_count: Set the total number of erase blocks available to LittleFS.

  • block_cycles: Estimate how many erase cycles a block can withstand before failing (used for wear leveling).

  • cache_size, lookahead_size: Set sizes for internal caching and lookahead buffers.

const struct lfs_config cfg = {
  // block device operations
  .read   = si91x_block_device_read,
  .prog   = si91x_block_device_prog,
  .erase  = si91x_block_device_erase,
  .sync   = si91x_block_device_sync,
  .lock   = si91x_block_device_lock,
  .unlock = si91x_block_device_unlock,

  // block device configuration
  .read_size      = 64,
  .prog_size      = 64,
  .block_size     = 4096,
  .block_count    = 64,
  .cache_size     = 256,
  .lookahead_size = 32,
  .block_cycles   = 500,
};                 

Declare the Control Block#

Declare an lfs_t object (the control block) to hold the internal state of the LittleFS instance.

lfs_t lfs;

Initialize LittleFS#

Call the lfs_mount() function, passing in the lfs_t control block and the lfs_config structure. The function attempts to mount the file system. If the mount fails (for example, on first boot), reformat and remount.

  // mount the filesystem
  err = lfs_mount(&lfs, &cfg);

  // reformat if we can't mount the filesystem
  // this should only happen on the first boot
  if (err) {
    err = lfs_format(&lfs, &cfg);
    err = lfs_mount(&lfs, &cfg);
  }

  // read current count
  err = lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
  if (err) {
    printf("Failed to open the file ");
    return;
  }
  lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));

  // update boot count
  boot_count += 1;
  lfs_file_rewind(&lfs, &file);
  lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));

  // remember the storage is not updated until the file is closed successfully
  lfs_file_close(&lfs, &file);

  // release any resources we were using
  lfs_unmount(&lfs);

  // print the boot count
  printf("boot_count: %ld", boot_count);