Datasheet

Table Of Contents
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2040/hardware_structs/include/hardware/structs/clocks.h Lines 34 - 38
34 typedef struct {
35 io_rw_32 ctrl;
36 io_rw_32 div;
37 io_rw_32 selected;
38 } clock_hw_t;
To configure a clock, we need to know the following pieces of information:
The frequency of the clock source
The mux / aux mux position of the clock source
The desired output frequency
The Pico SDK provides clock_configure to configure a clock:
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2_common/hardware_clocks/clocks.c Lines 39 - 110
Ê39 int clock_configure(enum clock_index clk_index, uint32_t src, uint32_t auxsrc, uint32_t
Ê src_freq, uint32_t freq) {
Ê40 uint32_t div;
Ê41
Ê42 assert(src_freq >= freq);
Ê43
Ê44 if (freq > src_freq)
Ê45 return -1;
Ê46
Ê47 // Div register is 24.8 int.frac divider so multiply by 2^8 (left shift by 8)
Ê48 div = (uint32_t) (((uint64_t) src_freq << 8) / freq);
Ê49
Ê50 clock_hw_t *clock = &clocks_hw->clk[clk_index];
Ê51
Ê52 // If increasing divisor, set divisor before source. Otherwise set source
Ê53 // before divisor. This avoids a momentary overspeed when e.g. switching
Ê54 // to a faster source and increasing divisor to compensate.
Ê55 if (div > clock->div)
Ê56 clock->div = div;
Ê57
Ê58 // If switching a glitchless slice (ref or sys) to an aux source, switch
Ê59 // away from aux *first* to avoid passing glitches when changing aux mux.
Ê60 // Assume (!!!) glitchless source 0 is no faster than the aux source.
Ê61 if (has_glitchless_mux(clk_index) && src ==
Ê CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX) {
Ê62 hw_clear_bits(&clock->ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS);
Ê63 while (!(clock->selected & 1u))
Ê64 tight_loop_contents();
Ê65 }
Ê66 // If no glitchless mux, cleanly stop the clock to avoid glitches
Ê67 // propagating when changing aux mux. Note it would be a really bad idea
Ê68 // to do this on one of the glitchless clocks (clk_sys, clk_ref).
Ê69 else {
Ê70 hw_clear_bits(&clock->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);
Ê71 if (configured_freq[clk_index] > 0) {
Ê72 // Delay for 3 cycles of the target clock, for ENABLE propagation.
Ê73 // Note XOSC_COUNT is not helpful here because XOSC is not
Ê74 // necessarily running, nor is timer... so, 3 cycles per loop:
Ê75 uint delay_cyc = configured_freq[clk_sys] / configured_freq[clk_index] + 1;
Ê76 asm volatile (
Ê77 "1: \n\t"
Ê78 "sub %0, #1 \n\t"
Ê79 "bne 1b"
RP2040 Datasheet
2.14. Clocks 169