Skip to content

Commit cd03a6f

Browse files
Add LittleFS as internal flash filesystem
Adds a LittleFS object which uses the ARMmbed littlefs embedded filesystem, https://github.com/ARMmbed/littlefs, to enable a new filesystem for onboard flash utilizing the exact same API as the existing SPIFFS filesystem. LittleFS is built for low memory systems that are subject to random power losses, is actively supported by the ARMmbed community, supports directories, and seems to be much faster in the large-ish read-mostly applications I use. LittleFS, however, has a larger minimum file allocation unit and does not do static wear levelling. This means that for systems that need many little files (<4K), have small SPIFFS areas (64K), or which have a large static set of files covering the majority of flash coupled with a frequently updated set of other files, it may not perform as well. Simply replace SPIFFS.begin() with LittleFS.begin() in your sketch, use LittleFS.open in place of SPIFFS.open to open files, and everything else just works thanks to the magic of @igrr's File base class. **LITTLEFS FLASH LAYOUT IS INCOMPATIBLE WITH SPIFFS** Since it is a completely different filesystem, you will need to reformat your flash (and lose any data therein) to use it. Tools to build the flash filesystem and upload are at https://github.com/earlephilhower/arduino-esp8266littlefs-plugin and https://github.com/earlephilhower/mklittlefs/ . The mklittlefs tool is installed as part of the Arduino platform installation, automatically. The included example shows a contrived read-mostly example and demonstrates how the same calls work on either SPIFFS.* or LittleFS.* Host tests are also included as part of CI. Directories are fully supported in LittleFS. This means that LittleFS will have a slight difference vs. SPIFFS when you use LittleFS.openDir()/Dir.next(). On SPIFFS dir.next() will return all filesystem entries, including ones in "subdirs" (because in SPIFFS there are no subdirs and "/" is the same as any other character in a filename). On LittleFS, dir.next() will only return entries in the directory specified, not subdirs. So to list files in "/subdir/..." you need to actually openDir("/subdir") and use Dir.next() to parse through just those elements. The returned filenames also only have the filename returned, not full paths. So on a FS with "/a/1", "/a/2" when you do openDir("/a"); dir.next().getName(); you get "1" and "2" and not "/a/1" and "/a/2" like in SPIFFS. This is consistent with POSIX ideas about reading directories and more natural for a FS. Most code will not be affected by this, but if you depend on openDir/Dir.next() you need to be aware of it. Corresponding ::mkdir, ::rmdir, ::isDirectory, ::isFile, ::openNextFile, and ::rewind methods added to Filesystem objects. Documentation has been updated with this and other LittleFS information. Subdirectories are made silently when they do not exist when you try and create a file in a subdir. They are silently removed when the last file in them is deleted. This is consistent with what SPIFFS does but is obviously not normal POSIX behavior. Since there has never been a "FS.mkdir()" method this is the only way to be compatible with legacy SPIFFS code. SPIFFS code has been refactored to pull out common flash_hal_* ops and placed in its own namespace, like LittleFS.
1 parent 6324241 commit cd03a6f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2094
-392
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "libraries/SoftwareSerial"]
88
path = libraries/SoftwareSerial
99
url = https://github.com/plerup/espsoftwareserial.git
10+
[submodule "libraries/LittleFS/lib/littlefs"]
11+
path = libraries/LittleFS/lib/littlefs
12+
url = https://github.com/ARMmbed/littlefs

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,5 @@ ESP8266 core files are licensed under LGPL.
138138
[axTLS](http://axtls.sourceforge.net/) library written by Cameron Rich, built from https://github.com/igrr/axtls-8266, is used in this project. It is distributed under [BSD license](https://github.com/igrr/axtls-8266/blob/master/LICENSE).
139139

140140
[BearSSL](https://bearssl.org) library written by Thomas Pornin, built from https://github.com/earlephilhower/bearssl-esp8266, is used in this project. It is distributed under the [MIT License](https://bearssl.org/#legal-details).
141+
142+
[LittleFS](https://github.com/ARMmbed/littlefs) library written by ARM Limited and released under the [BSD 3-clause license](https://github.com/ARMmbed/littlefs/blob/master/LICENSE.md).

cores/esp8266/Esp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,14 @@ uint32_t EspClass::getSketchSize() {
510510
return result;
511511
}
512512

513-
extern "C" uint32_t _SPIFFS_start;
513+
extern "C" uint32_t _FS_start;
514514

515515
uint32_t EspClass::getFreeSketchSpace() {
516516

517517
uint32_t usedSize = getSketchSize();
518518
// round one sector up
519519
uint32_t freeSpaceStart = (usedSize + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
520-
uint32_t freeSpaceEnd = (uint32_t)&_SPIFFS_start - 0x40200000;
520+
uint32_t freeSpaceEnd = (uint32_t)&_FS_start - 0x40200000;
521521

522522
#ifdef DEBUG_SERIAL
523523
DEBUG_SERIAL.printf("usedSize=%u freeSpaceStart=%u freeSpaceEnd=%u\r\n", usedSize, freeSpaceStart, freeSpaceEnd);

cores/esp8266/FS.cpp

+87-4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,44 @@ const char* File::name() const {
121121
return _p->name();
122122
}
123123

124+
const char* File::fullName() const {
125+
if (!_p)
126+
return nullptr;
127+
128+
return _p->fullName();
129+
}
130+
131+
bool File::isFile() const {
132+
if (!_p)
133+
return false;
134+
135+
return _p->isFile();
136+
}
137+
138+
bool File::isDirectory() const {
139+
if (!_p)
140+
return false;
141+
142+
return _p->isDirectory();
143+
}
144+
145+
void File::rewindDirectory() {
146+
if (!_fakeDir) {
147+
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
148+
} else {
149+
_fakeDir->rewind();
150+
}
151+
}
152+
153+
File File::openNextFile() {
154+
if (!_fakeDir) {
155+
_fakeDir = std::make_shared<Dir>(_baseFS->openDir(fullName()));
156+
}
157+
_fakeDir->next();
158+
return _fakeDir->openFile("r");
159+
}
160+
161+
124162
File Dir::openFile(const char* mode) {
125163
if (!_impl) {
126164
return File();
@@ -133,7 +171,7 @@ File Dir::openFile(const char* mode) {
133171
return File();
134172
}
135173

136-
return File(_impl->openFile(om, am));
174+
return File(_impl->openFile(om, am), _baseFS);
137175
}
138176

139177
String Dir::fileName() {
@@ -152,6 +190,20 @@ size_t Dir::fileSize() {
152190
return _impl->fileSize();
153191
}
154192

193+
bool Dir::isFile() const {
194+
if (!_impl)
195+
return false;
196+
197+
return _impl->isFile();
198+
}
199+
200+
bool Dir::isDirectory() const {
201+
if (!_impl)
202+
return false;
203+
204+
return _impl->isDirectory();
205+
}
206+
155207
bool Dir::next() {
156208
if (!_impl) {
157209
return false;
@@ -160,6 +212,14 @@ bool Dir::next() {
160212
return _impl->next();
161213
}
162214

215+
bool Dir::rewind() {
216+
if (!_impl) {
217+
return false;
218+
}
219+
220+
return _impl->rewind();
221+
}
222+
163223
bool FS::begin() {
164224
if (!_impl) {
165225
return false;
@@ -202,8 +262,7 @@ File FS::open(const char* path, const char* mode) {
202262
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
203263
return File();
204264
}
205-
206-
return File(_impl->open(path, om, am));
265+
return File(_impl->open(path, om, am), this);
207266
}
208267

209268
bool FS::exists(const char* path) {
@@ -221,7 +280,8 @@ Dir FS::openDir(const char* path) {
221280
if (!_impl) {
222281
return Dir();
223282
}
224-
return Dir(_impl->openDir(path));
283+
DirImplPtr p = _impl->openDir(path);
284+
return Dir(p, this);
225285
}
226286

227287
Dir FS::openDir(const String& path) {
@@ -239,6 +299,28 @@ bool FS::remove(const String& path) {
239299
return remove(path.c_str());
240300
}
241301

302+
bool FS::rmdir(const char* path) {
303+
if (!_impl) {
304+
return false;
305+
}
306+
return _impl->rmdir(path);
307+
}
308+
309+
bool FS::rmdir(const String& path) {
310+
return rmdir(path.c_str());
311+
}
312+
313+
bool FS::mkdir(const char* path) {
314+
if (!_impl) {
315+
return false;
316+
}
317+
return _impl->mkdir(path);
318+
}
319+
320+
bool FS::mkdir(const String& path) {
321+
return mkdir(path.c_str());
322+
}
323+
242324
bool FS::rename(const char* pathFrom, const char* pathTo) {
243325
if (!_impl) {
244326
return false;
@@ -251,6 +333,7 @@ bool FS::rename(const String& pathFrom, const String& pathTo) {
251333
}
252334

253335

336+
254337
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
255338
switch (mode[0]) {
256339
case 'r':

cores/esp8266/FS.h

+28-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace fs {
2828

2929
class File;
3030
class Dir;
31+
class FS;
3132

3233
class FileImpl;
3334
typedef std::shared_ptr<FileImpl> FileImplPtr;
@@ -48,7 +49,7 @@ enum SeekMode {
4849
class File : public Stream
4950
{
5051
public:
51-
File(FileImplPtr p = FileImplPtr()) : _p(p) {}
52+
File(FileImplPtr p = FileImplPtr(), FS *baseFS = nullptr) : _p(p), _fakeDir(nullptr), _baseFS(baseFS) { }
5253

5354
// Print methods:
5455
size_t write(uint8_t) override;
@@ -72,22 +73,41 @@ class File : public Stream
7273
void close();
7374
operator bool() const;
7475
const char* name() const;
76+
const char* fullName() const; // Includes path
77+
78+
bool isFile() const;
79+
bool isDirectory() const;
80+
81+
// Arduino "class SD" methods for compatibility
82+
size_t write(const char *str) { return write((const uint8_t*)str, strlen(str)); }
83+
void rewindDirectory();
84+
File openNextFile();
7585

7686
protected:
7787
FileImplPtr _p;
88+
89+
// Arduino SD class emulation
90+
std::shared_ptr<Dir> _fakeDir;
91+
FS *_baseFS;
7892
};
7993

8094
class Dir {
8195
public:
82-
Dir(DirImplPtr impl = DirImplPtr()): _impl(impl) { }
96+
Dir(DirImplPtr impl = DirImplPtr(), FS *baseFS = nullptr): _impl(impl), _baseFS(baseFS) { }
8397

8498
File openFile(const char* mode);
99+
85100
String fileName();
86101
size_t fileSize();
102+
bool isFile() const;
103+
bool isDirectory() const;
104+
87105
bool next();
106+
bool rewind();
88107

89108
protected:
90109
DirImplPtr _impl;
110+
FS *_baseFS;
91111
};
92112

93113
struct FSInfo {
@@ -125,6 +145,12 @@ class FS
125145
bool rename(const char* pathFrom, const char* pathTo);
126146
bool rename(const String& pathFrom, const String& pathTo);
127147

148+
bool mkdir(const char* path);
149+
bool mkdir(const String& path);
150+
151+
bool rmdir(const char* path);
152+
bool rmdir(const String& path);
153+
128154
protected:
129155
FSImplPtr _impl;
130156
};

cores/esp8266/FSImpl.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class FileImpl {
3636
virtual size_t size() const = 0;
3737
virtual void close() = 0;
3838
virtual const char* name() const = 0;
39+
virtual const char* fullName() const = 0;
40+
virtual bool isFile() const = 0;
41+
virtual bool isDirectory() const = 0;
3942
};
4043

4144
enum OpenMode {
@@ -57,7 +60,10 @@ class DirImpl {
5760
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
5861
virtual const char* fileName() = 0;
5962
virtual size_t fileSize() = 0;
63+
virtual bool isFile() const = 0;
64+
virtual bool isDirectory() const = 0;
6065
virtual bool next() = 0;
66+
virtual bool rewind() = 0;
6167
};
6268

6369
class FSImpl {
@@ -72,7 +78,8 @@ class FSImpl {
7278
virtual DirImplPtr openDir(const char* path) = 0;
7379
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
7480
virtual bool remove(const char* path) = 0;
75-
81+
virtual bool mkdir(const char* path) = 0;
82+
virtual bool rmdir(const char* path) = 0;
7683
};
7784

7885
} // namespace fs

cores/esp8266/Updater.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" {
2424
#include "user_interface.h"
2525
}
2626

27-
extern "C" uint32_t _SPIFFS_start;
27+
extern "C" uint32_t _FS_start;
2828

2929
UpdaterClass::UpdaterClass()
3030
: _async(false)
@@ -81,8 +81,8 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
8181
}
8282

8383
#ifdef DEBUG_UPDATER
84-
if (command == U_SPIFFS) {
85-
DEBUG_UPDATER.println(F("[begin] Update SPIFFS."));
84+
if (command == U_FS) {
85+
DEBUG_UPDATER.println(F("[begin] Update Filesystem."));
8686
}
8787
#endif
8888

@@ -106,7 +106,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
106106
//size of current sketch rounded to a sector
107107
size_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
108108
//address of the end of the space available for sketch and update
109-
uintptr_t updateEndAddress = (uintptr_t)&_SPIFFS_start - 0x40200000;
109+
uintptr_t updateEndAddress = (uintptr_t)&_FS_start - 0x40200000;
110110
//size of the update rounded to a sector
111111
size_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
112112
//address where we will start writing the update
@@ -124,8 +124,8 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
124124
return false;
125125
}
126126
}
127-
else if (command == U_SPIFFS) {
128-
updateStartAddress = (uintptr_t)&_SPIFFS_start - 0x40200000;
127+
else if (command == U_FS) {
128+
updateStartAddress = (uintptr_t)&_FS_start - 0x40200000;
129129
}
130130
else {
131131
// unknown command
@@ -273,8 +273,8 @@ bool UpdaterClass::end(bool evenIfRemaining){
273273
#ifdef DEBUG_UPDATER
274274
DEBUG_UPDATER.printf_P(PSTR("Staged: address:0x%08X, size:0x%08zX\n"), _startAddress, _size);
275275
}
276-
else if (_command == U_SPIFFS) {
277-
DEBUG_UPDATER.printf_P(PSTR("SPIFFS: address:0x%08X, size:0x%08zX\n"), _startAddress, _size);
276+
else if (_command == U_FS) {
277+
DEBUG_UPDATER.printf_P(PSTR("Filesystem: address:0x%08X, size:0x%08zX\n"), _startAddress, _size);
278278
#endif
279279
}
280280

@@ -386,7 +386,7 @@ bool UpdaterClass::_verifyHeader(uint8_t data) {
386386
return false;
387387
}
388388
return true;
389-
} else if(_command == U_SPIFFS) {
389+
} else if(_command == U_FS) {
390390
// no check of SPIFFS possible with first byte.
391391
return true;
392392
}
@@ -420,7 +420,7 @@ bool UpdaterClass::_verifyEnd() {
420420
}
421421

422422
return true;
423-
} else if(_command == U_SPIFFS) {
423+
} else if(_command == U_FS) {
424424
// SPIFFS is already over written checks make no sense any more.
425425
return true;
426426
}

cores/esp8266/Updater.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define UPDATE_ERROR_SIGN (12)
2121

2222
#define U_FLASH 0
23-
#define U_SPIFFS 100
23+
#define U_FS 100
2424
#define U_AUTH 200
2525

2626
#ifdef DEBUG_ESP_UPDATER

0 commit comments

Comments
 (0)