1 /*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include "pico.h"
8 #include "hardware/regs/clocks.h"
9 #include "hardware/platform_defs.h"
10 #include "hardware/clocks.h"
11 #include "hardware/pll.h"
12 #include "hardware/irq.h"
13 #include "hardware/gpio.h"
14
15 check_hw_layout(clocks_hw_t, clk[clk_adc].selected, CLOCKS_CLK_ADC_SELECTED_OFFSET);
16 check_hw_layout(clocks_hw_t, fc0.result, CLOCKS_FC0_RESULT_OFFSET);
17 check_hw_layout(clocks_hw_t, ints, CLOCKS_INTS_OFFSET);
18
19 static uint32_t configured_freq[CLK_COUNT];
20
21 static resus_callback_t _resus_callback;
22
23 // Clock muxing consists of two components:
24 // - A glitchless mux, which can be switched freely, but whose inputs must be
25 // free-running
26 // - An auxiliary (glitchy) mux, whose output glitches when switched, but has
27 // no constraints on its inputs
28 // Not all clocks have both types of mux.
has_glitchless_mux(clock_handle_t clock)29 static inline bool has_glitchless_mux(clock_handle_t clock) {
30 return clock == clk_sys || clock == clk_ref;
31 }
32
clock_stop(clock_handle_t clock)33 void clock_stop(clock_handle_t clock) {
34 clock_hw_t *clock_hw = &clocks_hw->clk[clock];
35 hw_clear_bits(&clock_hw->ctrl, CLOCKS_CLK_USB_CTRL_ENABLE_BITS);
36 configured_freq[clock] = 0;
37 }
38
39 /// \tag::clock_configure[]
clock_configure_internal(clock_handle_t clock,uint32_t src,uint32_t auxsrc,uint32_t actual_freq,uint32_t div)40 static void clock_configure_internal(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t actual_freq, uint32_t div) {
41 clock_hw_t *clock_hw = &clocks_hw->clk[clock];
42
43 // If increasing divisor, set divisor before source. Otherwise set source
44 // before divisor. This avoids a momentary overspeed when e.g. switching
45 // to a faster source and increasing divisor to compensate.
46 if (div > clock_hw->div)
47 clock_hw->div = div;
48
49 // If switching a glitchless slice (ref or sys) to an aux source, switch
50 // away from aux *first* to avoid passing glitches when changing aux mux.
51 // Assume (!!!) glitchless source 0 is no faster than the aux source.
52 if (has_glitchless_mux(clock) && src == CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX) {
53 hw_clear_bits(&clock_hw->ctrl, CLOCKS_CLK_REF_CTRL_SRC_BITS);
54 while (!(clock_hw->selected & 1u))
55 tight_loop_contents();
56 }
57 // If no glitchless mux, cleanly stop the clock to avoid glitches
58 // propagating when changing aux mux. Note it would be a really bad idea
59 // to do this on one of the glitchless clocks (clk_sys, clk_ref).
60 else {
61 // Disable clock. On clk_ref and clk_sys this does nothing,
62 // all other clocks have the ENABLE bit in the same position.
63 hw_clear_bits(&clock_hw->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);
64 if (configured_freq[clock] > 0) {
65 // Delay for 3 cycles of the target clock, for ENABLE propagation.
66 // Note XOSC_COUNT is not helpful here because XOSC is not
67 // necessarily running, nor is timer...
68 uint delay_cyc = configured_freq[clk_sys] / configured_freq[clock] + 1;
69 busy_wait_at_least_cycles(delay_cyc * 3);
70 }
71 }
72
73 // Set aux mux first, and then glitchless mux if this clock has one
74 hw_write_masked(&clock_hw->ctrl,
75 (auxsrc << CLOCKS_CLK_SYS_CTRL_AUXSRC_LSB),
76 CLOCKS_CLK_SYS_CTRL_AUXSRC_BITS
77 );
78
79 if (has_glitchless_mux(clock)) {
80 hw_write_masked(&clock_hw->ctrl,
81 src << CLOCKS_CLK_REF_CTRL_SRC_LSB,
82 CLOCKS_CLK_REF_CTRL_SRC_BITS
83 );
84 while (!(clock_hw->selected & (1u << src)))
85 tight_loop_contents();
86 }
87
88 // Enable clock. On clk_ref and clk_sys this does nothing,
89 // all other clocks have the ENABLE bit in the same position.
90 hw_set_bits(&clock_hw->ctrl, CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS);
91
92 // Now that the source is configured, we can trust that the user-supplied
93 // divisor is a safe value.
94 clock_hw->div = div;
95 configured_freq[clock] = actual_freq;
96 }
97
clock_configure(clock_handle_t clock,uint32_t src,uint32_t auxsrc,uint32_t src_freq,uint32_t freq)98 bool clock_configure(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t freq) {
99 assert(src_freq >= freq);
100
101 if (freq > src_freq)
102 return false;
103
104 uint32_t div = (uint32_t)((((uint64_t) src_freq) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / freq);
105 uint32_t actual_freq = (uint32_t) ((((uint64_t) src_freq) << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) / div);
106
107 clock_configure_internal(clock, src, auxsrc, actual_freq, div);
108 // Store the configured frequency
109 return true;
110 }
111
clock_configure_int_divider(clock_handle_t clock,uint32_t src,uint32_t auxsrc,uint32_t src_freq,uint32_t int_divider)112 void clock_configure_int_divider(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq, uint32_t int_divider) {
113 clock_configure_internal(clock, src, auxsrc, src_freq / int_divider, int_divider << CLOCKS_CLK_GPOUT0_DIV_INT_LSB);
114 }
115
clock_configure_undivided(clock_handle_t clock,uint32_t src,uint32_t auxsrc,uint32_t src_freq)116 void clock_configure_undivided(clock_handle_t clock, uint32_t src, uint32_t auxsrc, uint32_t src_freq) {
117 clock_configure_internal(clock, src, auxsrc, src_freq, 1u << CLOCKS_CLK_GPOUT0_DIV_INT_LSB);
118 }
119
120 /// \end::clock_configure[]
121
122 /// \tag::clock_get_hz[]
clock_get_hz(clock_handle_t clock)123 uint32_t clock_get_hz(clock_handle_t clock) {
124 return configured_freq[clock];
125 }
126 /// \end::clock_get_hz[]
127
clock_set_reported_hz(clock_handle_t clock,uint hz)128 void clock_set_reported_hz(clock_handle_t clock, uint hz) {
129 configured_freq[clock] = hz;
130 }
131
132 /// \tag::frequency_count_khz[]
frequency_count_khz(uint src)133 uint32_t frequency_count_khz(uint src) {
134 fc_hw_t *fc = &clocks_hw->fc0;
135
136 // If frequency counter is running need to wait for it. It runs even if the source is NULL
137 while(fc->status & CLOCKS_FC0_STATUS_RUNNING_BITS) {
138 tight_loop_contents();
139 }
140
141 // Set reference freq
142 fc->ref_khz = clock_get_hz(clk_ref) / 1000;
143
144 // FIXME: Don't pick random interval. Use best interval
145 fc->interval = 10;
146
147 // No min or max
148 fc->min_khz = 0;
149 fc->max_khz = 0xffffffff;
150
151 // Set SRC which automatically starts the measurement
152 fc->src = src;
153
154 while(!(fc->status & CLOCKS_FC0_STATUS_DONE_BITS)) {
155 tight_loop_contents();
156 }
157
158 // Return the result
159 return fc->result >> CLOCKS_FC0_RESULT_KHZ_LSB;
160 }
161 /// \end::frequency_count_khz[]
162
clocks_handle_resus(void)163 static void clocks_handle_resus(void) {
164 // Set clk_sys back to the ref clock rather than it being forced to clk_ref
165 // by resus. Call the user's resus callback if they have set one
166
167 // CLK SYS = CLK_REF. Must be running for this code to be running
168 uint clk_ref_freq = clock_get_hz(clk_ref);
169 clock_configure_undivided(clk_sys,
170 CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLK_REF,
171 0,
172 clk_ref_freq);
173
174 // Assert we have been resussed
175 assert(clocks_hw->resus.status & CLOCKS_CLK_SYS_RESUS_STATUS_RESUSSED_BITS);
176
177 // Now we have fixed clk_sys we can safely remove the resus
178 hw_set_bits(&clocks_hw->resus.ctrl, CLOCKS_CLK_SYS_RESUS_CTRL_CLEAR_BITS);
179 hw_clear_bits(&clocks_hw->resus.ctrl, CLOCKS_CLK_SYS_RESUS_CTRL_CLEAR_BITS);
180
181 // Now we should no longer be resussed
182 assert(!(clocks_hw->resus.status & CLOCKS_CLK_SYS_RESUS_STATUS_RESUSSED_BITS));
183
184 // Call the user's callback to notify them of the resus event
185 if (_resus_callback) {
186 _resus_callback();
187 }
188 }
189
clocks_irq_handler(void)190 static void clocks_irq_handler(void) {
191 // Clocks interrupt handler. Only resus but handle irq
192 // defensively just in case.
193 uint32_t ints = clocks_hw->ints;
194
195 if (ints & CLOCKS_INTE_CLK_SYS_RESUS_BITS) {
196 ints &= ~CLOCKS_INTE_CLK_SYS_RESUS_BITS;
197 clocks_handle_resus();
198 }
199
200 #ifndef NDEBUG
201 if (ints) {
202 panic("Unexpected clocks irq\n");
203 }
204 #endif
205 }
206
clocks_enable_resus(resus_callback_t resus_callback)207 void clocks_enable_resus(resus_callback_t resus_callback) {
208 // Restart clk_sys if it is stopped by forcing it
209 // to the default source of clk_ref. If clk_ref stops running this will
210 // not work.
211
212 // Store user's resus callback
213 _resus_callback = resus_callback;
214
215 irq_set_exclusive_handler(CLOCKS_IRQ, clocks_irq_handler);
216
217 // Enable the resus interrupt in clocks
218 clocks_hw->inte = CLOCKS_INTE_CLK_SYS_RESUS_BITS;
219
220 // Enable the clocks irq
221 irq_set_enabled(CLOCKS_IRQ, true);
222
223 // 2 * clk_ref freq / clk_sys_min_freq;
224 // assume clk_ref is 3MHz and we want clk_sys to be no lower than 1MHz
225 uint timeout = 2 * 3 * 1;
226
227 // Enable resus with the maximum timeout
228 clocks_hw->resus.ctrl = CLOCKS_CLK_SYS_RESUS_CTRL_ENABLE_BITS | timeout;
229 }
230
clock_gpio_init_int_frac16(uint gpio,uint src,uint32_t div_int,uint16_t div_frac16)231 void clock_gpio_init_int_frac16(uint gpio, uint src, uint32_t div_int, uint16_t div_frac16) {
232 // Bit messy but it's as much code to loop through a lookup
233 // table. The sources for each gpout generators are the same
234 // so just call with the sources from GP0
235 uint gpclk = 0;
236 if (gpio == 21) gpclk = clk_gpout0;
237 else if (gpio == 23) gpclk = clk_gpout1;
238 else if (gpio == 24) gpclk = clk_gpout2;
239 else if (gpio == 25) gpclk = clk_gpout3;
240 #if !PICO_RP2040
241 else if (gpio == 13) gpclk = clk_gpout0;
242 else if (gpio == 15) gpclk = clk_gpout1;
243 #endif
244 else {
245 invalid_params_if(HARDWARE_CLOCKS, true);
246 }
247
248 invalid_params_if(HARDWARE_CLOCKS, div_int >> REG_FIELD_WIDTH(CLOCKS_CLK_GPOUT0_DIV_INT));
249 // Set up the gpclk generator
250 clocks_hw->clk[gpclk].ctrl = (src << CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_LSB) |
251 CLOCKS_CLK_GPOUT0_CTRL_ENABLE_BITS;
252 #if REG_FIELD_WIDTH(CLOCKS_CLK_GPOUT0_DIV_FRAC) == 16
253 clocks_hw->clk[gpclk].div = (div_int << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) | (div_frac16 << CLOCKS_CLK_GPOUT0_DIV_FRAC_LSB);
254 #elif REG_FIELD_WIDTH(CLOCKS_CLK_GPOUT0_DIV_FRAC) == 8
255 clocks_hw->clk[gpclk].div = (div_int << CLOCKS_CLK_GPOUT0_DIV_INT_LSB) | ((div_frac16>>8u) << CLOCKS_CLK_GPOUT0_DIV_FRAC_LSB);
256 #else
257 #error unsupported number of fractional bits
258 #endif
259
260 // Set gpio pin to gpclock function
261 gpio_set_function(gpio, GPIO_FUNC_GPCK);
262 }
263
264 static const uint8_t gpin0_src[CLK_COUNT] = {
265 CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT0
266 CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT1
267 CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT2
268 CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_GPOUT3
269 CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_REF
270 CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_SYS
271 CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_PERI
272 #if !PICO_RP2040
273 CLOCKS_CLK_HSTX_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_HSTX
274 #endif
275 CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_USB
276 CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_ADC
277 #if PICO_RP2040
278 CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0, // CLK_RTC
279 #endif
280 };
281
282 // Assert GPIN1 is GPIN0 + 1
283 static_assert(CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT0_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
284 static_assert(CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT1_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
285 static_assert(CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT2_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
286 static_assert(CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_GPOUT3_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
287 static_assert(CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_REF_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
288 static_assert(CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
289 static_assert(CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
290 #if HAS_HSTX
291 static_assert(CLOCKS_CLK_HSTX_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_HSTX_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
292 #endif
293 static_assert(CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
294 static_assert(CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
295 #if HAS_RP2040_RTC
296 static_assert(CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN1 == (CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_GPIN0 + 1), "hw mismatch");
297 #endif
298
clock_configure_gpin(clock_handle_t clock,uint gpio,uint32_t src_freq,uint32_t freq)299 bool clock_configure_gpin(clock_handle_t clock, uint gpio, uint32_t src_freq, uint32_t freq) {
300 // Configure a clock to run from a GPIO input
301 uint gpin = 0;
302 if (gpio == 20) gpin = 0;
303 else if (gpio == 22) gpin = 1;
304 #if PICO_RP2350
305 else if (gpio == 12) gpin = 0;
306 else if (gpio == 14) gpin = 1;
307 #endif
308 else {
309 invalid_params_if(HARDWARE_CLOCKS, true);
310 }
311
312 // Work out sources. GPIN is always an auxsrc
313 uint src = 0;
314
315 // GPIN1 == GPIN0 + 1
316 uint auxsrc = gpin0_src[clock] + gpin;
317
318 if (has_glitchless_mux(clock)) {
319 // AUX src is always 1
320 src = 1;
321 }
322
323 // Set the GPIO function
324 gpio_set_function(gpio, GPIO_FUNC_GPCK);
325
326 // Now we have the src, auxsrc, and configured the gpio input
327 // call clock configure to run the clock from a gpio
328 return clock_configure(clock, src, auxsrc, src_freq, freq);
329 }
330
331 // everything running off the USB oscillator
set_sys_clock_48mhz(void)332 void set_sys_clock_48mhz(void) {
333 if (!running_on_fpga()) {
334 // Change clk_sys to be 48MHz. The simplest way is to take this from PLL_USB
335 // which has a source frequency of 48MHz
336 clock_configure_undivided(clk_sys,
337 CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
338 CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
339 USB_CLK_HZ);
340
341 // Turn off PLL sys for good measure
342 pll_deinit(pll_sys);
343
344 // CLK peri is clocked from clk_sys so need to change clk_peri's freq
345 clock_configure_undivided(clk_peri,
346 0,
347 CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS,
348 USB_CLK_HZ);
349 }
350 }
351
352 // PICO_CONFIG: PICO_CLOCK_ADJUST_PERI_CLOCK_WITH_SYS_CLOCK, When the SYS clock PLL is changed keep the peripheral clock attached to it, type=bool, default=0, advanced=true, group=hardware_clocks
353 #ifndef PICO_CLOCK_ADJUST_PERI_CLOCK_WITH_SYS_CLOCK
354 // support old incorrect spelling too
355 #ifdef PICO_CLOCK_AJDUST_PERI_CLOCK_WITH_SYS_CLOCK
356 #define PICO_CLOCK_ADJUST_PERI_CLOCK_WITH_SYS_CLOCK PICO_CLOCK_AJDUST_PERI_CLOCK_WITH_SYS_CLOCK
357 #else
358 // By default, when reconfiguring the system clock PLL settings after runtime initialization,
359 // the peripheral clock is switched to the 48MHz USB clock to ensure continuity of peripheral operation.
360 // Setting this value to 1 changes the behavior to have the peripheral clock re-configured
361 // to the system clock at it's new frequency.
362 #define PICO_CLOCK_ADJUST_PERI_CLOCK_WITH_SYS_CLOCK 0
363 #endif
364 #endif
365
set_sys_clock_pll(uint32_t vco_freq,uint post_div1,uint post_div2)366 void set_sys_clock_pll(uint32_t vco_freq, uint post_div1, uint post_div2) {
367 if (!running_on_fpga()) {
368 clock_configure_undivided(clk_sys,
369 CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
370 CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
371 USB_CLK_HZ);
372
373 pll_init(pll_sys, PLL_SYS_REFDIV, vco_freq, post_div1, post_div2);
374 uint32_t freq = vco_freq / (post_div1 * post_div2);
375
376 // Configure clocks
377 // CLK_REF is the XOSC source
378 clock_configure_undivided(clk_ref,
379 CLOCKS_CLK_REF_CTRL_SRC_VALUE_XOSC_CLKSRC,
380 0, // No aux mux
381 XOSC_HZ);
382
383 // CLK SYS = PLL SYS (usually) 125MHz / 1 = 125MHz
384 clock_configure_undivided(clk_sys,
385 CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
386 CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS,
387 freq);
388
389 #if PICO_CLOCK_ADJUST_PERI_CLOCK_WITH_SYS_CLOCK
390 clock_configure_undivided(clk_peri,
391 0,
392 CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS,
393 freq);
394 #else
395 clock_configure_undivided(clk_peri,
396 0, // Only AUX mux on ADC
397 CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
398 USB_CLK_HZ);
399 #endif
400 }
401 }
402
check_sys_clock_hz(uint32_t freq_hz,uint * vco_out,uint * postdiv1_out,uint * postdiv2_out)403 bool check_sys_clock_hz(uint32_t freq_hz, uint *vco_out, uint *postdiv1_out, uint *postdiv2_out) {
404 uint reference_freq_hz = XOSC_HZ / PLL_SYS_REFDIV;
405 for (uint fbdiv = 320; fbdiv >= 16; fbdiv--) {
406 uint vco_hz = fbdiv * reference_freq_hz;
407 if (vco_hz < PICO_PLL_VCO_MIN_FREQ_HZ || vco_hz > PICO_PLL_VCO_MAX_FREQ_HZ) continue;
408 for (uint postdiv1 = 7; postdiv1 >= 1; postdiv1--) {
409 for (uint postdiv2 = postdiv1; postdiv2 >= 1; postdiv2--) {
410 uint out = vco_hz / (postdiv1 * postdiv2);
411 if (out == freq_hz && !(vco_hz % (postdiv1 * postdiv2))) {
412 *vco_out = vco_hz;
413 *postdiv1_out = postdiv1;
414 *postdiv2_out = postdiv2;
415 return true;
416 }
417 }
418 }
419 }
420 return false;
421 }
422
423 // Note this impl is kept to preserve previous rounding behavior, vs calling check_sys_clock_hz
check_sys_clock_khz(uint32_t freq_khz,uint * vco_out,uint * postdiv1_out,uint * postdiv2_out)424 bool check_sys_clock_khz(uint32_t freq_khz, uint *vco_out, uint *postdiv1_out, uint *postdiv2_out) {
425 uint reference_freq_khz = (XOSC_HZ / PICO_KHZ) / PLL_SYS_REFDIV;
426 for (uint fbdiv = 320; fbdiv >= 16; fbdiv--) {
427 uint vco_khz = fbdiv * reference_freq_khz;
428 if (vco_khz < PICO_PLL_VCO_MIN_FREQ_HZ / PICO_KHZ || vco_khz > PICO_PLL_VCO_MAX_FREQ_HZ / PICO_KHZ) continue;
429 for (uint postdiv1 = 7; postdiv1 >= 1; postdiv1--) {
430 for (uint postdiv2 = postdiv1; postdiv2 >= 1; postdiv2--) {
431 uint out = vco_khz / (postdiv1 * postdiv2);
432 if (out == freq_khz && !(vco_khz % (postdiv1 * postdiv2))) {
433 *vco_out = vco_khz * PICO_KHZ;
434 *postdiv1_out = postdiv1;
435 *postdiv2_out = postdiv2;
436 return true;
437 }
438 }
439 }
440 }
441 return false;
442 }
443