Skip to main content
KBhon.1
Associate II
October 22, 2023
Question

RTC Losing time after power restart

  • October 22, 2023
  • 6 replies
  • 11725 views

Hello all,

I am working on RTC of STM32F401RC. I have connected a new CR2032 battery to VBAT.

My problem is that when I restart the power I lose time. I have disabled the calendar generation and setting time manually so there is no possibility of time reset due to SW. Apart from that, I also tried to set the backup register and read it after power restart, the backup register also resets to 0.

To make sure I also tried on other MCU STM32F407, and it still has the same issue. The RTC circuit for both F401 and F407 boards is the same and are populated on my custom designed boards.

In order to make sure the battery is not the issue, I used a new different battery, and also tried powering VBAT using 3.3V derived from different board and still the time gets reset.

Here is the schematic of my RTC circuit.

KBhon1_0-1697944232160.png

 

Main Code:

KBhon1_2-1697944416432.png

 

Here is my RTC init function:


static void MX_RTC_Init(void)
{

/* USER CODE BEGIN RTC_Init 0 */

/* USER CODE END RTC_Init 0 */

RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};

/* USER CODE BEGIN RTC_Init 1 */

/* USER CODE END RTC_Init 1 */

/** Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}

/* USER CODE BEGIN Check_RTC_BKUP */

/* USER CODE END Check_RTC_BKUP */

/* USER CODE BEGIN RTC_Init 2 */

/* USER CODE END RTC_Init 2 */

}

 

 

Please help me with this. I am kind of stuck and must solve this problem.

 

This topic has been closed for replies.

6 replies

waclawek.jan
Super User
October 22, 2023

VDDA/VREF+ is connected exactly how?

JW

KBhon.1
KBhon.1Author
Associate II
October 22, 2023

@waclawek.jan Thanks for the reply.

 

VDDA/VREF+ are connected together and are powered by using a separate LDO power supply. This is done to keep analog and digital voltages separate. Also, I would like to mention that VSSA/VREF- are connected together and are shorted to digital Gnd using a choke. This is done to keep analog and digital Gnds on separate sides (but still connected using choke).

Here is how the supplies are:

The please note in the image below VSSA/VREF- are on separate GND node and the choke L3 is used to connect both the digital and analog Gnds.

KBhon1_0-1697977093867.png

 

GLASS
Visitor II
October 22, 2023

Seems that vdda is not correctly decoupled. IC10 is on GND and not GND1.

having a digital ground "separated" from analog ground is a good routing practice to avoid high speed return current from digital altering analog ground. But Agnd and Gnd MUST be connected with LOW impedance in one point (Star).

F4 need that vdda is "near" vdd. Look at datasheet. So may be give a try shorting L3 and deliver vdda from vdd through a choke. Not using a dedicated LDO that is challengy to guaranteed vdda "near" vdd as required by F4...

 

KBhon.1
KBhon.1Author
Associate II
October 22, 2023

Thank @GLASS 

That was a really good find. We really missed and should have kept GND1 as reference GND for the regulator IC10. 

We have previously used VDDA and VDD by only having a choke in between them, however using a separate supply for VDDA has really greatly increased ADC readings stability.

The solutions suggested by other community members (using of Schottky diode) looks promising and I hope to get good results with those updates.

I will post my test results in a day and we will all see how it goes.

Thanks!

TDK
October 22, 2023

> I also tried to set the backup register and read it after power restart, the backup register also resets to 0.

Suggests this isn't purely an RTC issue, but a hardware issue.

I would suggest connecting VDDA/VDD directly per the datasheet recommendation as well as VSSA/VSS. You could also try out your code on known good hardware such as a nucleo board.

Note that VDD and VDDA must be within 300 mV of each other during power-up. Do you ensure that?

"If you feel a post has answered your question, please click ""Accept as Solution""."
KBhon.1
KBhon.1Author
Associate II
October 22, 2023

@TDK Thanks for the insight.

However we have followed ST app note AN2834 to design analog circuitry. On page 42, 43 it clearly suggests to keep analog and digital supplies separate and to connect analog and digital Gnds at one single point as we did using choke.

Here is the link for app note (Page 42,43)

https://www.st.com/resource/en/application_note/an2834-how-to-optimize-the-adc-accuracy-in-the-stm32-mcus-stmicroelectronics.pdf

 

Here I have attached its screenshots

KBhon1_0-1697982312022.png

KBhon1_1-1697982353610.png

 

I can remove choke on GND line and short them directly, however in our another test board there is no such choke but still it gives the same RTC problem. I really do not want to merge VDDA and VDD, since keeping them separately has impacted hugely on our analog reading section.

TDK
October 22, 2023

I understand. For the purposes of debugging, if powering them from a single rail fixes the problem, you now know what the issue is.

Other series of chips are more forgiving on separating VDD/VDDA, but mind the ±300mV requirement during startup/shutdown on the F4. Going to be hard or impossible to guarantee that with truly independent supplies.

TDK_1-1697983804210.png

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
KBhon.1
KBhon.1Author
Associate II
October 22, 2023

@TDK @AScha.3 

Thanks both of you for your valuable insights.

 

@AScha.3 

To confirm I get you correctly, Is this how you want me to test your 2nd option?

KBhon1_0-1697986603946.png

In this, I am considering

1. VDD and VDDA are still getting power from separate sources.

2. Keep mosfet pulled down at first and then give high to disconnect both the supplies.

 

AScha.3
Super User
October 22, 2023

right.

mos could be something like this: SI2367DS-T1-GE3  sot23

(at mouser: 30ct, 66 mOhm rds-on) 

"If you feel a post has answered your question, please click ""Accept as Solution""."
Piranha
Principal III
October 22, 2023

First, you are using the HAL_RTC_GetTime() improperly. Read the HAL documentation. Second, the HAL_RTC_Init() in the current latest CubeF4 v1.27.1 still reinitializes the RTC on every call and therefore looses seconds. Third, there really is no way of reading the RTC values properly with the HAL/Cube broken bloatware. Read this:

https://community.st.com/t5/stm32-mcus-embedded-software/32f417-rtc-errata-sheet-if-reading-subseconds-is-this-code-ok/m-p/207827

And everything related to RTC there: http://www.efton.sk/STM32/gotcha/

KBhon.1
KBhon.1Author
Associate II
October 23, 2023

Hello,

Thank you all for your valuable suggestions and insights.

As suggested, adding anti parallel Schottky diodes between VDDA and VDD worked and my RTC is now able to keep time after power restart. It seems that it was indeed the issue of that  ±300mV difference at the startup. 

I could not test Mosfet solution yet as I am unable to adjust the mosfet ckt near those 2 supply rails. However I will  certainly test it and share my results here as I feel going for the mosfet will give me better balance between RTC backup and ADC stability.

 

Thank you again for all the suggestions! :folded_hands: