Page 1 of 2

What causes the stm32 RTC to reset?

Posted: Mon Apr 27, 2020 8:33 am
by Bambo
Hi, i have a question about the RTC library, what would cause the RTC to reset?

Re: What causes the stm32 RTC to reset?

Posted: Mon Apr 27, 2020 8:55 am
by fpiSTM
No Vbat

Re: What causes the stm32 RTC to reset?

Posted: Mon Apr 27, 2020 9:24 am
by Bambo
Looks like when you use LowPower.shutdown() it also resets the RTC

Re: What causes the stm32 RTC to reset?

Posted: Wed Apr 29, 2020 10:30 am
by Bambo
It says in the documentation that the RTC doesn't reset over reboots, but when the unit reboots the RTC is reset?

See: https://github.com/stm32duino/STM32RTC at the bottom "Reset time management By default, if a time is set it will not be reset after a reboot."

Re: What causes the stm32 RTC to reset?

Posted: Wed Apr 29, 2020 11:10 am
by Bambo
Additionally if you use LowPower.deepSleep() the unit carries on processing, you have to use something like this:

Code: Select all

#include <STM32RTC.h>

#include <STM32LowPower.h>

STM32RTC& rtc = STM32RTC::getInstance();
volatile bool sleeping = false;
void callback(void*data)
{
	sleeping = false;
	Serial.println("Callback!");
	Serial.flush();
}

void setup() {
	// put your setup code here, to run once:
	Serial.begin(9600);
	Serial.println("Started");
	Serial.flush();

	LowPower.begin();
	
	rtc.begin();

	if (!rtc.isTimeSet())
	{
		Serial.println("Time did not persist.");
		Serial.flush();
	}
	else
	{
		Serial.println("Time persisted!");
		Serial.flush();
	}
	rtc.setHours(5);
	rtc.setMinutes(5);
	rtc.setSeconds(1);
	rtc.setWeekDay(1);
	rtc.setDay(1);
	rtc.setMonth(1);
	rtc.setYear(20);

	LowPower.enableWakeupFrom(&rtc, callback);
}

void loop() {
	Serial.println("Shutting down!");
	Serial.flush();


	int epochNow = rtc.getEpoch();
	int epochEnd = epochNow + 5;
	
	sleeping = true;
	rtc.setAlarmEpoch(epochEnd);
	LowPower.deepSleep();
	while (sleeping)
	{
	}

	Serial.print("EPOCH: ");
	Serial.println(rtc.getEpoch());
	
	Serial.println("Done!");
	Serial.flush();
}

Re: What causes the stm32 RTC to reset?

Posted: Sun May 03, 2020 10:29 pm
by GonzoG
You need to set RTC source to LSE if you want it to work in shutdown mode or without power (just with RTC battery).

Re: What causes the stm32 RTC to reset?

Posted: Mon May 04, 2020 12:55 pm
by Bambo
ok thank you, do you know what impact using the LSE clock for the RTC will have? does it affect TIM3 or any other timers? Thanks again.

Re: What causes the stm32 RTC to reset?

Posted: Sun May 10, 2020 11:06 pm
by GonzoG
It's just source for RTC, nothing else.
Don't know what board you're using, but with blue pills I found built-in RTC unreliable, probably due to bad quality oscillators.

Re: What causes the stm32 RTC to reset?

Posted: Wed May 20, 2020 10:47 am
by Bambo
Ok so i've added the setClockSource() to the RTC code but now it doesn't restart at all once shutdown is called? Here is the code i'm using to test it.

Code: Select all

#include <STM32RTC.h>

#include <STM32LowPower.h>

STM32RTC& rtc = STM32RTC::getInstance();
volatile bool sleeping = false;
void callback(void* data)
{
	sleeping = false;
	Serial.println("Callback!");
	Serial.flush();
}

void setup() {
	// put your setup code here, to run once:
	Serial.begin(9600);
	Serial.println("Started");
	Serial.flush();

	LowPower.begin();

	rtc.begin();
	rtc.setClockSource(STM32RTC::LSE_CLOCK);

	if (!rtc.isTimeSet())
	{
		Serial.println("Time did not persist.");
		Serial.flush();
	}
	else
	{
		Serial.println("Time persisted!");
		Serial.flush();
	}
	rtc.setHours(5);
	rtc.setMinutes(5);
	rtc.setSeconds(1);
	rtc.setWeekDay(1);
	rtc.setDay(1);
	rtc.setMonth(1);
	rtc.setYear(20);
	LowPower.enableWakeupFrom(&rtc, callback);
}

void loop() {
	Serial.println("Shutting down!");
	Serial.flush();
	LowPower.shutdown(1000);
	Serial.println("Done!");
	Serial.flush();
}
The output is
11:45:12.586 -> Started
11:45:12.722 -> Time did not persist.
11:45:12.757 -> Shutting down!
And then it doesn't start back up.

Re: What causes the stm32 RTC to reset?

Posted: Wed May 20, 2020 11:04 am
by fpiSTM
Which board? It have an LSE?