Skip to content

Commit

Permalink
machine: compute rp2 clock dividers from crystal and target frequency
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eliasnaur committed Feb 21, 2025
1 parent 6e97079 commit a805d75
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/machine/machine_rp2_clocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,19 @@ func (clk *clock) configure(src, auxsrc, srcFreq, freq uint32) {

}

const pllsysFB, pllsysPD1, pllsysPD2 uint32 = 125, 6, 2 // RP2040 running 125MHz with 1500MHz VCO.
var pllsysFB, pllsysPD1, pllsysPD2 uint32

// Compute dividers for RP2040 running 125MHz with 1500MHz VCO.
//
// Note that the entire init function is computed at compile time
// by interp.
func init() {
fb, _, pd1, pd2, err := pllSearch{LockRefDiv: 1}.CalcDivs(12*MHz, 125*MHz, MHz)
if err != nil {
panic(err)
}
pllsysFB, pllsysPD1, pllsysPD2 = uint32(fb), uint32(pd1), uint32(pd2)
}

// init initializes the clock hardware.
//
Expand Down Expand Up @@ -165,7 +177,7 @@ func (clks *clocksType) init() {
// REF FBDIV VCO POSTDIV
// pllSys: 12 / 1 = 12MHz * 125 = 1500MHZ / 6 / 2 = 125MHz
// pllUSB: 12 / 1 = 12MHz * 40 = 480 MHz / 5 / 2 = 48MHz
pllSys.init(1, uint32(pllsysFB), uint32(pllsysPD1), uint32(pllsysPD2))
pllSys.init(1, pllsysFB, pllsysPD1, pllsysPD2)
pllUSB.init(1, 40, 5, 2)

// Configure clocks
Expand Down
2 changes: 1 addition & 1 deletion src/machine/machine_rp2_pll.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var errVCOOverflow = errors.New("VCO calculation overflow; use lower MHz")
//
// Example for 12MHz crystal and RP2350:
//
// fbdiv, refdiv, pd1, pd2, _ := pllSearch{LockRefDiv:1}.CalcDivs(12*MHz, 133*MHz, MHz)
// fbdiv, refdiv, pd1, pd2, _ := pllSearch{LockRefDiv:1}.CalcDivs(12*MHz, 150*MHz, MHz)
type pllSearch struct {
LowerVCO bool
LockRefDiv uint8
Expand Down

0 comments on commit a805d75

Please sign in to comment.