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.
This commit is contained in:
robert-hh 2021-02-20 11:11:24 +01:00 committed by Damien George
parent 0461640983
commit 11cf742524
1 changed files with 12 additions and 3 deletions

View File

@ -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));