Skip to content

Commit ca209fd

Browse files
Gadgetoiddpgeorge
authored andcommitted
rp2/clocks_extra: Sync clock init with pico-sdk 2.3.0.
Bring the port's runtime_init_clocks() in line with the pico-sdk 2.3.0 version, since it was originally derived from this function. PLL_COMMON_REFDIV is deprecated in the SDK in favour of the separate PLL_SYS_REFDIV and PLL_USB_REFDIV macros; use those so a board that only sets the new per-PLL values is honoured, and so the port keeps building if the deprecated macro is eventually removed. Configure the divide-by-1 clocks (ref, sys, usb, adc, peri, hstx) with clock_configure_undivided(), and clk_rtc with clock_configure_int_divider(), as the SDK now does. Both avoid the 64-bit division that clock_configure() pulls in to compute a fractional divider. Drop the --wrap=runtime_init_clocks linker flag: the SDK declares runtime_init_clocks() as __weak, so a plain strong definition in the port overrides it, which makes the wrap redundant. Also switch the HSTX guard to HAS_HSTX and take RTC_CLOCK_FREQ_HZ from hardware/rtc.h, matching upstream. No functional change to the clock configuration on existing boards. Signed-off-by: Phil Howard <github@gadgetoid.com>
1 parent ba4415d commit ca209fd

2 files changed

Lines changed: 31 additions & 32 deletions

File tree

ports/rp2/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,6 @@ target_compile_options(${MICROPY_TARGET} PRIVATE
536536
537537
target_link_options(${MICROPY_TARGET} PRIVATE
538538
-Wl,--defsym=__micropy_c_heap_size__=${MICROPY_C_HEAP_SIZE}
539-
-Wl,--wrap=runtime_init_clocks
540539
-Wl,--print-memory-usage
541540
-Wl,--cref
542541
)

ports/rp2/clocks_extra.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66
#include "pico.h"
7+
#include "pico/runtime_init.h"
78
#include "clocks_extra.h"
89
#include "hardware/regs/clocks.h"
910
#include "hardware/platform_defs.h"
@@ -15,13 +16,8 @@
1516
#include "hardware/gpio.h"
1617
#include "hardware/ticks.h"
1718
#include "hardware/vreg.h"
18-
19-
#if PICO_RP2040
20-
// The RTC clock frequency is 48MHz divided by power of 2 (to ensure an integer
21-
// division ratio will be used in the clocks block). A divisor of 1024 generates
22-
// an RTC clock tick of 46875Hz. This frequency is relatively close to the
23-
// customary 32 or 32.768kHz 'slow clock' crystals and provides good timing resolution.
24-
#define RTC_CLOCK_FREQ_HZ (USB_CLK_KHZ * KHZ / 1024)
19+
#if HAS_RP2040_RTC
20+
#include "hardware/rtc.h"
2521
#endif
2622

2723
static void start_all_ticks(void) {
@@ -36,8 +32,8 @@ static void start_all_ticks(void) {
3632
}
3733
}
3834

39-
// Wrap the SDK's clocks_init() function to save code size
40-
void __wrap_runtime_init_clocks(void) {
35+
// Override the SDK's __weak runtime_init_clocks() with the USB-always variant.
36+
void runtime_init_clocks(void) {
4137
runtime_init_clocks_optional_usb(true);
4238
}
4339

@@ -61,15 +57,14 @@ void runtime_init_clocks_optional_usb(bool init_usb) {
6157
}
6258

6359
/// \tag::pll_init[]
64-
pll_init(pll_sys, PLL_COMMON_REFDIV, PLL_SYS_VCO_FREQ_HZ, PLL_SYS_POSTDIV1, PLL_SYS_POSTDIV2);
60+
pll_init(pll_sys, PLL_SYS_REFDIV, PLL_SYS_VCO_FREQ_HZ, PLL_SYS_POSTDIV1, PLL_SYS_POSTDIV2);
6561
if (init_usb) {
66-
pll_init(pll_usb, PLL_COMMON_REFDIV, PLL_USB_VCO_FREQ_HZ, PLL_USB_POSTDIV1, PLL_USB_POSTDIV2);
62+
pll_init(pll_usb, PLL_USB_REFDIV, PLL_USB_VCO_FREQ_HZ, PLL_USB_POSTDIV1, PLL_USB_POSTDIV2);
6763
}
6864
/// \end::pll_init[]
6965

7066
// Configure clocks
7167

72-
// todo amy, what is this N1,2,4 meant to mean?
7368
// RP2040 CLK_REF = XOSC (usually) 12MHz / 1 = 12MHz
7469
// RP2350 CLK_REF = XOSC (XOSC_MHZ) / N (1,2,4) = 12MHz
7570

@@ -81,67 +76,72 @@ void runtime_init_clocks_optional_usb(bool init_usb) {
8176
// non-aux inputs to the glitchless mux, so the aux select doesn't
8277
// matter. The value of 0 here happens to be the sys PLL.
8378

84-
clock_configure(clk_ref,
79+
clock_configure_undivided(clk_ref,
8580
CLOCKS_CLK_REF_CTRL_SRC_VALUE_XOSC_CLKSRC,
8681
0, // No aux mux
87-
XOSC_HZ,
8882
XOSC_HZ);
8983

84+
// This must be done after we've configured CLK_REF to XOSC due to the need to time a delay
9085
#if SYS_CLK_VREG_VOLTAGE_AUTO_ADJUST && defined(SYS_CLK_VREG_VOLTAGE_MIN)
9186
if (vreg_get_voltage() < SYS_CLK_VREG_VOLTAGE_MIN) {
9287
vreg_set_voltage(SYS_CLK_VREG_VOLTAGE_MIN);
88+
// wait for voltage to settle; must use CPU cycles as TIMER is not yet clocked correctly
9389
busy_wait_at_least_cycles((uint32_t)((SYS_CLK_VREG_VOLTAGE_AUTO_ADJUST_DELAY_US * (uint64_t)XOSC_HZ) / 1000000));
9490
}
9591
#endif
9692

9793
/// \tag::configure_clk_sys[]
9894
// CLK SYS = PLL SYS (usually) 125MHz / 1 = 125MHz
99-
clock_configure(clk_sys,
95+
clock_configure_undivided(clk_sys,
10096
CLOCKS_CLK_SYS_CTRL_SRC_VALUE_CLKSRC_CLK_SYS_AUX,
10197
CLOCKS_CLK_SYS_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS,
102-
SYS_CLK_KHZ * KHZ,
103-
SYS_CLK_KHZ * KHZ);
98+
SYS_CLK_HZ);
10499
/// \end::configure_clk_sys[]
105100

106101
if (init_usb) {
107102
// CLK USB = PLL USB 48MHz / 1 = 48MHz
108-
clock_configure(clk_usb,
103+
clock_configure_undivided(clk_usb,
109104
0, // No GLMUX
110105
CLOCKS_CLK_USB_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
111-
USB_CLK_KHZ * KHZ,
112-
USB_CLK_KHZ * KHZ);
106+
USB_CLK_HZ);
113107
}
114108

115109
// CLK ADC = PLL USB 48MHZ / 1 = 48MHz
116-
clock_configure(clk_adc,
110+
clock_configure_undivided(clk_adc,
117111
0, // No GLMUX
118112
CLOCKS_CLK_ADC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
119-
USB_CLK_KHZ * KHZ,
120-
USB_CLK_KHZ * KHZ);
113+
USB_CLK_HZ);
121114

122115
#if HAS_RP2040_RTC
123116
// CLK RTC = PLL USB 48MHz / 1024 = 46875Hz
117+
#if (USB_CLK_HZ % RTC_CLOCK_FREQ_HZ == 0)
118+
// this doesn't pull in 64 bit arithmetic
119+
clock_configure_int_divider(clk_rtc,
120+
0, // No GLMUX
121+
CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
122+
USB_CLK_HZ,
123+
USB_CLK_HZ / RTC_CLOCK_FREQ_HZ);
124+
#else
124125
clock_configure(clk_rtc,
125126
0, // No GLMUX
126127
CLOCKS_CLK_RTC_CTRL_AUXSRC_VALUE_CLKSRC_PLL_USB,
127-
USB_CLK_KHZ * KHZ,
128+
USB_CLK_HZ,
128129
RTC_CLOCK_FREQ_HZ);
129130
#endif
131+
#endif
130132

131133
// CLK PERI = clk_sys. Used as reference clock for UART and SPI serial.
132-
clock_configure(clk_peri,
134+
clock_configure_undivided(clk_peri,
133135
0,
134136
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLK_SYS,
135-
SYS_CLK_KHZ * KHZ,
136-
SYS_CLK_KHZ * KHZ);
137+
SYS_CLK_HZ);
137138

138-
#if PICO_RP2350
139+
#if HAS_HSTX
139140
// CLK_HSTX = clk_sys. Transmit bit clock for the HSTX peripheral.
140-
clock_configure(clk_hstx,
141+
clock_configure_undivided(clk_hstx,
141142
0,
142143
CLOCKS_CLK_HSTX_CTRL_AUXSRC_VALUE_CLK_SYS,
143-
SYS_CLK_KHZ * KHZ,
144-
SYS_CLK_KHZ * KHZ);
144+
SYS_CLK_HZ);
145145
#endif
146146

147147
// Finally, all clocks are configured so start the ticks

0 commit comments

Comments
 (0)