Section 6.18

Clock source and drift

Definition

A clock source is the oscillator from which a device’s counters and timers count time: an electronic circuit that produces a regular signal at a nominal frequency, usually a quartz crystal oscillator or a cheaper, less accurate RC (resistor-capacitor) oscillator. Clock drift is the growing difference between the time a clock reports and true time, caused by its oscillator running slightly faster or slower than its nominal frequency. Drift is a rate error accumulated over time, unlike an offset, which is a fixed difference at one instant.

The idea

Two data loggers are started together at noon, with readings that agree exactly. Logger A’s oscillator runs 20 parts per million fast, logger B’s 20 parts per million slow. Neither clock ever jumps, and every tick of each looks normal. After one day, an event that both loggers record at the same instant carries timestamps 3.456 s apart, as the worked example computes. Nothing failed: each oscillator ran at its own slightly wrong rate, and the small error added up every second.

Two runners who start side by side but keep slightly different paces are a fair picture. The distance between them starts at zero and grows steadily; the difference in pace is the rate error, and the distance is the drift. The picture stops being accurate in one respect: a runner chooses a pace, while an oscillator’s frequency wanders with temperature, supply voltage and age. The gap therefore grows along a curve rather than a straight line, and a correction measured today can be wrong tomorrow.

Usage of the words varies. Some literature on clock synchronization §9.36 calls the frequency difference the skew and uses drift for the rate at which the frequency itself changes. This entry calls the frequency difference the frequency error and uses drift for the accumulated time error it causes.

How it works

Common clock sources differ in accuracy, start-up time and power:

A crystal’s actual frequency error has several parts:

A chip usually has several clock sources and switches among them. Nordic Semiconductor’s nRF52840 §6.4 microcontroller §6.1 is an example.

The processor runs on HFCLK, and HFCLK uses HFINT unless some part of the chip requests HFXO; the radio needs HFXO while it operates, and software can request it. So the processor’s clock, and every count of processor cycles, has crystal accuracy only while HFXO runs and RC accuracy the rest of the time, while an LFCLK taken from LFXO keeps crystal accuracy throughout.

Turning counts into trustworthy time takes four steps:

  1. find which oscillator drives the counter at every moment of the interval;

  2. take that oscillator’s error bound for the conditions of the measurement: tolerance, temperature range and age;

  3. convert counts to time with the nominal frequency, and carry the error bound along with the result;

  4. where the bound is too large, calibrate: measure the counter against a better reference and correct the frequency.

Worked example

A crystal that is 20 ppm fast gains 20 μ\mus every second, because 20×10−6×1s=20μs20 \times 10^{-6} \times 1\ \text{s} = 20\ \mu\text{s}. One hour is 3600 s, so it gains 20×3600=72,000μs20 \times 3600 = 72{,}000\ \mu\text{s}, which is 72 ms. A crystal that is 20 ppm slow loses 72 ms in the same hour. Two loggers with these two crystals, started together, are 72+72=14472 + 72 = 144 ms apart after one hour. Their relative frequency error is 20−(−20)=4020 - (-20) = 40 ppm, which gives the same answer directly: 40×10−6×3600s=0.14440 \times 10^{-6} \times 3600\ \text{s} = 0.144 s. After one day, which is 86,400 s, they are 40×10−6×86,400s=3.45640 \times 10^{-6} \times 86{,}400\ \text{s} = 3.456 s apart.

Temperature adds error. A 32,768 Hz tuning-fork crystal with the typical coefficient of −0.034-0.034 ppm per degree Celsius squared, used at 0 degrees Celsius, is off by −0.034×(0−25)2=−0.034×625=−21.25-0.034 \times (0 - 25)^{2} = -0.034 \times 625 = -21.25 ppm, on top of its tolerance. Over one day that alone loses 21.25×10−6×86,400s≈1.8421.25 \times 10^{-6} \times 86{,}400\ \text{s} \approx 1.84 s.

Calibration §11.42 measures a fast RC-driven clock against a crystal. A program counts processor cycles, clocked by HFINT, across 32,768 periods of a 32,768 Hz crystal clock, which is nominally 1 s, and in this example reads 64,019,200 cycles instead of the nominal 64,000,000. The ratio is 64,019,200/64,000,000=1.000364{,}019{,}200 / 64{,}000{,}000 = 1.0003, so HFINT runs 300 ppm fast relative to the crystal. A code section that took 6,400,000 cycles is reported by the nominal conversion as 6,400,000/64,000,000=0.16{,}400{,}000 / 64{,}000{,}000 = 0.1 s, but actually took 6,400,000/64,019,200≈0.0999706{,}400{,}000 / 64{,}019{,}200 \approx 0.099970 s. The nominal conversion overstates it by about 30 μ\mus, which is 300×10−6×0.1300 \times 10^{-6} \times 0.1 s. The correction is only as good as the reference: if the crystal itself is within ±20\pm 20 ppm, the corrected value is known to within about ±20\pm 20 ppm, and only while HFINT’s temperature and supply voltage stay the same.

Stated precisely

