cancel
Showing results for 
Search instead for 
Did you mean: 

RTC smooth digital calibration in STM32F4 with HSE

pierre-yves
Associate III

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

10 REPLIES 10

> 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

pierre-yves
Associate III

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.

Nikita91
Lead II

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.

pierre-yves
Associate III

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.

pierre-yves
Associate III

Or if you have another method (different from smooth calibration) ...

Nikita91
Lead II

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.

A Maq
Senior

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:

https://www.st.com/en/embedded-software/x-cube-rtc.html

pierre-yves
Associate III

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.

A Maq
Senior

@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.