cancel
Showing results for 
Search instead for 
Did you mean: 

Hello, I am using STM32F407ZGT6 and have the issue with time of RTC peripheral which resets to initial value after power down the board while VBAT pin is connected to CR1220 battery and LSE is used as crystal. How can I solve it?

Aasda.3
Associate II
51 REPLIES 51

Thanks JTP.

unfortunately none of them didn't help me.

Thanks AScha.3.

when I do it, time of RTC doesn't work.

Thanks AScha.3.

when I comment this function, time of RTC doesn't work properly.

Thanks AScha.3.

When I comment this ( // MX_RTC_Init();), time of RTC doesn't work.

ok, but what about the backup ram ? reset?

+ Vbat battery has to be always connected !

If you feel a post has answered your question, please click "Accept as Solution".
JTP1
Lead

Seems that your are using actually LSI as RTC clock source and it is not powered with backup voltage. Maybe easiest to renerate code from cube... or then change in main.c function void SystemClock_Config(void):

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;

and in the stm32f4xx_hal_msp.c

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  if(hrtc->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspInit 0 */
 
  /* USER CODE END RTC_MspInit 0 */
 
  /** Initializes the peripherals clock
  */
    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
    PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
      Error_Handler();
    }
 
    /* Peripheral clock enable */
    __HAL_RCC_RTC_ENABLE();
  /* USER CODE BEGIN RTC_MspInit 1 */
 
  /* USER CODE END RTC_MspInit 1 */
  }
 
}

LSI -> LSE

RTC_BKP_DR is reset when unplugging power while the vbat pin is connected to CR1220 battery.

I did all configurations that you mentioned but I will do it again.

changing the cube package for stm32f4 can be effective (I'm using ver1.27)?

JTP1
Lead

OK, it should work pretty much just out-of-box. Just set the clock 32KHz configuration to LSE ja and enable RTC.

One more thing, when you try to comment out the MX_RTC_Init(), maybe your hrtc- struct was not initialized to point to RTC module, since it is initialized in MX_RTC_Init : hrtc.Instance = RTC;

So remember to init hrtc-struct somewhere else if not run MX_RTC_Init().

Peter BENSCH
ST Employee

I suspect the same problem we have discussed some time ago: if you use the HAL and let CubeMX generate your skeleton programme, the default time information is always set to the default value after a reset.

Background of this somewhat debatable decision: when developing programmes and debugging, a defined basic state should be set up after a reset.

The link above suggests a method for deactivating the reset of the RTC. However, it should then also be ensured that the RTC already has a basic state and is buffered via VBAT.

Hope this helps, even if it looks (and is) a bit makeshift?

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.