Datasheet

Table Of Contents
WARNING
It is assumed the source frequency the programmer provides is correct. If it is not then the frequency returned by
clock_get_hz will be inaccurate.
2.15.6.2. Using the frequency counter
To use the frequency counter, the programmer must:
Set the reference frequency: clk_ref
Set the mux position of the source they want to measure. See FC0_SRC
Wait for the DONE status bit in FC0_STATUS to be set
Read the result
The SDK defines a frequency_count function which takes the source as an argument and returns the frequency in kHz:
SDK: https://github.com/raspberrypi/pico-sdk/tree/master/src/rp2_common/hardware_clocks/clocks.c Lines 218 - 245
218 uint32_t frequency_count_khz(uint src) {
219 fc_hw_t *fc = &clocks_hw->fc0;
220
221 // If frequency counter is running need to wait for it. It runs even if the source is
Ê NULL
222 while(fc->status & CLOCKS_FC0_STATUS_RUNNING_BITS) {
223 tight_loop_contents();
224 }
225
226 // Set reference freq
227 fc->ref_khz = clock_get_hz(clk_ref) / 1000;
228
229 // FIXME: Don't pick random interval. Use best interval
230 fc->interval = 10;
231
232 // No min or max
233 fc->min_khz = 0;
234 fc->max_khz = 0xffffffff;
235
236 // Set SRC which automatically starts the measurement
237 fc->src = src;
238
239 while(!(fc->status & CLOCKS_FC0_STATUS_DONE_BITS)) {
240 tight_loop_contents();
241 }
242
243 // Return the result
244 return fc->result >> CLOCKS_FC0_RESULT_KHZ_LSB;
245 }
There is also a wrapper function to change the unit to MHz`:
SDK: https://github.com/raspberrypi/pico-sdk/tree/master/src/rp2_common/hardware_clocks/include/hardware/clocks.h Lines 148 - 150
148 static inline float frequency_count_mhz(uint src) {
149 return ((float) (frequency_count_khz(src))) / KHZ;
150 }
RP2040 Datasheet
2.15. Clocks 215