System calls
Reset reason
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() - System
System.resetReason()
Returns a code describing reason of the last device reset. The following codes are defined:
Constant | Numeric | Meaning | Data |
---|---|---|---|
RESET_REASON_NONE |
0 | Information is not available | 0 |
RESET_REASON_UNKNOWN |
10 | Unspecified reset reason | |
RESET_REASON_PIN_RESET |
20 | Reset button or reset pin | |
RESET_REASON_POWER_MANAGEMENT |
30 | Low-power management reset | |
RESET_REASON_POWER_DOWN |
40 | Power-down reset | |
RESET_REASON_POWER_BROWNOUT |
50 | Brownout reset | |
RESET_REASON_WATCHDOG |
60 | Hardware watchdog reset | |
RESET_REASON_UPDATE |
70 | Successful firmware update | |
RESET_REASON_UPDATE_ERROR |
80 | Firmware update error, deprecated | |
RESET_REASON_UPDATE_TIMEOUT |
90 | Firmware update timeout | |
RESET_REASON_FACTORY_RESET |
100 | Factory reset requested | 0 |
RESET_REASON_SAFE_MODE |
110 | Safe mode requested | 0 |
RESET_REASON_DFU_MODE |
120 | DFU mode requested | 0 |
RESET_REASON_PANIC |
130 | System panic | Panic code |
RESET_REASON_USER |
140 | User-requested reset | User-defined |
This is also uploaded to the cloud using the last_reset event after reconnecting to the cloud.
resetReasonData() - System
System.resetReasonData()
Returns a user-defined value that has been previously specified for the System.reset()
call.
If the reason is RESET_REASON_PANIC
, then the reasonReasonData() is the panic code. For more information on the meaning of these codes, see red blink SOS.
Numeric value | Constant |
---|---|
1 | HardFault |
2 | NMIFault |
3 | MemManage |
4 | BusFault |
5 | UsageFault |
6 | InvalidLenth |
7 | Exit |
8 | OutOfHeap |
9 | SPIOverRun |
10 | AssertionFailure |
11 | InvalidCase |
12 | PureVirtualCall |
13 | StackOverflow |
14 | HeapError |
15 | SecureFault |
reset() - System
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.