samples: boards: nrf: clock_skew: calculate corrected reference

The sample showed the error between the two clocks, but did not show
how to use the skew to reconstruct one clock from the other and the
resulting error.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2021-02-12 05:56:12 -06:00 committed by Carles Cufí
parent 26a65d8584
commit 55c4db9c4c
2 changed files with 89 additions and 42 deletions

View file

@ -8,6 +8,8 @@ Overview
This sample uses the API for correlating time sources to measure the
skew between HFCLK (used for the CPU) and LFCLK (used for system time).
It also shows how to use this skew to correct durations measured in the
LFCLK domain to durations in the HFCLK domain.
The ``CONFIG_APP_ENABLE_HFXO`` Kconfig option can be select to configure
the high frequency clock to use a crystal oscillator rather than the
@ -38,7 +40,7 @@ Sample Output
.. code-block:: console
*** Booting Zephyr OS build zephyr-v2.4.0-693-g4a3275faf567 ***
*** Booting Zephyr OS build v2.5.0-rc3-94-g06a4b650467b ***
Power-up clocks: LFCLK[ON]: Running LFXO ; HFCLK[OFF]: Running HFINT
Start TIMER_0: 0
Timer-running clocks: LFCLK[ON]: Running LFXO ; HFCLK[OFF]: Running HFINT
@ -46,71 +48,90 @@ Sample Output
Timer wraps every 268 s
Ty Latest Base Span Err
HF 00:00:00.015667
LF 00:00:00.404296
HF 00:00:00.015666
LF 00:00:00.404937
Started sync: 0
Ty Latest Base Span Err
HF 00:00:10.001151 00:00:00.015667 00:00:09.985483
LF 00:00:10.413818 00:00:00.404296 00:00:10.009521 00:00:00.024038
Skew 0.997599 ; err 2401411 ppb
HF 00:00:09.978436 00:00:00.015666 00:00:09.962769
LF 00:00:10.414520 00:00:00.404937 00:00:10.009582 00:00:00.046813
RHF 00:00:09.978435 -00:00:00.000000
Skew 0.995323 ; err 4676818 ppb
Ty Latest Base Span Err
HF 00:00:19.997456 00:00:00.015667 00:00:19.981788
LF 00:00:20.434265 00:00:00.404296 00:00:20.029968 00:00:00.048180
Skew 0.997595 ; err 2405464 ppb
HF 00:00:19.959136 00:00:00.015666 00:00:19.943469
LF 00:00:20.441589 00:00:00.404937 00:00:20.036651 00:00:00.093182
RHF 00:00:19.959117 -00:00:00.000018
Skew 0.995349 ; err 4650592 ppb
Ty Latest Base Span Err
HF 00:00:29.993845 00:00:00.015667 00:00:29.978178
LF 00:00:30.454650 00:00:00.404296 00:00:30.050354 00:00:00.072176
Skew 0.997598 ; err 2401828 ppb
HF 00:00:29.937181 00:00:00.015666 00:00:29.921514
LF 00:00:30.468627 00:00:00.404937 00:00:30.063690 00:00:00.142176
RHF 00:00:29.937175 -00:00:00.000005
Skew 0.995271 ; err 4729151 ppb
Ty Latest Base Span Err
HF 00:00:39.986181 00:00:00.015667 00:00:39.970514
LF 00:00:40.475036 00:00:00.404296 00:00:40.070739 00:00:00.100225
Skew 0.997499 ; err 2501189 ppb
HF 00:00:39.917347 00:00:00.015666 00:00:39.901680
LF 00:00:40.495666 00:00:00.404937 00:00:40.090728 00:00:00.189048
RHF 00:00:39.917339 -00:00:00.000008
Skew 0.995284 ; err 4715502 ppb
Ty Latest Base Span Err
HF 00:00:49.981516 00:00:00.015667 00:00:49.965848
LF 00:00:50.495422 00:00:00.404296 00:00:50.091125 00:00:00.125277
Skew 0.997499 ; err 2501010 ppb
HF 00:00:49.899493 00:00:00.015666 00:00:49.883826
LF 00:00:50.522674 00:00:00.404937 00:00:50.117736 00:00:00.233910
RHF 00:00:49.899486 -00:00:00.000007
Skew 0.995333 ; err 4667222 ppb
Ty Latest Base Span Err
HF 00:00:59.976042 00:00:00.015667 00:00:59.960375
LF 00:01:00.515808 00:00:00.404296 00:01:00.111511 00:00:00.151136
Skew 0.997486 ; err 2514243 ppb
HF 00:00:59.878166 00:00:00.015666 00:00:59.862499
LF 00:01:00.549713 00:00:00.404937 00:01:00.144775 00:00:00.282276
RHF 00:00:59.878154 -00:00:00.000011
Skew 0.995307 ; err 4693269 ppb
...
Ty Latest Base Span Err
HF 00:01:59.935661 00:00:00.015667 00:01:59.919994
LF 00:02:00.638153 00:00:00.404296 00:02:00.233856 00:00:00.313862
Skew 0.997390 ; err 2610445 ppb
HF 00:02:59.654855 00:00:00.015666 00:02:59.639188
LF 00:03:00.873901 00:00:00.404937 00:03:00.468963 00:00:00.829775
RHF 00:02:59.654857 00:00:00.000001
Skew 0.995402 ; err 4597902 ppb
...
Ty Latest Base Span Err
HF 00:04:59.769166 00:00:00.015667 00:04:59.753498
LF 00:05:01.005279 00:00:00.404296 00:05:00.600982 00:00:00.847484
Skew 0.997181 ; err 2819240 ppb
HF 00:04:59.410593 00:00:00.015666 00:04:59.394926
LF 00:05:01.198181 00:00:00.404937 00:05:00.793243 00:00:01.398317
RHF 00:04:59.410594 00:00:00.000001
Skew 0.995351 ; err 4648745 ppb
...
Ty Latest Base Span Err
HF 00:09:59.513787 00:00:00.015667 00:09:59.498119
LF 00:10:01.617156 00:00:00.404296 00:10:01.212860 00:00:01.714741
Skew 0.997148 ; err 2852201 ppb
HF 00:09:58.829511 00:00:00.015666 00:09:58.813845
LF 00:10:02.008911 00:00:00.404937 00:10:01.603973 00:00:02.790128
RHF 00:09:58.829509 -00:00:00.000002
Skew 0.995362 ; err 4637837 ppb
...
Ty Latest Base Span Err
HF 00:30:08.384536 00:00:00.015667 00:30:08.368868
LF 00:30:14.084594 00:00:00.404296 00:30:13.680297 00:00:05.311429
Skew 0.997072 ; err 2928495 ppb
HF 00:29:56.607589 00:00:00.015666 00:29:56.591923
LF 00:30:05.250732 00:00:00.404937 00:30:04.845794 00:00:08.253871
RHF 00:29:56.607585 -00:00:00.000004
Skew 0.995427 ; err 4573166 ppb
...
Ty Latest Base Span Err
HF 00:59:57.353602 00:00:00.015667 00:59:57.337934
LF 01:00:07.734375 00:00:00.404296 01:00:07.330078 00:00:09.992144
Skew 0.997230 ; err 2770006 ppb
HF 00:59:43.781443 00:00:00.015666 00:59:43.765776
LF 01:00:00.085113 00:00:00.404937 00:59:59.680175 00:00:15.914399
RHF 00:59:43.781535 00:00:00.000092
Skew 0.995579 ; err 4421055 ppb
...
Ty Latest Base Span Err
HF 02:59:33.181323 00:00:00.015667 02:59:33.165656
LF 03:00:03.434265 00:00:00.404296 03:00:03.029968 00:00:29.864312
Skew 0.997236 ; err 2764463 ppb
HF 01:59:37.666395 00:00:00.015666 01:59:37.650728
LF 02:00:09.810913 00:00:00.404937 02:00:09.405975 00:00:31.755247
RHF 01:59:37.666057 -00:00:00.000338
Skew 0.995595 ; err 4404723 ppb
...
Ty Latest Base Span Err
HF 05:59:55.031709 00:00:00.015667 05:59:55.016042
LF 06:00:57.120941 00:00:00.404296 06:00:56.716644 00:01:01.700602
Skew 0.997151 ; err 2849042 ppb
HF 05:58:33.905236 00:00:00.015666 05:58:33.889570
LF 06:00:08.604980 00:00:00.404937 06:00:08.200042 00:01:34.310472
RHF 05:58:33.904338 -00:00:00.000898
..
Skew 0.995635 ; err 4364550 ppb
Ty Latest Base Span Err
HF 11:56:59.503922 00:00:00.015666 11:56:59.488255
LF 12:00:06.793182 00:00:00.404937 12:00:06.388244 00:03:06.899989
RHF 11:56:59.503948 00:00:00.000025
Skew 0.995674 ; err 4325747 ppb

View file

@ -160,6 +160,32 @@ static void sync_work_handler(struct k_work *work)
if (rc > 0) {
float skew = timeutil_sync_estimate_skew(&sync_state);
/* Create a state with the current skew estimate. Use
* it to reconstruct the expected reference time from
* the latest local time, then display that time and
* its error from the latest reference time.
*/
uint64_t rec_ref;
struct timeutil_sync_state st2 = sync_state;
(void)timeutil_sync_state_set_skew(&st2, skew, NULL);
(void)timeutil_sync_ref_from_local(&st2, latest->local,
&rec_ref);
char err_sign = ' ';
uint64_t err_us;
if (rec_ref < latest->ref) {
err_sign = '-';
err_us = ref_to_us(latest->ref - rec_ref);
} else {
err_us = ref_to_us(rec_ref - latest->ref);
}
printf("RHF %s ",
us_to_text(ref_to_us(rec_ref)));
printf("%c%s\n", err_sign, us_to_text(err_us));
printf("Skew %f ; err %d ppb\n", skew,
timeutil_sync_skew_to_ppb(skew));
} else if (rc < 0) {