9
9

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 5 years have passed since last update.

Arduino-ESP32 WatchDogTimer

Last updated at Posted at 2017-12-19

Arduino-ESP32でウォッチドッグタイマーを使用する方法です。
※20181022 esp_restart_noos()コマンドが変更されていましたので更新しました。

WatchDogTimerのサンプルlibraries/ESP32/examples/Timer/WatchdogTimer/WatchdogTimer.inoがあります。

WatchdogTimer.ino
#include "esp_system.h"

#include "esp_system.h"

const int button = 0;         //gpio to use to trigger delay
const int wdtTimeout = 3000;  //time in ms to trigger the watchdog
hw_timer_t *timer = NULL;

void IRAM_ATTR resetModule() {
  ets_printf("reboot\n");
  esp_restart();
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("running setup");

  pinMode(button, INPUT_PULLUP);                    //init control pin
  timer = timerBegin(0, 80, true);                  //timer 0, div 80
  timerAttachInterrupt(timer, &resetModule, true);  //attach callback
  timerAlarmWrite(timer, wdtTimeout * 1000, false); //set time in us
  timerAlarmEnable(timer);                          //enable interrupt
}

void loop() {
  Serial.println("running main loop");

  timerWrite(timer, 0); //reset timer (feed watchdog)
  long loopTime = millis();
  //while button is pressed, delay up to 3 seconds to trigger the timer
  while (!digitalRead(button)) {
    Serial.println("button pressed");
    delay(500);
  }
  delay(1000); //simulate work
  loopTime = millis() - loopTime;
  
  Serial.print("loop time is = ");
  Serial.println(loopTime); //should be under 3000
}

ウォッチドッグを使う順序は以下

  1. セットアップ
    • タイマーの初期化 // timer = timerBegin(0, 80, true); //timer 0, div 80
      • 関数の引数はそれぞれ
        • uint8_t num タイマーの番号 (4つのタイマーのどれを使用するか指定する。0から3まで)
        • uint16_t divider ディバイダ Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more info).
        • bool countUp (カウントアップかカウントダウンかを指定する。/When set timer 0/1 time-base counter increment. When cleared timer 0 time-base counter decrement./)
    • コールバックの指定 // timerAttachInterrupt(timer, &resetModule, true);
    • ウォッチドッグタイマーのタイムアウト時間の指定 // timerAlarmWrite(timer, wdtTimeout * 1000, false)
  2. ウォッチドッグタイマーの有効化
    • timerAlarmEnable(timer);
  3. 監視したい場所でタイマーをリセットする。
    • timerWrite(timer, 0); //reset timer (feed watchdog)
  4. ウォッチドッグタイマーの無効化
    • timerAlarmDisable(timer);

トラブルシューティング

errormessage.sample1
Task watchdog got triggered. The following tasks did not reset the watchdog in time:
- IDLE (CPU 0)
Tasks currently running:
CPU 0: timeCheckTask
CPU 1: loopTask

以下を参考にしてください。
https://www.mgo-tec.com/blog-entry-trouble-shooting-esp32-wroom.html/4

9
9
1

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
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?