Skip to content

Commit 2a56ecd

Browse files
committed
rp2: switch to constants for clock definitions
This moves the clock definitions into the 2040 and 2350 machine files, and reverts them to constants. This also addresses tinygo-org#4673
1 parent 2ca264d commit 2a56ecd

27 files changed

+50
-120
lines changed

src/machine/board_ae_rp2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ const (
3333
GP29 Pin = GPIO29
3434
)
3535

36-
// System clock configuration
37-
var clockCfg = defaultSystemClockCfg
38-
3936
// I2C Default pins on Raspberry Pico.
4037
const (
4138
I2C0_SDA_PIN = GP4

src/machine/board_badger2040-w.go

-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ SPI0_CS_PIN Pin = QSPI_CS
6868
*/
6969
)
7070

71-
// System clock configuration
72-
var clockCfg = defaultSystemClockCfg
73-
7471
// USB CDC identifiers
7572
const (
7673
usb_STRING_PRODUCT = "Badger 2040 W"

src/machine/board_badger2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ SPI0_CS_PIN Pin = QSPI_CS
6767
*/
6868
)
6969

70-
// System clock configuration
71-
var clockCfg = defaultSystemClockCfg
72-
7370
// USB CDC identifiers
7471
const (
7572
usb_STRING_PRODUCT = "Badger 2040"

src/machine/board_challenger_rp2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const (
66
LED = GPIO24
77
)
88

9-
// System clock configuration
10-
var clockCfg = defaultSystemClockCfg
11-
129
// GPIO Pins
1310
const (
1411
D5 = GPIO2

src/machine/board_feather_rp2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
package machine
44

5-
// System clock configuration
6-
var clockCfg = defaultSystemClockCfg
7-
85
// GPIO Pins
96
const (
107
D4 = GPIO6

src/machine/board_gopher-badge.go

-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ const (
6161
SPI1_SDI_PIN Pin = NoPin
6262
)
6363

64-
// System clock configuration
65-
var clockCfg = defaultSystemClockCfg
66-
6764
// USB CDC identifiers
6865
const (
6966
usb_STRING_PRODUCT = "Gopher Badge"

src/machine/board_kb2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
package machine
44

5-
// System clock configuration
6-
var clockCfg = defaultSystemClockCfg
7-
85
// GPIO Pins
96
const (
107
D0 = GPIO0

src/machine/board_macropad-rp2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const (
66
NeopixelCount = 12
77
)
88

9-
// System clock configuration
10-
var clockCfg = defaultSystemClockCfg
11-
129
const (
1310
SWITCH = GPIO0
1411
BUTTON = GPIO0

src/machine/board_nano-rp2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ const (
9595
NINA_SOFT_FLOWCONTROL = false
9696
)
9797

98-
// System clock configuration
99-
var clockCfg = defaultSystemClockCfg
100-
10198
// USB CDC identifiers
10299
// https://github.com/arduino/ArduinoCore-mbed/blob/master/variants/NANO_RP2040_CONNECT/pins_arduino.h
103100
const (

src/machine/board_pico.go

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ const (
3535
LED Pin = GPIO25
3636
)
3737

38-
// System clock configuration
39-
var clockCfg = defaultSystemClockCfg
40-
4138
// I2C Default pins on Raspberry Pico.
4239
const (
4340
I2C0_SDA_PIN = GP4

src/machine/board_pico2.go

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ const (
3535
LED Pin = GPIO25
3636
)
3737

38-
// System clock configuration
39-
var clockCfg = defaultSystemClockCfg
40-
4138
// I2C Default pins on Raspberry Pico.
4239
const (
4340
I2C0_SDA_PIN = GP4

src/machine/board_qtpy_rp2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
package machine
44

5-
// System clock configuration
6-
var clockCfg = defaultSystemClockCfg
7-
85
// GPIO Pins
96
const (
107
SDA = GPIO24

src/machine/board_thingplus_rp2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
package machine
44

5-
// System clock configuration
6-
var clockCfg = defaultSystemClockCfg
7-
85
// GPIO Pins
96
const (
107
GP0 Pin = GPIO0 // TX

src/machine/board_thumby.go

-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ const (
4949
SPI1_SDI_PIN = NoPin
5050
)
5151

52-
// System clock configuration
53-
var clockCfg = defaultSystemClockCfg
54-
5552
// USB CDC identifiers
5653
const (
5754
usb_STRING_PRODUCT = "Thumby"

src/machine/board_tiny2350.go

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ const (
2929
LED = LED_RED
3030
)
3131

32-
// System clock configuration
33-
var clockCfg = defaultSystemClockCfg
34-
3532
// I2C Default pins on Tiny2350.
3633
const (
3734
I2C0_SDA_PIN = GP12

src/machine/board_trinkey_qt2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
package machine
1515

16-
// System clock configuration
17-
var clockCfg = defaultSystemClockCfg
18-
1916
// Onboard LEDs
2017
const (
2118
NEOPIXEL = GPIO27

src/machine/board_tufty2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ const (
5858
SPI1_SDI_PIN Pin = NoPin
5959
)
6060

61-
// System clock configuration
62-
var clockCfg = defaultSystemClockCfg
63-
6461
// USB CDC identifiers
6562
const (
6663
usb_STRING_PRODUCT = "Tufty 2040"

src/machine/board_waveshare-rp2040-zero.go

-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ const (
7575
SPI1_SDI_PIN Pin = D12
7676
)
7777

78-
// System clock configuration
79-
var clockCfg = defaultSystemClockCfg
80-
8178
// UART pins
8279
const (
8380
UART0_TX_PIN = GPIO0

src/machine/board_xiao-rp2040.go

-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ const (
6363
SPI1_SDI_PIN Pin = NoPin
6464
)
6565

66-
// System clock configuration
67-
var clockCfg = defaultSystemClockCfg
68-
6966
// UART pins
7067
const (
7168
UART0_TX_PIN = GPIO0

src/machine/machine_rp2_2040.go

+15-14
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,20 @@ const (
114114
)
115115

116116
// Clock frequency and PLL configuration
117-
var defaultSystemClockCfg = systemClockCfg{
118-
xoscFreq: 12,
119-
120-
pllSysFreq: 125 * MHz,
121-
pllSysVcoFreq: 1500 * MHz,
122-
pllSysPostDiv1: 6,
123-
pllSysPostDiv2: 2,
124-
125-
pllUSBFreq: 48 * MHz,
126-
pllUSBVcoFreq: 480 * MHz,
127-
pllUSBPostDiv1: 5,
128-
pllUSBPostDiv2: 2,
129-
}
117+
// Note that the system frequency is configured for 125 MHz to allow clk_peri to run at full speed
118+
const (
119+
xoscFreq uint32 = 12
120+
121+
pllSysFreq uint32 = 125 * MHz
122+
pllSysVcoFreq uint32 = 1500 * MHz
123+
pllSysPostDiv1 uint32 = 5
124+
pllSysPostDiv2 uint32 = 2
125+
126+
pllUSBFreq uint32 = 48 * MHz
127+
pllUSBVcoFreq uint32 = 480 * MHz
128+
pllUSBPostDiv1 uint32 = 5
129+
pllUSBPostDiv2 uint32 = 2
130+
)
130131

131132
// Configure configures the gpio pin as per mode.
132133
func (p Pin) Configure(config PinConfig) {
@@ -199,7 +200,7 @@ func (clks *clocksType) initRTC() {
199200
crtc := clks.clock(clkRTC)
200201
crtc.configure(0, // No GLMUX
201202
rp.CLOCKS_CLK_RTC_CTRL_AUXSRC_CLKSRC_PLL_USB,
202-
clockCfg.pllUSBFreq,
203+
pllUSBFreq,
203204
46875)
204205
}
205206

src/machine/machine_rp2_2350.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ const (
121121
)
122122

123123
// Clock frequency and PLL configuration
124-
var defaultSystemClockCfg = systemClockCfg{
125-
xoscFreq: 12,
126-
127-
pllSysFreq: 150 * MHz,
128-
pllSysVcoFreq: 1500 * MHz,
129-
pllSysPostDiv1: 5,
130-
pllSysPostDiv2: 2,
131-
132-
pllUSBFreq: 48 * MHz,
133-
pllUSBVcoFreq: 480 * MHz,
134-
pllUSBPostDiv1: 5,
135-
pllUSBPostDiv2: 2,
136-
}
124+
const (
125+
xoscFreq uint32 = 12
126+
127+
pllSysFreq uint32 = 150 * MHz
128+
pllSysVcoFreq uint32 = 1500 * MHz
129+
pllSysPostDiv1 uint32 = 5
130+
pllSysPostDiv2 uint32 = 2
131+
132+
pllUSBFreq uint32 = 48 * MHz
133+
pllUSBVcoFreq uint32 = 480 * MHz
134+
pllUSBPostDiv1 uint32 = 5
135+
pllUSBPostDiv2 uint32 = 2
136+
)
137137

138138
// Configure configures the gpio pin as per mode.
139139
func (p Pin) Configure(config PinConfig) {

src/machine/machine_rp2_clocks.go

+15-29
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import (
1010
)
1111

1212
func CPUFrequency() uint32 {
13-
return clockCfg.pllSysFreq
13+
return pllSysFreq
1414
}
1515

1616
// Returns the period of a clock cycle for the raspberry pi pico in nanoseconds.
1717
// Used in PWM API.
1818
func cpuPeriod() uint32 {
19-
return uint32(1e9) / clockCfg.pllSysFreq // TODO: Discards remainder
19+
return uint32(1e9) / pllSysFreq // TODO: Discards remainder
2020
}
2121

2222
// clockIndex identifies a hardware clock
@@ -143,26 +143,12 @@ func (clk *clock) configure(src, auxsrc, srcFreq, freq uint32) {
143143

144144
}
145145

146-
type systemClockCfg struct {
147-
xoscFreq uint32
148-
149-
pllSysFreq uint32
150-
pllSysVcoFreq uint32
151-
pllSysPostDiv1 uint32
152-
pllSysPostDiv2 uint32
153-
154-
pllUSBFreq uint32
155-
pllUSBVcoFreq uint32
156-
pllUSBPostDiv1 uint32
157-
pllUSBPostDiv2 uint32
158-
}
159-
160146
// init initializes the clock hardware.
161147
//
162148
// Must be called before any other clock function.
163149
func (clks *clocksType) init() {
164150
// Start the watchdog tick
165-
Watchdog.startTick(clockCfg.xoscFreq)
151+
Watchdog.startTick(xoscFreq)
166152

167153
// Disable resus that may be enabled from previous software
168154
rp.CLOCKS.SetCLK_SYS_RESUS_CTRL_CLEAR(0)
@@ -182,38 +168,38 @@ func (clks *clocksType) init() {
182168
// Configure PLLs
183169
// REF FBDIV VCO POSTDIV
184170
// pllSys: 12 / 1 = 12MHz * 125 = 1500MHZ / 6 / 2 = 125MHz
185-
pllSys.init(1, clockCfg.pllSysVcoFreq, clockCfg.pllSysPostDiv1, clockCfg.pllSysPostDiv2)
171+
pllSys.init(1, pllSysVcoFreq, pllSysPostDiv1, pllSysPostDiv2)
186172
// pllUSB: 12 / 1 = 12MHz * 40 = 480 MHz / 5 / 2 = 48MHz
187-
pllUSB.init(1, clockCfg.pllUSBVcoFreq, clockCfg.pllUSBPostDiv1, clockCfg.pllUSBPostDiv2)
173+
pllUSB.init(1, pllUSBVcoFreq, pllUSBPostDiv1, pllUSBPostDiv2)
188174

189175
// Configure clocks
190176
// clkRef = xosc (xoscFreq) / 1 = xoscFreq
191177
cref := clks.clock(clkRef)
192178
cref.configure(rp.CLOCKS_CLK_REF_CTRL_SRC_XOSC_CLKSRC,
193179
0, // No aux mux
194-
clockCfg.xoscFreq,
195-
clockCfg.xoscFreq)
180+
xoscFreq,
181+
xoscFreq)
196182

197183
// clkSys = pllSys (pllSysFreq) / 1 = pllSysFreq
198184
csys := clks.clock(clkSys)
199185
csys.configure(rp.CLOCKS_CLK_SYS_CTRL_SRC_CLKSRC_CLK_SYS_AUX,
200186
rp.CLOCKS_CLK_SYS_CTRL_AUXSRC_CLKSRC_PLL_SYS,
201-
clockCfg.pllSysFreq,
202-
clockCfg.pllSysFreq)
187+
pllSysFreq,
188+
pllSysFreq)
203189

204190
// clkUSB = pllUSB (pllUSBFreq) / 1 = 48MHz
205191
cusb := clks.clock(clkUSB)
206192
cusb.configure(0, // No GLMUX
207193
rp.CLOCKS_CLK_USB_CTRL_AUXSRC_CLKSRC_PLL_USB,
208-
clockCfg.pllUSBFreq,
209-
clockCfg.pllUSBFreq)
194+
pllUSBFreq,
195+
pllUSBFreq)
210196

211197
// clkADC = pllUSB (pllUSBFreq) / 1 = 48MHz
212198
cadc := clks.clock(clkADC)
213199
cadc.configure(0, // No GLMUX
214200
rp.CLOCKS_CLK_ADC_CTRL_AUXSRC_CLKSRC_PLL_USB,
215-
clockCfg.pllUSBFreq,
216-
clockCfg.pllUSBFreq)
201+
pllUSBFreq,
202+
pllUSBFreq)
217203

218204
clks.initRTC()
219205

@@ -223,6 +209,6 @@ func (clks *clocksType) init() {
223209
cperi := clks.clock(clkPeri)
224210
cperi.configure(0,
225211
rp.CLOCKS_CLK_PERI_CTRL_AUXSRC_CLK_SYS,
226-
clockCfg.pllSysFreq,
227-
clockCfg.pllSysFreq)
212+
pllSysFreq,
213+
pllSysFreq)
228214
}

src/machine/machine_rp2_i2c.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (i2c *I2C) SetBaudRate(br uint32) error {
162162
}
163163

164164
// I2C is synchronous design that runs from clk_sys
165-
freqin := clockCfg.pllSysFreq
165+
freqin := pllSysFreq
166166

167167
// TODO there are some subtleties to I2C timing which we are completely ignoring here
168168
period := (freqin + br/2) / br

src/machine/machine_rp2_pll.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var (
3030
//
3131
// Post Divider 2, postDiv2 with range 1-7.
3232
func (pll *pll) init(refdiv, vcoFreq, postDiv1, postDiv2 uint32) {
33-
refFreq := clockCfg.xoscFreq / refdiv
33+
refFreq := xoscFreq / refdiv
3434

3535
// What are we multiplying the reference clock by to get the vco freq
3636
// (The regs are called div, because you divide the vco output and compare it to the refclk)

0 commit comments

Comments
 (0)