STM32 - LittleFS Flash File System Interfacing

From Embedded Workshop
Revision as of 20:21, 19 August 2022 by JMerkle (talk | contribs)
Jump to navigation Jump to search

LittleFS Flash File System

In the Embedded world, LittleFS is quickly being adopted as the Embedded filesystem of choice.
Although SpiFFS has held that position for many years, it is no longer being maintained, while LittleFS is quickly being adopted.
LittleFS appears to be even more compact and has additional security in that it creates and maintains a CRC for each file, and verifies the CRC each time the file is read.

The Hardware

Using only a NUCLEO-F103RB. (Allocated the last 32K of program FLASH memory for LittleFS)

The Software

LittleFS requires three functions to interact with the FLASH memory:
1) ReadFlash
2) WriteFlash
3) EraseFlash
LittleFS requires a configuration data structure filled out so it knows about the FLASH
and some rather small RAM buffers.
1) Its smallest erasable sector size
2) The smallest block size it can read
3) The smallest block size it can write
It will also need some rather small RAM buffers
I put all my interface code into a separate file, littlefs_interface.c.
It includes:
lfs_read()  - function to read STM32F103RB FLASH memory
lfs_prog()  - function to program / write STM32F103RB FLASH memory
lfs_erase() - function to erase a STM32F103RB FLASH memory sector
lfs_sync()  - synchronization function - stub
struct lfs_config lfs_cfg; - filled out defining the flash, buffers, and access functions

Here's the Folder & File structure I created / used (not all of you will like it...)
NUCLEO-F103RB_LittleFS
   Core
      Src
         main.c
         command_line.c
         command_line.h
         littlefs_interface.c
         littlefs_interface.h
         hexdump.c
         ...
     LittleFS
        lfs.c
        lfs.h
        lfs_util.c
        lfs_util.h

Command Line Functions

Since I enjoy using a command line interface, additional functions are included:
int cl_dir(void);       - Display a file system directory
int cl_make_dir(void);  - Make a test directory
int cl_remove(void);    - Remove a file / directory
int cl_make_file(void); - Make a test file
int cl_cat(void);       - Display a file
int cl_file_dump(void); - Hexadecimal dump of the file contents
int cl_copy(void);      - File copy
int cl_rename(void);    - File / directory rename
... plus a few other test functions

My Source Code

Here's my example of interface functions along with command line routines:
littlefs_interface.c
littlefs_interface.h