Skip to content

Commit 03fe3a7

Browse files
committed
add littlefs surport.
1 parent a7792ad commit 03fe3a7

File tree

11 files changed

+676
-32
lines changed

11 files changed

+676
-32
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "filesystem/littlefs"]
2+
path = filesystem/littlefs
3+
url = https://github.com/ARMmbed/mbed-littlefs.git

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ include_directories(filesystem/diskio/include)
1717
include_directories(filesystem/fatfs/include)
1818
include_directories(filesystem/vfs/include)
1919
include_directories(tclap)
20+
include_directories(filesystem/littlefs/littlefs)
21+
include_directories(filesystem/littlefs/littlefs/bd)
2022

2123
aux_source_directory(. DIR_SRCS)
2224
aux_source_directory(esp_idf DIR_ESP_IDF)
2325
aux_source_directory(esp_idf/freertos DIR_RTOS)
2426
aux_source_directory(filesystem/diskio DIR_DISKIO)
2527
aux_source_directory(filesystem/fatfs DIR_FATFS)
2628
aux_source_directory(filesystem/vfs DIR_VFS)
29+
aux_source_directory(filesystem/littlefs/littlefs DIR_LITTLEFS)
30+
set(SRC_RAMBD filesystem/littlefs/littlefs/bd/lfs2_rambd.c )
2731

28-
add_executable(mkfatfs ${DIR_SRCS} ${DIR_ESP_IDF} ${DIR_RTOS} ${DIR_DISKIO} ${DIR_FATFS} ${DIR_VFS})
32+
add_executable(mkfatfs ${DIR_SRCS} ${DIR_ESP_IDF} ${DIR_RTOS} ${DIR_DISKIO} ${DIR_FATFS} ${DIR_VFS} ${DIR_LITTLEFS} ${SRC_RAMBD})
2933

3034
# add_subdirectory(esp_idf)
3135
# add_subdirectory(filesystem/diskio)

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,26 @@ Tool to build and unpack [FATFS](https://github.com/jkearins/ESP32_mkfatfs) imag
99
in windows:
1010
1111
mkfatfs {-c <pack_dir>|-u <dest_dir>|-l|-i} [-d <0-5>] [-b <number>]
12-
[-p <number>] [-s <number>] [--] [--version] [-h]
12+
[-p <number>] [-s <number>] [--] [--version] [-t <fstype>] [-h]
1313
<image_file>
1414
1515
in linux:
1616
./mkfatfs {-c <pack_dir>|-u <dest_dir>|-l|-i} [-d <0-5>] [-b <number>]
17-
[-p <number>] [-s <number>] [--] [--version] [-h]
17+
[-p <number>] [-s <number>] [--] [--version] [-t <fstype>] [-h]
1818
<image_file>
1919
20+
eg:
21+
pack:
22+
windows:
23+
mkfatfs.exe -c Test -s 0x200000 -t littlefs test.bin
24+
linux:
25+
./mkfatfs -c Test -s 0x200000 -t littlefs test.bin
26+
27+
unpack:
28+
windows:
29+
mkfatfs.exe -u Test -s 0 -t littlefs test.bin
30+
linux:
31+
./mkfatfs -u Test -s 0 -t littlefs test.bin
2032
2133
Where:
2234
@@ -45,6 +57,9 @@ Where:
4557
-s <number>, --size <number>
4658
fs image size, in bytes
4759
60+
-t <fstype> --type <fstype>
61+
fs type, surport fatfs and littlefs
62+
4863
--, --ignore_rest
4964
Ignores the rest of the labeled arguments following this flag.
5065
@@ -65,6 +80,10 @@ You need gcc (≥4.8) or clang(≥600.0.57), and make. On Windows, use MinGW.
6580
You need cmake (≥3.0.0).
6681
Youn can develop in vscode. install extension CMake and CMake Tools.
6782
More help for build project, please read the extension's help.
83+
in linux:
84+
cd mkfatfs
85+
cmake .
86+
make
6887

6988
## License
7089

filesystem/littlefs

Submodule littlefs added at 70aa179

main.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
#define TCLAP_SETBASE_ZERO 1
1919
#include "tclap/CmdLine.h"
2020
#include "tclap/UnlabeledValueArg.h"
21-
#include "pack.h"
21+
#include "pack_fat.h"
22+
#include "pack_littlefs.h"
2223

2324
#define APP_VERSION mkfatfs_VERSION
2425

@@ -30,7 +31,7 @@ static Action s_action = ACTION_NONE;
3031
static std::string s_dirName;
3132
static std::string s_imageName;
3233
static int s_imageSize;
33-
Pack pack = Pack();
34+
static std::string s_fs;
3435

3536
void processArgs(int argc, const char** argv) {
3637
TCLAP::CmdLine cmd("", ' ', APP_VERSION);
@@ -39,14 +40,16 @@ void processArgs(int argc, const char** argv) {
3940
TCLAP::SwitchArg listArg( "l", "list", "list files in fatFS image", false);
4041
TCLAP::SwitchArg visualizeArg( "i", "visualize", "visualize fatFS image", false);
4142
TCLAP::UnlabeledValueArg<std::string> outNameArg( "image_file", "fatFS image file", true, "", "image_file" );
42-
TCLAP::ValueArg<int> imageSizeArg( "s", "size", "fs image size, in bytes", false, 0x10000, "number" );
43+
TCLAP::ValueArg<int> imageSizeArg( "s", "size", "fs image size, in bytes", false, 0x200000, "number" );
4344
TCLAP::ValueArg<int> debugArg( "d", "debug", "Debug level. 0 means no debug output.", false, 0, "0-5" );
45+
TCLAP::ValueArg<std::string> fsType( "t", "type", "Select file system type, suport fatfs littlefs now.", false, "fatfs", "fatfs or littlefs" );
4446

4547
cmd.add( imageSizeArg );
4648
cmd.add(debugArg);
4749
std::vector<TCLAP::Arg*> args = {&packArg, &unpackArg, &listArg, &visualizeArg};
4850
cmd.xorAdd( args );
4951
cmd.add( outNameArg );
52+
cmd.add( fsType );
5053
cmd.parse( argc, argv );
5154

5255
if (debugArg.getValue() > 0) {
@@ -68,6 +71,7 @@ void processArgs(int argc, const char** argv) {
6871

6972
s_imageName = outNameArg.getValue();
7073
s_imageSize = imageSizeArg.getValue();
74+
s_fs = fsType.getValue();
7175
}
7276

7377
int main(int argc, const char * argv[]) {
@@ -78,12 +82,22 @@ int main(int argc, const char * argv[]) {
7882
return 1;
7983
}
8084

85+
Pack_fat pack_fat = Pack_fat();
86+
Pack_littlefs pack_littlefs = Pack_littlefs();
87+
8188
switch (s_action) {
8289
case ACTION_PACK:
83-
return pack.actionPack(s_dirName, s_imageName, s_imageSize);
90+
if(s_fs == "fatfs")
91+
return pack_fat.actionPack(s_dirName, s_imageName, s_imageSize);
92+
else if(s_fs == "littlefs")
93+
return pack_littlefs.actionPack(s_dirName, s_imageName, s_imageSize);
8494
break;
8595
case ACTION_UNPACK:
86-
return pack.actionUnpack(s_imageName, s_dirName, s_imageSize);
96+
if(s_fs == "fatfs")
97+
return pack_fat.actionUnpack(s_imageName, s_dirName, s_imageSize);
98+
else if(s_fs == "littlefs")
99+
return pack_littlefs.actionUnpack(s_imageName, s_dirName, s_imageSize);
100+
break;
87101
break;
88102
case ACTION_LIST:
89103

pack.cpp renamed to pack_fat.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Created by zhao hui jiang on 07/03/2020.
55
// Copyright (c). All rights reserved.
66
//
7-
#include "pack.h"
7+
#include "pack_fat.h"
88

99
static const char *BASE_PATH = "/fatfs";
1010
static RAM_handle_t s_ram_handle;
@@ -20,9 +20,9 @@ struct dirent_1 {
2020
char d_name[256]; /*!< zero-terminated file name */
2121
}dirent_t;
2222

23-
Pack::Pack()
23+
Pack_fat::Pack_fat()
2424
{}
25-
Pack::~Pack()
25+
Pack_fat::~Pack_fat()
2626
{}
2727

2828
/**
@@ -31,7 +31,7 @@ Pack::~Pack()
3131
* @param
3232
* @return True or false.
3333
*/
34-
bool Pack::fatfsMount(int s_imageSize) {
34+
bool Pack_fat::fsMount(int s_imageSize) {
3535
bool result;
3636
esp_vfs_fat_mount_config_t mountConfig;
3737
mountConfig.max_files = 4; //Max files.
@@ -47,7 +47,7 @@ bool Pack::fatfsMount(int s_imageSize) {
4747
* @param
4848
* @return True or false.
4949
*/
50-
bool Pack::fatfsUnmount() {
50+
bool Pack_fat::fsUnmount() {
5151
bool result;
5252

5353
result = (ESP_OK == emulate_esp_vfs_fat_spiflash_unmount(BASE_PATH, s_ram_handle));
@@ -63,7 +63,7 @@ bool Pack::fatfsUnmount() {
6363
return result;
6464
}
6565

66-
size_t Pack::getFileSize(FILE* fp)
66+
size_t Pack_fat::getFileSize(FILE* fp)
6767
{
6868
fseek(fp, 0L, SEEK_END);
6969
size_t size = (size_t) ftell(fp);
@@ -77,7 +77,7 @@ size_t Pack::getFileSize(FILE* fp)
7777
* @return True if exists otherwise false.
7878
*
7979
*/
80-
bool Pack::dirExists(const char* path) {
80+
bool Pack_fat::dirExists(const char* path) {
8181
DIR *d = opendir(path);
8282

8383
if (d) {
@@ -93,7 +93,7 @@ bool Pack::dirExists(const char* path) {
9393
* @param path Directory path.
9494
* @return True or false.
9595
*/
96-
bool Pack::dirCreate(const char* path) {
96+
bool Pack_fat::dirCreate(const char* path) {
9797
// Check if directory also exists.
9898
if (dirExists(path)) {
9999
return false;
@@ -117,7 +117,7 @@ bool Pack::dirCreate(const char* path) {
117117
* @param path Directory name, full path.
118118
* @return True or false.
119119
*/
120-
int Pack::parkDirToRamFS(const char* name) {
120+
int Pack_fat::parkDirToRamFS(const char* name) {
121121
std::string fileName = name;
122122
fileName += "/.";
123123

@@ -140,7 +140,7 @@ int Pack::parkDirToRamFS(const char* name) {
140140
* @param path_des des Directory path, full path.
141141
* @return True or false.
142142
*/
143-
int Pack::parkFileToRamFS(char* path_src, const char* path_des) {
143+
int Pack_fat::parkFileToRamFS(char* path_src, const char* path_des) {
144144
FILE* f_src = fopen(path_src, "rb"); //open file in pc.
145145
if (!f_src) {
146146
std::cerr << "error: failed to open " << path_src << " for reading" << std::endl;
@@ -200,7 +200,7 @@ int Pack::parkFileToRamFS(char* path_src, const char* path_des) {
200200
* @return True if exists otherwise false.
201201
*
202202
*/
203-
bool Pack::parkFilesToRamFS(const char* dirSrc, const char* dirDes)
203+
bool Pack_fat::parkFilesToRamFS(const char* dirSrc, const char* dirDes)
204204
{
205205
DIR *dir;
206206
bool error = false;
@@ -314,7 +314,7 @@ bool Pack::parkFilesToRamFS(const char* dirSrc, const char* dirDes)
314314
* @param path_des: des file, to pc
315315
* @return True or false.
316316
*/
317-
int Pack::unparkFileFromRamFS(const char* path_src, const char* path_des)
317+
int Pack_fat::unparkFileFromRamFS(const char* path_src, const char* path_des)
318318
{
319319
std::vector<uint8_t> temp_buf;
320320

@@ -354,7 +354,7 @@ int Pack::unparkFileFromRamFS(const char* path_src, const char* path_des)
354354
* @param dirDes directory/file in pc.
355355
* @return True or false.
356356
*/
357-
bool Pack::unparkFilesFromRamFS(const char* dirSrc, const char* dirDes)
357+
bool Pack_fat::unparkFilesFromRamFS(const char* dirSrc, const char* dirDes)
358358
{
359359
DIR *dir;
360360
struct dirent_1 *ent;
@@ -434,14 +434,14 @@ bool Pack::unparkFilesFromRamFS(const char* dirSrc, const char* dirDes)
434434
* @param
435435
* @return True or false.
436436
*/
437-
int Pack::actionPack(std::string s_dirName, std::string s_imageName, int s_imageSize) {
437+
int Pack_fat::actionPack(std::string s_dirName, std::string s_imageName, int s_imageSize) {
438438
int ret = 0; //0 - ok
439439

440440
// 1. resize g_flashmem and fill 0xff, it will used for RAM fat filesystem.
441441
g_flashmem.resize(s_imageSize, 0xff);
442442

443443
// 2. mount g_flashmem(in RAM) as a fat filesystem, mount point is BASE_PATH(root directory of the RAM filesystem).
444-
if (fatfsMount(s_imageSize)) {
444+
if (fsMount(s_imageSize)) {
445445
if (g_debugLevel > 0) {
446446
std::cout << "Mounted successfully" << std::endl;
447447
}
@@ -454,7 +454,7 @@ int Pack::actionPack(std::string s_dirName, std::string s_imageName, int s_image
454454
ret = parkFilesToRamFS(s_dirName.c_str(), BASE_PATH);
455455

456456
// 4. unmount the RAM fat filesystem.
457-
fatfsUnmount();
457+
fsUnmount();
458458

459459
// 5. open *.bin file witch read from esp32.
460460
FILE* fdres = fopen(s_imageName.c_str(), "wb");
@@ -483,7 +483,7 @@ int Pack::actionPack(std::string s_dirName, std::string s_imageName, int s_image
483483
* @param s_dirName directory in pc, we will read all files to it.
484484
* @return 0 success, 1 error
485485
*/
486-
int Pack::actionUnpack(std::string s_imageName, std::string s_dirName,int s_imageSize)
486+
int Pack_fat::actionUnpack(std::string s_imageName, std::string s_dirName,int s_imageSize)
487487
{
488488
int ret = 0;
489489

@@ -505,7 +505,7 @@ int Pack::actionUnpack(std::string s_imageName, std::string s_dirName,int s_imag
505505
fclose(f_src);
506506

507507
// 3. mount RAM(g_flashmem) file system, so we can use vfs read file in RAM file system.
508-
if (fatfsMount(s_imageSize)) {
508+
if (fsMount(s_imageSize)) {
509509
if (g_debugLevel > 0) {
510510
std::cout << "Mounted successfully" << std::endl;
511511
}
@@ -523,7 +523,7 @@ int Pack::actionUnpack(std::string s_imageName, std::string s_dirName,int s_imag
523523
}
524524

525525
// unmount file system
526-
fatfsUnmount();
526+
fsUnmount();
527527

528528
return ret;
529529
}

pack.h renamed to pack_fat.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@
2626
* @brief This class is used to pack/upack filesystem.
2727
*
2828
*/
29-
class Pack
29+
class Pack_fat
3030
{
3131
public:
32-
Pack();
33-
~Pack();
32+
Pack_fat();
33+
~Pack_fat();
3434
int actionPack(std::string s_dirName, std::string s_imageName, int s_imageSize);
3535
int actionUnpack(std::string s_imageName, std::string s_dirName,int s_imageSize);
3636

3737
private:
38-
bool fatfsMount(int s_imageSize);
39-
bool fatfsUnmount();
38+
bool fsMount(int s_imageSize);
39+
bool fsUnmount();
4040
size_t getFileSize(FILE* fp);
4141
bool dirExists(const char* path);
4242
bool dirCreate(const char* path);

0 commit comments

Comments
 (0)