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:
Quartz crystal oscillator. A small piece of quartz vibrates mechanically at a frequency set by its cut and size, and a circuit keeps it vibrating. Its frequency tolerance, the largest allowed error at 25 degrees Celsius, is quoted in ppm (parts per million): 1 ppm is an error of one part in . A common watch crystal runs at 32,768 Hz (hertz, cycles per second), which is Hz, so a 15-bit counter overflows exactly once per second; a tolerance of ppm is typical. A crystal takes far longer to start than an RC oscillator, from under a millisecond for a fast crystal to hundreds of milliseconds for a 32,768 Hz one, and needs parts outside the chip.
RC oscillator. A resistor and a capacitor set the frequency. It starts within microseconds, needs no external part and uses little power, but its frequency depends strongly on temperature and supply voltage, and its error is quoted in percent rather than ppm. It can be calibrated: measured against a crystal and corrected.
Derived clock. A PLL (phase-locked loop) multiplies a source’s frequency, and a divider, also called a prescaler, divides it. A derived clock has the same fractional error as its source: a crystal that is 20 ppm fast gives a clock that is 20 ppm fast after any multiplication or division.
A crystal’s actual frequency error has several parts:
the tolerance at 25 degrees Celsius, fixed at manufacture;
temperature: a 32,768 Hz tuning-fork crystal, the usual watch crystal, is fastest near 25 degrees Celsius and slows on both sides along a parabola, with a typical coefficient of ppm per degree Celsius squared;
aging: a slow change over months and years, typically a few ppm in the first year;
the circuit: the capacitance that the circuit board places across the crystal shifts its frequency, so a badly matched board adds an error of its own.
A chip usually has several clock sources and switches among them. Nordic Semiconductor’s nRF52840 §6.4 microcontroller §6.1 is an example.
Its low-frequency clock, named LFCLK, runs at 32,768 Hz from one of three sources: the LFXO (low-frequency crystal oscillator), driven by an external 32,768 Hz crystal; the LFRC (low-frequency RC oscillator), which software can calibrate against the high-frequency crystal to about ppm; or the LFSYNT (low-frequency synthesized clock), derived from the high-frequency clock.
Its high-frequency clock, named HFCLK, runs at 64 MHz (megahertz, millions of cycles per second) from one of two sources: the HFINT (high-frequency internal oscillator), an RC oscillator, or the HFXO (high-frequency crystal oscillator), driven by an external 32 MHz crystal.
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:
find which oscillator drives the counter at every moment of the interval;
take that oscillator’s error bound for the conditions of the measurement: tolerance, temperature range and age;
convert counts to time with the nominal frequency, and carry the error bound along with the result;
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 s every second, because . One hour is 3600 s, so it gains , 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 ms apart after one hour. Their relative frequency error is ppm, which gives the same answer directly: s. After one day, which is 86,400 s, they are s apart.
Temperature adds error. A 32,768 Hz tuning-fork crystal with the typical coefficient of ppm per degree Celsius squared, used at 0 degrees Celsius, is off by ppm, on top of its tolerance. Over one day that alone loses 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 , 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 s, but actually took s. The nominal conversion overstates it by about 30 s, which is s. The correction is only as good as the reference: if the crystal itself is within ppm, the corrected value is known to within about ppm, and only while HFINT’s temperature and supply voltage stay the same.
Stated precisely
Let be a clock’s nominal frequency and its actual frequency at true time . The fractional frequency error is , a pure number; in ppm it is . The clock reports , where is its reading at true time 0 and is the number of cycles counted since then, with running over true time. The clock’s time error is then the starting offset plus the accumulated frequency error. The integral is the drift.
If for every from 0 to , the drift is at most in magnitude: . If is a constant , the drift is exactly . For two clocks A and B with frequency errors and , the difference of their readings is and with bounds and the change in that difference is at most in magnitude.
These statements assume three things: the bound 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 and device B’s by , the gap between their readings changes by after a true elapsed time . With ppm and 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 and , the gap changes by at most . 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).