embedded config_error ai_generated true

RTC: calibration error, drift exceeds 5 ppm per day

ID: embedded/rtc-calibration-drift

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
1Evidence
2023-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
STM32Cube_FW_L4_V1.18.0 active
IAR EWARM 9.40.1 active
ARM GCC 10.3.1 active

Root Cause

RTC calibration register misconfiguration or temperature-induced frequency shift causes excessive time drift.

generic

中文

RTC校准寄存器配置错误或温度引起的频率偏移导致时间漂移过大。

Official Documentation

https://www.st.com/resource/en/application_note/dm00257205-rtc-calibration-on-stm32-microcontrollers-stmicroelectronics.pdf

Workarounds

  1. 85% success Recalibrate RTC using HAL_RTCEx_SetCalibration(): call HAL_RTCEx_SetCalibration(&hrtc, RTC_CALIBRATION_SIGN_POSITIVE, 0, 0x80); then measure drift and adjust CALP/CALM bits accordingly.
    Recalibrate RTC using HAL_RTCEx_SetCalibration(): call HAL_RTCEx_SetCalibration(&hrtc, RTC_CALIBRATION_SIGN_POSITIVE, 0, 0x80); then measure drift and adjust CALP/CALM bits accordingly.
  2. 80% success Implement temperature compensation in software: read internal temperature sensor and adjust calibration register via HAL_RTCEx_SetCalibration() every 10 minutes.
    Implement temperature compensation in software: read internal temperature sensor and adjust calibration register via HAL_RTCEx_SetCalibration() every 10 minutes.
  3. 90% success Use a dedicated external RTC chip (e.g., DS3231) over I2C and fallback to internal RTC only for backup.
    Use a dedicated external RTC chip (e.g., DS3231) over I2C and fallback to internal RTC only for backup.

中文步骤

  1. Recalibrate RTC using HAL_RTCEx_SetCalibration(): call HAL_RTCEx_SetCalibration(&hrtc, RTC_CALIBRATION_SIGN_POSITIVE, 0, 0x80); then measure drift and adjust CALP/CALM bits accordingly.
  2. Implement temperature compensation in software: read internal temperature sensor and adjust calibration register via HAL_RTCEx_SetCalibration() every 10 minutes.
  3. Use a dedicated external RTC chip (e.g., DS3231) over I2C and fallback to internal RTC only for backup.

Dead Ends

Common approaches that don't work:

  1. Manually adjusting RTC time every hour in firmware 95% fail

    Does not address root cause; drift continues between adjustments and wastes CPU cycles.

  2. Replacing the external crystal with a higher-precision one without recalibrating 80% fail

    Crystal tolerance alone does not compensate for internal RTC calibration errors.

  3. Disabling RTC calibration entirely 90% fail

    Drift worsens without calibration; not a viable fix for time-sensitive applications.