Skip to content

Commit

Permalink
subsys/fs: add support for littlefs
Browse files Browse the repository at this point in the history
littlefs is a fail-safe filesystem from ARM Mbed that has wear-leveling
capabilities.

Signed-off-by: Peter A. Bigot <[email protected]>
Signed-off-by: Jim Paris <[email protected]>
  • Loading branch information
pabigot authored and carlescufi committed Aug 6, 2019
1 parent b8be48e commit fb73fcd
Show file tree
Hide file tree
Showing 8 changed files with 781 additions and 6 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@
/subsys/fs/ @nashif
/subsys/fs/fcb/ @nvlsianpu
/subsys/fs/fuse_fs_access.c @vanwinkeljan
/subsys/fs/littlefs_fs.c @pabigot
/subsys/fs/nvs/ @Laczen
/subsys/logging/ @nordic-krch
/subsys/mgmt/ @carlescufi @nvlsianpu
Expand Down
1 change: 1 addition & 0 deletions include/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ enum fs_dir_entry_type {
enum fs_type {
FS_FATFS = 0,
FS_NFFS,
FS_LITTLEFS,
FS_TYPE_END,
};

Expand Down
2 changes: 1 addition & 1 deletion include/fs/fs_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
extern "C" {
#endif

#ifdef CONFIG_FILE_SYSTEM_NFFS
#if defined(CONFIG_FILE_SYSTEM_LITTLEFS) || defined(CONFIG_FILE_SYSTEM_NFFS)
#define MAX_FILE_NAME 256
#else /* FAT_FS */
#define MAX_FILE_NAME 12 /* Uses 8.3 SFN */
Expand Down
39 changes: 39 additions & 0 deletions include/fs/littlefs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2019 Bolt Innovation Management, LLC
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_FS_LITTLEFS_H_
#define ZEPHYR_INCLUDE_FS_LITTLEFS_H_

#include <zephyr/types.h>
#include <kernel.h>
#include <storage/flash_map.h>

#include <lfs.h>

#ifdef __cplusplus
extern "C" {
#endif

/** @brief Filesystem info structure for LittleFS mount */
struct fs_littlefs {
/* These structures are filled automatically at mount. */
struct lfs lfs;
struct lfs_config cfg;
const struct flash_area *area;
struct k_mutex mutex;

/* Static buffers */
u8_t read_buffer[CONFIG_FS_LITTLEFS_CACHE_SIZE];
u8_t prog_buffer[CONFIG_FS_LITTLEFS_CACHE_SIZE];
/* Multiple of 8 bytes, but 4-byte aligned (littlefs #239) */
u32_t lookahead_buffer[CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE / sizeof(u32_t)];
};

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_FS_LITTLEFS_H_ */
12 changes: 7 additions & 5 deletions subsys/fs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ if(CONFIG_FILE_SYSTEM)
zephyr_library()
zephyr_library_include_directories(${CMAKE_CURRENT_SOURCE_DIR})
zephyr_library_sources(fs.c fs_impl.c)
zephyr_library_sources_ifdef(CONFIG_FAT_FILESYSTEM_ELM fat_fs.c)
zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_NFFS nffs_fs.c)
zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_SHELL shell.c)
zephyr_library_sources_ifdef(CONFIG_FAT_FILESYSTEM_ELM fat_fs.c)
zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_LITTLEFS littlefs_fs.c)
zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_NFFS nffs_fs.c)
zephyr_library_sources_ifdef(CONFIG_FILE_SYSTEM_SHELL shell.c)

zephyr_library_link_libraries(FS)

target_link_libraries_ifdef(CONFIG_FAT_FILESYSTEM_ELM FS INTERFACE ELMFAT)
target_link_libraries_ifdef(CONFIG_FILE_SYSTEM_NFFS FS INTERFACE NFFS)
target_link_libraries_ifdef(CONFIG_FAT_FILESYSTEM_ELM FS INTERFACE ELMFAT)
target_link_libraries_ifdef(CONFIG_FILE_SYSTEM_LITTLEFS FS INTERFACE LITTLEFS)
target_link_libraries_ifdef(CONFIG_FILE_SYSTEM_NFFS FS INTERFACE NFFS)
endif()

add_subdirectory_ifdef(CONFIG_FCB ./fcb)
Expand Down
64 changes: 64 additions & 0 deletions subsys/fs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ config FILE_SYSTEM_NFFS
Note: NFFS requires 1-byte unaligned access to flash thus it
will not work on devices that support only aligned flash access.

config FILE_SYSTEM_LITTLEFS
bool "LittleFS file system support"
depends on FLASH_MAP
depends on FLASH_PAGE_LAYOUT
help
Enables LittleFS file system support.

config FILE_SYSTEM_SHELL
bool "Enable file system shell"
depends on SHELL
Expand Down Expand Up @@ -121,6 +128,63 @@ config NFFS_FILESYSTEM_MAX_BLOCK_SIZE

endmenu

menu "LittleFS Settings"
visible if FILE_SYSTEM_LITTLEFS

config FS_LITTLEFS_NUM_FILES
int "Maximum number of opened files"
default 4
help
This is a global maximum across all mounted littlefs filesystems.

config FS_LITTLEFS_NUM_DIRS
int "Maximum number of opened directories"
default 4
help
This is a global maximum across all mounted littlefs filesystems.

config FS_LITTLEFS_READ_SIZE
int "Minimum size of a block read"
default 16
help
All read operations will be a multiple of this value.

config FS_LITTLEFS_PROG_SIZE
int "Minimum size of a block program"
default 16
help
All program operations will be a multiple of this value.

config FS_LITTLEFS_CACHE_SIZE
int "Size of block caches in bytes"
default 64
help
Each cache buffers a portion of a block in RAM. The littlefs
needs a read cache, a program cache, and one additional cache
per file. Larger caches can improve performance by storing
more data and reducing the number of disk accesses. Must be a
multiple of the read and program sizes of the underlying flash
device, and a factor of the block size.

config FS_LITTLEFS_LOOKAHEAD_SIZE
int "Size of lookahead buffer in bytes"
default 32
help
A larger lookahead buffer increases the number of blocks found
during an allocation pass. The lookahead buffer is stored as a
compact bitmap, so each byte of RAM can track 8 blocks. Must
be a multiple of 8.

config FS_LITTLEFS_BLOCK_CYCLES
int "Number of erase cycles before moving data to another block"
default 512
help
For dynamic wear leveling, the number of erase cycles before data
is moved to another block. Set to a non-positive value to
disable leveling.

endmenu

endif # FILE_SYSTEM

source "subsys/fs/fcb/Kconfig"
Expand Down
Loading

0 comments on commit fb73fcd

Please sign in to comment.