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 s needs ticks much shorter than 3 s. 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 s, 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 , which counts at 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:
it does not count while a debugger holds the processor halted;
it stops whenever the chip stops the processor clock, as many chips do in their sleep states to save power;
through a change of processor clock frequency it keeps counting, but each count then stands for a different duration.
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 s. 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:
resolution: one tick must be much shorter than the interval;
continuity: the counter must keep counting through every state the interval passes through, such as sleep or a debugger halt;
wraparound: the interval must be at least one tick shorter than the counter’s wrap period, or the wraps must be counted;
rate: the clock that drives the counter, and its accuracy, must be known for the whole interval.
Worked example
Tick lengths and wrap periods:
An RTC at 32,768 Hz: one tick is s, about 30.518 s. A 24-bit counter has values, so it wraps after s.
A cycle counter at 64 MHz: one tick is s, which is 15.625 ns. A 32-bit counter has values, so it wraps after s.
A function that runs for 3 s. The cycle counter advances by cycles. The RTC advances by of a tick, so it shows a difference of 0 or 1 tick, that is, 0 s or 30.5 s, 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: cycles, which converts to 2 ms. The RTC counts the whole 12 ms: 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 s. Dividing the total of 96,000 cycles by 64 MHz gives s, which is 0.5 ms, or 25%, too short.
Stated precisely
Let a counter be bits wide and count at a constant rate of counts per second, and let and be two readings. The elapsed count is , and the elapsed time is . This is correct under three conditions: the counter counted without stopping between the readings; it advanced fewer than times between them; and is the counter’s actual rate, which carries the error of its clock source. Write and for rounded down and up to whole numbers. If the true interval is , the counter advances or times between the readings, depending on where they fall between ticks. The second condition therefore holds whenever , that is, whenever the interval is at least one tick shorter than the wrap period . 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 times, the two readings are equal, and . Under the three conditions, is or , so is within one tick, , of . This one-tick uncertainty is called counter quantization §11.35.
When the rate changes during the interval, split the interval into pieces with constant rates and counts ; then . 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:
Halt: it stops. Arm specifies that the counter does not count while a debugger holds the processor halted.
Sleep: it stops whenever the chip stops the processor clock in that sleep state, as many chips do to save power; the chip’s manual says which sleep states do.
Clock changes: it keeps counting, but each count then stands for a different duration, so converting the total with one frequency is wrong. Split the count at each change and divide each part by its own frequency.
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].