diff --git a/src/machine/board_ae_rp2040.go b/src/machine/board_ae_rp2040.go index 716cf72b31..ebaed80dbc 100644 --- a/src/machine/board_ae_rp2040.go +++ b/src/machine/board_ae_rp2040.go @@ -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. diff --git a/src/machine/board_badger2040-w.go b/src/machine/board_badger2040-w.go index d0982653b3..00f23dfa52 100644 --- a/src/machine/board_badger2040-w.go +++ b/src/machine/board_badger2040-w.go @@ -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" diff --git a/src/machine/board_badger2040.go b/src/machine/board_badger2040.go index 73f802a909..36fc9c08de 100644 --- a/src/machine/board_badger2040.go +++ b/src/machine/board_badger2040.go @@ -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" diff --git a/src/machine/board_challenger_rp2040.go b/src/machine/board_challenger_rp2040.go index 9a85aa0aef..de0b561980 100644 --- a/src/machine/board_challenger_rp2040.go +++ b/src/machine/board_challenger_rp2040.go @@ -4,9 +4,6 @@ package machine const ( LED = GPIO24 - - // Onboard crystal oscillator frequency, in MHz. - xoscFreq = 12 // MHz ) // GPIO Pins diff --git a/src/machine/board_feather_rp2040.go b/src/machine/board_feather_rp2040.go index 44091e56e9..813741c7d5 100644 --- a/src/machine/board_feather_rp2040.go +++ b/src/machine/board_feather_rp2040.go @@ -2,9 +2,6 @@ package machine -// Onboard crystal oscillator frequency, in MHz. -const xoscFreq = 12 // MHz - // GPIO Pins const ( D4 = GPIO6 diff --git a/src/machine/board_gopher-badge.go b/src/machine/board_gopher-badge.go index 7af27118b2..f62235c169 100644 --- a/src/machine/board_gopher-badge.go +++ b/src/machine/board_gopher-badge.go @@ -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" diff --git a/src/machine/board_kb2040.go b/src/machine/board_kb2040.go index 1a6f353623..be59277643 100644 --- a/src/machine/board_kb2040.go +++ b/src/machine/board_kb2040.go @@ -2,9 +2,6 @@ package machine -// Onboard crystal oscillator frequency, in MHz. -const xoscFreq = 12 // MHz - // GPIO Pins const ( D0 = GPIO0 diff --git a/src/machine/board_macropad-rp2040.go b/src/machine/board_macropad-rp2040.go index 78bd2b749e..8e8b11be42 100644 --- a/src/machine/board_macropad-rp2040.go +++ b/src/machine/board_macropad-rp2040.go @@ -4,9 +4,6 @@ package machine const ( NeopixelCount = 12 - - // Onboard crystal oscillator frequency, in MHz. - xoscFreq = 12 // MHz ) const ( diff --git a/src/machine/board_nano-rp2040.go b/src/machine/board_nano-rp2040.go index 8155523134..49876af1f6 100644 --- a/src/machine/board_nano-rp2040.go +++ b/src/machine/board_nano-rp2040.go @@ -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 ( diff --git a/src/machine/board_pico.go b/src/machine/board_pico.go index efbd6ef7dc..c6d22c6532 100644 --- a/src/machine/board_pico.go +++ b/src/machine/board_pico.go @@ -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. diff --git a/src/machine/board_pico2.go b/src/machine/board_pico2.go index 327c542fbc..2d4c73dec3 100644 --- a/src/machine/board_pico2.go +++ b/src/machine/board_pico2.go @@ -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. diff --git a/src/machine/board_qtpy_rp2040.go b/src/machine/board_qtpy_rp2040.go index 3eabf0c9b6..efa50c6335 100644 --- a/src/machine/board_qtpy_rp2040.go +++ b/src/machine/board_qtpy_rp2040.go @@ -2,9 +2,6 @@ package machine -// Onboard crystal oscillator frequency, in MHz. -const xoscFreq = 12 // MHz - // GPIO Pins const ( SDA = GPIO24 diff --git a/src/machine/board_thingplus_rp2040.go b/src/machine/board_thingplus_rp2040.go index 48292d261e..07ad991312 100644 --- a/src/machine/board_thingplus_rp2040.go +++ b/src/machine/board_thingplus_rp2040.go @@ -2,9 +2,6 @@ package machine -// Onboard crystal oscillator frequency, in MHz. -const xoscFreq = 12 // MHz - // GPIO Pins const ( GP0 Pin = GPIO0 // TX diff --git a/src/machine/board_thumby.go b/src/machine/board_thumby.go index f89a8b7059..080a3a1ef7 100644 --- a/src/machine/board_thumby.go +++ b/src/machine/board_thumby.go @@ -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" diff --git a/src/machine/board_tiny2350.go b/src/machine/board_tiny2350.go index f04fa061b6..32da28b6a8 100644 --- a/src/machine/board_tiny2350.go +++ b/src/machine/board_tiny2350.go @@ -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. diff --git a/src/machine/board_trinkey_qt2040.go b/src/machine/board_trinkey_qt2040.go index 1a49c6d927..4da294400b 100644 --- a/src/machine/board_trinkey_qt2040.go +++ b/src/machine/board_trinkey_qt2040.go @@ -13,9 +13,6 @@ package machine -// Onboard crystal oscillator frequency, in MHz -const xoscFreq = 12 // MHz - // Onboard LEDs const ( NEOPIXEL = GPIO27 diff --git a/src/machine/board_tufty2040.go b/src/machine/board_tufty2040.go index 57d244f28b..62b8052661 100644 --- a/src/machine/board_tufty2040.go +++ b/src/machine/board_tufty2040.go @@ -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" diff --git a/src/machine/board_waveshare-rp2040-zero.go b/src/machine/board_waveshare-rp2040-zero.go index 00ddc53a51..a861a2f212 100644 --- a/src/machine/board_waveshare-rp2040-zero.go +++ b/src/machine/board_waveshare-rp2040-zero.go @@ -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 diff --git a/src/machine/board_xiao-rp2040.go b/src/machine/board_xiao-rp2040.go index b010314557..83689588d4 100644 --- a/src/machine/board_xiao-rp2040.go +++ b/src/machine/board_xiao-rp2040.go @@ -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 diff --git a/src/machine/machine_rp2_2040.go b/src/machine/machine_rp2_2040.go index 484d8e923a..e44c3f2721 100644 --- a/src/machine/machine_rp2_2040.go +++ b/src/machine/machine_rp2_2040.go @@ -113,6 +113,22 @@ const ( 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 + + pllSysFreq uint32 = 125 * 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 { @@ -184,12 +200,10 @@ func (clks *clocksType) initRTC() { 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, diff --git a/src/machine/machine_rp2_2350.go b/src/machine/machine_rp2_2350.go index 4e12bebe35..78c66af9e4 100644 --- a/src/machine/machine_rp2_2350.go +++ b/src/machine/machine_rp2_2350.go @@ -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 { @@ -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() } @@ -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) } diff --git a/src/machine/machine_rp2_clocks.go b/src/machine/machine_rp2_clocks.go index cc152a7f82..6c952e73f4 100644 --- a/src/machine/machine_rp2_clocks.go +++ b/src/machine/machine_rp2_clocks.go @@ -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 @@ -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() @@ -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) } diff --git a/src/machine/machine_rp2_i2c.go b/src/machine/machine_rp2_i2c.go index 2552eb94e6..cf267eac67 100644 --- a/src/machine/machine_rp2_i2c.go +++ b/src/machine/machine_rp2_i2c.go @@ -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 diff --git a/src/machine/machine_rp2_spi.go b/src/machine/machine_rp2_spi.go index faab9839af..5eecc85a2d 100644 --- a/src/machine/machine_rp2_spi.go +++ b/src/machine/machine_rp2_spi.go @@ -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. @@ -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) diff --git a/src/machine/machine_rp2_uart.go b/src/machine/machine_rp2_uart.go index c984d41424..93d8d8924f 100644 --- a/src/machine/machine_rp2_uart.go +++ b/src/machine/machine_rp2_uart.go @@ -75,7 +75,7 @@ func (uart *UART) Configure(config UARTConfig) error { // SetBaudRate sets the baudrate to be used for the UART. func (uart *UART) SetBaudRate(br uint32) { - div := 8 * 125 * MHz / br + div := 8 * pllSysFreq / br ibrd := div >> 7 var fbrd uint32