-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Adding LITTLEFS w/o rebuild the IDF #4096
Changes from 5 commits
7a66810
99c854c
0602a04
0ab506c
4f1f7ad
d056c40
72cf6b6
5a7ac01
d882cfa
abe9ea3
6bf0962
c5646b0
5bd5698
158f63d
82c6257
e9cee69
056a5ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
#include <Arduino.h> | ||
#include "FS.h" | ||
#include <LITTLEFS.h> | ||
|
||
/* You only need to format SPIFFS the first time you run a | ||
test or else use the LITTLEFS plugin to create a partition | ||
https://github.com/me-no-dev/arduino-esp32fs-plugin */ | ||
#define FORMAT_LITTLEFS_IF_FAILED true | ||
|
||
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ | ||
Serial.printf("Listing directory: %s\r\n", dirname); | ||
|
||
File root = fs.open(dirname); | ||
if(!root){ | ||
Serial.println("- failed to open directory"); | ||
return; | ||
} | ||
if(!root.isDirectory()){ | ||
Serial.println(" - not a directory"); | ||
return; | ||
} | ||
|
||
File file = root.openNextFile(); | ||
while(file){ | ||
if(file.isDirectory()){ | ||
Serial.print(" DIR : "); | ||
Serial.println(file.name()); | ||
if(levels){ | ||
listDir(fs, file.name(), levels -1); | ||
} | ||
} else { | ||
Serial.print(" FILE: "); | ||
Serial.print(file.name()); | ||
Serial.print("\tSIZE: "); | ||
Serial.println(file.size()); | ||
} | ||
file = root.openNextFile(); | ||
} | ||
} | ||
|
||
void readFile(fs::FS &fs, const char * path){ | ||
Serial.printf("Reading file: %s\r\n", path); | ||
|
||
File file = fs.open(path); | ||
if(!file || file.isDirectory()){ | ||
Serial.println("- failed to open file for reading"); | ||
return; | ||
} | ||
|
||
Serial.println("- read from file:"); | ||
while(file.available()){ | ||
Serial.write(file.read()); | ||
} | ||
file.close(); | ||
} | ||
|
||
void writeFile(fs::FS &fs, const char * path, const char * message){ | ||
Serial.printf("Writing file: %s\r\n", path); | ||
|
||
File file = fs.open(path, FILE_WRITE); | ||
if(!file){ | ||
Serial.println("- failed to open file for writing"); | ||
return; | ||
} | ||
if(file.print(message)){ | ||
Serial.println("- file written"); | ||
} else { | ||
Serial.println("- write failed"); | ||
} | ||
file.close(); | ||
} | ||
|
||
void appendFile(fs::FS &fs, const char * path, const char * message){ | ||
Serial.printf("Appending to file: %s\r\n", path); | ||
|
||
File file = fs.open(path, FILE_APPEND); | ||
if(!file){ | ||
Serial.println("- failed to open file for appending"); | ||
return; | ||
} | ||
if(file.print(message)){ | ||
Serial.println("- message appended"); | ||
} else { | ||
Serial.println("- append failed"); | ||
} | ||
file.close(); | ||
} | ||
|
||
void renameFile(fs::FS &fs, const char * path1, const char * path2){ | ||
Serial.printf("Renaming file %s to %s\r\n", path1, path2); | ||
if (fs.rename(path1, path2)) { | ||
Serial.println("- file renamed"); | ||
} else { | ||
Serial.println("- rename failed"); | ||
} | ||
} | ||
|
||
void deleteFile(fs::FS &fs, const char * path){ | ||
Serial.printf("Deleting file: %s\r\n", path); | ||
if(fs.remove(path)){ | ||
Serial.println("- file deleted"); | ||
} else { | ||
Serial.println("- delete failed"); | ||
} | ||
} | ||
|
||
void testFileIO(fs::FS &fs, const char * path){ | ||
Serial.printf("Testing file I/O with %s\r\n", path); | ||
|
||
static uint8_t buf[512]; | ||
size_t len = 0; | ||
File file = fs.open(path, FILE_WRITE); | ||
if(!file){ | ||
Serial.println("- failed to open file for writing"); | ||
return; | ||
} | ||
|
||
size_t i; | ||
Serial.print("- writing" ); | ||
uint32_t start = millis(); | ||
for(i=0; i<2048; i++){ | ||
if ((i & 0x001F) == 0x001F){ | ||
Serial.print("."); | ||
} | ||
file.write(buf, 512); | ||
} | ||
Serial.println(""); | ||
uint32_t end = millis() - start; | ||
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end); | ||
file.close(); | ||
|
||
file = fs.open(path); | ||
start = millis(); | ||
end = start; | ||
i = 0; | ||
if(file && !file.isDirectory()){ | ||
len = file.size(); | ||
size_t flen = len; | ||
start = millis(); | ||
Serial.print("- reading" ); | ||
while(len){ | ||
size_t toRead = len; | ||
if(toRead > 512){ | ||
toRead = 512; | ||
} | ||
file.read(buf, toRead); | ||
if ((i++ & 0x001F) == 0x001F){ | ||
Serial.print("."); | ||
} | ||
len -= toRead; | ||
} | ||
Serial.println(""); | ||
end = millis() - start; | ||
Serial.printf("- %u bytes read in %u ms\r\n", flen, end); | ||
file.close(); | ||
} else { | ||
Serial.println("- failed to open file for reading"); | ||
} | ||
} | ||
|
||
void setup(){ | ||
Serial.begin(115200); | ||
if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED)){ | ||
Serial.println("LITTLEFS Mount Failed"); | ||
return; | ||
} | ||
|
||
listDir(LITTLEFS, "/", 0); | ||
writeFile(LITTLEFS, "/hello.txt", "Hello "); | ||
appendFile(LITTLEFS, "/hello.txt", "World!\r\n"); | ||
readFile(LITTLEFS, "/hello.txt"); | ||
renameFile(LITTLEFS, "/hello.txt", "/foo.txt"); | ||
readFile(LITTLEFS, "/foo.txt"); | ||
deleteFile(LITTLEFS, "/foo.txt"); | ||
testFileIO(LITTLEFS, "/test.txt"); | ||
deleteFile(LITTLEFS, "/test.txt"); | ||
Serial.println( "Test complete" ); | ||
} | ||
|
||
void loop(){ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=LITTLEFS | ||
version=1.0 | ||
author=LL | ||
maintainer=LL | ||
sentence=ESP32 LITTLEFS File System | ||
paragraph= | ||
category=Data Storage | ||
url= | ||
architectures=esp32 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//esp_err_t esp_vfs_littlefs_register(const esp_vfs_littlefs_conf_t * conf); | ||
|
||
//esp_err_t esp_vfs_littlefs_unregister(const char* partition_label); | ||
//esp_err_t esp_littlefs_format(const char* partition_label); | ||
//esp_err_t esp_littlefs_info(const char* partition_label, size_t *total_bytes, size_t *used_bytes); | ||
|
||
//#define LFS_NAME "spiffs" | ||
static constexpr const char LFS_NAME[] = "spiffs"; | ||
|
||
#include "vfs_api.h" | ||
|
||
extern "C" { | ||
#include <sys/unistd.h> | ||
#include <sys/stat.h> | ||
#include <dirent.h> | ||
#include "esp_littlefs.h" | ||
} | ||
|
||
#include "LITTLEFS.h" | ||
|
||
using namespace fs; | ||
|
||
class LITTLEFSImpl : public VFSImpl | ||
{ | ||
public: | ||
LITTLEFSImpl(); | ||
virtual ~LITTLEFSImpl() { } | ||
virtual bool exists(const char* path); | ||
}; | ||
|
||
LITTLEFSImpl::LITTLEFSImpl() | ||
{ | ||
} | ||
|
||
bool LITTLEFSImpl::exists(const char* path) | ||
{ | ||
File f = open(path, "r"); | ||
return (f == true) && !f.isDirectory(); | ||
} | ||
|
||
LITTLEFSFS::LITTLEFSFS() : FS(FSImplPtr(new LITTLEFSImpl())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to:
and remove the definition of class LITTLEFSImpl. The customization is not necessary for LITTLEFS as the default behavior from VFSImpl() will work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
{ | ||
|
||
} | ||
|
||
bool LITTLEFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles) | ||
{ | ||
if(esp_littlefs_mounted(LFS_NAME)){ | ||
log_w("LITTLEFS Already Mounted!"); | ||
return true; | ||
} | ||
|
||
esp_vfs_littlefs_conf_t conf = { | ||
.base_path = basePath, | ||
.partition_label = LFS_NAME, | ||
//.max_files = maxOpenFiles, | ||
.format_if_mount_failed = false | ||
}; | ||
|
||
esp_err_t err = esp_vfs_littlefs_register(&conf); | ||
if(err == ESP_FAIL && formatOnFail){ | ||
if(format()){ | ||
err = esp_vfs_littlefs_register(&conf); | ||
} | ||
} | ||
if(err != ESP_OK){ | ||
log_e("Mounting LITTLEFS failed! Error: %d", err); | ||
return false; | ||
} | ||
_impl->mountpoint(basePath); | ||
return true; | ||
} | ||
|
||
void LITTLEFSFS::end() | ||
{ | ||
if(esp_littlefs_mounted(LFS_NAME)){ | ||
esp_err_t err = esp_vfs_littlefs_unregister(LFS_NAME); | ||
if(err){ | ||
log_e("Unmounting LITTLEFS failed! Error: %d", err); | ||
return; | ||
} | ||
_impl->mountpoint(NULL); | ||
} | ||
} | ||
|
||
bool LITTLEFSFS::format() | ||
{ | ||
disableCore0WDT(); | ||
esp_err_t err = esp_littlefs_format(LFS_NAME); | ||
enableCore0WDT(); | ||
if(err){ | ||
log_e("Formatting LITTLEFS failed! Error: %d", err); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
size_t LITTLEFSFS::totalBytes() | ||
{ | ||
size_t total,used; | ||
if(esp_littlefs_info(LFS_NAME, &total, &used)){ | ||
return 0; | ||
} | ||
return total; | ||
} | ||
|
||
size_t LITTLEFSFS::usedBytes() | ||
{ | ||
size_t total,used; | ||
if(esp_littlefs_info(LFS_NAME, &total, &used)){ | ||
return 0; | ||
} | ||
return used; | ||
} | ||
|
||
LITTLEFSFS LITTLEFS; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#ifndef _LITTLEFS_H_ | ||
#define _LITTLEFS_H_ | ||
|
||
#include "FS.h" | ||
|
||
namespace fs | ||
{ | ||
|
||
class LITTLEFSFS : public FS | ||
{ | ||
public: | ||
LITTLEFSFS(); | ||
bool begin(bool formatOnFail=false, const char * basePath="/littlefs", uint8_t maxOpenFiles=5); | ||
bool format(); | ||
size_t totalBytes(); | ||
size_t usedBytes(); | ||
void end(); | ||
}; | ||
|
||
} | ||
|
||
extern fs::LITTLEFSFS LITTLEFS; | ||
|
||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@me-no-dev would it be feasible to provide a default impl for VFSImpl that can be used by the various FS impls rather than each one copying/pasting the same but with a different name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm, this class can be dropped entirely.