System calls
Reset reason
System.resetReason, resetReason
Since 0.6.0:
The system can track the hardware and software resets of the device.
// EXAMPLE
// Restart in safe mode if the device previously reset due to a PANIC (SOS code)
void setup() {
System.enableFeature(FEATURE_RESET_INFO);
if (System.resetReason() == RESET_REASON_PANIC) {
System.enterSafeMode();
}
}
You can also pass in your own data as part of an application-initiated reset:
// EXAMPLE
void setup() {
System.enableFeature(FEATURE_RESET_INFO);
// Reset the device 3 times in a row
if (System.resetReason() == RESET_REASON_USER) {
uint32_t data = System.resetReasonData();
if (data < 3) {
System.reset(data + 1);
}
} else {
// This will set the reset reason to RESET_REASON_USER
System.reset(1);
}
}
Note: This functionality requires FEATURE_RESET_INFO
flag to be enabled in order to work.
resetReason()
Returns a code describing reason of the last device reset. The following codes are defined:
RESET_REASON_PIN_RESET
: Reset button or reset pinRESET_REASON_POWER_MANAGEMENT
: Low-power management resetRESET_REASON_POWER_DOWN
: Power-down resetRESET_REASON_POWER_BROWNOUT
: Brownout resetRESET_REASON_WATCHDOG
: Hardware watchdog resetRESET_REASON_UPDATE
: Successful firmware updateRESET_REASON_UPDATE_TIMEOUT
: Firmware update timeoutRESET_REASON_FACTORY_RESET
: Factory reset requestedRESET_REASON_SAFE_MODE
: Safe mode requestedRESET_REASON_DFU_MODE
: DFU mode requestedRESET_REASON_PANIC
: System panicRESET_REASON_USER
: User-requested resetRESET_REASON_UNKNOWN
: Unspecified reset reasonRESET_REASON_NONE
: Information is not available
resetReasonData()
Returns a user-defined value that has been previously specified for the System.reset()
call.
reset(uint32_t data)
This overloaded method accepts an arbitrary 32-bit value, stores it to the backup register and resets the device. The value can be retrieved via resetReasonData()
method after the device has restarted.