Skip to content

Never waking up when alarm is set without reseting RTC #82

Closed
stm32duino/STM32RTC
#77
@ClementFoucher

Description

@ClementFoucher

Hello,

Note that I'm confused about where to post this issue, as the problem described below is linked to the sleep modes, but the partial workaround I found involves the RTC library.

I'm experiencing an issue where after being put in sleep or deepSleep mode with a programed alarm time, the µC seems to never wake up.
The issue depicted here was tested with the following Nucleo boards: L476RG and G474RE; and the following Adafruit board: STM32F405 Feather Express.

When I try to use the sleep modes in combination with a RTC alarm-based wake up at a specific time, the board will never wake up unless the RTC has been reset when calling its begin() function. When true is passed to that function, the behavior is correct. However, what I would like to do involves preserving the RTC time across reset (the µC has an attached battery to preserve its time), thus resetting RTC at each boot is not a viable solution.

Note that I used the very latest versions of RTC and Low Power libraries: #f620b53 version for RTC and #1a17d41 version for Low Power. I even integrated the latest PR #79 of the Low Power library without more luck.

Here is a minimal test I set up, displaying current time every minute on the serial console. Behavior can be switched between correct and faulty behavior using the MODE macro as depicted in the comments:

#include <Arduino.h>
#include <STM32RTC.h>
#include <STM32LowPower.h>

// Mode 0: works correctly, but RTC has to be reset so time can't be preserved across reboot
//#define MODE 0
// Mode 1: RTC time preserved, but never wakes up
#define MODE 1

STM32RTC& rtc = STM32RTC::getInstance();

void displayTime(uint8_t h, uint8_t m, uint8_t s)
{
	String text;
	text  = String(h);
	text += ":";
	if (m < 10) text += "0";
	text += String(m);
	text += ":";
	if (s < 10) text += "0";
	text += String(s);
	text += '\n';

	Serial.print(text);
	Serial.flush();
}

void sleepTillNextMinute(uint8_t h, uint8_t m)
{
	m = (m+1)%60;
	if (m==0) h = (h+1)%24;

	rtc.setAlarmTime(h, m, 0);
	rtc.enableAlarm(rtc.MATCH_HHMMSS);
	LowPower.deepSleep(); // or .sleep(): same behavior
}

void setup()
{
	Serial.begin(115200);

	rtc.setClockSource(STM32RTC::LSE_CLOCK);

#if MODE == 0
	// Working, but clock is reinitialized at every reset
	rtc.begin(true);
	rtc.setTime(7, 30, 45);
#elif MODE == 1
	// Time is correctly preserved, but board will never wake up
	rtc.begin();
	if (!rtc.isConfigured())
		rtc.setTime(7, 30, 45);
#endif

	LowPower.begin();
}

void loop()
{
	uint8_t h, m, s;
	rtc.getTime(&h, &m, &s, nullptr);
	displayTime(h, m, s);
	sleepTillNextMinute(h, m);
}

Thanks for your support.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions