Home

Awesome

Raspberry Pi Pico littlefs USB Flash Memory Interface

This project demonstrates a method for mounting littlefs via USB to facilitate easy retrieval of sensor data and other information stored on microcontrollers from a standard PC.

Littlefs is widely used as a reliable file system for microcontrollers. However, since this file system is not supported by ordinary host PCs, special software and procedures are required for file read and write operations. The core idea of this project is to add an intermediate conversion layer to the USB Mass Storage Class device driver of the microcontroller, mimicking littlefs as a FAT file system. This allows littlefs to be manipulated from the host PC as if it were a USB flash drive with a common FAT file system.

Demo Overview

The demo operates as follows:

Build and Installation

The build requires updating the littlefs git submodule and instructing CMake how to retrieve the pico-sdk.

git submodule update --init

mkdir build; cd build
PICO_SDK_FETCH_FROM_GIT=1 cmake ..
make

In this instruction, the PICO_SDK_FETCH_FROM_GIT environment variable is specified when CMake is run, instructing it to clone the pico-sdk from github. If you want to specify a pico-sdk that has already been deployed locally, specify it in the PICO_SDK_PATH environment variable.

After successful compilation, littlefs-usb.uf2 will be generated. Simply drag and drop it onto your Raspberry Pi Pico to install and run the application.

Limitations

The current implementation has several limitations:

Mimicking Process

FAT12 is a very simple file system and can be easily mimicked. Depending on the location of the block device requested by the USB host, the microcontroller assembles and returns the appropriate FAT12 block.

Upon USB connection, all files in the littlefs file system are searched to build a cache of FAT directory entries. Read requests from the USB host determine the type (file or directory) of the requested object based on the cache. Requests for directories are sent directly from the cache, while requests for files open the corresponding file in littlefs and send its content. Write requests involve updating the cache and reflecting changes in littlefs. The cache is updated based on the differences in directory entries.

See FAT_OPERATION.md for details on the sequence of disk operations.

Testing

The tests directory contains code to verify the API's behavior. After building and installing the test code on the Pico, the unit tests are executed directly on the device, and results are sent via UART.

make tests

To run the tests, transfer the tests/tests.uf2 file to your Pico. For a more comprehensive debugging experience, connect the Raspberry Pi Debug Probe to the SWD Debug and UART Serial interfaces of the Pico. Use the following command to install and run the tests:

make run_tests