Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Separate system clock config for rp2xxx devices #4674

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/machine/board_ae_rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const (
GP27 Pin = GPIO27
GP28 Pin = GPIO28
GP29 Pin = GPIO29

// Onboard crystal oscillator frequency, in MHz.
xoscFreq = 12 // MHz
)

// I2C Default pins on Raspberry Pico.
Expand Down
5 changes: 0 additions & 5 deletions src/machine/board_badger2040-w.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ SPI0_CS_PIN Pin = QSPI_CS
*/
)

// Onboard crystal oscillator frequency, in MHz.
const (
xoscFreq = 12 // MHz
)

// USB CDC identifiers
const (
usb_STRING_PRODUCT = "Badger 2040 W"
Expand Down
5 changes: 0 additions & 5 deletions src/machine/board_badger2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ SPI0_CS_PIN Pin = QSPI_CS
*/
)

// Onboard crystal oscillator frequency, in MHz.
const (
xoscFreq = 12 // MHz
)

// USB CDC identifiers
const (
usb_STRING_PRODUCT = "Badger 2040"
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_challenger_rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ package machine

const (
LED = GPIO24

// Onboard crystal oscillator frequency, in MHz.
xoscFreq = 12 // MHz
)

// GPIO Pins
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_feather_rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

package machine

// Onboard crystal oscillator frequency, in MHz.
const xoscFreq = 12 // MHz

// GPIO Pins
const (
D4 = GPIO6
Expand Down
5 changes: 0 additions & 5 deletions src/machine/board_gopher-badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ const (
SPI1_SDI_PIN Pin = NoPin
)

// Onboard crystal oscillator frequency, in MHz.
const (
xoscFreq = 12 // MHz
)

// USB CDC identifiers
const (
usb_STRING_PRODUCT = "Gopher Badge"
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_kb2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

package machine

// Onboard crystal oscillator frequency, in MHz.
const xoscFreq = 12 // MHz

// GPIO Pins
const (
D0 = GPIO0
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_macropad-rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ package machine

const (
NeopixelCount = 12

// Onboard crystal oscillator frequency, in MHz.
xoscFreq = 12 // MHz
)

const (
Expand Down
5 changes: 0 additions & 5 deletions src/machine/board_nano-rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ const (
NINA_SOFT_FLOWCONTROL = false
)

// Onboard crystal oscillator frequency, in MHz.
const (
xoscFreq = 12 // MHz
)

// USB CDC identifiers
// https://github.com/arduino/ArduinoCore-mbed/blob/master/variants/NANO_RP2040_CONNECT/pins_arduino.h
const (
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_pico.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const (

// Onboard LED
LED Pin = GPIO25

// Onboard crystal oscillator frequency, in MHz.
xoscFreq = 12 // MHz
)

// I2C Default pins on Raspberry Pico.
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_pico2.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const (

// Onboard LED
LED Pin = GPIO25

// Onboard crystal oscillator frequency, in MHz.
xoscFreq = 12 // MHz
)

// I2C Default pins on Raspberry Pico.
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_qtpy_rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

package machine

// Onboard crystal oscillator frequency, in MHz.
const xoscFreq = 12 // MHz

// GPIO Pins
const (
SDA = GPIO24
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_thingplus_rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

package machine

// Onboard crystal oscillator frequency, in MHz.
const xoscFreq = 12 // MHz

// GPIO Pins
const (
GP0 Pin = GPIO0 // TX
Expand Down
5 changes: 0 additions & 5 deletions src/machine/board_thumby.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ const (
SPI1_SDI_PIN = NoPin
)

// Onboard crystal oscillator frequency, in MHz.
const (
xoscFreq = 12 // MHz
)

// USB CDC identifiers
const (
usb_STRING_PRODUCT = "Thumby"
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_tiny2350.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ const (
LED_GREEN Pin = GPIO19
LED_BLUE Pin = GPIO20
LED = LED_RED

// Onboard crystal oscillator frequency, in MHz.
xoscFreq = 12 // MHz
)

// I2C Default pins on Tiny2350.
Expand Down
3 changes: 0 additions & 3 deletions src/machine/board_trinkey_qt2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

package machine

// Onboard crystal oscillator frequency, in MHz
const xoscFreq = 12 // MHz

// Onboard LEDs
const (
NEOPIXEL = GPIO27
Expand Down
5 changes: 0 additions & 5 deletions src/machine/board_tufty2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ const (
SPI1_SDI_PIN Pin = NoPin
)

// Onboard crystal oscillator frequency, in MHz.
const (
xoscFreq = 12 // MHz
)

// USB CDC identifiers
const (
usb_STRING_PRODUCT = "Tufty 2040"
Expand Down
5 changes: 0 additions & 5 deletions src/machine/board_waveshare-rp2040-zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ const (
SPI1_SDI_PIN Pin = D12
)

// Onboard crystal oscillator frequency, in MHz.
const (
xoscFreq = 12 // MHz
)

// UART pins
const (
UART0_TX_PIN = GPIO0
Expand Down
5 changes: 0 additions & 5 deletions src/machine/board_xiao-rp2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ const (
SPI1_SDI_PIN Pin = NoPin
)

// Onboard crystal oscillator frequency, in MHz.
const (
xoscFreq = 12 // MHz
)

// UART pins
const (
UART0_TX_PIN = GPIO0
Expand Down
20 changes: 17 additions & 3 deletions src/machine/machine_rp2_2040.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@
fnXIP pinFunc = 0
)

// Clock frequency and PLL configuration
// Note that the system frequency is configured for 125 MHz to allow clk_peri to run at full speed
const (
xoscFreq uint32 = 12

Check failure on line 119 in src/machine/machine_rp2_2040.go

View workflow job for this annotation

GitHub Actions / assert-test-linux

xoscFreq redeclared in this block

Check failure on line 119 in src/machine/machine_rp2_2040.go

View workflow job for this annotation

GitHub Actions / build-macos (macos-14)

xoscFreq redeclared in this block

pllSysFreq uint32 = 125 * MHz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rp2040 datasheet says that its cores are capable of 133 MHz. A cursory glance at the datasheet didn't mention any downside to that speed, so why is TinyGo running at 125 MHz? Perhaps it's because of non-integer periods that you mention in the PR description for rp2350? If so, this PR might consider rp2040 at 133 MHz for the same reasons you chose 150 MHz for rp2350.

Failing all that, a comment would be nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I don't know why it was running at 125 either, but this seems plausible.

pllSysVcoFreq uint32 = 1500 * MHz
pllSysPostDiv1 uint32 = 5
pllSysPostDiv2 uint32 = 2

pllUSBFreq uint32 = 48 * MHz
pllUSBVcoFreq uint32 = 480 * MHz
pllUSBPostDiv1 uint32 = 5
pllUSBPostDiv2 uint32 = 2
)

// Configure configures the gpio pin as per mode.
func (p Pin) Configure(config PinConfig) {
if p == NoPin {
Expand Down Expand Up @@ -184,12 +200,10 @@
crtc := clks.clock(clkRTC)
crtc.configure(0, // No GLMUX
rp.CLOCKS_CLK_RTC_CTRL_AUXSRC_CLKSRC_PLL_USB,
48*MHz,
pllUSBFreq,
46875)
}

func (clks *clocksType) initTicks() {} // No ticks on RP2040

// startTick starts the watchdog tick.
// cycles needs to be a divider that when applied to the xosc input,
// produces a 1MHz clock. So if the xosc frequency is 12MHz,
Expand Down
25 changes: 19 additions & 6 deletions src/machine/machine_rp2_2350.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ const (
fnNULL pinFunc = 0x1f
)

// Clock frequency and PLL configuration
const (
xoscFreq uint32 = 12

pllSysFreq uint32 = 150 * MHz
pllSysVcoFreq uint32 = 1500 * MHz
pllSysPostDiv1 uint32 = 5
pllSysPostDiv2 uint32 = 2

pllUSBFreq uint32 = 48 * MHz
pllUSBVcoFreq uint32 = 480 * MHz
pllUSBPostDiv1 uint32 = 5
pllUSBPostDiv2 uint32 = 2
)

// Configure configures the gpio pin as per mode.
func (p Pin) Configure(config PinConfig) {
if p == NoPin {
Expand Down Expand Up @@ -199,12 +214,6 @@ func irqSet(num uint32, enabled bool) {

func (clks *clocksType) initRTC() {} // No RTC on RP2350.

func (clks *clocksType) initTicks() {
rp.TICKS.SetTIMER0_CTRL_ENABLE(0)
rp.TICKS.SetTIMER0_CYCLES(12)
rp.TICKS.SetTIMER0_CTRL_ENABLE(1)
}

func EnterBootloader() {
enterBootloader()
}
Expand All @@ -213,5 +222,9 @@ func EnterBootloader() {
// On RP2040, the watchdog contained a tick generator used to generate a 1μs tick for the watchdog. This was also
// distributed to the system timer. On RP2350, the watchdog instead takes a tick input from the system-level ticks block. See Section 8.5.
func (wd *watchdogImpl) startTick(cycles uint32) {
rp.TICKS.SetTIMER0_CTRL_ENABLE(0)
rp.TICKS.SetTIMER0_CYCLES(cycles)
rp.TICKS.SetTIMER0_CTRL_ENABLE(1)

rp.TICKS.WATCHDOG_CTRL.SetBits(1)
}
38 changes: 18 additions & 20 deletions src/machine/machine_rp2_clocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

func CPUFrequency() uint32 {
return 125 * MHz
return pllSysFreq
}

// Returns the period of a clock cycle for the raspberry pi pico in nanoseconds.
// Used in PWM API.
func cpuPeriod() uint32 {
return 1e9 / CPUFrequency()
return uint32(1e9) / pllSysFreq // TODO: Discards remainder
}

// clockIndex identifies a hardware clock
Expand Down Expand Up @@ -168,38 +168,38 @@ func (clks *clocksType) init() {
// Configure PLLs
// REF FBDIV VCO POSTDIV
// pllSys: 12 / 1 = 12MHz * 125 = 1500MHZ / 6 / 2 = 125MHz
pllSys.init(1, pllSysVcoFreq, pllSysPostDiv1, pllSysPostDiv2)
// pllUSB: 12 / 1 = 12MHz * 40 = 480 MHz / 5 / 2 = 48MHz
pllSys.init(1, 1500*MHz, 6, 2)
pllUSB.init(1, 480*MHz, 5, 2)
pllUSB.init(1, pllUSBVcoFreq, pllUSBPostDiv1, pllUSBPostDiv2)

// Configure clocks
// clkRef = xosc (12MHz) / 1 = 12MHz
// clkRef = xosc (xoscFreq) / 1 = xoscFreq
cref := clks.clock(clkRef)
cref.configure(rp.CLOCKS_CLK_REF_CTRL_SRC_XOSC_CLKSRC,
0, // No aux mux
12*MHz,
12*MHz)
xoscFreq,
xoscFreq)

// clkSys = pllSys (125MHz) / 1 = 125MHz
// clkSys = pllSys (pllSysFreq) / 1 = pllSysFreq
csys := clks.clock(clkSys)
csys.configure(rp.CLOCKS_CLK_SYS_CTRL_SRC_CLKSRC_CLK_SYS_AUX,
rp.CLOCKS_CLK_SYS_CTRL_AUXSRC_CLKSRC_PLL_SYS,
125*MHz,
125*MHz)
pllSysFreq,
pllSysFreq)

// clkUSB = pllUSB (48MHz) / 1 = 48MHz
// clkUSB = pllUSB (pllUSBFreq) / 1 = 48MHz
cusb := clks.clock(clkUSB)
cusb.configure(0, // No GLMUX
rp.CLOCKS_CLK_USB_CTRL_AUXSRC_CLKSRC_PLL_USB,
48*MHz,
48*MHz)
pllUSBFreq,
pllUSBFreq)

// clkADC = pllUSB (48MHZ) / 1 = 48MHz
// clkADC = pllUSB (pllUSBFreq) / 1 = 48MHz
cadc := clks.clock(clkADC)
cadc.configure(0, // No GLMUX
rp.CLOCKS_CLK_ADC_CTRL_AUXSRC_CLKSRC_PLL_USB,
48*MHz,
48*MHz)
pllUSBFreq,
pllUSBFreq)

clks.initRTC()

Expand All @@ -209,8 +209,6 @@ func (clks *clocksType) init() {
cperi := clks.clock(clkPeri)
cperi.configure(0,
rp.CLOCKS_CLK_PERI_CTRL_AUXSRC_CLK_SYS,
125*MHz,
125*MHz)

clks.initTicks()
pllSysFreq,
pllSysFreq)
}
2 changes: 1 addition & 1 deletion src/machine/machine_rp2_i2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (i2c *I2C) SetBaudRate(br uint32) error {
}

// I2C is synchronous design that runs from clk_sys
freqin := CPUFrequency()
freqin := pllSysFreq

// TODO there are some subtleties to I2C timing which we are completely ignoring here
period := (freqin + br/2) / br
Expand Down
4 changes: 2 additions & 2 deletions src/machine/machine_rp2_spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
}

func (spi SPI) SetBaudRate(br uint32) error {
const freqin uint32 = 125 * MHz
var freqin uint32 = pllSysFreq
const maxBaud uint32 = 66.5 * MHz // max output frequency is 66.5MHz on rp2040. see Note page 527.
// Find smallest prescale value which puts output frequency in range of
// post-divide. Prescale is an even number from 2 to 254 inclusive.
Expand All @@ -130,7 +130,7 @@ func (spi SPI) SetBaudRate(br uint32) error {
}

func (spi SPI) GetBaudRate() uint32 {
const freqin uint32 = 125 * MHz
var freqin uint32 = pllSysFreq
prescale := spi.Bus.SSPCPSR.Get()
postdiv := ((spi.Bus.SSPCR0.Get() & rp.SPI0_SSPCR0_SCR_Msk) >> rp.SPI0_SSPCR0_SCR_Pos) + 1
return freqin / (prescale * postdiv)
Expand Down
Loading
Loading