0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ESP32 で SPIFFS を使う

Posted at

環境

  • Arduino 1.8.13
  • esp32 by Espressif Systes バージョン1.0.4

コード

プログラム開始時に data1,data2,data3 値を読み込み


//   init filesystem

 TFTDisplayOut(notifyInitializing );
 bool res = SPIFFS.begin(true); // FORMAT_SPIFFS_IF_FAILED
 Serial.print("SPIFFS.begining...");
 if (!res) {
   Serial.println("fail");
   handleFatalError("ファイルシステムを開始できません");
 }
 Serial.println("OK");

 // set file read
 File fp = SPIFFS.open(recordfile, "r");
 if (!fp) {
   SPIFFS.format();
   Serial.println("Spiffs formatted");
   File fp = SPIFFS.open(recordfile, "w");
   if (!fp) {
     Serial.println("Write Opening recordfile failed");
     handleFatalError("ファイルに記録できません");
   } else {
     fp.println(data1);
     fp.println(data2);
     fp.println(data3);
     fp.close();
     File fp = SPIFFS.open(recordfile, "r");
   }
 }
 if (!fp) {
   Serial.println("Opening recordfile failed");
     handleFatalError("ファイルに記録できません");
 } else {
   int temp;
   temp = fp.readStringUntil('\n').toInt(); if ( 0 != temp ) data1 = temp;
   temp = fp.readStringUntil('\n').toInt(); if ( 0 != temp ) data2 = temp;
   temp = fp.readStringUntil('\n').toInt(); if ( 0 != temp ) data3 = temp;

   Serial.print("data1:");
   Serial.println(data1);
   Serial.print("data2:");
   Serial.println(data2);
   Serial.print("data3:");
   Serial.println(data3);
   fp.close();
 }
 TFTDisplayOut(notifyInitializeEnd );


データ更新時に data1,data2,data3 値を書き込み


  Serial.println("SPIFFS record");
  File fp = SPIFFS.open(recordfile, "w");
  if (!fp) {
    Serial.println("Opening recordfile failed");
    handleFatalError("ファイルに記録できません");
} else {
    fp.println(data1);
    fp.println(data2);
    fp.println(data3);
    fp.close();
  }

実行

最初に実行すると、


  bool res = SPIFFS.begin(true); // FORMAT_SPIFFS_IF_FAILED

が実行された時点で


E (470) SPIFFS: mount failed, -10025

がシリアルモニタに出力され、フォーマットが行われる。フォーマットに必要な時間は数十秒あるため、あらかじめ


  TFTDisplayOut(notifyInitializing );

として液晶ディスプレイに初期化表示を出しておく。

念の為


  File fp = SPIFFS.open(recordfile, "r");
  if (!fp) {
    SPIFFS.format();
    ...

ともしたが、ここは必要ないかな?

エラー

Guru Meditation Error が発生する


Guru Meditation Error: Core  1 panic'ed (Cache disabled but cached memory region accessed)
Core 1 register dump:
PC      : 0x400dc5b8  PS      : 0x00060034  A0      : 0x80081724  A1      : 0x3ffbe790  
A2      : 0x00000009  A3      : 0x20000000  A4      : 0x00000400  A5      : 0x3ffc0c54  
A6      : 0x00000000  A7      : 0x3ffb1a8c  A8      : 0x800811b7  A9      : 0x00000000  
A10     : 0x3ffc0c64  A11     : 0x3f400fb8  A12     : 0x00000000  A13     : 0x3ffb1a30  
A14     : 0x00000000  A15     : 0x00000005  SAR     : 0x00000011  EXCCAUSE: 0x00000007  
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  
Core 1 was running in ISR context:
EPC1    : 0x4008e090  EPC2    : 0x00000000  EPC3    : 0x00000000  EPC4    : 0x400dc5b8

Backtrace: 0x400dc5b8:0x3ffbe790 0x40081721:0x3ffbe7b0 0x40084ba9:0x3ffbe7d0 0x4008e08d:0x3ffb1ab0 0x4008e91d:0x3ffb1ad0 0x40087648:0x3ffb1af0 0x400f9cb3:0x3ffb1b60 0x400e2491:0x3ffb1b90 0x400e26c7:0x3ffb1bc0 0x400e3c33:0x3ffb1bf0 0x400e57ae:0x3ffb1c30 0x400e207c:0x3ffb1c70 0x400e0dc4:0x3ffb1ca0 0x400e952d:0x3ffb1d00 0x4000bcc5:0x3ffb1d20 0x400ea889:0x3ffb1d40 0x400d9e9f:0x3ffb1d60 0x400da1ef:0x3ffb1d90 0x400da4c9:0x3ffb1dd0 0x400d9baf:0x3ffb1e50 0x400d6102:0x3ffb1e80 0x400dc6fb:0x3ffb1fb0 0x40088a3d:0x3ffb1fd0

Rebooting...

これはタイマー割り込みを設定していた時に発生した。以下のような20ミリ秒ごとのタイマー割り込みを SPIFFS を操作する前に書いていたが、 SPIFFS を操作する後に記述するように変更。

  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 20000, true);  // 20m sec
  timerAlarmEnable(timer);

また、データ更新処理においては以下のように timer をストップしてから SPIFFS 処理をしたあと、 timer を再開する。


  timerAlarmDisable(timer);    // stop alarm
  timerDetachInterrupt(timer);  // detach interrupt
  timerEnd(timer);      // end timer
  Serial.println("tiner stop");
   
/*
  ~ SPIFFS 処理をここに書く ~
*/

  timer = timerBegin(0, 80, true);
  timerAttachInterrupt(timer, &onTimer, true);
  timerAlarmWrite(timer, 20000, true);  // 20m sec
  timerAlarmEnable(timer);
  Serial.println("tiner setted again");
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?