状況
スイッチにつなげたピンに状態変化割り込みを
attachInterrupt(SWITCH_PIN, RELEASE1, RISING);
と設定して、スイッチを連打すると時々リセットがかかってしまう。
環境
- ESP32 by Espressif System Version 1.0.4
- Arduino 1.8.13
- ESP32 WROOM-32E
エラー出力
リセット時、シリアルモニターに以下のエラーが出る
Guru Meditation Error: Core 1 panic'ed (Inte
rrupt wdt timeout on CPU1)
コード
/* Core 1 panic'ed (Interrupt wdt timeout on CPU1)
* Error Check program
*/
# define SWITCH_PIN 35// or 12,13, 14,34,35
# define BEEP_PIN 2
# define DELAY 0 // if 0 then sometime error
int timercounter = 0;
static int SWPushed = 0;
static int SWPushing = 0;
void IRAM_ATTR RELEASE1() {
static unsigned long nowtime;
static unsigned long oldtime = 0;
Serial.println("RELEASE"); // sometime error
nowtime = millis();
if ( 150 < ( nowtime - oldtime )) {
SWPushed = 1;
SWPushing = 0;
}
oldtime = nowtime;
}
void setup() {
Serial.begin(115200);
pinMode(SWITCH_PIN, INPUT_PULLUP);
attachInterrupt(SWITCH_PIN, RELEASE1, RISING);
// when reboot attention
pinMode(BEEP_PIN, OUTPUT);
digitalWrite(BEEP_PIN, HIGH); // 再起動した時にわかりやすいように音を鳴らす
delay(4000);
digitalWrite(BEEP_PIN, LOW);
}
void loop()
{
for ( int i = 0 ; i < 20 ; i++)
{
Serial.println("########");
delay(DELAY);
}
delay(100);
}
調査
# define DELAY 0 // if 0 then sometime error
これを 1 にするとエラーが起きなくなる。
あるいは、以下の割り込みルーチン内の Serial.println をコメントアウトするとエラーが起きなくなる。
void IRAM_ATTR RELEASE1() {
static unsigned long nowtime;
static unsigned long oldtime = 0;
Serial.println("RELEASE"); // sometime error
仮説
for ( int i = 0 ; i < 20 ; i++)
{
Serial.println("########");
delay(DELAY);
}
メインルーチン中のこの箇所の DELAY が 0 だとシリアル出力しっぱなしになる。
割り込み中のシリアル出力と競合し、ウォッチドッグが発動してしまうのではないか。
対処
割り込み中のシリアル出力をコメントアウトで対処した。数万回スイッチを押したがリセットは起きなくなった。