Section 6.19

RTC versus cycle counter

Full name: Real-time clock versus cycle counter.

Definition

An RTC (real-time clock) is a hardware timer that counts a slow clock, typically 32,768 Hz from a crystal, and keeps counting while the processor sleeps; some RTCs also keep the calendar date and time, while others, such as Nordic Semiconductor’s real-time counter, only count. A cycle counter is a register that the processor increments once per processor clock cycle, for example 64 million times a second. The difference that matters: the RTC is built for long, low-power timekeeping, and the cycle counter for fine resolution while the processor runs.

The idea

Two timing jobs pull in opposite directions. Timing a function that runs for 3 μ\mus needs ticks much shorter than 3 μ\mus. Timing a ten-minute sleep needs a counter that keeps running while the processor is stopped to save power, and that does not wrap around, that is, return to zero, during those ten minutes. A 32,768 Hz RTC ticks every 30.5 μ\mus, far too coarse for the function, but keeps counting through sleep. A cycle counter at 64 MHz (megahertz, millions of cycles per second) ticks every 15.625 ns, fine enough for the function, but it stops whenever the processor’s clock stops, and a 32-bit one wraps after about 67 s. Each counter fits one job and fails at the other.

A wall clock §9.36 and a stopwatch that runs only while someone holds its button are a fair picture. The wall clock keeps going whatever anyone does, but shows only coarse steps. The stopwatch measures finely, but only the time during which it was held. The picture stops being accurate in one respect: the stopwatch’s speed here is not fixed. If the processor clock changes frequency, the cycle counter keeps counting, but each count stands for a different length of time.

How it works

The RTC. An RTC counts a low-frequency clock, usually 32,768 Hz from a crystal, through a prescaler, a divider that slows the count. The low-frequency clock and the RTC draw very little power, so they stay on while the processor sleeps, and a compare register in the RTC can wake the processor at a chosen count. A calendar RTC keeps seconds, minutes, hours and the date in registers, often powered by a backup battery. A counter-only RTC keeps a count, and software turns the count into a date. Nordic Semiconductor’s nRF52840 §6.4 microcontroller §6.1 has the counter-only kind: a 24-bit counter with a 12-bit prescaler value qq, which counts at 32768/(q+1)32768 / (q + 1) counts per second.

The cycle counter. On Armv7-M processors such as the Cortex-M4 §6.3, the cycle counter is the 32-bit register CYCCNT (cycle count) of the DWT (Data Watchpoint §14.29 and Trace) unit, a debug block of the processor. The architecture makes it optional, and software must enable it before it counts, as the DWT cycle counter §6.20 entry explains. It counts processor clock cycles, so three facts decide what it measures:

An operating system’s uptime clock. An RTOS (real-time operating system) usually keeps its time with a hardware timer, not with the cycle counter. The Zephyr RTOS §6.33 on Nordic’s nRF52 series of chips, which includes the nRF52840, drives its system timer from one of the chip’s RTCs at 32,768 Hz, and its default configuration counts 32,768 kernel ticks per second, so one tick is one RTC count, about 30.5 μ\mus. Its function k_uptime_get() returns the time since the system timer started in whole milliseconds, k_uptime_ticks() returns it in ticks, and k_cycle_get_32() returns the system timer’s own hardware count, which on these chips is the 32,768 Hz RTC count, not processor cycles. The timer driver extends the 24-bit RTC count in software, so the uptime does not wrap every 512 s; it is a monotonic clock §9.35, one that never goes backwards. The kernel starts the system timer during its initialization, so uptime 0 is not the moment of reset. Time spent in a bootloader §6.30, in the reset handler and in start-up code before the kernel started its timer is not on this clock.

Choosing a counter for a measurement takes four checks:

  1. resolution: one tick must be much shorter than the interval;

  2. continuity: the counter must keep counting through every state the interval passes through, such as sleep or a debugger halt;

  3. wraparound: the interval must be at least one tick shorter than the counter’s wrap period, or the wraps must be counted;

  4. rate: the clock that drives the counter, and its accuracy, must be known for the whole interval.

Worked example

Tick lengths and wrap periods:

A function that runs for 3 μ\mus. The cycle counter advances by 3×10−6×64×106=1923 \times 10^{-6} \times 64 \times 10^{6} = 192 cycles. The RTC advances by 3/30.518≈0.0983 / 30.518 \approx 0.098 of a tick, so it shows a difference of 0 or 1 tick, that is, 0 μ\mus or 30.5 μ\mus, depending on where the function falls between ticks. The RTC cannot time this function.

