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_NONE: Information is not available (0)
  • RESET_REASON_UNKNOWN: Unspecified reset reason (10)
  • RESET_REASON_PIN_RESET: Reset button or reset pin (20)
  • RESET_REASON_POWER_MANAGEMENT: Low-power management reset (30)
  • RESET_REASON_POWER_DOWN: Power-down reset (40)
  • RESET_REASON_POWER_BROWNOUT: Brownout reset (50)
  • RESET_REASON_WATCHDOG: Hardware watchdog reset (60)
  • RESET_REASON_UPDATE: Successful firmware update (70)
  • RESET_REASON_UPDATE_ERROR: Firmware update error, deprecated (80)
  • RESET_REASON_UPDATE_TIMEOUT: Firmware update timeout (90)
  • RESET_REASON_FACTORY_RESET: Factory reset requested (100)
  • RESET_REASON_SAFE_MODE: Safe mode requested (110)
  • RESET_REASON_DFU_MODE: DFU mode requested (120)
  • RESET_REASON_PANIC: System panic (130)
  • RESET_REASON_USER: User-requested reset (140)

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.