1+ // ----------------------------------------------------------------------------------
2+ // ResetReason2.ino
3+ //
4+ // Prints last reset reason of ESP32
5+ // This uses the mechanism in IDF that persists crash reasons across a reset.
6+ // See https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/misc_system_api.html#software-reset
7+ //
8+ // This example uses esp_reset_reason() instead of rtc_get_reset_reason(0).
9+ // This example forces a WDT timeout in 10s.
10+ //
11+ // Note enableLoopWDT() cannot be used here because it also takes care of
12+ // resetting WDT. See cores/esp32/main.cpp::loopTask().
13+ //
14+ // Author: David McCurley
15+ // Public Domain License
16+ // ----------------------------------------------------------------------------------
17+ // 2023.04.21 r1.0 Initial release
18+ // ----------------------------------------------------------------------------------
19+
20+ #include " esp_task_wdt.h"
21+
22+ // Converts reason type to a C string.
23+ // Type is located in /tools/sdk/esp32/include/esp_system/include/esp_system.h
24+ const char * resetReasonName (esp_reset_reason_t r) {
25+ switch (r) {
26+ case ESP_RST_UNKNOWN: return " Unknown" ;
27+ case ESP_RST_POWERON: return " PowerOn" ; // Power on or RST pin toggled
28+ case ESP_RST_EXT: return " ExtPin" ; // External pin - not applicable for ESP32
29+ case ESP_RST_SW: return " Reboot" ; // esp_restart()
30+ case ESP_RST_PANIC: return " Crash" ; // Exception/panic
31+ case ESP_RST_INT_WDT: return " WDT_Int" ; // Interrupt watchdog (software or hardware)
32+ case ESP_RST_TASK_WDT: return " WDT_Task" ; // Task watchdog
33+ case ESP_RST_WDT: return " WDT_Other" ; // Other watchdog
34+ case ESP_RST_DEEPSLEEP: return " Sleep" ; // Reset after exiting deep sleep mode
35+ case ESP_RST_BROWNOUT: return " BrownOut" ; // Brownout reset (software or hardware)
36+ case ESP_RST_SDIO: return " SDIO" ; // Reset over SDIO
37+ default : return " " ;
38+ }
39+ }
40+
41+ void PrintResetReason (void ) {
42+ esp_reset_reason_t r = esp_reset_reason ();
43+ if (r==ESP_RST_POWERON) {
44+ delay (6000 ); // Wait for serial monitor to connect
45+ }
46+ Serial.printf (" \r\n Reset reason %i - %s\r\n " , r, resetReasonName (r));
47+ }
48+
49+ void setup () {
50+ Serial.begin (115200 );
51+ PrintResetReason ();
52+
53+ // Start WDT monitor
54+ if (esp_task_wdt_add (NULL ) != ESP_OK) {
55+ log_e (" Failed to add current task to WDT" );
56+ }
57+ }
58+
59+ // Enable esp_task_wdt_reset() below to prevent a WDT reset
60+ void loop () {
61+ Serial.printf (" Alive %lums\r\n " , millis ());
62+ // esp_task_wdt_reset();
63+ delay (1000 ); // 1s delay
64+ }
0 commit comments