cancel
Showing results for 
Search instead for 
Did you mean: 

How to solve the stm32g4 internal RTC module drift problem (15 seconds in 2 days)?

HK
Associate II

I am using the stm32g474rct6 RTC module. over a duration of 2 days, the drift was over 15 seconds. I tested it in standard room conditions. How can I solve this problem ?

3 REPLIES 3

86ppm, it's not THAT bad.

First, if you are Cube, make sure this is not consequence of the unnecessary initialization (that would delay the clock half a second in average per reset).

Slow/fast running oscillator can be compensated digitally within RTC, read RTC smooth digital calibration subchapter in RM; but maybe you should tweak the load capacitors first to get closer to nominal. Read AN2867.

JW

Thanks for answer.

I use it as shown in the picture below. Is there any problem for this structure ? I guess the block I marked creates a delay every time I run the main function, is it right ?

thanks in advance.

int main(void)
 
{
     .....
     o_RTC_Init();
    ....
}
 
/**
 * @brief provide to initialize the RTC module once reading HAL_RTCEx_BKUPRead.
 */
uint8_t o_RTC_Init(void)
{
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_12;
  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){
    return FALSE;
  }
 
  if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
    /* RTC configuration  */
    s_time.Hours = 1;
    s_time.Minutes = 0;
    s_time.Seconds = 0;
    s_time.TimeFormat = RTC_HOURFORMAT12_AM;
    s_time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
    s_time.StoreOperation = RTC_STOREOPERATION_RESET;
 
    s_date.Year = 20;
    s_date.Month = RTC_MONTH_JANUARY;
    s_date.Date = 1;
    s_date.WeekDay = RTC_WEEKDAY_WEDNESDAY;
 
    if(HAL_RTC_SetDate(&hrtc, &s_date, RTC_FORMAT_BIN) != HAL_OK){
      return FALSE;
    }
 
    if(HAL_RTC_SetTime(&hrtc, &s_time, RTC_FORMAT_BIN) != HAL_OK){
      return FALSE;
    }
 
    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR0, 0x32F2);
  }else{
    /* Clear source Reset Flag */
    __HAL_RCC_CLEAR_RESET_FLAGS();
  }
 
  v_RTC_CheckDateTime();
 
  return TRUE;
}

0693W000007CUY5QAO.png