2020-05-04 12:57 AM
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:
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!
2020-05-04 12:59 AM
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);
}
}
2020-05-04 03:21 AM
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
2020-05-04 05:19 AM
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?
2020-05-04 12:57 PM
When power is off, processor can't run interrupts, because, well, there's no power...
JW
2020-05-11 04:41 AM
Yes, but there is a lower power mode, that may allow interrupts, isn't there?
2020-05-11 08:05 AM
Yes but that would need you have some battery connected to VDD.
VBAT pin can't power the processor itself.
JW
2020-05-12 12:25 AM
Ok, so The f103 does not properly support time, only time without date. Or is there another way to let the date increase?
2020-05-12 01:08 AM
> Or is there another way to let the date increase?
Yes. See my first reply.
JW