Last active
June 29, 2022 17:33
-
-
Save Tech500/e406653d2e459a5affb2c9157df350c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <rom/rtc.h> | |
#include <Ticker.h> | |
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ | |
void verbose_print_reset_reason(int reason) | |
{ | |
switch ( reason) | |
{ | |
case 1 : Serial.println ("Vbat power on reset");break; | |
case 3 : Serial.println ("Software reset digital core");break; | |
case 4 : Serial.println ("Legacy watch dog reset digital core");break; | |
case 5 : Serial.println ("Deep Sleep reset digital core");break; | |
case 6 : Serial.println ("Reset by SLC module, reset digital core");break; | |
case 7 : Serial.println ("Timer Group0 Watch dog reset digital core");break; | |
case 8 : Serial.println ("Timer Group1 Watch dog reset digital core");break; | |
case 9 : Serial.println ("RTC Watch dog Reset digital core");break; | |
case 10 : Serial.println ("Instrusion tested to reset CPU");break; | |
case 11 : Serial.println ("Time Group reset CPU");break; | |
case 12 : Serial.println ("Software reset CPU");break; | |
case 13 : Serial.println ("RTC Watch dog Reset CPU");break; | |
case 14 : Serial.println ("for APP CPU, reseted by PRO CPU");break; | |
case 15 : Serial.println ("Reset when the vdd voltage is not stable");break; | |
case 16 : Serial.println ("RTC Watch dog reset digital core and rtc module");break; | |
default : Serial.println ("NO_MEAN"); | |
} | |
} | |
Ticker secondTick; | |
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; | |
volatile int watchdogCounter; | |
int totalwatchdogCounter; | |
void IRAM_ATTR ISRwatchdog() { | |
portENTER_CRITICAL_ISR(&mux); | |
watchdogCounter++; | |
portEXIT_CRITICAL_ISR(&mux); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("CPU0 reset reason:"); | |
//print_reset_reason(rtc_get_reset_reason(0)); | |
verbose_print_reset_reason(rtc_get_reset_reason(0)); | |
Serial.println("CPU1 reset reason:"); | |
//print_reset_reason(rtc_get_reset_reason(1)); | |
verbose_print_reset_reason(rtc_get_reset_reason(1)); | |
/* | |
// Set ESP32 to go to deep sleep to see a variation | |
// in the reset reason. Device will sleep for 5 seconds. | |
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); | |
Serial.println("Going to sleep"); | |
esp_deep_sleep(5 * uS_TO_S_FACTOR); | |
*/ | |
secondTick.attach(1, ISRwatchdog); //watchdog ISR triggers every 1 second | |
delay(50 * 1000); //test watchdog | |
} | |
void loop() { | |
if (watchdogCounter > 45) | |
{ | |
totalwatchdogCounter++; | |
Serial.println("watchdog Triggered"); | |
Serial.println("Watchdog restart"); | |
ESP.restart(); | |
} | |
//watchdogCounter = 0; | |
} | |
//https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ResetReason/ResetReason.ino |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment