cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to store the time of RTC in STM32F042k6 when connected to Duracell DL2032 Battery.

ABogg.1
Associate II

Hello all,

I am unable to save time or restore proper time after I remove Vdd from my MCU when connected to a external 3V li ion battery (DL2032). After every power cycle my time gets reset to its initial default value.

When I supply 3V from a regulated power supply RTC time works as expected. Can anyone please suggest me what kind of external batteries are to be used for STM32F042k6 or is there a special circuit that I should follow?

Thanks in advance!

Amith B G

1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

It is possible to use the RTC with the STM32F042K6 if the MCU is powered by VDD. As long as VDD is supplied, the RTC will also run in STOP mode.

But it is not possible to buffer the VBAT domain due to the missing VBAT pin.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

12 REPLIES 12
Peter BENSCH
ST Employee

The battery type does not matter if the voltage is in the target range, you only have to connect the battery to the pin Vbat.

It looks like it's a software problem instead. Did you create the skeleton of the program using STM32CubeMX or STM32CubeIDE?

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
ABogg.1
Associate II

Thanks Peter for your response.

I am measuring the 3V across VDD (Vbatt) and Gnd terminal when the chip is powered by external battery. Yes, it might also be a software problem. I created the skeleton using STM32CubeMX and have attached a snippet of my configuration.

In the main.c file, I am using HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN); and HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN); api to read time and date.

Please do let me know your thoughts on this.

Thanks & regards,

Amith B G

0693W000005CIGzQAO.png

Peter BENSCH
ST Employee

First of all: in STM32CubeMX you have selected LSI as clock source, whose main usage is to clock the IWDG (Independent Watchdog), which is required for safety applications, where accuracy doesn't matter. The LSI is an independent but rather imprecise clock - the datasheet of the STM32F042 mentions 30...50kHz, i.e. +-25%. You can get an error of +-6 hours/day (yes, six hours) using the LSI instead of the more precise LSE for the RTC.

But back to your main issue: it is probably raised by a feature (not a bug 😊) of STM32CubeMX, which sets the predefined values for time/date and so on in the skeleton program, so it always overwrites (resets) the current RTC values after a reset. This predefined behavior can be useful i.e. when debugging a program, but is somewhat inappropriate when introducing an application being rolled out. A solution compatible to the workflow of STM32CubeMX and STM32CubeIDE has already been discussed here.

When your question is answered, please close this topic by choosing Select as Best.

Good luck!

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
ABogg.1
Associate II

Thanks for your reply!

Yes, I did change the code and added pragma's as explained. It partially solved my problem i.e after every power cycle I can store time or not reset to the initial configured values but I am not able to get the latest or current time after a power cycle since and my time gets incremented from the previously stored values.

regards,

Amith B G

Thanks for your reply!

Yes, I did change the code and added pragma's as explained. It partially solved my problem i.e after every power cycle I can store time or not reset to the initial configured values but I am not able to get the latest or current time after a power cycle since and my time gets incremented from the previously stored values.

regards,

Amith B G

Peter BENSCH
ST Employee

Please don't forget to call HAL_RTC_GetDate() after HAL_RTC_GetTime() when using the HAL, even if you currently don't need the date information.

This is also described in the functions of HAL_RTC_GetDate and  HAL_RTC_GetDate in the RTC driver of the STM32, e.g. stm32f0xx_hal_rtc.c:

You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values in the higher-order calendar shadow registers to ensure consistency between the time and date values. Reading RTC current time locks the values in calendar shadow registers until Current date is read to ensure consistency between the time and date values.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Yes, I am calling HAL_RTC_GetDate() after HAL_RTC_GetTime() as I'll be using both date and time information.

Thanks

Amith B G

ABogg.1
Associate II

@Peter BENSCH​  I am not sure if I was clear with my issue earlier.

After adding pragma's I am able to store the last counted time and retrieve the same value after I power up the device. Now, issue is that the time not is getting incremented when the device is powered with the external battery.

Hope I am clear this time.

Regards,

Amith B G

Peter BENSCH
ST Employee

Sorry, I accidentally didn't notice the entire part number, but it plays a crucial role here: you are currently using the STM32F042K6, which is coming in a 32-pin package. However, the pin VBAT is only available with the 48-pin packages, not with the 20-, 28-, 32-, 36-pin packages.

The only chance now is to change to the STM32F042C6 in 48-pin-package, alternatively other devices with packages larger than 36 pins.

Sorry for the confusion.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.