1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ... */
#include "hardware/clocks.h"
#include "hardware/pll.h"
#include "hardware/resets.h"
void pll_init(PLL pll, uint refdiv, uint vco_freq, uint post_div1, uint post_div2) {
uint32_t ref_freq = XOSC_HZ / refdiv;
assert(vco_freq >= PICO_PLL_VCO_MIN_FREQ_HZ && vco_freq <= PICO_PLL_VCO_MAX_FREQ_HZ);
uint32_t fbdiv = vco_freq / ref_freq;
assert(fbdiv >= 16 && fbdiv <= 320);
assert((post_div1 >= 1 && post_div1 <= 7) && (post_div2 >= 1 && post_div2 <= 7));
assert(ref_freq <= (vco_freq / 16));
uint32_t pdiv = (post_div1 << PLL_PRIM_POSTDIV1_LSB) |
(post_div2 << PLL_PRIM_POSTDIV2_LSB);
if ((pll->cs & PLL_CS_LOCK_BITS) &&
(refdiv == (pll->cs & PLL_CS_REFDIV_BITS)) &&
(fbdiv == (pll->fbdiv_int & PLL_FBDIV_INT_BITS)) &&
(pdiv == (pll->prim & (PLL_PRIM_POSTDIV1_BITS | PLL_PRIM_POSTDIV2_BITS)))) {
return;
}if ((pll->cs & PLL_CS_LOCK_BITS) && (refdiv == (pll->cs & PLL_CS_REFDIV_BITS)) && (fbdiv == (pll->fbdiv_int & PLL_FBDIV_INT_BITS)) && (pdiv == (pll->prim & (PLL_PRIM_POSTDIV1_BITS | PLL_PRIM_POSTDIV2_BITS)))) { ... }
reset_unreset_block_num_wait_blocking(PLL_RESET_NUM(pll));
pll->cs = refdiv;
pll->fbdiv_int = fbdiv;
uint32_t power = PLL_PWR_PD_BITS |
PLL_PWR_VCOPD_BITS;
hw_clear_bits(&pll->pwr, power);
while (!(pll->cs & PLL_CS_LOCK_BITS)) tight_loop_contents();
pll->prim = pdiv;
hw_clear_bits(&pll->pwr, PLL_PWR_POSTDIVPD_BITS);
}{ ... }
void pll_deinit(PLL pll) {
pll->pwr = PLL_PWR_BITS;
}{ ... }