LoginSignup
1
1

More than 1 year has passed since last update.

WatchdogTimerを試す

Posted at

WatchdogTimer(ウォッチドッグタイマー)

ESP32(M5Stack)とArduinoにて、ウォッチドッグタイマーの動作を確認する。いずれも、Arduino IDEを利用しているが、ウォッチドッグタイマーはマイコン依存の部分が多々あり、低レベルではそれぞれ異なるFunctionが用いられている。また、両者にて精度に差があったので、それも記載する。

ウォッチドッグの内容

ランダムな時間(1-2500ms)Sleep中、Sleep開始から2秒経過した場合、ウォッチドッグタイマーによりリセットする。

ESP32

参考URL

ソースコード

#include "esp_system.h"

#define TIMEOUT 2000
hw_timer_t *timer = NULL;

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

void setup()
{
  Serial.begin(115200);  
  randomSeed(millis());

  timer = timerBegin(0, 80, true);                  //timer 0, div 80
  timerAttachInterrupt(timer, &resetModule, true);  //attach callback
  timerAlarmWrite(timer, TIMEOUT * 1000, false);    //set time in us
  timerAlarmEnable(timer);                          //enable interrupt

  Serial.println("Setup done");
}

void loop()
{
  uint16_t val;
  timerWrite(timer, 0); //reset timer (feed watchdog)
  val = random(1, 2500);
  Serial.printf("Sleep %d ms\n", val);
  delay(val);   // Process which might take time
}
  • randomSeed()にて乱数を初期設定。
  • ウォッチドッグタイマー関連初期化(ハンドラの設定、Enable化など)。
  • timerWrite()実行後、timerAlarmWrite()で指定した時間(TIMEOUT * 1000 us)である2秒後にリセット(WatchdogTimer実行)される。
  • 1-2500msの間でSleep(delay())。

結果

M5StackWatchDogRandom2.png

2072msのときにリセット(WatchdogTimer実行)しているのが見られる。

Arduino

参考URL

ソースコード

#include <avr/wdt.h>

void setup() {
  Serial.begin(115200);
  randomSeed(millis());
  wdt_enable(WDTO_2S);
  Serial.println("Setup done");
}

void loop() {
  char buf[16];
  uint16_t val;
  wdt_reset();
  val = random(1, 2500);
  sprintf(buf, "Sleep %d ms", val);  
  Serial.println(buf);
  delay(val);
}

  • randomSeed()にて乱数を初期設定。
  • wdt_reset()実行後、wdt_enable()で指定した時間(WDTO_2S)である2秒後にリセット(WatchdogTimer実行)される。
  • 1-2500msの間でSleep(delay())。

結果

ArduinoWatchDogRandom.png

2128msではリセットせず、2215msではリセットしている。幾度となくトライした、2秒以上でリセットしていないケースが、Arduinoでは多々見受けられた。精度が悪いのであろうか?(詳細未調査)

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