Timed sleep with wlinterrupt

Please carefully read the comments at the beginning of the sketch below and the wakeup pin settings before trying this.
Unfortunately, the current ArduinoLowPower.cpp has a bug that needs to be fixed.

------------------------------------------------------------------------------------------
// BoardBoard Library : Silicon Labs 2.2.0
// Board Select       : Seeed Studio XIAO MG24 (Sense)
// Plotocol stack     : None 
//----------------------------------------------------------------------------------------------
// 2025/02/17

// IMPORTANT NOTE:Cannot upload new sketches during DeepSleep
// When PC0(D0)(0)==LOW and reset, enable to upload. After uploading, be sure to set PC0==HIGH and Reset. 
// \Arduino15\packages\SiliconLabs\hardware\silabs\2.2.0\libraries\ArduinoLowPower\src\ArduinoLowPower.cpp:L234
//      Overrides the WEAK escapeHatch() function in main.cpp to provide a way to prevent
//      the device from going to EM4 too quickly. If the device enters EM4 sleep the programmer
//      is not able to communicate with it - and without this mechanism it can easily be bricked.
//      This function will run before any user code and keep the device awake when the built-in button
//      (or the configured escape pin) is pressed during startup so that the programmer has a chance to
//      communicate with it.
//      void escape_hatch()
// \Arduino15\packages\SiliconLabs\hardware\silabs\2.2.0\variants\xiao_mg24\pins_arduino.h:L114
//      #define DEEP_SLEEP_ESCAPE_PIN   D0 // PC0

// Reference Manual, 24.3.12.3 Pin Function Tables, Table 24.3. GPIO Alternate Function Table
// WAKE_UP PC0(ESCAPE), PC5(SCL), PC7(RX), PA5(MOSI), back side : PB1, PB3, PD2, PD5 
// When PC0 is used for the WAKE_UP function, set it LOW for a short time to distinguish it from the ESCAPR function.

// If set timer and external pin at the same time, wake up immediately
// Need to modify \Arduino15\packages\SiliconLabs\hardware\silabs\2.2.0\libraries\ArduinoLowPower\src\ArduinoLowPower.cpp
// LINE130 ~ LINE134
/*
//  this->setupDeepSleepWakeUpPin();
//
//  EMU_EM4Init_TypeDef em4_init = EMU_EM4INIT_DEFAULT;
//  EMU_EM4Init(&em4_init);
//  EMU_EnterEM4();
  EMU_EM4Init_TypeDef em4_init = EMU_EM4INIT_DEFAULT;
  em4_init.pinRetentionMode = emuPinRetentionEm4Exit;
  EMU_EM4Init(&em4_init);
  this->setupDeepSleepWakeUpPin();
  EMU_EnterEM4();
*/

#include <Arduino.h>
#include "ArduinoLowPower.h"

#define WAKE_UP_PIN PC0 // PC0(ESCAPE), PC5(SCL), PC7(RX), PA5(MOSI), back side : PB1, PB3, PD2, PD5

void setup()
{
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE);
  Serial.println("Deep sleep with external or timed wakeup");
}

void loop()
{
  digitalWrite(LED_BUILTIN, LED_BUILTIN_ACTIVE);
  delay(500);
  digitalWrite(LED_BUILTIN, LED_BUILTIN_INACTIVE);
  delay(500);

  Serial.printf("Going to deep sleep for 10s at %\lu\n", millis());
  delay(10);

  // enter Deep Sleep
  LowPower.attachInterruptWakeup(WAKE_UP_PIN, nullptr, FALLING);  // available GPIO wakeup only while deep sleep
  LowPower.deepSleep(10000);                                      // not abailable GPIO wakeup while light sleep
}

1 Like