Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ set(LIBRARY_SRCS
libraries/FFat/src/FFat.cpp
libraries/FS/src/FS.cpp
libraries/FS/src/vfs_api.cpp
libraries/LITTLEFS/src/LITTLEFS.cpp
libraries/LITTLEFS/src/esp_littlefs.c
libraries/LITTLEFS/src/lfs.c
libraries/LITTLEFS/src/lfs_util.c
libraries/LITTLEFS/src/littlefs_api.c
libraries/HTTPClient/src/HTTPClient.cpp
libraries/HTTPUpdate/src/HTTPUpdate.cpp
libraries/NetBIOS/src/NetBIOS.cpp
Expand Down Expand Up @@ -187,6 +192,7 @@ set(COMPONENT_ADD_INCLUDEDIRS
libraries/ESPmDNS/src
libraries/FFat/src
libraries/FS/src
libraries/LITTLEFS/src
libraries/HTTPClient/src
libraries/HTTPUpdate/src
libraries/NetBIOS/src
Expand Down
182 changes: 182 additions & 0 deletions libraries/LITTLEFS/examples/LittleFS_test/LittleFS_test.ino
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(){

}
9 changes: 9 additions & 0 deletions libraries/LITTLEFS/library.properties
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
131 changes: 131 additions & 0 deletions libraries/LITTLEFS/src/LITTLEFS.cpp
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
Copy link
Collaborator

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the same?

Copy link
Collaborator

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.

{
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()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to:

LITTLEFSFS::LITTLEFSFS() : FS(FSImplPtr(new VFSImpl()))

and remove the definition of class LITTLEFSImpl. The customization is not necessary for LITTLEFS as the default behavior from VFSImpl() will work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;

38 changes: 38 additions & 0 deletions libraries/LITTLEFS/src/LITTLEFS.h
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
Loading