Skip to content

Commit 29a253a

Browse files
authored
Add initial SPIFFS library (espressif#627)
* Add initial SPIFFS library * Fix Deep Sleep Examples * Missed one example
1 parent 39fb8c3 commit 29a253a

File tree

8 files changed

+324
-11
lines changed

8 files changed

+324
-11
lines changed

libraries/ESP32/examples/DeepSleep/ExternalWakeUp/ExternalWakeUp.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ Method to print the reason by which ESP32
3030
has been awaken from sleep
3131
*/
3232
void print_wakeup_reason(){
33-
esp_deep_sleep_wakeup_cause_t wakeup_reason;
33+
esp_sleep_wakeup_cause_t wakeup_reason;
3434

35-
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
35+
wakeup_reason = esp_sleep_get_wakeup_cause();
3636

3737
switch(wakeup_reason)
3838
{
@@ -66,7 +66,7 @@ void setup(){
6666
Note that using internal pullups/pulldowns also requires
6767
RTC peripherals to be turned on.
6868
*/
69-
esp_deep_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
69+
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
7070

7171
//If you were to use ext1, you would use it like
7272
//esp_deep_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);

libraries/ESP32/examples/DeepSleep/TimerWakeUp/TimerWakeUp.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Method to print the reason by which ESP32
2929
has been awaken from sleep
3030
*/
3131
void print_wakeup_reason(){
32-
esp_deep_sleep_wakeup_cause_t wakeup_reason;
32+
esp_sleep_wakeup_cause_t wakeup_reason;
3333

34-
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
34+
wakeup_reason = esp_sleep_get_wakeup_cause();
3535

3636
switch(wakeup_reason)
3737
{
@@ -59,7 +59,7 @@ void setup(){
5959
First we configure the wake up source
6060
We set our ESP32 to wake up every 5 seconds
6161
*/
62-
esp_deep_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
62+
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
6363
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
6464
" Seconds");
6565

libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Method to print the reason by which ESP32
2020
has been awaken from sleep
2121
*/
2222
void print_wakeup_reason(){
23-
esp_deep_sleep_wakeup_cause_t wakeup_reason;
23+
esp_sleep_wakeup_cause_t wakeup_reason;
2424

25-
wakeup_reason = esp_deep_sleep_get_wakeup_cause();
25+
wakeup_reason = esp_sleep_get_wakeup_cause();
2626

2727
switch(wakeup_reason)
2828
{
@@ -42,7 +42,7 @@ has been awaken from sleep
4242
void print_wakeup_touchpad(){
4343
touch_pad_t pin;
4444

45-
touchPin = esp_deep_sleep_get_touchpad_wakeup_status();
45+
touchPin = esp_sleep_get_touchpad_wakeup_status();
4646

4747
switch(touchPin)
4848
{
@@ -80,7 +80,7 @@ void setup(){
8080
touchAttachInterrupt(T3, callback, Threshold);
8181

8282
//Configure Touchpad as wakeup source
83-
esp_deep_sleep_enable_touchpad_wakeup();
83+
esp_sleep_enable_touchpad_wakeup();
8484

8585
//Go to sleep now
8686
Serial.println("Going to sleep now");

libraries/ESP32/examples/ResetReason/ResetReason.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void setup() {
7777

7878
// Set ESP32 to go to deep sleep to see a variation
7979
// in the reset reason. Device will sleep for 5 seconds.
80-
esp_deep_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
80+
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
8181
Serial.println("Going to sleep");
8282
esp_deep_sleep(5 * uS_TO_S_FACTOR);
8383
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#include "FS.h"
2+
#include "SPIFFS.h"
3+
4+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
5+
Serial.printf("Listing directory: %s\n", dirname);
6+
7+
File root = fs.open(dirname);
8+
if(!root){
9+
Serial.println("Failed to open directory");
10+
return;
11+
}
12+
if(!root.isDirectory()){
13+
Serial.println("Not a directory");
14+
return;
15+
}
16+
17+
File file = root.openNextFile();
18+
while(file){
19+
if(file.isDirectory()){
20+
Serial.print(" DIR : ");
21+
Serial.println(file.name());
22+
if(levels){
23+
listDir(fs, file.name(), levels -1);
24+
}
25+
} else {
26+
Serial.print(" FILE: ");
27+
Serial.print(file.name());
28+
Serial.print(" SIZE: ");
29+
Serial.println(file.size());
30+
}
31+
file = root.openNextFile();
32+
}
33+
}
34+
35+
void readFile(fs::FS &fs, const char * path){
36+
Serial.printf("Reading file: %s\n", path);
37+
38+
File file = fs.open(path);
39+
if(!file){
40+
Serial.println("Failed to open file for reading");
41+
return;
42+
}
43+
44+
Serial.print("Read from file: ");
45+
while(file.available()){
46+
Serial.write(file.read());
47+
}
48+
}
49+
50+
void writeFile(fs::FS &fs, const char * path, const char * message){
51+
Serial.printf("Writing file: %s\n", path);
52+
53+
File file = fs.open(path, FILE_WRITE);
54+
if(!file){
55+
Serial.println("Failed to open file for writing");
56+
return;
57+
}
58+
if(file.print(message)){
59+
Serial.println("File written");
60+
} else {
61+
Serial.println("Write failed");
62+
}
63+
}
64+
65+
void appendFile(fs::FS &fs, const char * path, const char * message){
66+
Serial.printf("Appending to file: %s\n", path);
67+
68+
File file = fs.open(path, FILE_APPEND);
69+
if(!file){
70+
Serial.println("Failed to open file for appending");
71+
return;
72+
}
73+
if(file.print(message)){
74+
Serial.println("Message appended");
75+
} else {
76+
Serial.println("Append failed");
77+
}
78+
}
79+
80+
void renameFile(fs::FS &fs, const char * path1, const char * path2){
81+
Serial.printf("Renaming file %s to %s\n", path1, path2);
82+
if (fs.rename(path1, path2)) {
83+
Serial.println("File renamed");
84+
} else {
85+
Serial.println("Rename failed");
86+
}
87+
}
88+
89+
void deleteFile(fs::FS &fs, const char * path){
90+
Serial.printf("Deleting file: %s\n", path);
91+
if(fs.remove(path)){
92+
Serial.println("File deleted");
93+
} else {
94+
Serial.println("Delete failed");
95+
}
96+
}
97+
98+
void testFileIO(fs::FS &fs, const char * path){
99+
File file = fs.open(path);
100+
static uint8_t buf[512];
101+
size_t len = 0;
102+
uint32_t start = millis();
103+
uint32_t end = start;
104+
if(file){
105+
len = file.size();
106+
size_t flen = len;
107+
start = millis();
108+
while(len){
109+
size_t toRead = len;
110+
if(toRead > 512){
111+
toRead = 512;
112+
}
113+
file.read(buf, toRead);
114+
len -= toRead;
115+
}
116+
end = millis() - start;
117+
Serial.printf("%u bytes read for %u ms\n", flen, end);
118+
file.close();
119+
} else {
120+
Serial.println("Failed to open file for reading");
121+
}
122+
123+
124+
file = fs.open(path, FILE_WRITE);
125+
if(!file){
126+
Serial.println("Failed to open file for writing");
127+
return;
128+
}
129+
130+
size_t i;
131+
start = millis();
132+
for(i=0; i<2048; i++){
133+
file.write(buf, 512);
134+
}
135+
end = millis() - start;
136+
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
137+
file.close();
138+
}
139+
140+
void setup(){
141+
Serial.begin(115200);
142+
if(!SPIFFS.begin()){
143+
Serial.println("SPIFFS Mount Failed");
144+
return;
145+
}
146+
147+
listDir(SPIFFS, "/", 0);
148+
writeFile(SPIFFS, "/hello.txt", "Hello ");
149+
appendFile(SPIFFS, "/hello.txt", "World!\n");
150+
readFile(SPIFFS, "/hello.txt");
151+
deleteFile(SPIFFS, "/foo.txt");
152+
renameFile(SPIFFS, "/hello.txt", "/foo.txt");
153+
readFile(SPIFFS, "/foo.txt");
154+
testFileIO(SPIFFS, "/test.txt");
155+
}
156+
157+
void loop(){
158+
159+
}

libraries/SPIFFS/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=SPIFFS
2+
version=1.0
3+
author=Hristo Gochkov, Ivan Grokhtkov
4+
maintainer=Hristo Gochkov <hristo@espressif.com>
5+
sentence=ESP32 SPIFFS File System
6+
paragraph=
7+
category=Data Storage
8+
url=
9+
architectures=esp32

libraries/SPIFFS/src/SPIFFS.cpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "vfs_api.h"
16+
17+
extern "C" {
18+
#include <sys/unistd.h>
19+
#include <sys/stat.h>
20+
#include <dirent.h>
21+
#include "esp_spiffs.h"
22+
}
23+
#include "SPIFFS.h"
24+
25+
using namespace fs;
26+
27+
SPIFFSFS::SPIFFSFS(FSImplPtr impl)
28+
: FS(impl)
29+
{}
30+
31+
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles)
32+
{
33+
if(esp_spiffs_mounted(NULL)){
34+
log_w("SPIFFS Already Mounted!");
35+
return true;
36+
}
37+
38+
esp_vfs_spiffs_conf_t conf = {
39+
.base_path = basePath,
40+
.partition_label = NULL,
41+
.max_files = maxOpenFiles,
42+
.format_if_mount_failed = formatOnFail
43+
};
44+
45+
esp_err_t err = esp_vfs_spiffs_register(&conf);
46+
if(err){
47+
log_e("Mounting SPIFFS failed! Error: %d", err);
48+
return false;
49+
}
50+
_impl->mountpoint(basePath);
51+
return true;
52+
}
53+
54+
void SPIFFSFS::end()
55+
{
56+
if(esp_spiffs_mounted(NULL)){
57+
esp_err_t err = esp_vfs_spiffs_unregister(NULL);
58+
if(err){
59+
log_e("Unmounting SPIFFS failed! Error: %d", err);
60+
return;
61+
}
62+
_impl->mountpoint(NULL);
63+
}
64+
}
65+
66+
bool SPIFFSFS::format()
67+
{
68+
esp_err_t err = esp_spiffs_format(NULL);
69+
if(err){
70+
log_e("Formatting SPIFFS failed! Error: %d", err);
71+
return false;
72+
}
73+
return true;
74+
}
75+
76+
size_t SPIFFSFS::totalBytes()
77+
{
78+
size_t total,used;
79+
if(esp_spiffs_info(NULL, &total, &used)){
80+
return 0;
81+
}
82+
return total;
83+
}
84+
85+
size_t SPIFFSFS::usedBytes()
86+
{
87+
size_t total,used;
88+
if(esp_spiffs_info(NULL, &total, &used)){
89+
return 0;
90+
}
91+
return used;
92+
}
93+
94+
bool SPIFFSFS::exists(const char* path)
95+
{
96+
File f = open(path, "r");
97+
return (f == true) && !f.isDirectory();
98+
}
99+
100+
bool SPIFFSFS::exists(const String& path)
101+
{
102+
return exists(path.c_str());
103+
}
104+
105+
106+
SPIFFSFS SPIFFS = SPIFFSFS(FSImplPtr(new VFSImpl()));

libraries/SPIFFS/src/SPIFFS.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#ifndef _SPIFFS_H_
15+
#define _SPIFFS_H_
16+
17+
#include "FS.h"
18+
19+
namespace fs
20+
{
21+
22+
class SPIFFSFS : public FS
23+
{
24+
public:
25+
SPIFFSFS(FSImplPtr impl);
26+
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10);
27+
bool format();
28+
size_t totalBytes();
29+
size_t usedBytes();
30+
void end();
31+
bool exists(const char* path);
32+
bool exists(const String& path);
33+
};
34+
35+
}
36+
37+
extern fs::SPIFFSFS SPIFFS;
38+
39+
#endif /* _SPIFFS_H_ */

0 commit comments

Comments
 (0)