Clock Monitor Frequency Check

Browse source code on GitHub

Overview

This sample uses the clock monitor API in CLOCK_MONITOR_MODE_WINDOW mode: the hardware continuously compares a clock against programmable thresholds and reports every crossing through a callback, which logs whether the clock ran too fast or too slow. The main loop only prints a periodic heartbeat.

The window is centred on CONFIG_SAMPLE_EXPECTED_HZ, widened by CONFIG_SAMPLE_TOLERANCE_PPM and refreshed every CONFIG_SAMPLE_WINDOW_NS. Leaving the expected frequency at 0 derives the centre from the clock’s actual rate through the clock control API.

Requirements

Building and Running

west build -b frdm_mcxe31b samples/drivers/clock_monitor/check_freq
west flash

On a board using the FREQME peripheral, add the shared overlay:

west build -b frdm_mcxn236 samples/drivers/clock_monitor/check_freq -- -DEXTRA_DTC_OVERLAY_FILE="../clock_monitor.overlay"
west flash

Sample Output

[00:00:00.000,000] <inf> sample: clock check running (expected ~0 Hz, tolerance +/-50000 ppm, window 1000000 ns)
[00:00:05.000,000] <inf> sample: heartbeat: clock check running
[00:00:10.000,000] <inf> sample: heartbeat: clock check running

A clock drifting outside the window, or stopping, is reported from the callback:

[00:00:12.345,000] <err> sample: [cmu-fc@2bc000] monitored clock below lower threshold (or lost)

See also

Clock Monitor