This commit is contained in:
2026-05-22 21:52:50 +03:00
commit be7c60e4dd
1854 changed files with 583428 additions and 0 deletions
@@ -0,0 +1,286 @@
#include <Arduino.h>
#include "FS.h"
#include <LittleFS.h>
/* You only need to format LittleFS the first time you run a
test or else use the LITTLEFS plugin to create a partition
https://github.com/lorol/arduino-esp32littlefs-plugin
If you test two partitions, you need to use a custom
partition.csv file, see in the sketch folder */
//#define TWOPART
#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.path(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print("\tSIZE: ");
Serial.println(file.size());
}
file = root.openNextFile();
}
}
void createDir(fs::FS &fs, const char *path) {
Serial.printf("Creating Dir: %s\n", path);
if (fs.mkdir(path)) {
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
void removeDir(fs::FS &fs, const char *path) {
Serial.printf("Removing Dir: %s\n", path);
if (fs.rmdir(path)) {
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}
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");
}
}
// SPIFFS-like write and delete file, better use #define CONFIG_LITTLEFS_SPIFFS_COMPAT 1
void writeFile2(fs::FS &fs, const char *path, const char *message) {
if (!fs.exists(path)) {
if (strchr(path, '/')) {
Serial.printf("Create missing folders of: %s\r\n", path);
char *pathStr = strdup(path);
if (pathStr) {
char *ptr = strchr(pathStr, '/');
while (ptr) {
*ptr = 0;
fs.mkdir(pathStr);
*ptr = '/';
ptr = strchr(ptr + 1, '/');
}
}
free(pathStr);
}
}
Serial.printf("Writing file to: %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 deleteFile2(fs::FS &fs, const char *path) {
Serial.printf("Deleting file and empty folders on path: %s\r\n", path);
if (fs.remove(path)) {
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}
char *pathStr = strdup(path);
if (pathStr) {
char *ptr = strrchr(pathStr, '/');
if (ptr) {
Serial.printf("Removing all empty folders on path: %s\r\n", path);
}
while (ptr) {
*ptr = 0;
fs.rmdir(pathStr);
ptr = strrchr(pathStr, '/');
}
free(pathStr);
}
}
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 %lu 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("- %zu bytes read in %lu ms\r\n", flen, end);
file.close();
} else {
Serial.println("- failed to open file for reading");
}
}
void setup() {
Serial.begin(115200);
#ifdef TWOPART
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED, "/lfs2", 5, "part2")) {
Serial.println("part2 Mount Failed");
return;
}
appendFile(LittleFS, "/hello0.txt", "World0!\r\n");
readFile(LittleFS, "/hello0.txt");
LittleFS.end();
Serial.println("Done with part2, work with the first lfs partition...");
#endif
if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
Serial.println("LittleFS Mount Failed");
return;
}
Serial.println("SPIFFS-like write file to new path and delete it w/folders");
writeFile2(LittleFS, "/new1/new2/new3/hello3.txt", "Hello3");
listDir(LittleFS, "/", 3);
deleteFile2(LittleFS, "/new1/new2/new3/hello3.txt");
listDir(LittleFS, "/", 3);
createDir(LittleFS, "/mydir");
writeFile(LittleFS, "/mydir/hello2.txt", "Hello2");
listDir(LittleFS, "/", 1);
deleteFile(LittleFS, "/mydir/hello2.txt");
removeDir(LittleFS, "/mydir");
listDir(LittleFS, "/", 1);
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() {}
@@ -0,0 +1,8 @@
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x100000,
app1, app, ota_1, ,0x100000,
spiffs, data, spiffs, ,0x1D0000,
part2, data, spiffs, ,0x20000,
#1048576
1 # Name, Type, SubType, Offset, Size, Flags
2 nvs, data, nvs, 0x9000, 0x5000,
3 otadata, data, ota, 0xe000, 0x2000,
4 app0, app, ota_0, 0x10000, 0x100000,
5 app1, app, ota_1, ,0x100000,
6 spiffs, data, spiffs, ,0x1D0000,
7 part2, data, spiffs, ,0x20000,
8 #1048576
@@ -0,0 +1,222 @@
#include "FS.h"
//#include "SPIFFS.h"
#include "LittleFS.h"
#include <time.h>
#include <WiFi.h>
#define SPIFFS LittleFS
/* This examples uses "quick re-define" of SPIFFS to run
an existing sketch with LittleFS instead of SPIFFS
You only need to format LittleFS the first time you run a
test or else use the LittleFS plugin to create a partition
https://github.com/lorol/arduino-esp32littlefs-plugin */
#define FORMAT_LITTLEFS_IF_FAILED true
const char *ssid = "yourssid";
const char *password = "yourpass";
long timezone = 1;
byte daysavetime = 1;
void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\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.print(file.name());
time_t t = file.getLastWrite();
struct tm tmstruct;
localtime_r(&t, &tmstruct);
Serial.printf(
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min,
tmstruct.tm_sec
);
if (levels) {
listDir(fs, file.path(), levels - 1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.print(file.size());
time_t t = file.getLastWrite();
struct tm tmstruct;
localtime_r(&t, &tmstruct);
Serial.printf(
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min,
tmstruct.tm_sec
);
}
file = root.openNextFile();
}
}
void createDir(fs::FS &fs, const char *path) {
Serial.printf("Creating Dir: %s\n", path);
if (fs.mkdir(path)) {
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
void removeDir(fs::FS &fs, const char *path) {
Serial.printf("Removing Dir: %s\n", path);
if (fs.rmdir(path)) {
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}
void readFile(fs::FS &fs, const char *path) {
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
return;
}
Serial.print("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\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\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\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\n", path);
if (fs.remove(path)) {
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
}
void setup() {
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Contacting Time Server");
configTime(3600 * timezone, daysavetime * 3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
struct tm tmstruct;
delay(2000);
tmstruct.tm_year = 0;
getLocalTime(&tmstruct, 5000);
Serial.printf(
"\nNow is : %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min,
tmstruct.tm_sec
);
Serial.println("");
if (!SPIFFS.begin(FORMAT_LITTLEFS_IF_FAILED)) {
Serial.println("LittleFS Mount Failed");
return;
}
Serial.println("----list 1----");
listDir(SPIFFS, "/", 1);
Serial.println("----remove old dir----");
removeDir(SPIFFS, "/mydir");
Serial.println("----create a new dir----");
createDir(SPIFFS, "/mydir");
Serial.println("----remove the new dir----");
removeDir(SPIFFS, "/mydir");
Serial.println("----create the new again----");
createDir(SPIFFS, "/mydir");
Serial.println("----create and work with file----");
writeFile(SPIFFS, "/mydir/hello.txt", "Hello ");
appendFile(SPIFFS, "/mydir/hello.txt", "World!\n");
Serial.println("----list 2----");
listDir(SPIFFS, "/", 1);
Serial.println("----attempt to remove dir w/ file----");
removeDir(SPIFFS, "/mydir");
Serial.println("----remove dir after deleting file----");
deleteFile(SPIFFS, "/mydir/hello.txt");
removeDir(SPIFFS, "/mydir");
Serial.println("----list 3----");
listDir(SPIFFS, "/", 1);
Serial.println("Test complete");
}
void loop() {}
@@ -0,0 +1,3 @@
requires_any:
- CONFIG_SOC_WIFI_SUPPORTED=y
- CONFIG_ESP_WIFI_REMOTE_ENABLED=y
+9
View File
@@ -0,0 +1,9 @@
name=LittleFS
version=3.3.7
author=
maintainer=
sentence=LittleFS for esp32
paragraph=LittleFS for esp32
category=Data Storage
url=
architectures=esp32
+130
View File
@@ -0,0 +1,130 @@
// 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.
#include "LittleFS.h"
#ifdef CONFIG_LITTLEFS_PAGE_SIZE
#include "vfs_api.h"
extern "C" {
#include <sys/unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include "esp_littlefs.h"
}
using namespace fs;
class LittleFSImpl : public VFSImpl {
public:
LittleFSImpl();
virtual ~LittleFSImpl() {}
};
LittleFSImpl::LittleFSImpl() {}
LittleFSFS::LittleFSFS() : FS(FSImplPtr(new LittleFSImpl())), partitionLabel_(NULL) {}
LittleFSFS::~LittleFSFS() {
if (partitionLabel_) {
free(partitionLabel_);
partitionLabel_ = NULL;
}
}
bool LittleFSFS::begin(bool formatOnFail, const char *basePath, uint8_t maxOpenFiles, const char *partitionLabel) {
(void)maxOpenFiles;
if (partitionLabel_) {
free(partitionLabel_);
partitionLabel_ = NULL;
}
if (partitionLabel) {
partitionLabel_ = strdup(partitionLabel);
}
if (esp_littlefs_mounted(partitionLabel_)) {
log_w("LittleFS Already Mounted!");
return true;
}
esp_vfs_littlefs_conf_t conf = {
.base_path = basePath,
.partition_label = partitionLabel_,
.partition = NULL,
.format_if_mount_failed = false,
.read_only = false,
.dont_mount = false,
.grow_on_mount = true
};
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(partitionLabel_)) {
esp_err_t err = esp_vfs_littlefs_unregister(partitionLabel_);
if (err) {
log_e("Unmounting LittleFS failed! Error: %d", err);
return;
}
_impl->mountpoint(NULL);
}
}
bool LittleFSFS::format() {
esp_log_level_set("*", ESP_LOG_NONE);
bool wdt_active = disableCore0WDT();
esp_err_t err = esp_littlefs_format(partitionLabel_);
if (wdt_active) {
enableCore0WDT();
}
esp_log_level_set("*", (esp_log_level_t)CONFIG_LOG_DEFAULT_LEVEL);
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(partitionLabel_, &total, &used)) {
return 0;
}
return total;
}
size_t LittleFSFS::usedBytes() {
size_t total, used;
if (esp_littlefs_info(partitionLabel_, &total, &used)) {
return 0;
}
return used;
}
LittleFSFS LittleFS;
#endif /* CONFIG_LITTLEFS_PAGE_SIZE */
+46
View File
@@ -0,0 +1,46 @@
// 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 "sdkconfig.h"
#ifdef CONFIG_LITTLEFS_PAGE_SIZE
#include "FS.h"
namespace fs {
class LittleFSFS : public FS {
public:
LittleFSFS();
~LittleFSFS();
bool begin(bool formatOnFail = false, const char *basePath = "/littlefs", uint8_t maxOpenFiles = 10, const char *partitionLabel = "spiffs");
bool format();
size_t totalBytes();
size_t usedBytes();
void end();
private:
char *partitionLabel_;
};
} // namespace fs
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LITTLEFS)
extern fs::LittleFSFS LittleFS;
#endif
#endif /* CONFIG_LITTLEFS_PAGE_SIZE */
#endif