Let f0f_0 be a clock’s nominal frequency and f(t)f(t) its actual frequency at true time tt. The fractional frequency error is y(t)=(f(t)−f0)/f0y(t) = (f(t) - f_0) / f_0, a pure number; in ppm it is y(t)×106y(t) \times 10^{6}. The clock reports R(t)=R(0)+N(t)/f0R(t) = R(0) + N(t) / f_0, where R(0)R(0) is its reading at true time 0 and N(t)=∫0tf(τ)dτN(t) = \int_0^t f(\tau)\, d\tau is the number of cycles counted since then, with τ\tau running over true time. The clock’s time error x(t)=R(t)−tx(t) = R(t) - t is then x(t)=x(0)+∫0ty(τ)dτ,x(t) = x(0) + \int_0^t y(\tau)\, d\tau, the starting offset x(0)x(0) plus the accumulated frequency error. The integral is the drift.

If |y(τ)|≤ε|y(\tau)| \le \varepsilon for every τ\tau from 0 to tt, the drift is at most εt\varepsilon t in magnitude: |x(t)−x(0)|≤εt|x(t) - x(0)| \le \varepsilon t. If yy is a constant y0y_0, the drift is exactly y0ty_0 t. For two clocks A and B with frequency errors yAy_A and yBy_B, the difference of their readings is RA(t)−RB(t)=(xA(0)−xB(0))+∫0t(yA(τ)−yB(τ))dτ,R_A(t) - R_B(t) = \bigl(x_A(0) - x_B(0)\bigr) + \int_0^t \bigl(y_A(\tau) - y_B(\tau)\bigr)\, d\tau, and with bounds εA\varepsilon_A and εB\varepsilon_B the change in that difference is at most (εA+εB)t(\varepsilon_A + \varepsilon_B)\, t in magnitude.

These statements assume three things: the bound ε\varepsilon covers every condition during the interval, including temperature, supply voltage and any switch of clock source; the counter counts without stopping; and any counter wraparound §6.21 has been accounted for.

Limits and common mistakes

Resolution is not accuracy. A 64 MHz counter resolves 15.625 ns, but if its oscillator is 1% fast, a reading of 1 s is off by 10 ms. More digits in a timestamp do not reduce oscillator error; the Resolution versus precision versus accuracy §11.34 entry separates the three ideas.

Equal nominal frequencies do not make two clocks agree. Two 32,768 Hz crystals from the same reel differ within their tolerance, and two RC oscillators differ far more.

A calibration goes stale. It is valid for the temperature, supply voltage and age at which it was made, and an RC oscillator’s frequency moves noticeably when the temperature changes.

A switch of clock source changes the rate in the middle of a measurement. On the nRF52840, processor-cycle counts taken while HFXO is off come from HFINT, and counts taken while HFXO runs come from the crystal; one conversion factor for both mixes two accuracies.

Two clocks on the same chip can disagree. A processor cycle counter that counts an RC-driven processor clock and an operating system’s uptime clock that counts a crystal-driven 32,768 Hz clock can differ by hundreds of ppm over the same interval. The crystal-driven clock is the more accurate of the two, and the disagreement measures the RC oscillator’s error, not a software fault.

Jitter is a different error. Jitter is the short-term, random wobble in the timing of individual ticks; it averages out over many ticks, while a frequency error accumulates.

Timestamps from two devices belong to two clock domains §13.29, groups of timestamps that each share one clock. Comparing them needs a mapping between the two clocks, an offset and a relative rate estimated from events that both devices observe, with the remaining uncertainty stated; the Wall clock and clock synchronization entry treats this mapping. Subtracting raw timestamps of two devices mixes both clocks’ offsets and drifts into the result.

Questions answered

How do two devices’ clocks diverge over a long experiment?

Their readings separate by their initial offset plus the accumulated difference of their frequency errors, so the gap grows roughly in proportion to the elapsed time. Each device’s clock counts an oscillator whose actual frequency differs slightly from its nominal frequency, by an amount quoted in ppm (parts per million). If device A’s oscillator is fast by the fraction yAy_A and device B’s by yBy_B, the gap between their readings changes by (yA−yB)t(y_A - y_B)\, t after a true elapsed time tt. With +20+20 ppm and −20-20 ppm the difference is 40 ppm, which is 144 ms per hour and 3.456 s per day.

Over a long experiment the frequency errors themselves change with temperature, supply voltage, age and any switch of clock source, so the gap grows along a curve rather than a straight line. If the two errors stay within bounds εA\varepsilon_A and εB\varepsilon_B, the gap changes by at most (εA+εB)t(\varepsilon_A + \varepsilon_B)\, t. To compare the two devices’ timestamps, record events that both devices observe at the start, at the end and preferably in between, estimate the offset and the relative rate from them, map one clock onto the other, and report the remaining uncertainty. Subtracting the raw timestamps of two devices is meaningful only after that mapping.

Related entries: Wall clock and clock synchronization (§9.36), Counter quantization (§11.35), RTC versus cycle counter (§6.19), Resolution versus precision versus accuracy (§11.34), Calibration (§11.42), DWT cycle counter (§6.20).