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 Num Gen 3 Gen 4 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.

On devices with an external hardware watchdog, including the Tracker One, Monitor One, Tracker SoM, and Muon, the RESET_REASON_WATCHDOG is not used. The reset reason will either be RESET_REASON_PIN_RESET or RESET_REASON_POWER_MANAGEMENT.

Not all reset reasons are supported on all MCUs.


Gen 3 Devices (nRF52) (B-Series SoM, Tracker SoM, Tracker One, Boron, Argon, and E404X):

Gen 3 devices cannot differentiate between RESET_REASON_POWER_DOWN and RESET_REASON_POWER_BROWNOUT and return RESET_REASON_POWER_DOWN in both cases.


Gen 4 Devices (RTL872x) (P2, Photon 2, and M-SoM):

Some reset reasons stored in retained memory (backup RAM, SRAM). On the RTL872x platform, this is cleared on system reset, but in some cases the contents will be saved to flash before reset. In some situations it is not safe or not possible to save the reset reason before reboot, so on the M-SoM, P2, and Photon 2 the reset reason will often default to none.

Gen 4 devices cannot differtiate between RESET_REASON_PIN_RESET or RESET_REASON_POWER_MANAGEMENT and returns RESET_REASON_POWER_MANAGEMENT in both cases.


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.