Skip to content

Commit 9b2a6a9

Browse files
committed
rp2/modmachine.c: Set the peripheral frequency with machine.freq().
By default, the peripheral clock for UART and SPI is set to 48 MHz and will not be affected by the MCU clock change. This can be changed by a second argument to machine.freq(freq, peripheral_freq). The second argument must be either 48 MHz or identical with the first argument. Note that UART and SPI baud rates may have to be re-configured after changing the MCU clock. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent a355e5c commit 9b2a6a9

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

docs/rp2/quickref.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,25 @@ The MicroPython REPL is accessed via the USB serial port. Tab-completion is usef
3131
find out what methods an object has. Paste mode (ctrl-E) is useful to paste a
3232
large slab of Python code into the REPL.
3333

34-
The :mod:`machine` module::
34+
The :mod:`machine` module:
35+
36+
machine.freq() allows to change the MCU frequency and control the peripheral
37+
frequency for UART and SPI. Usage::
38+
39+
machine.freq(MCU_frequency[, peripheral_frequency=48_000_000])
40+
41+
The MCU frequency can be set in a range from less than 48 MHz to about 250MHz.
42+
The default at boot time is 125 MHz. The peripheral frequency must be either
43+
48 MHz or identical to the MCU frequency, with 48 MHz as the default.
44+
If the peripheral frequency is changed, any already existing instance of
45+
UART and SPI will change it's baud rate and may have to be re-configured::
3546

3647
import machine
3748

3849
machine.freq() # get the current frequency of the CPU
39-
machine.freq(240000000) # set the CPU frequency to 240 MHz
50+
machine.freq(240000000) # set the CPU frequency to 240 MHz and keep
51+
# the UART frequency at 48MHz
52+
machine.freq(125000000, 125000000) # set the CPU and UART frequency to 125 MHz
4053

4154
The :mod:`rp2` module::
4255

ports/rp2/modmachine.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
9696
if (!set_sys_clock_khz(freq / 1000, false)) {
9797
mp_raise_ValueError(MP_ERROR_TEXT("cannot change frequency"));
9898
}
99+
if (n_args > 1) {
100+
mp_int_t freq_peri = mp_obj_get_int(args[1]);
101+
if (freq_peri != (USB_CLK_KHZ * KHZ)) {
102+
if (freq_peri == freq) {
103+
clock_configure(clk_peri,
104+
0,
105+
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS,
106+
freq,
107+
freq);
108+
} else {
109+
mp_raise_ValueError(MP_ERROR_TEXT("peripheral freq must be 48_000_000 or the same as the MCU freq"));
110+
}
111+
}
112+
}
99113
#if MICROPY_HW_ENABLE_UART_REPL
100114
setup_default_uart();
101115
mp_uart_init();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy