STM32 - LittleFS Flash File System Interfacing

From Embedded Workshop
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

Download the LittleFS code from https://github.com/littlefs-project/littlefs
For this project I used version 2.5, but any newer version should also work.

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
When using any of the LittleFS functions, it is good to check the return code from any
function call.  The file, lfs.h, provides a list of possible error return codes.
The value LFS_ERR_OK ( 0 ), is returned for success.  Any negative value is considered
an error.
If you ever have to debug an issue with LittleFS, you can enable LFS_TRACE, LFS_DEBUG,
LFS_WARN, and/or LFS_ERROR macros, defined in lfs_util.h.  The LFS_TRACE macro allows
a user to "look inside the code" as each of the internal functions are called.
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

Notes

The FLASH Program and Erase functions appear to require 64MHz to function correctly.
Setting the system clock speed to 72MHz produces unstable results.