cancel
Showing results for 
Search instead for 
Did you mean: 

Periodic RTC calibration STM32F1xx

eliast1234
Associate II
Posted on April 08, 2015 at 15:56

Hello,

MyProject: Calibrate RTC LSE clock continuously based on an external time reference.

MyProblem: Calculation of re-calibration value.

Description: I start a calibration measurement interval time t1 and stop it at t2. These two are reference times. I also note the corresponding RTC times t1_rtc and t2_rtc.

Based on these values I calculate the time difference in PPM and and adjust my RTC prescaler and calibration value, e.g., call them ''PS_OLD'' and ''CAL_OLD''. This works fine since I have no calibration value in RTCCR register.

Like this, I want to do periodic measurements and repeat the calibration, for example every 10 days or weekly.

Problem: I do not know how to calculate the new calibration value, if I have already a calibration value in RTCCR register; e.g., calculation of new prescaler and calibration value PS_NEW and CAL_NEW?

hope I was able to illustrate the problem and thanks for any contribution...

10 REPLIES 10
Posted on April 08, 2015 at 17:54

Whose project for whom?

Is the external reference provided as HSE, or some other clock/timer source?

Can you output the RTCCLK via the TAMPER pin, and feed it as a trigger into another timer?

Have you read AN2604 ''STM32F101xx and STM32F103xx RTC calibration�?.

If it were my project, I'd measure what the PPM error was of the source clock, and adjust the calibration and prescaler based on that. I'd do some experiments to characterize the behaviour, and compare that to how it's documented (slowing 0.954PPM per increment).

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
eliast1234
Associate II
Posted on April 09, 2015 at 09:04

hello Mr. clive1,

thank you for your quick response and showing me that my formulation of the problem is quite confusing ;)

a new and hopefully better formulation:

the project is for the school. a timekeeper device. the reference clock is the 50 Hz power line mains signal. I count the cycles in SW over a long time (e.g. a few days) and assume that it delivers exactly 50 Hz (Europe) when averaged over so long time (it is actually pretty close to 50 Hz, exactly ~49.9999998Hz). This is good enough for me; I want RTC time to be as close to 50 Hz time as possible.

I know about HSE/timer based calibration or outputting LSE/64 on a pin etc. But in my case not relevant.

My problem is about setting a logical relationship between measureed/calculated ppm value and current psc and cal values. The question is a bit difficult for me but may be a simple one for a professional:

What is the effective frequency of RTC clock when I set prescaler to X and calibration value to Y? Hm this formulation sound totally different but it is the same thing.

(In my calculations I use the current effective frequency of RTC to find the deviation of RTC time from the power line time.)

I re-realize I must learn a lot more to do better problem description.

---------------

Example:

Currently RTC_PRL is set to 0x7FFA and RTCCR_CAL is 31.

a) What is the effective RTC clk frequency in this case (RTC_PRL=0x7ffA, CAL=31)?

b) I measure a interval and compare it against 50 Hz mains clock and found the PPM 520 or -520. What shall be the new values of RTC_PRL and RTCCR_CAL?

Posted on April 09, 2015 at 13:42

Well the calibration only allows for a slowing of up to 121PPM

It does this by stuffing clock(s) over a 1048576 tick interval.

So you can either measure the PPM error of the source in absolute terms to create a specific calibration term, or measure it in relative terms and adjust the calibration term up and down respectively.

Think of everything in terms of dividers. They are dividing down the 32768 Hz source. Quantify the number of ticks over the observation period. The RTC should do this over a long period. Wait for the second transition.

If the crystal is generating X ticks, the RTC is going to see X + ((X / 1048576) * PPM) ticks after the calibration stage. This is then going to be divided down by the Prescaler stage and counted off in seconds.

Figure out what X is over your known observation time, and then refactor your PPM and PRESCALER to achieve the correct number of SECONDS.  Adjust the time, and apply the new settings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
eliast1234
Associate II
Posted on April 09, 2015 at 15:36

Thanks a lot.

Yeah, I'm trying to figure that out for more than a week :)

But, it is like you are asking me my own question ;)

I read a lot about calibration seen all examples also from other vendors like microchip etc. But given examples are simple and similar.

May be I'm too stupid and shall let it be with the science class.

eliast1234
Associate II
Posted on April 09, 2015 at 16:25

Anybody else willing to help further with the questions a) and b) above?

Any further input is much appreciated!

Thanks.
Posted on April 09, 2015 at 18:37

a) 32763.96863082 Hz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
eliast1234
Associate II
Posted on April 09, 2015 at 19:22

Hello Mr. clive1,

thank you sir.

First of all, I think I also might be able to understand your previous message little by little. I know you gave there all the info I need if I was able to understand it fully ;). it will take much longer until then.

Do I have to still wait for second tick if my observation time is very long. It might be difficult since I'm counting the expected mains zero crossings and reading RTC CNT and DIV register to get the corresponding time in seconds and subseconds resolution but not necessarily at the time of a seconds tick.

What dou you mean by correct SECONDS? It is the seconds I derive from mains signal, right?
eliast1234
Associate II
Posted on April 09, 2015 at 20:18

So according to the ''ticks seen'' formula X + X/2^20*PPM

Assuming PS1 and PPM1 are already known from the previous calibration stage, to find PS2 and PPM2, the following equation must be satisfied:

(PSC1 + (PSC1/2^20) * PPM1) * refTimeInSeconds = (PSC2 + (PSC2/2^20) * PPM2) * measTimeInSeconds

refTinmeInSeconds is the time ''correct'' seconds from mains cycle counting and

measTimeinSeconds is the elapsed time calculated from RTC readings.

Right?

Posted on April 09, 2015 at 20:55

Reading the document over I think it eat clocks, not insert them

You need to solve the equation, first with the set of measured numbers to get the tick count over a known observation period with the CURRENT settings. And then you have to solve it with the now known TICKS/SECOND value and create two NEW values for PPM and PRESCALER that fit.

(XTALTICKS - ((XTALTICKS / 1048576) * PPM)) / PRESCALER = SECONDS

Solve for XTALTICKS with the *RTC* values of PPM, PRESCALER, SECONDS

Ratio it based on RTC SECONDS, vs REAL SECONDS, to get a frequency for the underlying clock.

Then solve for new PPM and PRESCALER values, based on the true XTALTICKS per second, and SECONDS = 1

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..