Skip to content

Commit 92c130c

Browse files
authored
machine: compute rp2 clock dividers from crystal and target frequency (#4747)
Follow-up to #4728 which implemented the algorithm for finding the dividers. The calculation is computed at compile time by interp, as verified by building example/blinky1 for -target pico.
1 parent 31c4bc1 commit 92c130c

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/machine/machine_rp2_clocks.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,19 @@ func (clk *clock) configure(src, auxsrc, srcFreq, freq uint32) {
137137

138138
}
139139

140-
const pllsysFB, pllsysPD1, pllsysPD2 uint32 = 125, 6, 2 // RP2040 running 125MHz with 1500MHz VCO.
140+
var pllsysFB, pllsysPD1, pllsysPD2 uint32
141+
142+
// Compute clock dividers.
143+
//
144+
// Note that the entire init function is computed at compile time
145+
// by interp.
146+
func init() {
147+
fb, _, pd1, pd2, err := pllSearch{LockRefDiv: 1}.CalcDivs(xoscFreq*MHz, uint64(CPUFrequency()), MHz)
148+
if err != nil {
149+
panic(err)
150+
}
151+
pllsysFB, pllsysPD1, pllsysPD2 = uint32(fb), uint32(pd1), uint32(pd2)
152+
}
141153

142154
// init initializes the clock hardware.
143155
//
@@ -165,7 +177,7 @@ func (clks *clocksType) init() {
165177
// REF FBDIV VCO POSTDIV
166178
// pllSys: 12 / 1 = 12MHz * 125 = 1500MHZ / 6 / 2 = 125MHz
167179
// pllUSB: 12 / 1 = 12MHz * 40 = 480 MHz / 5 / 2 = 48MHz
168-
pllSys.init(1, uint32(pllsysFB), uint32(pllsysPD1), uint32(pllsysPD2))
180+
pllSys.init(1, pllsysFB, pllsysPD1, pllsysPD2)
169181
pllUSB.init(1, 40, 5, 2)
170182

171183
// Configure clocks

src/machine/machine_rp2_pll.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ var errVCOOverflow = errors.New("VCO calculation overflow; use lower MHz")
111111
//
112112
// Example for 12MHz crystal and RP2350:
113113
//
114-
// fbdiv, refdiv, pd1, pd2, _ := pllSearch{LockRefDiv:1}.CalcDivs(12*MHz, 133*MHz, MHz)
114+
// fbdiv, refdiv, pd1, pd2, _ := pllSearch{LockRefDiv:1}.CalcDivs(12*MHz, 150*MHz, MHz)
115115
type pllSearch struct {
116116
LowerVCO bool
117117
LockRefDiv uint8

0 commit comments

Comments
 (0)