2020-04-14 05:36 AM
Hello,
I am taking the liberty of writing to you for information on the calibration of the RTC.
At present the RTC uses the HSE clock. The HSE clock is derived from a 25MHz quartz and then divided to be at 1MHz. This quartz has a tolerance of 20ppm.
The microcontroller is a STM32F4.
I want to calibrate the RTC to cancel the quartz drift but I can't do it.
Here is the configuration of the RTC on the clock side:
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV25;
RTC_handle.Init.AsynchPrediv = 124;
RTC_handle.Init.SynchPrediv = 7999;
With this configuration the RTC clock is at 1Hz (ck_spre).
To quantify the error on the HSE clock, I count the number of milliseconds of the timer TIM1 which is at 1ms
For a period of 32 seconds, so I have 32.293s, an error of 293ms.
Is the method good?
How to calculate the compensation ?
How to use the HAL_RTCEx_SetSmoothCalib function?
Thank you in advance
Best Regards
2020-04-14 06:07 AM
> The microcontroller is a STM32F4.
Which one?
> To quantify the error on the HSE clock, I count the number of milliseconds of the timer TIM1 which is at 1ms
What is the primary clock of TIM1?
If it's the same HSE, then TIM1's setup is wrong (and you can't measure HSE-derived clock against another HSE-derived clock).
I also would not use Cube/HAL functions for something which is out of usual. Read the RM and use registers directly.
JW
2020-04-14 06:28 AM
Okay, thank you for your response.
It's a STM32F427ZITx.
The TIM1 is clocked by a clock derived from HSE (APB2 comes from SYSCLK which is made from HSE).
How can I do ?
Can I simply turn on the RTC and read the deviation from the reference after a few seconds?
It's not clear in the manual.
The formulas aren't explicit... or I misunderstood.
2020-04-14 10:45 AM
As I undestand the clock of the RTC and the clock of the timer comes from the same HSE. You will obviously measure a deviation of 0 because it is the same clock!
To calibrate the HSE you need another clock with better accuracy than 20 ppm (TCXO, OCXO...). To measure something you need a measuring instrument: another clock.
At this level you will have to take account of everything: temperature, humidity, shocks ... Crystals are good sensors of many things.
2020-04-18 04:30 PM
Hello
Sorry I'm a little late answering.
Here is an example, I use an external clock as a reference.
The test period is 32 seconds.
- Reference clock start = 19/4/2020 01 :12 :31 :546
After 32 seconds
- Reference clock stop = 19/4/2020 01 :13 :03 :568
- STM32 time = 19/04/2020 01 :13 :02 :951
- Test time = 32.0214189 seconds
RTC is slow for 0.6173973 seconds.
How to calculate the calibration parameters and use the smooth calibration function (HAL_RTCEx_SetSmoothCalib)
Thank you in advance for your help and for your answers.
2020-04-18 05:14 PM
Or if you have another method (different from smooth calibration) ...
2020-04-19 01:30 AM
0.6 sec over 32 sec is roughly 1.8% error. Far from average quartz precision which is > 0.0001%
There is something wrong with your measurement method.
2020-04-19 02:22 PM
ST has provided very detailed examples which can give you better idea. Please download RTC specific example code which has relevant example follow this link below:
2020-04-22 06:31 AM
Hello
Thank you for your answer, the examples were helpful.
How do you add milliseconds when programming an hour?
I know how to convert milliseconds into SubSeconds.
But the HAL_RTC_SetTime function does not apply them.
2020-04-24 02:47 AM
@pierre-yves I am glad it was helpful.
In order to add milliseconds you have to use Sub-Second register of RTC that is (SSR)
Register itself is Read Only therefore you cannot write anything on it.
Therefore we have to use a SHIFTR register of the RTC to perfom that.