cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f405 rtc help

Angelo1
Associate II
Posted on May 12, 2014 at 09:29

Dear all,

i am using STM32F405 with internal osc. clock.

I connected a 3V lithium battery as VBAT, and would like to use the RTC to keep time when power is off.

I read some samples here and there and implemented some RTC init / setup routines (finally using RTC_SetTime / RTC_SetDate). Until now, i can save time / date to RTC, but if i power off and on after 10 seconds, i see the time set as the same of power off (10 seconds old) while i was expecting the updated time.

Every help is appreciated.

Regards

angelo

19 REPLIES 19
chen
Associate II
Posted on May 12, 2014 at 15:23

Hi

It could be a number of things (I have not done this myself so I am not 100% sure which it is) :

Battery Backup domain - must be turn on to enable LSI to run off battery.

The LSI clock configure to run in STOP/STANDBY mode - not sure if this needs to be done or not.

The LSI must be enabled - I think you have done this (right?)

Angelo1
Associate II
Posted on May 12, 2014 at 19:02 Many thanks, i looked for some samples in the net, but still getting the same issue, after re-power on, i still find the power off time. Like if no RTC time increment happen when power is off. I attach my code, that's very similar to a sample found in the forum. I am wandering if the time increment on battery domain is completely handled by RTC hw or some interrupt must be set.

uint32_t SynchPrediv = 0, AsynchPrediv = 0;
#define BKP_VALUE 0x32F1
void
rtc_setup_time(
int
DD, 
int
MM, 
int
YY, 
int
hh, 
int
mm, 
int
ss)
{
RTC_TimeTypeDef rtc_time;
RTC_DateTypeDef rtc_date;
/* Set the Time */
rtc_time.RTC_Hours = hh;
rtc_time.RTC_Minutes = mm;
rtc_time.RTC_Seconds = ss;
/* Set the Date */
rtc_date.RTC_Date = DD;
rtc_date.RTC_Month = MM;
rtc_date.RTC_Year = YY;
rtc_date.RTC_WeekDay = 0; 
//RTC_Weekday_Wednesday;
/* Set Current Time and Date */
RTC_SetTime(RTC_Format_BIN, &rtc_time);
RTC_SetDate(RTC_Format_BIN, &rtc_date);
/* Disable the Alarm A */
RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
/* MARK HAS BEEN FIRST INITIALIZED */
RTC_WriteBackupRegister(RTC_BKP_DR0, BKP_VALUE);
}
static
void
rtc_first_config(
void
)
{
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while
(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) { }
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
SynchPrediv = 0x18F;
AsynchPrediv = 0x63;
/* Enable the RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
}
void
rtc_init()
{
/* CHECK IF IT NEEDS SETUP */
if
(RTC_ReadBackupRegister(RTC_BKP_DR0) != BKP_VALUE) {
RTC_InitTypeDef rtc_init;
rtc_first_config();
/* Configure the RTC data register and RTC prescaler */
rtc_init.RTC_AsynchPrediv = AsynchPrediv;
rtc_init.RTC_SynchPrediv = SynchPrediv;
rtc_init.RTC_HourFormat = RTC_HourFormat_24;
/* Now RTC init */
RTC_Init(&rtc_init);
rtc_setup_time(01, 01, 14, 00, 00, 00);
} 
else
{
/* Enable the PWR clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
/* Allow access to RTC */
PWR_BackupAccessCmd(ENABLE);
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait for RTC APB registers synchronisation */
RTC_WaitForSynchro();
/* Clear the RTC Alarm Flag */
RTC_ClearFlag(RTC_FLAG_ALRAF);
/*
* Clear the EXTI Line 17 Pending bit
* (Connected internally to RTC Alarm)
*/
EXTI_ClearITPendingBit(EXTI_Line17);
}
}

Thanks, angelo
chen
Associate II
Posted on May 13, 2014 at 12:25

Hi

As I said, you need to

Battery Backup domain - must be turn on to enable LSI to run off battery.

This may or may no be needed :

The LSI clock configure to run in STOP/STANDBY mode - not sure if this needs to be done or not.

Sorry, I do not have the time to write the code for you. I am a professional Embbeded software engineer with a job to do.

I can give you pointers and explain how the device works but writing and debugging the code is up to you.

jpeacock2399
Associate II
Posted on May 13, 2014 at 15:39

You need to use the LSE oscillator for the RTC if you want it to work with VBAT.  The LSI oscillator shuts down when main power is off.  Only the LSE oscillator remains running without VDD.

  Jack Peacock

Angelo1
Associate II
Posted on May 13, 2014 at 18:45

ahaaaa, thanks.

Now is clear. Fortunately, i already designed the board PCB with an external crystal. Will go for ext clock.

Many thanks.

angelo

Linda
Associate II
Posted on June 15, 2015 at 12:38

Hi, guys,

I have the same problem as you now.I have installed VBAT and 

 LSE oscillator

I am wondering wether you have fix it? How do you fix it?

Thanks a lot

Posted on June 15, 2015 at 13:00

I have the same problem as you now. I am wondering whether you have fix it? How do you fix it?

By having an external LSE, and have it functional...

Seem to think that was covered in your other thread, unless you'd reading some other problem into this one.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 15, 2015 at 13:08

It's important to enable the LSE, and have it selected as the RTC source clock.

If the clock's not actually functional, nothing else is going to behave correctly.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 15, 2015 at 13:42

You've edited the post at least three times.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..