cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Losing time after power restart

KBhon.1
Associate III

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.

 

15 REPLIES 15

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".

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...

 

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!

Piranha
Chief II

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/

If you try Schottky diodes approch, GND and GND1 MUST be connected directly. Not through L3.

If you try to keep LDO to feed Vdda with separate GND and GND1, you must verify that 24v gnd is GND1 and that vdd supply is referenced to GND. Keeping GND and GND1 connected in only one point (perhaps @pads of L3, but without any choke... ;) )

 

KBhon.1
Associate III

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: