2.18. PLL

2.18.1. Overview

The PLL is designed to take a reference clock, and multiply it using a VCO (Voltage Controlled Oscillator) with a feedback loop. The VCO must run at high frequencies (between 750 and 1600MHz), so there are two dividers, known as post dividers that can divide the VCO frequency before it is distributed to the clock generators on the chip.

There are two PLLs in RP2040. They are:

Figure 35. On both PLLs, the FREF (reference) input is connected to the crystal oscillator's XI input. The PLL contains a VCO, which is locked to a constant ratio of the reference clock via the feedback loop (phase-frequency detector and loop filter). This can synthesise very high frequencies, which may be divided down by the post-dividers.

Block diagram of the PLL circuit. The FREF input goes to a divider (÷1-63). The output of this divider goes to a PFD (Phase-Frequency Detector). The PFD also receives feedback from a Feedback Divide (÷16-320) block. The PFD output goes to a VCO (Voltage Controlled Oscillator). The VCO output goes to two post-dividers (POSTDIV1 and POSTDIV2), each with a ÷1-7 divider. The output of POSTDIV1 goes to a BYPASS block. The output of POSTDIV2 goes to a BYPASS block. The BYPASS block output goes to the CLKSSCG output. The VCO output also goes to a Lock Detect block, which outputs LOCK. The VCO output also goes to the FOUTVCO output. The output of the BYPASS block goes to the FOUTPOSTDIV output. A legend indicates: Yellow box = Analog circuits, Orange box = Post divider rate circuits, Blue box = Reference rate circuits.
Block diagram of the PLL circuit. The FREF input goes to a divider (÷1-63). The output of this divider goes to a PFD (Phase-Frequency Detector). The PFD also receives feedback from a Feedback Divide (÷16-320) block. The PFD output goes to a VCO (Voltage Controlled Oscillator). The VCO output goes to two post-dividers (POSTDIV1 and POSTDIV2), each with a ÷1-7 divider. The output of POSTDIV1 goes to a BYPASS block. The output of POSTDIV2 goes to a BYPASS block. The BYPASS block output goes to the CLKSSCG output. The VCO output also goes to a Lock Detect block, which outputs LOCK. The VCO output also goes to the FOUTVCO output. The output of the BYPASS block goes to the FOUTPOSTDIV output. A legend indicates: Yellow box = Analog circuits, Orange box = Post divider rate circuits, Blue box = Reference rate circuits.

2.18.2. Calculating PLL parameters

To configure the PLL, you must know the frequency of the reference clock, which on RP2040 is routed directly from the crystal oscillator. This will often be a 12MHz crystal, for compatibility with RP2040's USB bootrom. The PLL's final output frequency FOUTPOSTDIV can then be calculated as \( (FREF / REFDIV) \times FBDIV / (POSTDIV1 \times POSTDIV2) \) . With a desired output frequency in mind, you must select PLL parameters according to the following constraints of the PLL design:

Additionally, the maximum frequencies of the chip's clock generators (attached to \( F_{OUTPOSTDIV} \) ) must be respected. For the system PLL this is 133MHz, and for the USB PLL, 48MHz.

info icon NOTE

The crystal oscillator on RP2040 is designed for crystals between 5 and 15MHz, so typically \( REFDIV \) should be 1. If the application circuit drives a faster reference directly into the XI input, and a low VCO frequency is desired, the reference divisor can be increased to keep the PLL input within a suitable range.

lightbulb icon TIP

When two different values are required for \( POSTDIV1 \) and \( POSTDIV2 \) , it's preferable to assign the higher value to \( POSTDIV1 \) , for lower power consumption.

In the RP2040 reference design (see the Minimal Design Example in Hardware design with RP2040 ), which attaches a 12MHz crystal to the crystal oscillator, this implies that the minimum achievable and legal VCO frequency is \( 12\text{MHz} \times 63 = 756\text{MHz} \) , and the maximum VCO is \( 12\text{MHz} \times 133 = 1596\text{MHz} \) , so \( FBDIV \) must remain in the range 63 \( \rightarrow \) 133. For example, setting \( FBDIV \) to 100 would synthesise a 1200MHz VCO frequency. A \( POSTDIV1 \) value of 6 and a \( POSTDIV2 \) value of 2 would divide this by 12 in total, producing a clean 100MHz at the PLL's final output.

