Timed wake up & sleep using RTC (Xiao)

Hello!

I am currently working on a project which uses a Seeeduino Xiao with I2C sensors as a data logger. I want the device to wake up every 10 minutes, take readings and switch off. However, after implementing some code I found online a problem arose. The device will only go on standby for either one minute, one hour, one-day ect no longer or shorter. It is to do with the RTC code in the setup(). The device wakeups up once the two dates match. I’ve tried to mess around with the matching criteria and other code but had no luck so far. Would you have to change the time of the clocks once the device wakes up from standby?

Code:


#include <RTC_SAMD21.h>
#include <DateTime.h>
#include <EnergySaving.h>



EnergySaving myPowSave;
RTC_SAMD21 myRtc;


void setup()
{

  SD.begin(chipSelect);
  myRtc.begin();

  //RTC Setup for Stand - by mode
  myRtc.adjust(DateTime(2022, 9, 15, 19, 5, 0) );
  myRtc.attachInterrupt(dummyfunc);
  myPowSave.begin(WAKE_RTC_ALARM);

  myRtc.disableAlarm();
  DateTime timeNow = myRtc.now();
  const uint16_t uiAlmNextMin = 10;
  DateTime timeAlarm = DateTime(timeNow.year(), timeNow.month(), timeNow.day(), timeNow.hour(), timeNow.minute()+uiAlmNextMin, timeNow.second());
  myRtc.setAlarm(timeAlarm);
  myRtc.enableAlarm(myRtc.MATCH_MMSS);


}

void loop()
{
  delay(300); 

for(uint8_t uilp=0; uilp<2; uilp++)
{
  digitalWrite(PIN_LED,LOW);
  delay(1000);
  digitalWrite(PIN_LED,HIGH);
  delay(1000);
}
  myPowSave.standby();

  //proceed after wakeup


}

void dummyfunc(){
}


Any help would be appreciated by this newbie :slight_smile:

Suppose you want to set the alarm for some future time and you have set variables with values equal to the number of days, hours, minutes, and seconds from now.
alrmDay
alrmHr
alrmMin
alrmSec

Now, DateTime has a convenient ‘+’ operator defined, and you can use the DateTime function TimeSpan() to calculate the time for setting the alarm.

Something like this:

    // You already do this
    DateTime timeNow = myRtc.now();

    // Create a new DateTime object to set the alarm with
    DateTime timeAlarm(timeNow + TimeSpan(alrmDay, alrmHr, alrmMin, alrmSec));

Regards,

Dave

Steps
Open STM32CubeIDE
Create a new project using the NUCLEO-L476 board

Graphical user interface, text, application Description automatically generated

Enter a name for the project.
For this article I will call my project “RTCWakeUP”
Graphical user interface, application Description automatically generated

Initialize all peripherals with their default settings:
To do this please click on Yes below:
Graphical user interface, text, application Description automatically generated
Select RTC
In the “Pinout & Configuration” Tab, under Timers, select RTC:
Graphical user interface, application Description automatically generated
Activate the clock source and enable the Internal WakeUp
First click on “Activate Clock Source” to enable the clock to the RTCperipheral.
For WakeUp, select “Internal WakeUp” as opposed to outputting the WakeUp signal to a pin. This Internal WakeUp will be responsible for waking up the STM32 from low power mode after the programmed period of time set in the RTC.
Graphical user interface, application Description automatically generated
Configure RTC
We will use the values we calculated earlier in this article. Select Wake Up Clock to be RTCCLK /16 and then set the Wake Up Counter to 0x500B.
Graphical user interface, application Description automatically generated

Enable RTC global interrupts
We will be entering STOP mode using WFI (Wait For Interrupts) so to wake up from STOP mode we will need to enable interrupts for the RTC.
Graphical user interface, application Description automatically generated
Set the clock source of the RTC to LSE (external 32.768 KHz crystal that is on the Nucleo board):
In the Clock Configuration Tab select LSE for the RTC source Mux as seen below:
Diagram Description automatically generated

Generate Code
Save the project and that will generate the code.
Adding code
Now we are going to add some code to put the STM32 in a low power mode: STOP 2 mode and wakeup with the RTC that we have configured.
STOP2 mode is one of the lowest power modes offered in the STM32L4 and that offers a fast wakeup time and preserved RAM and registers configuration.
In main, execution loops on turning the LED on for 1 second, entering STOP 2 mode and then the RTC waking up the STM32 after 10 seconds. When the RTC wakes up the device, execution goes to the RTC ISR to clear some RTC fags and then goes back to main to continue code execution.

/* USER CODE BEGIN WHILE */
while (1)
{

            HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
            HAL_Delay(1000);
            HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

            
            HAL_SuspendTick();
            HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0x500B, RTC_WAKEUPCLOCK_RTCCLK_DIV16);

            /* Enter STOP 2 mode */
            HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
            HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
            SystemClock_Config();
         HAL_ResumeTick();
/* USER CODE END WHILE */

Now build the code, load it and reset the board to start execution.
You will see the LED turn on for one second and then off for 10 seconds (STM32 in low power mode) and then RTC wakes up the STM32 and the LED will be turned on again and this will be continuously repeated.

This may help you,
Rachel Gomez