diff options
author | robert-hh <robert@hammelrath.com> | 2021-02-20 11:11:24 +0100 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2021-03-12 00:48:46 +1100 |
commit | 11cf742524202fa5fc062f3e6e3040a82f49b190 (patch) | |
tree | d529f7884731f69277ddbba39da64974041f9273 | |
parent | 046164098337cc79dd0f6c710f49bdf6765aa711 (diff) |
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.
-rw-r--r-- | ports/rp2/modmachine.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index dbaafabe8..139309281 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)); |