2.18.2.1. Jitter vs Power Consumption

There are often several sets of PLL configuration parameters which achieve, or are very close to, the desired output frequency. It is up to the programmer to decide whether to prioritise low PLL power consumption, or lower jitter , which is cycle-to-cycle variation in the PLL's output clock period. This is not a concern as far as system stability is concerned, because RP2040's digital logic is designed with margin for the worst-case possible jitter on the system clock, but a highly accurate clock is often needed for audio and video applications, or where data is being transmitted and received in accordance with a specification. For example, the USB specification defines a maximum amount of allowable jitter.

Jitter is minimised by running the VCO at the highest possible frequency, so that higher post-divide values can be used. For example, \( 1500\text{MHz VCO} / 6 / 2 = 125\text{MHz} \) . To reduce power consumption, the VCO frequency should be as low as possible. For example: \( 750\text{MHz VCO} / 6 / 1 = 125\text{MHz} \) .

Another consideration here is that slightly adjusting the output frequency may allow a much lower VCO frequency to be achieved, by bringing the output to a closer rational multiple of the input. Indeed the exact desired frequency may not be exactly achievable with any allowable VCO frequency, or combination of divisors.

SDK provides a Python script that searches for the best VCO and post divider options for a desired output frequency:

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2-common/hardware_clocks/scripts/vccalc.py

1 #!/usr/bin/env python3
2
3 import argparse
4 import sys
5
6 # Fixed hardware parameters
7 fbdiv_range = range(16, 320 + 1)
8 postdiv_range = range(1, 7 + 1)
9 ref_min = 5
10 refdiv_min = 1
11 refdiv_max = 63
12
13 def validRefdiv(string):
14     if ((int(string) < refdiv_min) or (int(string) > refdiv_max)):
15         raise ValueError("REFDIV must be in the range {} to {}".format(refdiv_min,
16                                 refdiv_max))
17     return int(string)
18
19 parser = argparse.ArgumentParser(description="PLL parameter calculator")
20 parser.add_argument("--input", "-i", default=12, help="Input (reference) frequency. Default
21     12 MHz", type=float)
22 parser.add_argument("--ref-min", default=5, help="Override minimum reference frequency.
23     Default 5 MHz", type=float)
24 parser.add_argument("--vco-max", default=1600, help="Override maximum VCO frequency. Default
25     1600 MHz", type=float)
26 parser.add_argument("--vco-min", default=750, help="Override minimum VCO frequency. Default
27     750 MHz", type=float)
28 parser.add_argument("--cmake", action="store_true", help="Print out a CMake snippet to apply
29     the selected PLL parameters to your program")
30 parser.add_argument("--cmake-only", action="store_true", help="Same as --cmake, but do not
31     print anything other than the CMake output")
32 parser.add_argument("--cmake-executable-name", default="<program>", help="Set the executable
33     name to use in the generated CMake output")
34 parser.add_argument("--lock-refdiv", help="Lock REFDIV to specified number in the range {} to
35     {}".format(refdiv_min, refdiv_max), type=validRefdiv)
36 parser.add_argument("--low-vco", "-l", action="store_true", help="Use a lower VCO frequency
37     when possible. This reduces power consumption, at the cost of increased jitter")
38 parser.add_argument("output", help="Output frequency in MHz.", type=float)
39 args = parser.parse_args()
40
41 refdiv_range = range(refdiv_min, max(refdiv_min, min(refdiv_max, int(args.input / args
42     .ref_min))) + 1)
43 if args.lock_refdiv:
44     print("Locking REFDIV to", args.lock_refdiv)
45     refdiv_range = [args.lock_refdiv]
46
47 best = (0, 0, 0, 0, 0, 0)
48 best_margin = args.output
49
50 for refdiv in refdiv_range:
51     for fbdiv in fbdiv_range:
52         vco = args.input / refdiv * fbdiv
53         if vco < args.vco_min or vco > args.vco_max:
54             continue
55         # pd1 is inner loop so that we prefer higher ratios of pd1:pd2
56         for pd2 in postdiv_range:
57             for pd1 in postdiv_range:
58                 out = vco / pd1 / pd2
59                 margin = abs(out - args.output)
60                 vco_is_better = vco < best[5] if args.low_vco else vco > best[5]
61                 if ((vco * 1000) % (pd1 * pd2)):
62                     continue
63                 if margin < best_margin or (abs(margin - best_margin) < 1e-9 and
64                     vco_is_better):
65                     best = (out, fbdiv, pd1, pd2, refdiv, vco)
66                     best_margin = margin
67
68 best_out, best_fbdiv, best_pd1, best_pd2, best_refdiv, best_vco = best
69
70 if best[0] > 0:
71     cmake_output = \
72 f"""target_compile_definitions({args.cmake_executable_name} PRIVATE
73     PLL_SYS_REFDIV={best_refdiv}
74     PLL_SYS_VCO_FREQ_HZ={int((args.input * 1_000_000) / best_refdiv * best_fbdiv)}
75     PLL_SYS_POSTDIV1={best_pd1}
64     PLL_SYS_POSTDIV2={best_pd2}
65     SYS_CLK_HZ={int((args.input * 1_000_000) / (best_refdiv * best_pd1 * best_pd2) *
    best_fbdiv)}
66 )
67 """
68 if not args.cmake_only:
69     print("Requested: {} MHz".format(args.output))
70     print("Achieved: {} MHz".format(best_out))
71     print("REFDIV: {}".format(best_refdiv))
72     print("FBDIV: {} (VCO = {} MHz)".format(best_fbdiv, args.input / best_refdiv *
    best_fbdiv))
73     print("PD1: {}".format(best_pd1))
74     print("PD2: {}".format(best_pd2))
75     if best_refdiv != 1:
76         print(
77             "\nThis requires a non-default REFDIV value.\n"
78             "Add the following to your CMakeLists.txt to apply the REFDIV:\n"
79         )
80     elif args.cmake or args.cmake_only:
81         print("")
82     if args.cmake or args.cmake_only or best_refdiv != 1:
83         print(cmake_output)
84 else:
85     sys.exit("No solution found")

Given an input and output frequency, this script will find the best possible set of PLL parameters to get as close as possible. Where multiple equally good combinations are found, it returns the parameters which yield the highest VCO frequency, for best output stability. The -l or --low-vco flag will instead prefer lower frequencies, for reduced power consumption.

Here a 48MHz output is requested:

$ ./vcocalc.py 48
Requested: 48.0 MHz
Achieved: 48.0 MHz
FBDIV: 120 (VCO = 1440 MHz)
PD1: 6
PD2: 5

Asking for a 48MHz output with a lower VCO frequency, if possible:

$ ./vcocalc.py -l 48
Requested: 48.0 MHz
Achieved: 48.0 MHz
FBDIV: 64 (VCO = 768 MHz)
PD1: 4
PD2: 4

For a 125MHz system clock with a 12MHz input, the minimum VCO frequency is quite high.

$ ./vcocalc.py -l 125
Requested: 125.0 MHz
Achieved: 125.0 MHz
FBDIV: 125 (VCO = 1500 MHz)
PD1: 6

PD2: 2

We can restrict the search to lower VCO frequencies, so that the script will consider looser frequency matches. Note that, whilst a 750MHz VCO would be ideal here, we can't achieve exactly 750MHz by multiplying the 12MHz input by an integer, which is why the previous invocation returned such a high VCO frequency.

$ ./vcocalc.py -l 125 --vco-max 800
Requested: 125.0 MHz
Achieved: 126.0 MHz
FBDIV: 63 (VCO = 756 MHz)
PD1: 6
PD2: 1

A 126MHz system clock may be a tolerable deviation from the desired 125MHz, and generating this clock consumes less power at the PLL.

2.18.3. Configuration

The SDK uses the following PLL settings:

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_clocks/include/hardware/clocks.h Lines 143 - 164

143 // There are two PLLs in RP-series microcontrollers:
144 // 1. The 'SYS PLL' generates the system clock, the frequency is defined by `SYS_CLK_KHZ`.
145 // 2. The 'USB PLL' generates the USB clock, the frequency is defined by `USB_CLK_KHZ`.
146 //
147 // The two PLLs use the crystal oscillator output directly as their reference frequency input;
148 // the PLLs reference
149 // frequency cannot be reduced by the dividers present in the clocks block. The crystal
150 // frequency is defined by `XOSC_HZ` (or
151 // `XOSC_KHZ` or `XOSC_MHZ`).
152 //
153 // The system's default definitions are correct for the above frequencies with a 12MHz
154 // crystal frequency. If different frequencies are required, these must be defined in
155 // the board configuration file together with the revised PLL settings
156 // Use `vcocalc.py` to check and calculate new PLL settings if you change any of these
157 // frequencies.
158 //
159 // Default PLL configuration RP2040:
160 //
161 // REF      FBDIV VCO      POSTDIV
162 // PLL SYS: 12 / 1 = 12MHz * 125 = 1500MHz / 6 / 2 = 125MHz
163 // PLL USB: 12 / 1 = 12MHz * 100 = 1200MHz / 5 / 5 = 48MHz
164 //
165 // Default PLL configuration RP2350:
166 //
167 // REF      FBDIV VCO      POSTDIV
168 // PLL SYS: 12 / 1 = 12MHz * 125 = 1500MHz / 5 / 2 = 150MHz
169 // PLL USB: 12 / 1 = 12MHz * 100 = 1200MHz / 5 / 5 = 48MHz

The pll_init function in the SDK, which we will examine below, asserts that all of these conditions are true before attempting to configure the PLL.

The SDK defines the PLL control registers as a struct. It then maps them into memory for each instance of the PLL.

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2040/hardware_structs/include/hardware/structs/pll.h Lines 27 - 53

27 typedef struct {
28     _REG_(PLL_CS_OFFSET) // PLL_CS
29     // Control and Status
30     // 0x00000000 [31] LOCK          (0) PLL is locked
31     // 0x00000100 [8]  BYPASS        (0) Passes the reference clock to the output instead of
    the...
32     // 0x0000003f [5:0] REFDIV        (0x01) Divides the PLL input reference clock
33     io_rw_32 cs;
34
35     _REG_(PLL_PWR_OFFSET) // PLL_PWR
36     // Controls the PLL power modes
37     // 0x00000020 [5]  VCOPD          (1) PLL VCO powerdown +
38     // 0x00000008 [3]  POSTDIVPD      (1) PLL post divider powerdown +
39     // 0x00000004 [2]  DSMPD          (1) PLL DSM powerdown +
40     // 0x00000001 [0]  PD              (1) PLL powerdown +
41     io_rw_32 pwr;
42
43     _REG_(PLL_FBDIV_INT_OFFSET) // PLL_FBDIV_INT
44     // Feedback divisor
45     // 0x00000fff [11:0] FBDIV_INT    (0x000) see ctrl reg description for constraints
46     io_rw_32 fbdiv_int;
47
48     _REG_(PLL_PRIM_OFFSET) // PLL_PRIM
49     // Controls the PLL post dividers for the primary output
50     // 0x00070000 [18:16] POSTDIV1     (0x7) divide by 1-7
51     // 0x00007000 [14:12] POSTDIV2     (0x7) divide by 1-7
52     io_rw_32 prim;
53 } pll_hw_t;

The SDK defines pll_init which is used to configure, or reconfigure a PLL. It starts by clearing any previous power state in the PLL, then calculates the appropriate feedback divider value. There are assertions to check these values satisfy the constraints above.

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_pll/pll.c Lines 13 - 21

13 void pll_init(PLL pll, uint refdiv, uint vco_freq, uint post_div1, uint post_div2) {
14     uint32_t ref_freq = XOSC_HZ / refdiv;
15
16     // Check vco freq is in an acceptable range
17     assert(vco_freq >= PICO_PLL_VCO_MIN_FREQ_HZ && vco_freq <= PICO_PLL_VCO_MAX_FREQ_HZ);
18
19     // What are we multiplying the reference clock by to get the vco freq
20     // (The regs are called div, because you divide the vco output and compare it to the
    refclk)
21     uint32_t fbdiv = vco_freq / ref_freq;

The programming sequence for the PLL is as follows:

SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2-common/hardware_pll/pll.c Lines 42 - 69

42     if ((pll->cs & PLL_CS_LOCK_BITS) &&
43         (refdiv == (pll->cs & PLL_CS_REFDIV_BITS)) &&
44         (fbdiv == (pll->fbdiv_int & PLL_FBDIV_INT_BITS)) &&
45         (pdiv == (pll->prim & (PLL_PRIM_POSTDIV1_BITS | PLL_PRIM_POSTDIV2_BITS)))) {
46         // do not disrupt PLL that is already correctly configured and operating
47         return;
48     }
49
50     reset_unreset_block_num_wait_blocking(PLL_RESET_NUM(pll));
51
52     // Load VCO-related dividers before starting VCO
53     pll->cs = refdiv;
54     pll->fbdiv_int = fbdiv;
55
56     // Turn on PLL
57     uint32_t power = PLL_PWR_PD_BITS | // Main power
58                   PLL_PWR_VCO_PD_BITS; // VCO Power
59
60     hw_clear_bits(&pll->pwr, power);
61
62     // Wait for PLL to lock
63     while (!(pll->cs & PLL_CS_LOCK_BITS)) tight_loop_contents();
64
65     // Set up post dividers
66     pll->prim = pdiv;
67
68     // Turn on post divider
69     hw_clear_bits(&pll->pwr, PLL_PWR_POSTDIVPD_BITS);

Note the VCO is turned on first, followed by the post dividers so the PLL does not output a dirty clock while the VCO is locking.

2.18.4. List of Registers

The PLL_SYS and PLL_USB registers start at base addresses of 0x40028000 and 0x4002c000 respectively (defined as PLL_SYS_BASE and PLL_USB_BASE in SDK).

Table 274. List of PLL registers

OffsetNameInfo
0x0CSControl and Status
0x4PWRControls the PLL power modes.
0x8FBDIV_INTFeedback divisor
0xcPRIMControls the PLL post dividers for the primary output

PLL: CS Register

Offset: 0x0

Description

Control and Status
GENERAL CONSTRAINTS:
Reference clock frequency min=5MHz, max=800MHz
Feedback divider min=16, max=320
VCO frequency min=750MHz, max=1600MHz

Table 275. CS Register

BitsDescriptionTypeReset
31LOCK : PLL is lockedRO0x0
30:9Reserved.--
8BYPASS : Passes the reference clock to the output instead of the divided VCO. The VCO continues to run so the user can switch between the reference clock and the divided VCO but the output will glitch when doing so.RW0x0
7:6Reserved.--
5:0REFDIV : Divides the PLL input reference clock.
Behaviour is undefined for div=0.
PLL output will be unpredictable during refdiv changes, wait for lock=1 before using it.
RW0x01

PLL: PWR Register

Offset: 0x4

Description

Controls the PLL power modes.

Table 276. PWR Register

BitsDescriptionTypeReset
31:6Reserved.--
5VCOPD : PLL VCO powerdown
To save power set high when PLL output not required or bypass=1.
RW0x1
4Reserved.--
3POSTDIVPD : PLL post divider powerdown
To save power set high when PLL output not required or bypass=1.
RW0x1
2DSMPD : PLL DSM powerdown
Nothing is achieved by setting this low.
RW0x1
1Reserved.--
0PD : PLL powerdown
To save power set high when PLL output not required.
RW0x1

PLL: FBDIV_INT Register

Offset: 0x8

Description

Feedback divisor

(note: this PLL does not support fractional division)

Table 277. FBDIV_INT Register

BitsDescriptionTypeReset
31:12Reserved.--
11:0see ctrl reg description for constraintsRW0x000

PLL: PRIM Register

Offset: 0xc

Description

Controls the PLL post dividers for the primary output

(note: this PLL does not have a secondary output)
the primary output is driven from VCO divided by postdiv1*postdiv2

Table 278. PRIM
Register

BitsDescriptionTypeReset
31:19Reserved.--
18:16POSTDIV1 : divide by 1-7RW0x7
15Reserved.--
14:12POSTDIV2 : divide by 1-7RW0x7
11:0Reserved.--