Skip to content

Add LittleFS as an optional filesystem, API compatible w/SPIFFS (but not on-flash-format compatible) #5511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
cd03a6f
Add LittleFS as internal flash filesystem
earlephilhower Dec 15, 2018
3d24167
Merge branch 'master' into littlefs
earlephilhower Jan 7, 2019
c22b9d4
Fix up merge blank line issue
Jan 7, 2019
a42bc48
Merge in the FSConfig changs from SDFS PR
Jan 7, 2019
8016386
Merge branch 'master' into littlefs
earlephilhower Jan 9, 2019
4acc649
Merge branch 'master' into littlefs
earlephilhower Feb 27, 2019
e3e8de3
Fix merge errors
earlephilhower Feb 27, 2019
13e985c
Update to use v2-alpha branch
earlephilhower Mar 1, 2019
3340b95
V2 of littlefs is now in upstream/master
earlephilhower Apr 11, 2019
31e925a
Update test to support non-creation-ordered files
earlephilhower Apr 11, 2019
49f4618
Merge branch 'master' of https://github.com/esp8266/Arduino into litt…
earlephilhower Apr 11, 2019
ddafe05
Fix LittleFS.truncate implementation
earlephilhower Apr 11, 2019
da1383c
Merge branch 'master' into littlefs
earlephilhower Apr 11, 2019
d7fe68d
Fix SDFS tests
earlephilhower Apr 12, 2019
90ea0ba
Merge branch 'master' into littlefs
earlephilhower Apr 12, 2019
c3ac058
Update to point to mklittlefs v2
earlephilhower Apr 12, 2019
e3e6860
Remove extra FS::write(const char *s) method
earlephilhower Apr 12, 2019
4a827a5
Minimize spurious differences from master
earlephilhower Apr 12, 2019
0874498
Dramatically reduce memory usage
earlephilhower Apr 14, 2019
6b33dcf
Merge branch 'master' into littlefs
earlephilhower Apr 14, 2019
dc3e14a
Add @d-a-v's host emulation for LittleFS
earlephilhower Apr 15, 2019
7435409
Merge branch 'master' into littlefs
earlephilhower Apr 15, 2019
6f9ac16
Fix SW Serial library version
earlephilhower Apr 15, 2019
9d2ffed
Merge branch 'master' into littlefs
earlephilhower Apr 18, 2019
78fbb64
Fix free space reporting
earlephilhower Apr 18, 2019
88a9cf6
Update littlefs to latest upstream
earlephilhower Apr 23, 2019
c0594ef
Merge branch 'master' into littlefs
earlephilhower Apr 23, 2019
7bdfaf0
Remove sdfat version included by accident
earlephilhower May 1, 2019
fbe1d48
Merge branch 'master' into littlefs
earlephilhower May 1, 2019
f0fb051
Update SDFAT to include MOCK changes required
earlephilhower May 1, 2019
d1666e5
Merge branch 'master' into littlefs
earlephilhower May 1, 2019
8e732d2
Merge branch 'master' into littlefs
earlephilhower May 2, 2019
e7d5a45
Merge branch 'master' into littlefs
earlephilhower May 12, 2019
a70facc
Merge branch 'master' of https://github.com/esp8266/Arduino into litt…
earlephilhower May 12, 2019
a6d17e3
Merge branch 'littlefs' of https://github.com/earlephilhower/Arduino …
earlephilhower May 13, 2019
3ab6d76
Merge branch 'master' into littlefs
earlephilhower May 13, 2019
e016613
Update to include SD.h test of file append
earlephilhower May 17, 2019
edbd1ba
Merge branch 'master' into littlefs
earlephilhower May 17, 2019
bd57b00
Merge branch 'master' into littlefs
earlephilhower May 20, 2019
bf06492
Merge branch 'master' into littlefs
d-a-v May 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
[submodule "libraries/SoftwareSerial"]
path = libraries/SoftwareSerial
url = https://github.com/plerup/espsoftwareserial.git
[submodule "libraries/LittleFS/lib/littlefs"]
path = libraries/LittleFS/lib/littlefs
url = https://github.com/ARMmbed/littlefs.git
[submodule "libraries/ESP8266SdFat"]
path = libraries/ESP8266SdFat
url = https://github.com/earlephilhower/ESP8266SdFat.git
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,5 @@ ESP8266 core files are licensed under LGPL.
[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).

