What causes the stm32 RTC to reset?

Post here all questions related to STM32 core if you can't find a relevant section!
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

What causes the stm32 RTC to reset?

Post by Bambo »

Hi, i have a question about the RTC library, what would cause the RTC to reset?
by fpiSTM » Wed May 20, 2020 2:02 pm
Bambo wrote: Wed May 20, 2020 11:21 am Bug report created here: https://github.com/stm32duino/STM32LowPower/issues/22
This is not a bug.
Simply wrong usage of the API. See my comment here:
https://github.com/stm32duino/STM32LowP ... -631491658
Go to full post
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: What causes the stm32 RTC to reset?

Post by fpiSTM »

No Vbat
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: What causes the stm32 RTC to reset?

Post by Bambo »

Looks like when you use LowPower.shutdown() it also resets the RTC
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: What causes the stm32 RTC to reset?

Post 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."
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: What causes the stm32 RTC to reset?

Post 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();
}
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: What causes the stm32 RTC to reset?

Post 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).
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: What causes the stm32 RTC to reset?

Post 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.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: What causes the stm32 RTC to reset?

Post 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.
Bambo
Posts: 75
Joined: Wed Jan 15, 2020 8:36 pm

Re: What causes the stm32 RTC to reset?

Post 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.
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: What causes the stm32 RTC to reset?

Post by fpiSTM »

Which board? It have an LSE?
Post Reply

Return to “General discussion”