Unable to store the time of RTC in STM32F042k6 when connected to Duracell DL2032 Battery.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-20 05:42 AM
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
Solved! Go to Solution.
- Labels:
-
RTC
-
STM32F0 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-24 07:47 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-23 01:20 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-23 05:03 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-23 05:32 AM
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 :smiling_face_with_smiling_eyes:) 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-23 08:07 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-23 08:08 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-23 11:58 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-24 04:08 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-24 04:15 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-11-24 05:06 AM
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