cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L072 Periodic 4ms time drift on RTC/LPTIM 1Hz interrupt using LSE

Augustin23
Visitor

Hello,

I am working with an STM32L072 and trying to generate a 1Hz interrupt with as little time drift as possible. For this, I am using the RTC wakeup interrupt at 1Hz, with the LSE (32.768 kHz quartz, 10 ppm) as the clock source.

On several products, I observe a periodic time drift of about 4ms. The period and sign of the drift vary from one product to another, but the pattern is repeated consistently. The attached graph shows that the drift is periodic for each product, but with different periods.

To measure the interrupt timing, I set a flag in the interrupt handler that triggers a USB message to the computer. This explains the noise in the reception time, but not the 4ms discontinuities or their periodicity. The MCU remains in run mode during these tests. And there is no reset of the board.

The 4ms drift corresponds to approximately 0.0039978s, which is equal to 131 LSE ticks (131/32768). This seems significant and unexpected.

Initially, I suspected an issue with my RTC configuration, so I switched to a 1Hz low-power timer (LPTIM) also clocked by the LSE, but observed the same behavior. During testing, the temperature is constant and all products are powered simultaneously via USB, so I assume the power supply is stable.

I am looking for an explanation for this periodic discontinuity and a way to eliminate or reduce it in my 1Hz timer implementation.

 

Augustin23_0-1764078715761.png

void MX_RTC_Init(void)
{
  /** 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.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enable the WakeUp
  */
  if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) != HAL_OK)
  {
    Error_Handler();
  }

}

Thank you in advance,

Best Regards,

Augustin

1 REPLY 1
TDK
Super User

How are you calculating the y value of the points in your plot? Please be precise. Code helps. I'm a little confused why it has so much jitter, even without the 4ms discontinuities.

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