Skip to main content
MMuts.1
Associate
May 4, 2020 7:57 AM UTC
Question

RTC date update - Problem with VBAT / Low Power Mode

Hi all,

I searched the forum for my specific problem, but i did not find any solution for this weird occurrence. On my STM32F103 i run the RTC, which is working fine, as long as it is connected to the USB power. I store the date on 2 backup registers, which get updated when the date is changed and get read, when the MCU starts. When I set the time for testing purposes to e.g. 23:55, pull out the stm32 board from the main power supply, so that it is powered by VBAT, the date seem to be successfully incremented at 00:00. I check that, by debugging the board a few minutes later. The time works fine in any case. It is just the date that causes problems.

The weird problem, that occurs is the following:

When I power off the board and leave it connected to VBAT overnight, or even multiple days, the date value is not incremented at all. How can it be possible, that 5 minutes in low power mode, triggers a date storage in the backup register, while a full day doesn't?

I'm using the cube IDE with the standard RTC HAL. I tried 2 solutions to extend the RTC code to store the date in the backup register:

  1. Write the backup register in "stm32f1xx_hal_rtc.c"@"RTC_DateUpdate". Each time the date is updated, this function is called. I just added the code to store the date and it worked.
  2. I enabled the interrupt for seconds (HAL_RTCEx_RTCEventCallback) and put some conditionals in it, so that the date is always stored, when it updates.

Both derivatives do not work with VBAT connected a longer time (e.g. a day). Please note, that for my purposes, i assume i cannot set the stm32 in the low power mode manually, because it is a "usb stick", that should be unpluggable on demand. Or can I?

Is it possible, that the code in the callback function (2.) is not executed?

I really appreciate any hint or explanation.

Thanks in advance!

This topic has been closed for replies.

8 replies

MMuts.1
MMuts.1Author
Associate
May 4, 2020 7:59 AM UTC

The code, that currently runs:

void init_rtc_time(RTC_HandleTypeDef * hrtc)
 
{
 
 HAL_PWR_EnableBkUpAccess();
 
 __HAL_RCC_BKP_CLK_ENABLE(); //turn on the backup area clock
 
 __HAL_RCC_PWR_CLK_ENABLE(); //turn on the power clock
 
 
 hrtc->Instance = RTC;
 
 hrtc->Init.AsynchPrediv = RTC_AUTO_1_SECOND;
 
 hrtc->Init.OutPut = RTC_OUTPUTSOURCE_NONE;
 
 if (HAL_RTC_Init(hrtc) != HAL_OK)
 
 {
 
 Error_Handler();
 
 }
 
 
 RTC_DateTypeDef DateToUpdate = {0};
 
 
 // Get date values from 16 bit Backup register RTC_BKP_DR3 and RTC_BKP_DR4
 
 uint32_t weekday_and_month_r = HAL_RTCEx_BKUPRead(hrtc, RTC_BKP_DR3);
 
 uint32_t day_and_year_r = HAL_RTCEx_BKUPRead(hrtc, RTC_BKP_DR4);
 
 
 memcpy(&DateToUpdate.WeekDay, &weekday_and_month_r, 2U );
 
 memcpy(&DateToUpdate.Date, &day_and_year_r, 2U );
 
 
 if (HAL_RTC_SetDate(hrtc, &DateToUpdate, RTC_FORMAT_BIN) != HAL_OK)
 
 {
 
 Error_Handler();
 
 }
 
 __HAL_RTC_SECOND_ENABLE_IT(hrtc,RTC_IT_SEC); //turn on RTC clock seconds interrupt
 
 RTC_TimeTypeDef time = {0};
 
 HAL_RTC_GetTime(&time_utils_hrtc, &time, RTC_FORMAT_BIN);
 
 
}
 
 
/*
 
 * Save the date at 00:00:01 and and 00:00:05
 
 * Gets called in second interrupt routine HAL_RTCEx_RTCEventCallback
 
 */
 
void date_backup_routine()
 
{
 
 RTC_TimeTypeDef time = {0};
 
 HAL_RTC_GetTime(&time_utils_hrtc, &time, RTC_FORMAT_BIN);
 
 if( time.Hours == 0 && time.Minutes == 0 && ( time.Seconds == 1 || time.Seconds == 5 )) // save date twice (@ 00:01 and 00:02)
 
 {
 
 RTC_DateTypeDef date = {0};
 
 HAL_RTC_GetDate(&time_utils_hrtc, &date, RTC_FORMAT_BIN);
 
 //Store in 16 bit Backup register
 
 u32 weekday_and_month;
 
 u32 day_and_year;
 
 
 memcpy(&weekday_and_month,&date.WeekDay,2U);
 
 memcpy(&day_and_year,&date.Date,2U);
 
 
 HAL_RTCEx_BKUPWrite(&time_utils_hrtc, RTC_BKP_DR3, weekday_and_month);
 
 HAL_RTCEx_BKUPWrite(&time_utils_hrtc, RTC_BKP_DR4, day_and_year);
 
 }
 
}

waclawek.jan
Super User
May 4, 2020 10:21 AM UTC

Why don't you drop the"library"and simply run RTC add second counter from some chosen epoch, maybe even using the standard functions prototyped in <time.h> ?

JW

MMuts.1
MMuts.1Author
Associate
May 4, 2020 12:19 PM UTC

Good idea, but it looks like, that the interrupt function is not even executed, when the power is off and battery is connected. Do i have to configure that somewhere?

waclawek.jan
Super User
May 4, 2020 7:57 PM UTC

When power is off, processor can't run interrupts, because, well, there's no power...

JW

MMuts.1
MMuts.1Author
Associate
May 11, 2020 11:41 AM UTC

Yes, but there is a lower power mode, that may allow interrupts, isn't there?

waclawek.jan
Super User
May 11, 2020 3:05 PM UTC

Yes but that would need you have some battery connected to VDD.

VBAT pin can't power the processor itself.

JW

MMuts.1
MMuts.1Author
Associate
May 12, 2020 7:25 AM UTC

Ok, so The f103 does not properly support time, only time without date. Or is there another way to let the date increase?

waclawek.jan
Super User
May 12, 2020 8:08 AM UTC

> Or is there another way to let the date increase?

Yes. See my first reply.

JW