##Arduino IDEのESP32のSPIFFSで書込が変
という現象に合いました。よく分からんので検証してみました。
テストコードは以下になります。サンプルのSPIFFSスケッチを一部書換えてテストしています。
SPIFFS_WriteTest.ino
#include "FS.h"
#include "SPIFFS.h"
/*
* ESP32 file make write test
*
*/
#define FORMAT_SPIFFS_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 testFileWR(fs::FS &fs, const char * path){
Serial.printf("Testing file Write with %s\r\n", path);
#define SIZE 512
static uint8_t buf[SIZE];
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;
int b;
int n = 80;
Serial.print("- writing" );
uint32_t start = millis();
for(i=0; i<n; i++){
if ((i & 0x001F) == 0x001F){
Serial.print(".");
}
b=file.write(buf, SIZE);
if(b<SIZE){
Serial.print(" Write Error!! .. ");
Serial.println(b);
break;
}
}
Serial.println("");
uint32_t end = millis() - start;
Serial.printf(" - %u bytes written in %u ms\r\n", n * SIZE, end);
file.close();
}
void setup(){
Serial.begin(115200);
Serial.println("Now Formatting...");
SPIFFS.format();
Serial.println("END");
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
Serial.println("SPIFFS Mount Failed");
return;
}
listDir(SPIFFS, "/", 0);
Serial.printf("Total :%lu\r\n",SPIFFS.totalBytes());
Serial.printf("Used :%lu\r\n",SPIFFS.usedBytes());
// Serial.println( "Test complete" );
}
/*
* TEST LOOP
*
*/
int n=1;
char fn[30];
void loop()
{
delay(2000);
sprintf(fn,"/%08d.txt",n++);
// deleteFile(SPIFFS, fn);
testFileWR(SPIFFS, fn);
Serial.println(fn);
Serial.printf("Total :%lu\r\n",SPIFFS.totalBytes());
Serial.printf("Used :%lu\r\n",SPIFFS.usedBytes());
}
ビルドオプションは No-OTA (2MB App/2MB SPIFFS)でビルドしました。
40960バイトづつファイルを追加していきます。
最初は問題なく 315msecくらいでファイルが作成されていきます。
result1.txt
Now Formatting...
END
Listing directory: /
Total :1856396
Used :0
Testing file Write with /00000001.txt
- writing..
- 40960 bytes written in 315 ms
/00000001.txt
Total :1856396
Used :41666
Testing file Write with /00000002.txt
- writing..
- 40960 bytes written in 314 ms
/00000002.txt
Total :1856396
Used :83332
Testing file Write with /00000003.txt
- writing..
- 40960 bytes written in 314 ms
/00000003.txt
Total :1856396
Used :124998
Testing file Write with /00000004.txt
- writing..
- 40960 bytes written in 314 ms
保存容量が1.2MBytesを越えたあたりから書込速度が遅くなります。
result2.txt
Testing file Write with /00000028.txt
- writing..
- 40960 bytes written in 314 ms
/00000028.txt
Total :1856396
Used :1166648
Testing file Write with /00000029.txt
- writing..
- 40960 bytes written in 313 ms
/00000029.txt
Total :1856396
Used :1208314
Testing file Write with /00000030.txt
- writing..
- 40960 bytes written in 1853 ms
/00000030.txt
Total :1856396
Used :1249980
Testing file Write with /00000031.txt
- writing..
- 40960 bytes written in 7824 ms
/00000031.txt
Total :1856396
Used :1291646
Testing file Write with /00000032.txt
- writing..
- 40960 bytes written in 7982 ms
/00000032.txt
Total :1856396
Used :1333312
保存容量いっぱいになる前に書込みが不能となります。
result3.txt
Testing file Write with /00000037.txt
- writing..
- 40960 bytes written in 7507 ms
/00000037.txt
Total :1856396
Used :1541642
Testing file Write with /00000038.txt
- writing..
- 40960 bytes written in 36843 ms
/00000038.txt
Total :1856396
Used :1583308
Testing file Write with /00000039.txt
- writing. Write Error!! .. 0
- 40960 bytes written in 47481 ms
/00000039.txt
Total :1856396
Used :1600125
Testing file Write with /00000040.txt
- writing Write Error!! .. 0
ESP32DevKitC , DevKit互換機で試してみたけど同じ。また、一回の書込み容量を増やしても減らしても状況は変わりませんでした。なにやら良く分かりませんね。