Skip to content

Commit a481fac

Browse files
committed
Add FFat FS driver.
1 parent 5e936c9 commit a481fac

File tree

5 files changed

+584
-0
lines changed

5 files changed

+584
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
#include "FS.h"
2+
#include "FFat.h"
3+
4+
// This file should be compiled with 'Partition Scheme' (in Tools menu)
5+
// set to 'Default with ffat' if you have a 4MB ESP32 dev module or
6+
// set to '16M Fat' if you have a 16MB ESP32 dev module.
7+
8+
// You only need to format FFat the first time you run a test
9+
#define FORMAT_FFAT true
10+
11+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
12+
Serial.printf("Listing directory: %s\r\n", dirname);
13+
14+
File root = fs.open(dirname);
15+
if(!root){
16+
Serial.println("- failed to open directory");
17+
return;
18+
}
19+
if(!root.isDirectory()){
20+
Serial.println(" - not a directory");
21+
return;
22+
}
23+
24+
File file = root.openNextFile();
25+
while(file){
26+
if(file.isDirectory()){
27+
Serial.print(" DIR : ");
28+
Serial.println(file.name());
29+
if(levels){
30+
listDir(fs, file.name(), levels -1);
31+
}
32+
} else {
33+
Serial.print(" FILE: ");
34+
Serial.print(file.name());
35+
Serial.print("\tSIZE: ");
36+
Serial.println(file.size());
37+
}
38+
file = root.openNextFile();
39+
}
40+
}
41+
42+
void readFile(fs::FS &fs, const char * path){
43+
Serial.printf("Reading file: %s\r\n", path);
44+
45+
File file = fs.open(path);
46+
if(!file || file.isDirectory()){
47+
Serial.println("- failed to open file for reading");
48+
return;
49+
}
50+
51+
Serial.println("- read from file:");
52+
while(file.available()){
53+
Serial.write(file.read());
54+
}
55+
}
56+
57+
void writeFile(fs::FS &fs, const char * path, const char * message){
58+
Serial.printf("Writing file: %s\r\n", path);
59+
60+
File file = fs.open(path, FILE_WRITE);
61+
if(!file){
62+
Serial.println("- failed to open file for writing");
63+
return;
64+
}
65+
if(file.print(message)){
66+
Serial.println("- file written");
67+
} else {
68+
Serial.println("- frite failed");
69+
}
70+
}
71+
72+
void appendFile(fs::FS &fs, const char * path, const char * message){
73+
Serial.printf("Appending to file: %s\r\n", path);
74+
75+
File file = fs.open(path, FILE_APPEND);
76+
if(!file){
77+
Serial.println("- failed to open file for appending");
78+
return;
79+
}
80+
if(file.print(message)){
81+
Serial.println("- message appended");
82+
} else {
83+
Serial.println("- append failed");
84+
}
85+
}
86+
87+
void renameFile(fs::FS &fs, const char * path1, const char * path2){
88+
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
89+
if (fs.rename(path1, path2)) {
90+
Serial.println("- file renamed");
91+
} else {
92+
Serial.println("- rename failed");
93+
}
94+
}
95+
96+
void deleteFile(fs::FS &fs, const char * path){
97+
Serial.printf("Deleting file: %s\r\n", path);
98+
if(fs.remove(path)){
99+
Serial.println("- file deleted");
100+
} else {
101+
Serial.println("- delete failed");
102+
}
103+
}
104+
105+
void testFileIO(fs::FS &fs, const char * path){
106+
Serial.printf("Testing file I/O with %s\r\n", path);
107+
108+
static uint8_t buf[512];
109+
size_t len = 0;
110+
File file = fs.open(path, FILE_WRITE);
111+
if(!file){
112+
Serial.println("- failed to open file for writing");
113+
return;
114+
}
115+
116+
size_t i;
117+
Serial.print("- writing" );
118+
uint32_t start = millis();
119+
for(i=0; i<2048; i++){
120+
if ((i & 0x001F) == 0x001F){
121+
Serial.print(".");
122+
}
123+
file.write(buf, 512);
124+
}
125+
Serial.println("");
126+
uint32_t end = millis() - start;
127+
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
128+
file.close();
129+
130+
file = fs.open(path);
131+
start = millis();
132+
end = start;
133+
i = 0;
134+
if(file && !file.isDirectory()){
135+
len = file.size();
136+
size_t flen = len;
137+
start = millis();
138+
Serial.print("- reading" );
139+
while(len){
140+
size_t toRead = len;
141+
if(toRead > 512){
142+
toRead = 512;
143+
}
144+
file.read(buf, toRead);
145+
if ((i++ & 0x001F) == 0x001F){
146+
Serial.print(".");
147+
}
148+
len -= toRead;
149+
}
150+
Serial.println("");
151+
end = millis() - start;
152+
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
153+
file.close();
154+
} else {
155+
Serial.println("- failed to open file for reading");
156+
}
157+
}
158+
159+
void setup(){
160+
Serial.begin(115200);
161+
Serial.setDebugOutput(true);
162+
if (FORMAT_FFAT) FFat.format();
163+
if(!FFat.begin()){
164+
Serial.println("FFat Mount Failed");
165+
return;
166+
}
167+
168+
Serial.printf("Total space: %10u\n", FFat.totalBytes());
169+
Serial.printf("Free space: %10u\n", FFat.freeBytes());
170+
listDir(FFat, "/", 0);
171+
writeFile(FFat, "/hello.txt", "Hello ");
172+
appendFile(FFat, "/hello.txt", "World!\r\n");
173+
readFile(FFat, "/hello.txt");
174+
renameFile(FFat, "/hello.txt", "/foo.txt");
175+
readFile(FFat, "/foo.txt");
176+
deleteFile(FFat, "/foo.txt");
177+
testFileIO(FFat, "/test.txt");
178+
Serial.printf("Free space: %10u\n", FFat.freeBytes());
179+
deleteFile(FFat, "/test.txt");
180+
Serial.println( "Test complete" );
181+
}
182+
183+
void loop(){
184+
185+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#include "FS.h"
2+
#include "FFat.h"
3+
#include <time.h>
4+
#include <WiFi.h>
5+
6+
const char* ssid = "your-ssid";
7+
const char* password = "your-password";
8+
9+
long timezone = 1;
10+
byte daysavetime = 1;
11+
12+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
13+
Serial.printf("Listing directory: %s\n", dirname);
14+
15+
File root = fs.open(dirname);
16+
if(!root){
17+
Serial.println("Failed to open directory");
18+
return;
19+
}
20+
if(!root.isDirectory()){
21+
Serial.println("Not a directory");
22+
return;
23+
}
24+
25+
File file = root.openNextFile();
26+
while(file){
27+
if(file.isDirectory()){
28+
Serial.print(" DIR : ");
29+
Serial.print (file.name());
30+
time_t t= file.getLastWrite();
31+
struct tm * tmstruct = localtime(&t);
32+
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);
33+
if(levels){
34+
listDir(fs, file.name(), levels -1);
35+
}
36+
} else {
37+
Serial.print(" FILE: ");
38+
Serial.print(file.name());
39+
Serial.print(" SIZE: ");
40+
Serial.print(file.size());
41+
time_t t= file.getLastWrite();
42+
struct tm * tmstruct = localtime(&t);
43+
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);
44+
}
45+
file = root.openNextFile();
46+
}
47+
}
48+
49+
void createDir(fs::FS &fs, const char * path){
50+
Serial.printf("Creating Dir: %s\n", path);
51+
if(fs.mkdir(path)){
52+
Serial.println("Dir created");
53+
} else {
54+
Serial.println("mkdir failed");
55+
}
56+
}
57+
58+
void removeDir(fs::FS &fs, const char * path){
59+
Serial.printf("Removing Dir: %s\n", path);
60+
if(fs.rmdir(path)){
61+
Serial.println("Dir removed");
62+
} else {
63+
Serial.println("rmdir failed");
64+
}
65+
}
66+
67+
void readFile(fs::FS &fs, const char * path){
68+
Serial.printf("Reading file: %s\n", path);
69+
70+
File file = fs.open(path);
71+
if(!file){
72+
Serial.println("Failed to open file for reading");
73+
return;
74+
}
75+
76+
Serial.print("Read from file: ");
77+
while(file.available()){
78+
Serial.write(file.read());
79+
}
80+
file.close();
81+
}
82+
83+
void writeFile(fs::FS &fs, const char * path, const char * message){
84+
Serial.printf("Writing file: %s\n", path);
85+
86+
File file = fs.open(path, FILE_WRITE);
87+
if(!file){
88+
Serial.println("Failed to open file for writing");
89+
return;
90+
}
91+
if(file.print(message)){
92+
Serial.println("File written");
93+
} else {
94+
Serial.println("Write failed");
95+
}
96+
file.close();
97+
}
98+
99+
void appendFile(fs::FS &fs, const char * path, const char * message){
100+
Serial.printf("Appending to file: %s\n", path);
101+
102+
File file = fs.open(path, FILE_APPEND);
103+
if(!file){
104+
Serial.println("Failed to open file for appending");
105+
return;
106+
}
107+
if(file.print(message)){
108+
Serial.println("Message appended");
109+
} else {
110+
Serial.println("Append failed");
111+
}
112+
file.close();
113+
}
114+
115+
void renameFile(fs::FS &fs, const char * path1, const char * path2){
116+
Serial.printf("Renaming file %s to %s\n", path1, path2);
117+
if (fs.rename(path1, path2)) {
118+
Serial.println("File renamed");
119+
} else {
120+
Serial.println("Rename failed");
121+
}
122+
}
123+
124+
void deleteFile(fs::FS &fs, const char * path){
125+
Serial.printf("Deleting file: %s\n", path);
126+
if(fs.remove(path)){
127+
Serial.println("File deleted");
128+
} else {
129+
Serial.println("Delete failed");
130+
}
131+
}
132+
133+
void setup(){
134+
Serial.begin(115200);
135+
// We start by connecting to a WiFi network
136+
Serial.println();
137+
Serial.println();
138+
Serial.print("Connecting to ");
139+
Serial.println(ssid);
140+
141+
WiFi.begin(ssid, password);
142+
143+
while (WiFi.status() != WL_CONNECTED) {
144+
delay(500);
145+
Serial.print(".");
146+
}
147+
Serial.println("WiFi connected");
148+
Serial.println("IP address: ");
149+
Serial.println(WiFi.localIP());
150+
Serial.println("Contacting Time Server");
151+
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
152+
struct tm tmstruct ;
153+
delay(2000);
154+
tmstruct.tm_year = 0;
155+
getLocalTime(&tmstruct, 5000);
156+
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);
157+
Serial.println("");
158+
159+
if(!FFat.begin(true)){
160+
Serial.println("FFat Mount Failed");
161+
return;
162+
}
163+
164+
listDir(FFat, "/", 0);
165+
removeDir(FFat, "/mydir");
166+
createDir(FFat, "/mydir");
167+
deleteFile(FFat, "/hello.txt");
168+
writeFile(FFat, "/hello.txt", "Hello ");
169+
appendFile(FFat, "/hello.txt", "World!\n");
170+
listDir(FFat, "/", 0);
171+
}
172+
173+
void loop(){
174+
175+
}
176+
177+

libraries/FFat/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=FFat
2+
version=1.0
3+
author=Hristo Gochkov, Ivan Grokhtkov, Larry Bernstone
4+
maintainer=Hristo Gochkov <[email protected]>
5+
sentence=ESP32 FAT on Flash File System
6+
paragraph=
7+
category=Data Storage
8+
url=
9+
architectures=esp32

0 commit comments

Comments
 (0)