Skip to content

Commit 5e19757

Browse files
committed
Merge pull request esp8266#1495 from hallard/master
Added function to return reset reason in human readable format
2 parents 5708c69 + c81abd2 commit 5e19757

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

cores/esp8266/Esp.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,28 @@ bool EspClass::checkFlashConfig(bool needsEquals) {
329329
return false;
330330
}
331331

332+
String EspClass::getResetReason(void) {
333+
char buff[32];
334+
if (resetInfo.reason == REASON_DEFAULT_RST) { // normal startup by power on
335+
strcpy_P(buff, PSTR("Power on"));
336+
} else if (resetInfo.reason == REASON_WDT_RST) { // hardware watch dog reset
337+
strcpy_P(buff, PSTR("Hardware Watchdog"));
338+
} else if (resetInfo.reason == REASON_EXCEPTION_RST) { // exception reset, GPIO status won’t change
339+
strcpy_P(buff, PSTR("Exception"));
340+
} else if (resetInfo.reason == REASON_SOFT_WDT_RST) { // software watch dog reset, GPIO status won’t change
341+
strcpy_P(buff, PSTR("Software Watchdog"));
342+
} else if (resetInfo.reason == REASON_SOFT_RESTART) { // software restart ,system_restart , GPIO status won’t change
343+
strcpy_P(buff, PSTR("Software/System restart"));
344+
} else if (resetInfo.reason == REASON_DEEP_SLEEP_AWAKE) { // wake up from deep-sleep
345+
strcpy_P(buff, PSTR("Deep-Sleep Wake"));
346+
} else if (resetInfo.reason == REASON_EXT_SYS_RST) { // external system reset
347+
strcpy_P(buff, PSTR("External System"));
348+
} else {
349+
strcpy_P(buff, PSTR("Unknown"));
350+
}
351+
return String(buff);
352+
}
353+
332354
String EspClass::getResetInfo(void) {
333355
if(resetInfo.reason != 0) {
334356
char buff[200];

cores/esp8266/Esp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class EspClass {
131131
uint32_t getFreeSketchSpace();
132132
bool updateSketch(Stream& in, uint32_t size, bool restartOnFail = false, bool restartOnSuccess = true);
133133

134+
String getResetReason();
134135
String getResetInfo();
135136
struct rst_info * getResetInfoPtr();
136137

doc/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ title: Change Log
66

77
### Core
88

9+
- Add function to know last reset resaon.
910
- Allow control of enabling debug and debug level from IDE
1011
- Make HardwareSerial::begin() and end() interrupt safe
1112
- Put HardwareSerial and cbuf methods called from interrupt context in RAM

doc/libraries.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ APIs related to deep sleep and watchdog timer are available in the `ESP` object,
8383

8484
`ESP.restart()` restarts the CPU.
8585

86+
`ESP.getResetReason()` returns String containing the last reset resaon in human readable format.
87+
8688
`ESP.getFreeHeap()` returns the free heap size.
8789

8890
`ESP.getChipId()` returns the ESP8266 chip ID as a 32-bit integer.

tools/sdk/include/user_interface.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
#endif
2424

2525
enum rst_reason {
26-
REASON_DEFAULT_RST = 0,
27-
REASON_WDT_RST = 1,
28-
REASON_EXCEPTION_RST = 2,
29-
REASON_SOFT_WDT_RST = 3,
30-
REASON_SOFT_RESTART = 4,
31-
REASON_DEEP_SLEEP_AWAKE = 5,
32-
REASON_EXT_SYS_RST = 6
26+
REASON_DEFAULT_RST = 0, /* normal startup by power on */
27+
REASON_WDT_RST = 1, /* hardware watch dog reset */
28+
REASON_EXCEPTION_RST = 2, /* exception reset, GPIO status won’t change */
29+
REASON_SOFT_WDT_RST = 3, /* software watch dog reset, GPIO status won’t change */
30+
REASON_SOFT_RESTART = 4, /* software restart ,system_restart , GPIO status won’t change */
31+
REASON_DEEP_SLEEP_AWAKE = 5, /* wake up from deep-sleep */
32+
REASON_EXT_SYS_RST = 6 /* external system reset */
3333
};
3434

3535
struct rst_info{

0 commit comments

Comments
 (0)