XIAO MG24 Sense RTC_DATA_ATTR issue

Hi Dave_Reese,

File–>Examples–>Arduino Low Power–>DeepSleepMemory–>DeepSleepMemory.ino
with a few changes, and it works.

//----------------------------------------------------------------------------------------------
// BoardBoard Library : Silicon Labs 2.2.0
// Board Select       : Seeed Studio XIAO MG24 (Sense)
// Plotocol stack     : None 
//----------------------------------------------------------------------------------------------
// IMPORTANT NOTE:Cannot upload new sketches during DeepSleep
//                need to reset after upload

/*
   ArduinoLowPower deep sleep example with timed wakeup and memory retention

   The example shows the basic usage of the Arduino Low Power library by putting the device into deep sleep.
   The device will remain in deep sleep until an external interrupt is triggered or the sleep timer expires.
   During deep sleep the whole device is powered down except for a minimal set of peripherals (like the Back-up RAM and RTC).
   This means that the CPU is stopped and the RAM contents are lost - the device will start from the beginning of the sketch after waking up.

   This example utilizes the deep sleep memory (Back-up RAM) to retain data during deep sleep.
   The back-up RAM is a small amount of RAM (128 bytes) which is retained even in deep sleep.

   This example is compatible with all Silicon Labs Arduino boards.

   Author: Tamas Jozsi (Silicon Labs)
 */

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

// pin definition
#define USER_SW  PC3 // (3), D3 for example

void setup()
{
  Serial.begin(115200);
  Serial.println("---- Deep sleep memory test ----");

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  // Check if we woke up from deep sleep or were reset by an other cause
  if (LowPower.wokeUpFromDeepSleep()) {
    Serial.println("Woke up from deep sleep");
  } else {
    Serial.println("Something else caused a reset other than sleeping");
    // Clear the deep sleep memory at address 0 and 1
    LowPower.deepSleepMemoryWrite(0, 0);
    LowPower.deepSleepMemoryWrite(1, 0);
  }

  // if user sw is on, blink and enable to upload new sketch
  pinMode(USER_SW, INPUT_PULLUP);
  if(digitalRead(USER_SW) == LOW) {
    Serial.println("enable to upload new sketch");
    while(true) {
      digitalWrite(LED_BUILTIN, LOW);
      delay(50);
      digitalWrite(LED_BUILTIN, HIGH);
      delay(50);
    }
  }
  Serial.println("end of setup");
}

void loop()
{
  digitalWrite(LED_BUILTIN, LOW);
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);

  // Read the current sleep cycle counter from the deep sleep memory (address 0)
  uint32_t sleep_cycles = LowPower.deepSleepMemoryRead(0);
  Serial.printf("Sleep cycle counter: %lu\n", sleep_cycles);
  // Increment the sleep cycle counter and write it back to the deep sleep memory
  sleep_cycles++;
  LowPower.deepSleepMemoryWrite(0, sleep_cycles);

  // Read the total uptime counter from the deep sleep memory (address 1)
  uint32_t total_uptime = LowPower.deepSleepMemoryRead(1);
  Serial.printf("Total uptime: %lu\n", total_uptime + millis());
  // Increment the total uptime counter and write it back to the deep sleep memory
  total_uptime += millis();
  LowPower.deepSleepMemoryWrite(1, total_uptime);

  // Go to deep sleep
  // IMPORTANT NOTE:Cannot upload new sketches during DeepSleep
  Serial.println("Going to deep sleep\n");
  Serial.end();
  LowPower.deepSleep(5000);
}

Be sure to look at the following links as well.

2 Likes