Work, sleep, work. A thread runs for 1 ms, sleeps for 10 ms with the processor clock stopped, and runs for 1 ms more. The cycle counter counts only the two running periods: 2×64,000=128,0002 \times 64{,}000 = 128{,}000 cycles, which converts to 2 ms. The RTC counts the whole 12 ms: 0.012×32,768=393.2160.012 \times 32{,}768 = 393.216 ticks, so it shows 393 or 394 ticks, about 11.99 ms or 12.02 ms. The 10 ms of sleep appear only on the RTC.

A clock change. On a hypothetical chip, a section of code runs for 64,000 cycles at 64 MHz and then for 32,000 cycles after the processor clock has dropped to 32 MHz. The true time is 64,000/64,000,000+32,000/32,000,000=0.001+0.001=0.00264{,}000 / 64{,}000{,}000 + 32{,}000 / 32{,}000{,}000 = 0.001 + 0.001 = 0.002 s. Dividing the total of 96,000 cycles by 64 MHz gives 96,000/64,000,000=0.001596{,}000 / 64{,}000{,}000 = 0.0015 s, which is 0.5 ms, or 25%, too short.

Stated precisely

Let a counter be ww bits wide and count at a constant rate of ff counts per second, and let c1c_1 and c2c_2 be two readings. The elapsed count is Δc=(c2−c1)mod2w\Delta c = (c_2 - c_1) \bmod 2^{w}, and the elapsed time is Δt=Δc/f\Delta t = \Delta c / f. This is correct under three conditions: the counter counted without stopping between the readings; it advanced fewer than 2w2^{w} times between them; and ff is the counter’s actual rate, which carries the error of its clock source. Write ⌊x⌋\lfloor x \rfloor and ⌈x⌉\lceil x \rceil for xx rounded down and up to whole numbers. If the true interval is TT, the counter advances ⌊fT⌋\lfloor fT \rfloor or ⌈fT⌉\lceil fT \rceil times between the readings, depending on where they fall between ticks. The second condition therefore holds whenever fT≤2w−1fT \le 2^{w} - 1, that is, whenever the interval is at least one tick shorter than the wrap period 2w/f2^{w} / f. An interval shorter than the wrap period by less than one tick is not enough: when the first reading comes just before a tick and the second just after one, the counter advances 2w2^{w} times, the two readings are equal, and Δc=0\Delta c = 0. Under the three conditions, Δc\Delta c is ⌊fT⌋\lfloor fT \rfloor or ⌈fT⌉\lceil fT \rceil, so Δt\Delta t is within one tick, 1/f1 / f, of TT. This one-tick uncertainty is called counter quantization §11.35.

When the rate changes during the interval, split the interval into pieces i=1,…,mi = 1, \ldots, m with constant rates fif_i and counts Δci\Delta c_i; then Δt=∑i=1mΔci/fi\Delta t = \sum_{i=1}^{m} \Delta c_i / f_i. If the counter stops for part of the interval, no formula recovers the stopped time from that counter alone; another clock that kept running must supply it.

Limits and common mistakes

A cycle counter belongs to one processor. Readings from two processors, or from two devices, come from different clocks and cannot be subtracted.

At the same width, a faster counter wraps sooner: a 32-bit counter wraps after about 67.1 s at 64 MHz, but after about 134.2 s at 32 MHz.

An RTC count is not a date. Turning it into calendar time, such as UTC (Coordinated Universal Time), needs a starting date and time set by software, and a correction for the clock drift §6.18 of the RTC’s crystal.

Function names can mislead. On nRF52 chips, Zephyr’s k_cycle_get_32() counts the 32,768 Hz RTC, not processor cycles; processor cycles come from the DWT cycle counter.

“Time since boot” depends on where the clock starts. An uptime clock that starts during kernel initialization misses everything that ran before it, so a boot time measured on it covers only part of the boot.

Finer resolution is not better accuracy. The two counters can disagree on the same chip because they count different oscillators, and the RTC, when a crystal drives it, is usually the more accurate one: a processor clock taken from an RC (resistor-capacitor) oscillator can be off by far more than a crystal. The Clock source and drift entry explains the difference.

Questions answered

Does the cycle counter keep advancing during sleep, halt, or clock changes?

Not in every case. A cycle counter is a register that the processor increments once per processor clock cycle, such as the CYCCNT register of the DWT (Data Watchpoint and Trace) unit on Arm Cortex-M processors, so it advances only while the processor clock runs:

For intervals that include sleep or a halt, or that last longer than the counter’s wrap period, which is about 67 s for a 32-bit counter at 64 MHz (megahertz), use an RTC (real-time clock): a low-power timer that counts a 32,768 Hz crystal clock and keeps counting through sleep. Use the cycle counter for short code sections that run at one known, constant processor clock frequency.

Related entries: DWT cycle counter (§6.20), Counter wraparound (§6.21), Monotonic clock (§9.35), Clock source and drift (§6.18), Zephyr RTOS (§6.33), Counter quantization (§11.35). Further reading: [81].