From 11cf742524202fa5fc062f3e6e3040a82f49b190 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sat, 20 Feb 2021 11:11:24 +0100 Subject: [PATCH] rp2/modmachine: Allow changing CPU clock frequency. Using the standard machine.freq(). The safe ranges tested were 10 and 12-270MHz, at which USB REPL still worked. Requested settings can be checked with the script: pico-sdk/src/rp2_common/hardware_clocks/scripts/vcocalc.py. At frequencies like 300MHz the script still signaled OK, but USB did not work any more. --- ports/rp2/modmachine.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index dbaafabe86..1393092818 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -36,6 +36,7 @@ #include "hardware/clocks.h" #include "hardware/watchdog.h" #include "pico/bootrom.h" +#include "pico/stdlib.h" #include "pico/unique_id.h" #define RP2_RESET_PWRON (1) @@ -80,10 +81,18 @@ STATIC mp_obj_t machine_bootloader(void) { } MP_DEFINE_CONST_FUN_OBJ_0(machine_bootloader_obj, machine_bootloader); -STATIC mp_obj_t machine_freq(void) { - return MP_OBJ_NEW_SMALL_INT(clock_get_hz(clk_sys)); +STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + return MP_OBJ_NEW_SMALL_INT(clock_get_hz(clk_sys)); + } else { + mp_int_t freq = mp_obj_get_int(args[0]); + if (!set_sys_clock_khz(freq / 1000, false)) { + mp_raise_ValueError(MP_ERROR_TEXT("cannot change frequency")); + } + return mp_const_none; + } } -MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 1, machine_freq); STATIC mp_obj_t machine_idle(void) { best_effort_wfe_or_timeout(make_timeout_time_ms(1));