Skip to content

Commit

Permalink
rp2/modmachine.c: Allow to use the MCU clock for the peripherals.
Browse files Browse the repository at this point in the history
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, use_mcu_clk). If use_mcu_clck
is present and True, the mcu_clock is used for UART and SPI. Note
that UART and SPI baud rates may have to be re-configured after changing
the MCU clock.
The peripheral clock cannot be set to arbitrary values. So it follows
either the USB clock of 48MHz or the MCU clock.

Side change: Allow more than one argument for machine.freq().

Signed-off-by: robert-hh <robert@hammelrath.com>
  • Loading branch information
robert-hh committed Jul 6, 2024
1 parent 1ae441d commit c8e4511
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/rp2/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ The :mod:`machine` module::
import machine

machine.freq() # get the current frequency of the CPU
machine.freq(240000000) # set the CPU frequency to 240 MHz
machine.freq(240000000) # set the CPU frequency to 240 MHz and keep
# the UART frequency at 48MHz
machine.freq(125000000, True) # set the CPU and UART frequency to 125 MHz

The :mod:`rp2` module::

Expand Down
2 changes: 1 addition & 1 deletion extmod/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
return mp_const_none;
}
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq);
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 4, machine_freq);

static mp_obj_t machine_lightsleep(size_t n_args, const mp_obj_t *args) {
mp_machine_lightsleep(n_args, args);
Expand Down
7 changes: 7 additions & 0 deletions ports/rp2/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
if (!set_sys_clock_khz(freq / 1000, false)) {
mp_raise_ValueError(MP_ERROR_TEXT("cannot change frequency"));
}
if (n_args > 1 && mp_obj_get_int(args[1]) != 0) {
clock_configure(clk_peri,
0,
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS,
freq,
freq);
}
#if MICROPY_HW_ENABLE_UART_REPL
setup_default_uart();
mp_uart_init();
Expand Down

0 comments on commit c8e4511

Please sign in to comment.