STM32F407VET6 Black: RTC not keeping time

Post Reply
Jan Erik
Posts: 2
Joined: Thu Mar 18, 2021 11:21 pm
Answers: 1

STM32F407VET6 Black: RTC not keeping time

Post by Jan Erik »

The problem: with a battery in place, the RTC doesn't keep time when power is removed.

Am I wrong in thinking that the RTC should keep time on battery power alone? From other posts on this forum, I realize that I can't expect a very accurate clock but I had assumed that it would keep running when unplugged.

I believe the connection between the battery and the microcontroller is good: without a battery, rtc.isTimeSet() returns false after unplugging the usb cable and plugging it back in. With the battery and the LSI clock source, rtc.isTimeSet() returns true after unplugging the usb cable. The code I used, based on cutting and pasting from Frederic Pillon's RTC_Tests program, is below.

Also, with a battery in place, the time when power was removed is preserved. When plugged back in, it picks up where it left off. With no battery, the time reverts to the time when the microcontroller was programmed.

This board is the one with the built in SD card reader and battery. In other tests, it has behaved as expected. I've been able to get the SD card working along with several I2C sensors (SHT35, BME280) and a one wire temperature sensor (ds18b20).

My appreciation to the developers who have made it possible to use the Arduino ide with STM32 boards. I'm new to the world of microcontrollers and the Arduino environment has made it much more accessible than it would have been otherwise.

Any suggestions would be appreciated.
Jan Erik

Code: Select all

/*
  Cut and pasted from RTC_Tests
  by Frederic Pillon for STMicroelectronics
*/

#include <STM32RTC.h>
STM32RTC& rtc = STM32RTC::getInstance();
static const char* mydate = __DATE__;
static const char* mytime = __TIME__;

static byte seconds = 0;
static byte minutes = 0;
static byte hours = 0;

static byte weekDay = 1;
static byte day = 0;
static byte month = 0;
static byte year = 0;

static STM32RTC::Hour_Format hourFormat = STM32RTC::HOUR_24;
static STM32RTC::AM_PM period = STM32RTC::AM;

void setup()
{
  int forceClockSet = 0;
  while(!Serial) {}
  Serial.println("With battery, LSE_Clock, flashed without removing power");
  STM32RTC::Source_Clock clkSource = rtc.LSE_CLOCK;
  Serial.print("clkSource: ");
  Serial.println(clkSource);
  rtc.begin(hourFormat);
  if (forceClockSet){
    Serial.println("unconditional rtc_config");
    rtc_config(clkSource, rtc.HOUR_24, mydate, mytime);
  }
  if (rtc.isTimeSet()) {
    Serial.println("clock is set");
  } else {
    Serial.println("setting clock");
    rtc_config(clkSource, rtc.HOUR_24, mydate, mytime);
  }
  printDateTime(5, 1000, false);
}

void loop() {
}

Code: Select all

 
// Testing reboot with no battery
No battery, LSI_Clock, flashed without removing power
clkSource: 0
setting clock
03/21/21	12:24:17
03/21/21	12:24:18
03/21/21	12:24:19
03/21/21	12:24:20
03/21/21	12:24:21
// Reboot without removing power, rtc.isTimeSet returns true
No battery, LSI_Clock, flashed without removing power
clkSource: 0
clock is set
03/21/21	12:24:31
03/21/21	12:24:32
03/21/21	12:24:33
03/21/21	12:24:34
03/21/21	12:24:35
// Unplug usb and plug back in, rtc.isTimeSet returns false, clock goes back to time at compile
No battery, LSI_Clock, flashed without removing power
clkSource: 0
setting clock
03/21/21	12:24:17
03/21/21	12:24:18
03/21/21	12:24:19
03/21/21	12:24:20
03/21/21	12:24:21


/* Tests with battery */

With battery, LSI_Clock, flashed after removing power
clkSource: 0
setting clock
03/21/21	12:39:59
03/21/21	12:40:00
03/21/21	12:40:01
03/21/21	12:40:02
03/21/21	12:40:03

// BT0 -> Gnd, reset without removing usb cable
With battery, LSI_Clock, flashed after removing power
clkSource: 0
clock is set
03/21/21	12:41:47
03/21/21	12:41:48
03/21/21	12:41:49
03/21/21	12:41:50
03/21/21	12:41:51

// Unplug usb, wait 5 minutes, plug usb back in, system time is now 12:45:30
// Time when unplugged was stored but clock didn't advance
With battery, LSI_Clock, flashed after removing power
clkSource: 0
clock is set
03/21/21	12:42:33
03/21/21	12:42:34
03/21/21	12:42:35
03/21/21	12:42:36
03/21/21	12:42:37
/[code]
by Jan Erik » Mon Mar 22, 2021 1:29 am
Thanks to GonzoG and ag123.

The missing ingredient was

Code: Select all

 rtc.setClockSource(Source_Clock::LSE_CLOCK ); 
 
I'm now off and running.
Jan Erik
Go to full post
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: STM32F407VET6 Black: RTC not keeping time

Post by GonzoG »

You need to set clock source to LSE before rtc.begin().
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: STM32F407VET6 Black: RTC not keeping time

Post by ag123 »

while i've not experimented with this, try using in setup()

Code: Select all

rtc.setClockSource(Source_Clock::LSE_CLOCK );
rtc.begin(hourFormat);
or even

Code: Select all

rtc.setClockSource(Source_Clock::LSE_CLOCK );
if(rtc.isTimeSet())
	rtc.begin(hourFormat);
VBAT has to be powered up (e.g. with a coin cell) if at times you intend to disconnect VDD
Jan Erik
Posts: 2
Joined: Thu Mar 18, 2021 11:21 pm
Answers: 1

Re: STM32F407VET6 Black: RTC not keeping time

Post by Jan Erik »

Thanks to GonzoG and ag123.

The missing ingredient was

Code: Select all

 rtc.setClockSource(Source_Clock::LSE_CLOCK ); 
 
I'm now off and running.
Jan Erik
Post Reply

Return to “STM32F4 based boards”