-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add FSTools with examples of how to convert between SPIFFS and LITTLEFS. #7696
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
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0fb1ebc
Add FSTools with examples of how to convert between SPIFFS and LITTLEFS.
sticilface 745db9a
Oops. Need to pass layout by reference in order to capture the corre…
sticilface cbf4073
Update FSTools.cpp
sticilface d382a33
Fix unused variable i
sticilface ee1481a
Parsed with restyle.sh. Compile with all errors.
sticilface e9d486b
Merge branch 'master' of https://github.com/sticilface/Arduino
sticilface 4afa2d4
remove unused variable
sticilface 5022cf0
fix different sign complication error
sticilface 4db1137
Merge branch 'master' into master
earlephilhower f4a42a0
Merge branch 'master' of https://github.com/esp8266/Arduino
sticilface 9feb47a
Fix indentation to spaces
sticilface 7db7ab8
Merge branch 'master' of https://github.com/sticilface/Arduino
sticilface 9f80841
Update FSTools.cpp
sticilface 84fdf15
Merge branch 'master' into master
earlephilhower 7e1fb05
Merge branch 'master' into master
d-a-v dc44227
Merge branch 'master' into master
earlephilhower 494e51f
Merge branch 'master' into master
d-a-v 319f1d0
style
d-a-v 7e9968c
style
d-a-v 80c685d
Merge branch 'master' into master
d-a-v 3c43f18
style
d-a-v dbf0795
Merge branch 'master' into master
d-a-v 6ff023c
Merge branch 'master' into master
d-a-v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add FSTools with examples of how to convert between SPIFFS and LITTLEFS.
- Loading branch information
commit 0fb1ebc3c0fddfddcd35b771df2248bc329e765c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
#include "FSTools.h" | ||
#include "LittleFS.h" | ||
#include <spiffs_api.h> | ||
#include <ESP.h> | ||
|
||
|
||
namespace FST { | ||
|
||
const layout layout_512k32 (0x40273000, 0x4027B000, 0x100, 0x1000 ); | ||
const layout layout_512k64 (0x4026B000, 0x4027B000, 0x100, 0x1000 ); | ||
const layout layout_512k128 (0x4025B000, 0x4027B000, 0x100, 0x1000 ); | ||
|
||
const layout layout_1m64 (0x402EB000, 0x402FB000, 0x100, 0x1000 ); | ||
const layout layout_1m128 (0x402DB000, 0x402FB000, 0x100, 0x1000 ); | ||
const layout layout_1m144 (0x402D7000, 0x402FB000, 0x100, 0x1000 ); | ||
const layout layout_1m160 (0x402D3000, 0x402FB000, 0x100, 0x1000 ); | ||
const layout layout_1m192 (0x402CB000, 0x402FB000, 0x100, 0x1000 ); | ||
const layout layout_1m256 (0x402BB000, 0x402FB000, 0x100, 0x1000 ); | ||
const layout layout_1m512 (0x4027B000, 0x402FB000, 0x100, 0x2000 ); | ||
|
||
const layout layout_2m64 (0x403F0000, 0x403FB000, 0x100, 0x1000 ); | ||
const layout layout_2m128 (0x403E0000, 0x403FB000, 0x100, 0x1000 ); | ||
const layout layout_2m256 (0x403C0000, 0x403FB000, 0x100, 0x1000 ); | ||
const layout layout_2m512 (0x40380000, 0x403FA000, 0x100, 0x2000 ); | ||
const layout layout_2m1m (0x40300000, 0x403FA000, 0x100, 0x2000 ); | ||
|
||
const layout layout_4m1m (0x40500000, 0x405FA000, 0x100, 0x2000 ); | ||
const layout layout_4m2m (0x40400000, 0x405FA000, 0x100, 0x2000 ); | ||
const layout layout_4m3m (0x40300000, 0x405FA000, 0x100, 0x2000 ); | ||
|
||
const layout layout_8m6m (0x40400000, 0x409FA000, 0x100, 0x2000 ); | ||
const layout layout_8m7m (0x40300000, 0x409FA000, 0x100, 0x2000 ); | ||
|
||
const layout layout_16m14m (0x40400000, 0x411FA000, 0x100, 0x2000 ); | ||
const layout layout_16m15m (0x40300000, 0x411FA000, 0x100, 0x2000 ); | ||
|
||
}; | ||
|
||
FSTools::FSTools() | ||
{ | ||
|
||
} | ||
|
||
FSTools::~FSTools() | ||
{ | ||
reset(); | ||
} | ||
|
||
|
||
bool FSTools::attemptToMountFS(fs::FS & fs) | ||
{ | ||
LittleFSConfig littleFSCfg(false); | ||
SPIFFSConfig SPIFFSCfg(false); | ||
// try to apply the "safe" no format config to the FS... doesn't matter which.. just need one to apply.. | ||
if (!fs.setConfig(littleFSCfg) && ! fs.setConfig(SPIFFSCfg) ) | ||
{ | ||
return false; | ||
} | ||
return fs.begin(); | ||
} | ||
|
||
|
||
bool FSTools::mountAlternativeFS( FST::FS_t type, const FST::layout layout, bool keepMounted ) | ||
{ | ||
FSConfig * pCfg{nullptr}; | ||
LittleFSConfig littleFSCfg(false); | ||
SPIFFSConfig SPIFFSCfg(false); | ||
reset(); | ||
|
||
switch (type) { | ||
case FST::SPIFFS : { | ||
_pFS.reset( new FS(FSImplPtr(new spiffs_impl::SPIFFSImpl (_getStartAddr(layout) , _getSize(layout) , layout.page, layout.block, 5))) ); | ||
pCfg = &SPIFFSCfg; | ||
break; | ||
} | ||
case FST::LITTLEFS : { | ||
_pFS.reset( new FS(FSImplPtr(new littlefs_impl::LittleFSImpl(_getStartAddr(layout) , _getSize(layout) , layout.page, layout.block, 5))) ); | ||
pCfg = &littleFSCfg; | ||
break; | ||
} | ||
}; | ||
|
||
if (_pFS && pCfg && _pFS->setConfig(*pCfg) && _pFS->begin()) { | ||
if (!keepMounted) { | ||
_pFS->end(); | ||
} | ||
_mounted = true; | ||
_layout = &layout; | ||
return true; | ||
} | ||
|
||
if (_pFS) { | ||
_pFS.reset(); | ||
} | ||
_mounted = false; | ||
return false; | ||
|
||
}; | ||
|
||
|
||
bool FSTools::mounted() | ||
{ | ||
return _mounted; | ||
}; | ||
|
||
|
||
|
||
|
||
bool FSTools::moveFS(fs::FS & destinationFS) | ||
{ | ||
uint32_t sourceFileCount = 0; | ||
uint32_t sourceByteTotal = 0; | ||
bool result = false; | ||
|
||
// do not init new fs... until old one is copied to test... | ||
if (!_mounted || !_pFS) { | ||
//Serial.println("Source FS not mounted"); | ||
return false; | ||
} | ||
|
||
|
||
uint32_t startSector = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1)); | ||
//uint32_t endSector = (uint32_t)&_FS_start - FST::lowestSPIFFSstartAddr; | ||
uint32_t lowestFSStart = 0x40300000; | ||
|
||
if (_layout) { | ||
lowestFSStart = _layout->startAddr; | ||
} | ||
|
||
uint32_t endSector = lowestFSStart - 0x40200000; | ||
|
||
uint32_t tempFSsize = endSector - startSector; | ||
|
||
//Serial.printf("TempFS: start: %u, end: %u, size: %u, sketchSize = %u, _FS_start = %u\n", startSector, endSector, tempFSsize, ESP.getSketchSize(), (uint32_t)&_FS_start ); | ||
|
||
fileListIterator(*_pFS, "/", [&sourceFileCount, &sourceByteTotal, this](File & f) { | ||
if (f) { | ||
sourceFileCount++; | ||
sourceByteTotal += f.size(); | ||
_dumpFileInfo(f); | ||
} | ||
}); | ||
|
||
//Serial.printf("%u Files Found Total Size = %u\n", sourceFileCount, sourceByteTotal); | ||
//Serial.printf("Size of dummy FS = %u\n", tempFSsize); | ||
|
||
FS tempFS = FS(FSImplPtr(new littlefs_impl::LittleFSImpl(startSector, tempFSsize, FS_PHYS_PAGE, FS_PHYS_BLOCK, 5))); | ||
|
||
if (tempFS.format() && tempFS.begin()) { | ||
if (_copyFS(*_pFS,tempFS)) { | ||
//Serial.println("Files copied to temp File System"); | ||
reset(); | ||
sticilface marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (destinationFS.format() && destinationFS.begin()) // must format then mount the new FS | ||
{ | ||
if (_copyFS(tempFS, destinationFS)) { | ||
//Serial.println("Files copied back to new FS"); | ||
result = true; | ||
} | ||
} else { | ||
//Serial.println("Error Mounting "); | ||
} | ||
} else { | ||
//Serial.println("Copy Failed"); | ||
} | ||
tempFS.end(); | ||
} else { | ||
//Serial.println("Failed to begin() TempFS"); | ||
} | ||
return result; | ||
}; | ||
|
||
void FSTools::reset() | ||
{ | ||
_mounted = false; | ||
_layout = nullptr; | ||
if (_pFS) { | ||
_pFS->end(); | ||
_pFS.reset(); | ||
} | ||
} | ||
|
||
void FSTools::fileListIterator(FS & fs, const char * dirName, FST::FileCb Cb ) | ||
{ | ||
Dir dir = fs.openDir(dirName); | ||
while (dir.next()) { | ||
//const char * fileName = dir.fileName(); | ||
if (dir.isFile()) { | ||
File f = dir.openFile("r"); | ||
if (Cb) { | ||
Cb(f); | ||
} | ||
} else { | ||
fileListIterator(fs, dir.fileName().c_str() , Cb); | ||
} | ||
} | ||
} | ||
|
||
|
||
uint32_t FSTools::_getStartAddr(const FST::layout & layout) | ||
{ | ||
return ( layout.startAddr - 0x40200000 ); | ||
} | ||
|
||
uint32_t FSTools::_getSize(const FST::layout & layout) | ||
{ | ||
return (layout.endAddr - layout.startAddr ); | ||
} | ||
|
||
|
||
void FSTools::_dumpFileInfo(File & f) | ||
{ | ||
if (f) { | ||
//Serial.printf_P(PSTR(" File: %-30s [%8uB]\n"), f.fullName(), f.size() ); | ||
} | ||
} | ||
|
||
bool FSTools::_copyFS(FS & sourceFS, FS & destFS) | ||
{ | ||
uint32_t sourceFileCount = 0; | ||
uint32_t sourceByteTotal = 0; | ||
|
||
fileListIterator(sourceFS, "/", [&sourceFileCount, &sourceByteTotal](File & f) { | ||
if (f) { | ||
sourceFileCount++; | ||
sourceByteTotal += f.size(); | ||
} | ||
}); | ||
|
||
size_t count = 0; | ||
fileListIterator(sourceFS, "/", [&count, &destFS](File & sourceFile){ | ||
if (sourceFile) { | ||
File destFile = destFS.open(sourceFile.fullName(), "w"); | ||
bool copied = false; | ||
if (destFile) { | ||
destFile.setTimeout(5000); | ||
size_t written = destFile.write(sourceFile); | ||
if (written == sourceFile.size()) { | ||
copied = true; | ||
count++; | ||
} | ||
} | ||
destFile.close(); | ||
sourceFile.close(); | ||
} | ||
}); | ||
|
||
return (count == sourceFileCount); | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#pragma once | ||
|
||
#include <FS.h> | ||
#include <memory> | ||
#include <functional> | ||
/* | ||
|
||
A temporary FS is made between the END of the sketch... and the start of the partition you try to mount, to maximise the available space for copying the FS. | ||
The WORST case this is at 0x40300000 which is for a 3m FS on 4m flash.. leaving 460Kb for copying. | ||
|
||
|
||
I have not worked out a way to prevent linking of ALL the layouts except to use #ifdef guards. Ideas welcome as it uses 400B ROM. | ||
*/ | ||
|
||
namespace FST { | ||
|
||
struct layout { | ||
layout(uint32_t s, uint32_t e, uint32_t p, uint32_t b) : startAddr(s), endAddr(e), page(p), block(b) {}; | ||
uint32_t startAddr{0}; | ||
uint32_t endAddr{0}; | ||
uint32_t page{0}; | ||
uint32_t block{0}; | ||
}; | ||
|
||
enum FS_t : uint8_t { | ||
SPIFFS, | ||
LITTLEFS | ||
}; | ||
|
||
extern const layout layout_512k32; | ||
sticilface marked this conversation as resolved.
Show resolved
Hide resolved
|
||
extern const layout layout_512k64; | ||
extern const layout layout_512k128; | ||
|
||
extern const layout layout_1m64; | ||
extern const layout layout_1m128; | ||
extern const layout layout_1m144; | ||
extern const layout layout_1m160; | ||
extern const layout layout_1m192; | ||
extern const layout layout_1m256; | ||
extern const layout layout_1m512; | ||
|
||
extern const layout layout_2m64; | ||
extern const layout layout_2m128; | ||
extern const layout layout_2m256; | ||
extern const layout layout_2m512; | ||
extern const layout layout_2m1m; | ||
|
||
extern const layout layout_4m1m; | ||
extern const layout layout_4m2m; | ||
extern const layout layout_4m3m; | ||
|
||
extern const layout layout_8m6m; | ||
extern const layout layout_8m7m; | ||
|
||
extern const layout layout_16m14m; | ||
extern const layout layout_16m15m; | ||
|
||
|
||
typedef std::function<void(File & f)> FileCb; | ||
|
||
|
||
}; | ||
|
||
|
||
|
||
//376884 | ||
//376452 | ||
|
||
|
||
class FSTools { | ||
public: | ||
|
||
|
||
|
||
FSTools(); | ||
~FSTools(); | ||
bool attemptToMountFS(fs::FS & fs); | ||
bool mountAlternativeFS( FST::FS_t type, const FST::layout layout, bool keepMounted = false ); | ||
bool mounted(); | ||
bool moveFS(fs::FS & destinationFS); | ||
void reset(); | ||
void fileListIterator(FS & fs, const char * dirName, FST::FileCb Cb ); | ||
|
||
private: | ||
uint32_t _getStartAddr(const FST::layout & layout); | ||
uint32_t _getSize(const FST::layout & layout); | ||
void _dumpFileInfo(File & f); | ||
bool _copyFS(FS & sourceFS, FS & destFS); | ||
|
||
std::unique_ptr<fs::FS> _pFS; | ||
bool _mounted{false}; | ||
const FST::layout * _layout{nullptr}; | ||
|
||
|
||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.