2014-05-12 12:29 AM
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 angelo2014-05-12 06:23 AM
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?)2014-05-12 10:02 AM
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
2014-05-13 03:25 AM
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.2014-05-13 06:39 AM
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 Peacock2014-05-13 09:45 AM
2015-06-15 03:38 AM
Hi, guys,
I have the same problem as you now.I have installed VBAT andLSE oscillator
I am wondering wether you have fix it? How do you fix it?Thanks a lot2015-06-15 04:00 AM
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.2015-06-15 04:08 AM
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.2015-06-15 04:42 AM
You've edited the post at least three times.