[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).

[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).
4 changes: 2 additions & 2 deletions cores/esp8266/Esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,14 @@ uint32_t EspClass::getSketchSize() {
return result;
}

extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _FS_start;

uint32_t EspClass::getFreeSketchSpace() {

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

#ifdef DEBUG_SERIAL
DEBUG_SERIAL.printf("usedSize=%u freeSpaceStart=%u freeSpaceEnd=%u\r\n", usedSize, freeSpaceStart, freeSpaceEnd);
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class File : public Stream
int read() override;
int peek() override;
void flush() override;
size_t readBytes(char *buffer, size_t length) override {
size_t readBytes(char *buffer, size_t length) override {
return read((uint8_t*)buffer, length);
}
size_t read(uint8_t* buf, size_t size);
Expand Down
20 changes: 10 additions & 10 deletions cores/esp8266/Updater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern "C" {
#include "user_interface.h"
}

extern "C" uint32_t _SPIFFS_start;
extern "C" uint32_t _FS_start;

UpdaterClass::UpdaterClass()
: _async(false)
Expand Down Expand Up @@ -87,8 +87,8 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
}

#ifdef DEBUG_UPDATER
if (command == U_SPIFFS) {
DEBUG_UPDATER.println(F("[begin] Update SPIFFS."));
if (command == U_FS) {
DEBUG_UPDATER.println(F("[begin] Update Filesystem."));
}
#endif

Expand All @@ -112,7 +112,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
//size of current sketch rounded to a sector
size_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
//address of the end of the space available for sketch and update
uintptr_t updateEndAddress = (uintptr_t)&_SPIFFS_start - 0x40200000;
uintptr_t updateEndAddress = (uintptr_t)&_FS_start - 0x40200000;
//size of the update rounded to a sector
size_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
//address where we will start writing the update
Expand All @@ -130,8 +130,8 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
return false;
}
}
else if (command == U_SPIFFS) {
updateStartAddress = (uintptr_t)&_SPIFFS_start - 0x40200000;
else if (command == U_FS) {
updateStartAddress = (uintptr_t)&_FS_start - 0x40200000;
}
else {
// unknown command
Expand Down Expand Up @@ -279,8 +279,8 @@ bool UpdaterClass::end(bool evenIfRemaining){
#ifdef DEBUG_UPDATER
DEBUG_UPDATER.printf_P(PSTR("Staged: address:0x%08X, size:0x%08zX\n"), _startAddress, _size);
}
else if (_command == U_SPIFFS) {
DEBUG_UPDATER.printf_P(PSTR("SPIFFS: address:0x%08X, size:0x%08zX\n"), _startAddress, _size);
else if (_command == U_FS) {
DEBUG_UPDATER.printf_P(PSTR("Filesystem: address:0x%08X, size:0x%08zX\n"), _startAddress, _size);
#endif
}

Expand Down Expand Up @@ -392,7 +392,7 @@ bool UpdaterClass::_verifyHeader(uint8_t data) {
return false;
}
return true;
} else if(_command == U_SPIFFS) {
} else if(_command == U_FS) {
// no check of SPIFFS possible with first byte.
return true;
}
Expand Down Expand Up @@ -426,7 +426,7 @@ bool UpdaterClass::_verifyEnd() {
}

return true;
} else if(_command == U_SPIFFS) {
} else if(_command == U_FS) {
// SPIFFS is already over written checks make no sense any more.
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion cores/esp8266/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define UPDATE_ERROR_SIGN (12)

#define U_FLASH 0
#define U_SPIFFS 100
#define U_FS 100
#define U_AUTH 200

#ifdef DEBUG_ESP_UPDATER
Expand Down
30 changes: 15 additions & 15 deletions cores/esp8266/spiffs_hal.cpp → cores/esp8266/flash_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#include <Arduino.h>
#include <stdlib.h>
#include <algorithm>
#include "spiffs/spiffs.h"
#include "debug.h"
#include "flash_hal.h"

extern "C" {
#include "c_types.h"
Expand All @@ -42,10 +42,10 @@ alignedBegin: ^
alignedEnd: ^
*/

int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
int32_t flash_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
optimistic_yield(10000);

uint32_t result = SPIFFS_OK;
uint32_t result = FLASH_HAL_OK;
uint32_t alignedBegin = (addr + 3) & (~3);
uint32_t alignedEnd = (addr + size) & (~3);
if (alignedEnd < alignedBegin) {
Expand All @@ -58,7 +58,7 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
if (!ESP.flashRead(alignedBegin - 4, &tmp, 4)) {
DEBUGV("_spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n",
__LINE__, addr, size, alignedBegin, alignedEnd);
return SPIFFS_ERR_INTERNAL;
return FLASH_HAL_READ_ERROR;
}
memcpy(dst, ((uint8_t*) &tmp) + 4 - nb, nb);
}
Expand All @@ -68,7 +68,7 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
alignedEnd - alignedBegin)) {
DEBUGV("_spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n",
__LINE__, addr, size, alignedBegin, alignedEnd);
return SPIFFS_ERR_INTERNAL;
return FLASH_HAL_READ_ERROR;
}
}

Expand All @@ -78,7 +78,7 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
if (!ESP.flashRead(alignedEnd, &tmp, 4)) {
DEBUGV("_spif_read(%d) addr=%x size=%x ab=%x ae=%x\r\n",
__LINE__, addr, size, alignedBegin, alignedEnd);
return SPIFFS_ERR_INTERNAL;
return FLASH_HAL_READ_ERROR;
}

memcpy(dst + size - nb, &tmp, nb);
Expand All @@ -99,7 +99,7 @@ int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {

static const int UNALIGNED_WRITE_BUFFER_SIZE = 512;

int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
int32_t flash_hal_write(uint32_t addr, uint32_t size, const uint8_t *src) {
optimistic_yield(10000);

uint32_t alignedBegin = (addr + 3) & (~3);
Expand All @@ -116,7 +116,7 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
if (!ESP.flashWrite(alignedBegin - 4, (uint32_t*) tmp, 4)) {
DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n",
__LINE__, addr, size, alignedBegin, alignedEnd);
return SPIFFS_ERR_INTERNAL;
return FLASH_HAL_WRITE_ERROR;
}
}

Expand All @@ -128,7 +128,7 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
alignedEnd - alignedBegin)) {
DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n",
__LINE__, addr, size, alignedBegin, alignedEnd);
return SPIFFS_ERR_INTERNAL;
return FLASH_HAL_WRITE_ERROR;
}
}
else {
Expand All @@ -140,7 +140,7 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
if (!ESP.flashWrite(alignedBegin, (uint32_t*) buf, willCopy)) {
DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n",
__LINE__, addr, size, alignedBegin, alignedEnd);
return SPIFFS_ERR_INTERNAL;
return FLASH_HAL_WRITE_ERROR;
}

sizeLeft -= willCopy;
Expand All @@ -158,14 +158,14 @@ int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
if (!ESP.flashWrite(alignedEnd, &tmp, 4)) {
DEBUGV("_spif_write(%d) addr=%x size=%x ab=%x ae=%x\r\n",
__LINE__, addr, size, alignedBegin, alignedEnd);
return SPIFFS_ERR_INTERNAL;
return FLASH_HAL_WRITE_ERROR;
}
}

return SPIFFS_OK;
return FLASH_HAL_OK;
}

