From c8e451113231d0642d3621ba577ad6cc435fb139 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 22 Feb 2024 09:03:06 +0100 Subject: [PATCH] rp2/modmachine.c: Allow to use the MCU clock for the peripherals. 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 --- docs/rp2/quickref.rst | 4 +++- extmod/modmachine.c | 2 +- ports/rp2/modmachine.c | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/rp2/quickref.rst b/docs/rp2/quickref.rst index ecf3f31861c22..0c2989d0d850e 100644 --- a/docs/rp2/quickref.rst +++ b/docs/rp2/quickref.rst @@ -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:: diff --git a/extmod/modmachine.c b/extmod/modmachine.c index 2a7e315bbb0ce..960d3cae956d6 100644 --- a/extmod/modmachine.c +++ b/extmod/modmachine.c @@ -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); diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index 2cc79369f6149..073cb6b30dc83 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -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();