Skip to content

Commit 7a66810

Browse files
committed
Adding LITTLEFS w/o rebuild the IDF. Compatible with 3.3.
based on https://github.com/joltwallet/esp_littlefs This will be my first PR, hope will do it right.
1 parent b92c58d commit 7a66810

16 files changed

+8566
-188
lines changed

libraries/LITTLEFS/LICENSE

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

libraries/LITTLEFS/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# LITTLEFS
2+
## LittleFS library for arduino-esp32
3+
4+
- A LittleFS wrapper for Arduino ESP32 of [Mbed LittleFS](https://github.com/ARMmbed/littlefs)
5+
- Based on [ESP-IDF port of joltwallet/esp_littlefs](https://github.com/joltwallet/esp_littlefs) , thank you Brian!
6+
- Independent library, no need ESP-IDF SDK and arduino + littlefs component recompiling / re-integration, but it could be implemented this way, too.
7+
- Functionality is the same and SPIFFS partition scheme and data folder meaning are kept
8+
- You can use either LITTLEFS or SPIFFS but not both simultaneously on given Arduino project
9+
10+
### Installation
11+
12+
- <b>So far it is tested only with commit b92c58d of arduino-esp32 core (IDF 3.3). The release 1.0.4 is on an older IDF!</b>
13+
- Copy <b>LITTLEFS</b> folder to Arduino IDE embedded libraries place
14+
- For Win, the default place of arduino-esp32 core libraries is somewhere like:
15+
```C:\Users\<username>\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries ```
16+
- Alternatively, you can put it to your usual libraries place
17+
18+
### Usage
19+
20+
- In your existing code, replace SPIFFS like this
21+
```
22+
#define USE_LittleFS
23+
24+
#include <FS.h>
25+
#ifdef USE_LittleFS
26+
#define SPIFFS LITTLEFS
27+
#include <LITTLEFS.h>
28+
#else
29+
#include <SPIFFS.h>
30+
#endif
31+
```
32+
### Differences with SPIFFS (and in this implementation)
33+
34+
- LittleFS has folders, you my need to tweak your code to iterate files in folders
35+
- Root: /someting = something, so attention to /
36+
- Lower level littlefs library cannot mount on NULL as partition_label name, while SPIFFS can
37+
- Lower level littlefs library does not need maxOpenFiles parameter
38+
- Speed (LITTLEFS_Test.ino) 1048576 bytes written in 16238 ms / 1048576 bytes read in 918 ms
39+
- Speed (SPIFFS_Test.ino) 1048576 bytes written in 65971 ms / 1048576 bytes read in 680 ms
40+
41+
42+
### Arduino ESP32 LittleFS filesystem upload tool
43+
44+
- Download the tool archive from [here](https://github.com/lorol/arduino-esp32littlefs-plugin/raw/master/src/bin/esp32littlefs.jar)
45+
- In your Arduino sketchbook directory, create tools directory if it doesn't exist yet.
46+
- Unpack the tool into tools directory (the path will look like ```<home_dir>/Arduino/tools/ESP32LittleFS/tool/esp32littlefs.jar```).
47+
- You need the [mklittlefs tool](https://github.com/earlephilhower/mklittlefs) Download the [release](https://github.com/earlephilhower/mklittlefs/releases) and copy it to
48+
packages\esp32\tools\mkspiffs\<mklittlefs rev. x.x.x>\ or on checkout (dev) environment to: packages\esp32\hardware\esp32\<release>\tools\mklittlefs\
49+
- Restart Arduino IDE.
50+
51+
## Credits and license
52+
53+
- This work is based on [Mbed LittleFS](https://github.com/ARMmbed/littlefs) , [ESP-IDF port of joltwallet/esp_littlefs](https://github.com/joltwallet/esp_littlefs) , [Espressif Arduino core for the ESP32, the ESP-IDF - SPIFFS Library](https://github.com/espressif/arduino-esp32/tree/master/libraries/SPIFFS)
54+
- Licensed under GPL v2 ([text](LICENSE))
55+
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#include <Arduino.h>
2+
#include "FS.h"
3+
#include <LITTLEFS.h>
4+
5+
/* You only need to format SPIFFS the first time you run a
6+
test or else use the LITTLEFS plugin to create a partition
7+
https://github.com/me-no-dev/arduino-esp32fs-plugin */
8+
#define FORMAT_LITTLEFS_IF_FAILED true
9+
10+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
11+
Serial.printf("Listing directory: %s\r\n", dirname);
12+
13+
File root = fs.open(dirname);
14+
if(!root){
15+
Serial.println("- failed to open directory");
16+
return;
17+
}
18+
if(!root.isDirectory()){
19+
Serial.println(" - not a directory");
20+
return;
21+
}
22+
23+
File file = root.openNextFile();
24+
while(file){
25+
if(file.isDirectory()){
26+
Serial.print(" DIR : ");
27+
Serial.println(file.name());
28+
if(levels){
29+
listDir(fs, file.name(), levels -1);
30+
}
31+
} else {
32+
Serial.print(" FILE: ");
33+
Serial.print(file.name());
34+
Serial.print("\tSIZE: ");
35+
Serial.println(file.size());
36+
}
37+
file = root.openNextFile();
38+
}
39+
}
40+
41+
void readFile(fs::FS &fs, const char * path){
42+
Serial.printf("Reading file: %s\r\n", path);
43+
44+
File file = fs.open(path);
45+
if(!file || file.isDirectory()){
46+
Serial.println("- failed to open file for reading");
47+
return;
48+
}
49+
50+
Serial.println("- read from file:");
51+
while(file.available()){
52+
Serial.write(file.read());
53+
}
54+
file.close();
55+
}
56+
57+
void writeFile(fs::FS &fs, const char * path, const char * message){
58+
Serial.printf("Writing file: %s\r\n", path);
59+
60+
File file = fs.open(path, FILE_WRITE);
61+
if(!file){
62+
Serial.println("- failed to open file for writing");
63+
return;
64+
}
65+
if(file.print(message)){
66+
Serial.println("- file written");
67+
} else {
68+
Serial.println("- write failed");
69+
}
70+
file.close();
71+
}
72+
73+
void appendFile(fs::FS &fs, const char * path, const char * message){
74+
Serial.printf("Appending to file: %s\r\n", path);
75+
76+
File file = fs.open(path, FILE_APPEND);
77+
if(!file){
78+
Serial.println("- failed to open file for appending");
79+
return;
80+
}
81+
if(file.print(message)){
82+
Serial.println("- message appended");
83+
} else {
84+
Serial.println("- append failed");
85+
}
86+
file.close();
87+
}
88+
89+
void renameFile(fs::FS &fs, const char * path1, const char * path2){
90+
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
91+
if (fs.rename(path1, path2)) {
92+
Serial.println("- file renamed");
93+
} else {
94+
Serial.println("- rename failed");
95+
}
96+
}
97+
98+
void deleteFile(fs::FS &fs, const char * path){
99+
Serial.printf("Deleting file: %s\r\n", path);
100+
if(fs.remove(path)){
101+
Serial.println("- file deleted");
102+
} else {
103+
Serial.println("- delete failed");
104+
}
105+
}
106+
107+
void testFileIO(fs::FS &fs, const char * path){
108+
Serial.printf("Testing file I/O with %s\r\n", path);
109+
110+
static uint8_t buf[512];
111+
size_t len = 0;
112+
File file = fs.open(path, FILE_WRITE);
113+
if(!file){
114+
Serial.println("- failed to open file for writing");
115+
return;
116+
}
117+
118+
size_t i;
119+
Serial.print("- writing" );
120+
uint32_t start = millis();
121+
for(i=0; i<2048; i++){
122+
if ((i & 0x001F) == 0x001F){
123+
Serial.print(".");
124+
}
125+
file.write(buf, 512);
126+
}
127+
Serial.println("");
128+
uint32_t end = millis() - start;
129+
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
130+
file.close();
131+
132+
file = fs.open(path);
133+
start = millis();
134+
end = start;
135+
i = 0;
136+
if(file && !file.isDirectory()){
137+
len = file.size();
138+
size_t flen = len;
139+
start = millis();
140+
Serial.print("- reading" );
141+
while(len){
142+
size_t toRead = len;
143+
if(toRead > 512){
144+
toRead = 512;
145+
}
146+
file.read(buf, toRead);
147+
if ((i++ & 0x001F) == 0x001F){
148+
Serial.print(".");
149+
}
150+
len -= toRead;
151+
}
152+
Serial.println("");
153+
end = millis() - start;
154+
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
155+
file.close();
156+
} else {
157+
Serial.println("- failed to open file for reading");
158+
}
159+
}
160+
161+
void setup(){
162+
Serial.begin(115200);
163+
if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
164+
Serial.println("LITTLEFS Mount Failed");
165+
return;
166+
}
167+
168+
listDir(LITTLEFS, "/", 0);
169+
writeFile(LITTLEFS, "/hello.txt", "Hello ");
170+
appendFile(LITTLEFS, "/hello.txt", "World!\r\n");
171+
readFile(LITTLEFS, "/hello.txt");
172+
renameFile(LITTLEFS, "/hello.txt", "/foo.txt");
173+
readFile(LITTLEFS, "/foo.txt");
174+
deleteFile(LITTLEFS, "/foo.txt");
175+
testFileIO(LITTLEFS, "/test.txt");
176+
deleteFile(LITTLEFS, "/test.txt");
177+
Serial.println( "Test complete" );
178+
}
179+
180+
void loop(){
181+
182+
}

libraries/LITTLEFS/library.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name":"LITTLEFS",
3+
"description":"LITTLEFS File System Library for ESP32",
4+
"keywords":"littlefs, spiffs",
5+
"authors":
6+
{
7+
"name": "LL",
8+
"maintainer": true
9+
},
10+
"repository":
11+
{
12+
"type": "git",
13+
"url": "https://github.com/lorol/LITTLEFS.git"
14+
},
15+
"version": "1.0",
16+
"license": "LGPL-2.0",
17+
"frameworks": "arduino",
18+
"platforms": "espressif32",
19+
"build": {
20+
"libCompatMode": 2
21+
}
22+
}

libraries/LITTLEFS/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=LITTLEFS
2+
version=1.0
3+
author=LL
4+
maintainer=LL
5+
sentence=ESP32 LITTLEFS File System
6+
paragraph=
7+
category=Data Storage
8+
url=
9+
architectures=esp32

0 commit comments

Comments
 (0)