int32_t spiffs_hal_erase(uint32_t addr, uint32_t size) {
int32_t flash_hal_erase(uint32_t addr, uint32_t size) {
if ((size & (SPI_FLASH_SEC_SIZE - 1)) != 0 ||
(addr & (SPI_FLASH_SEC_SIZE - 1)) != 0) {
DEBUGV("_spif_erase called with addr=%x, size=%d\r\n", addr, size);
Expand All @@ -177,8 +177,8 @@ int32_t spiffs_hal_erase(uint32_t addr, uint32_t size) {
optimistic_yield(10000);
if (!ESP.flashEraseSector(sector + i)) {
DEBUGV("_spif_erase addr=%x size=%d i=%d\r\n", addr, size, i);
return SPIFFS_ERR_INTERNAL;
return FLASH_HAL_ERASE_ERROR;
}
}
return SPIFFS_OK;
return FLASH_HAL_OK;
}
49 changes: 49 additions & 0 deletions cores/esp8266/flash_hal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef flash_hal_h
#define flash_hal_h

/*
flash_hal.h - API for accessing raw flash for filesystems
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.

This code was influenced by NodeMCU and Sming libraries, and first version of
Arduino wrapper written by Hristo Gochkov.

This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef ARDUINO
extern "C" uint32_t _FS_start;
extern "C" uint32_t _FS_end;
extern "C" uint32_t _FS_page;
extern "C" uint32_t _FS_block;

#define FS_PHYS_ADDR ((uint32_t) (&_FS_start) - 0x40200000)
#define FS_PHYS_SIZE ((uint32_t) (&_FS_end) - (uint32_t) (&_FS_start))
#define FS_PHYS_PAGE ((uint32_t) &_FS_page)
#define FS_PHYS_BLOCK ((uint32_t) &_FS_block)
#endif

// Return values of the following functions
#define FLASH_HAL_OK (0)
#define FLASH_HAL_READ_ERROR (-1)
#define FLASH_HAL_WRITE_ERROR (-2)
#define FLASH_HAL_ERASE_ERROR (-3)

extern int32_t flash_hal_write(uint32_t addr, uint32_t size, const uint8_t *src);
extern int32_t flash_hal_erase(uint32_t addr, uint32_t size);
extern int32_t flash_hal_read(uint32_t addr, uint32_t size, uint8_t *dst);

#endif // !defined(flash_hal_h)
4 changes: 2 additions & 2 deletions cores/esp8266/spiffs/spiffs.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct spiffs_t;
/* spi read call function type */
typedef s32_t (*spiffs_read)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *dst);
/* spi write call function type */
typedef s32_t (*spiffs_write)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *src);
typedef s32_t (*spiffs_write)(struct spiffs_t *fs, u32_t addr, u32_t size, const u8_t *src);
/* spi erase call function type */
typedef s32_t (*spiffs_erase)(struct spiffs_t *fs, u32_t addr, u32_t size);

Expand All @@ -93,7 +93,7 @@ typedef s32_t (*spiffs_erase)(struct spiffs_t *fs, u32_t addr, u32_t size);
/* spi read call function type */
typedef s32_t (*spiffs_read)(u32_t addr, u32_t size, u8_t *dst);
/* spi write call function type */
typedef s32_t (*spiffs_write)(u32_t addr, u32_t size, u8_t *src);
typedef s32_t (*spiffs_write)(u32_t addr, u32_t size, const u8_t *src);
/* spi erase call function type */
typedef s32_t (*spiffs_erase)(u32_t addr, u32_t size);
#endif // SPIFFS_HAL_CALLBACK_EXTRA
Expand Down
14 changes: 9 additions & 5 deletions cores/esp8266/spiffs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

using namespace fs;

namespace spiffs_impl {

FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode)
{
if (!isSpiffsFilenameValid(path)) {
Expand Down Expand Up @@ -108,6 +110,8 @@ bool isSpiffsFilenameValid(const char* name)
return len > 0 && len < SPIFFS_OBJ_NAME_LEN;
}

}; // namespace

// these symbols should be defined in the linker script for each flash layout
#ifndef CORE_MOCK
#ifdef ARDUINO
Expand All @@ -116,11 +120,11 @@ bool isSpiffsFilenameValid(const char* name)
#endif

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)
FS SPIFFS = FS(FSImplPtr(new SPIFFSImpl(
SPIFFS_PHYS_ADDR,
SPIFFS_PHYS_SIZE,
SPIFFS_PHYS_PAGE,
SPIFFS_PHYS_BLOCK,
FS SPIFFS = FS(FSImplPtr(new spiffs_impl::SPIFFSImpl(
FS_PHYS_ADDR,
FS_PHYS_SIZE,
FS_PHYS_PAGE,
FS_PHYS_BLOCK,
SPIFFS_MAX_OPEN_FILES)));
#endif // ARDUINO
#endif // !CORE_MOCK
Expand Down
Loading