WatchdogTimer

はじめに

ウォッチドッグタイマーの例です。3秒間0番ピンがLOWになると、ESP32をリスタートします。

プログラム

定義等

1
2
3
4
5
6
#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;
 

buttonは、スイッチを接続するピン番号(ここでは0)です。このピンがLOWの間は、delay()を呼び出し、タイマーをリセットする機会を奪います。

wdtTimeoutは、ウォッチドッグを起動するまでの時間です。3000を設定しています。実際には3秒を意味します。

timerは、hw_timer_t型へのポインタで、タイマーを表す変数です。

resetModule()

 7
 8
 9
10
11
void ARDUINO_ISR_ATTR resetModule() {
  ets_printf("reboot\n");
  esp_restart();
}
 

この関数は、タイマー割り込みが発生したときに呼び出される関数です。ARDUINO_ISR_ATTR属性を付けることで、このコードがIRAM(Instruction RAM)に配置されるようにしています。esp32では、割り込みハンドラはIRAMに配置する必要があるようです。

ARDUINO_ISR_ATTRは、esp32-hal.hで、定義されています。

ets_printf()は、シリアルモニタに文字を表示する関数です。

esp_restart_noos()は、ESP32をリスタートする関数です。内部では、自身が使ってない方のコアを止めたり、ハードウェアをリセットしたりしているようです。

この関数は、タイマーが3秒間リセットされないと呼ばれるようにsetup()で設定しています。

setup()

12
13
14
15
16
17
18
19
20
21
22
23
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
}
 

pinMode()によりbuttonを入力モード(プルアップあり)に切り替えます。

timerBegin()は、タイマーの初期設定を行います。タイマー番号0、分周比80を設定しています。

timerAttachInterrupt()は、タイマーに割り込みハンドラを設定します。

timerAlarmWrite()で、タイマーの値(割り込みのタイミング)を設定します。クロックの周波数は80MHzで、timerBegin()で、分周比を80に設定しているので、周波数は、80,000,000 / 80 = 1,000,000 Hz となるため、1チックあたり、1マイクロ秒となります。ここで、第2引数に3000000を設定しているので、タイマーは3秒ごとに起動されます。第3引数はfalseなので、タイマーは周期的ではなく、一度だけ起動されます。

最後に、timerAlarmEnable()で、タイマーを開始します。

loop()

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
}

timerWrite()で、現在のタイマー値を0に設定します。これにより、タイマーをリセットします。

digitalRead()でbuttonがLOWになっているのを検出すると、delay()を使って500ミリ秒待ちます。

loop()の先頭付近で取得したmillis()の値と、終了付近で取得したmillis()の値の差を表示します。

loop()の処理に、3秒以上要してしまうと、resetModule()が呼び出されて、システムがリセットされます。

バージョン

Hardware:ESP-WROOM-32
Software:Arduino core for the ESP32 2.0.4

最終更新日

November 1, 2022

inserted by FC2 system