From e2ab7a4751e867e7981425f60bf2ef54c6bae355 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 19 Apr 2023 17:04:54 -0700 Subject: [PATCH] Change voltage. Refine docs --- .../common-hal/microcontroller/Processor.c | 1 + ports/raspberrypi/Makefile | 2 ++ .../common-hal/microcontroller/Processor.c | 19 ++++++++++++++++++- shared-bindings/microcontroller/Processor.c | 8 ++++++-- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ports/mimxrt10xx/common-hal/microcontroller/Processor.c b/ports/mimxrt10xx/common-hal/microcontroller/Processor.c index 34400e3f41..8df21268ec 100644 --- a/ports/mimxrt10xx/common-hal/microcontroller/Processor.c +++ b/ports/mimxrt10xx/common-hal/microcontroller/Processor.c @@ -59,6 +59,7 @@ void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, freq != 720 && freq != 816 && freq != 912 && freq != 960 && freq != 1008) { mp_raise_ValueError(translate("Frequency must be 24, 150, 396, 450, 528, 600, 720, 816, 912, 960 or 1008 Mhz")); } + SystemCoreClock = setarmclock(frequency); } diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index 395a5553e2..98a5d7d77a 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -129,6 +129,7 @@ INC += \ -isystem sdk/src/rp2_common/hardware_sync/include/ \ -isystem sdk/src/rp2_common/hardware_timer/include/ \ -isystem sdk/src/rp2_common/hardware_uart/include/ \ + -isystem sdk/src/rp2_common/hardware_vreg/include/ \ -isystem sdk/src/rp2_common/hardware_watchdog/include/ \ -isystem sdk/src/rp2_common/hardware_xosc/include/ \ -isystem sdk/src/rp2_common/pico_multicore/include/ \ @@ -216,6 +217,7 @@ SRC_SDK := \ src/rp2_common/hardware_sync/sync.c \ src/rp2_common/hardware_timer/timer.c \ src/rp2_common/hardware_uart/uart.c \ + src/rp2_common/hardware_vreg/vreg.c \ src/rp2_common/hardware_watchdog/watchdog.c \ src/rp2_common/hardware_xosc/xosc.c \ src/rp2_common/pico_bootrom/bootrom.c \ diff --git a/ports/raspberrypi/common-hal/microcontroller/Processor.c b/ports/raspberrypi/common-hal/microcontroller/Processor.c index 66083a357d..56f2fd7fb1 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Processor.c +++ b/ports/raspberrypi/common-hal/microcontroller/Processor.c @@ -32,10 +32,12 @@ #include "common-hal/microcontroller/Processor.h" #include "shared-bindings/microcontroller/Processor.h" #include "shared-bindings/microcontroller/ResetReason.h" +#include "shared-bindings/time/__init__.h" #include "pico/stdlib.h" #include "src/rp2_common/hardware_adc/include/hardware/adc.h" #include "src/rp2_common/hardware_clocks/include/hardware/clocks.h" +#include "src/rp2_common/hardware_vreg/include/hardware/vreg.h" #include "src/rp2040/hardware_regs/include/hardware/regs/vreg_and_chip_reset.h" #include "src/rp2040/hardware_regs/include/hardware/regs/watchdog.h" @@ -62,9 +64,24 @@ uint32_t common_hal_mcu_processor_get_frequency(void) { } void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency) { - if (!set_sys_clock_khz(frequency / 1000, false)) { + uint vco, postdiv1, postdiv2; + uint32_t freq_khz = frequency / 1000; + if (!check_sys_clock_khz(freq_khz, &vco, &postdiv1, &postdiv2)) { mp_arg_error_invalid(MP_QSTR_frequency); } + // These voltages are approximate based on the PicoDVI examples. + enum vreg_voltage voltage = VREG_VOLTAGE_1_10; + if (freq_khz >= 400000) { + voltage = VREG_VOLTAGE_1_30; + } else if (freq_khz >= 300000) { + voltage = VREG_VOLTAGE_1_20; + } else if (freq_khz > 133000) { + voltage = VREG_VOLTAGE_1_20; + } + vreg_set_voltage(voltage); + // Wait for a stable voltage + common_hal_time_delay_ms(10); + set_sys_clock_khz(freq_khz, false); } void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) { diff --git a/shared-bindings/microcontroller/Processor.c b/shared-bindings/microcontroller/Processor.c index 6ae0212a6a..e4819047da 100644 --- a/shared-bindings/microcontroller/Processor.c +++ b/shared-bindings/microcontroller/Processor.c @@ -70,8 +70,12 @@ //| **Limitations:** On most boards, ``frequency`` is read-only. Setting //| the ``frequency`` is possible on RP2040 boards and some i.MX boards. //| -//| .. warning:: On RP2040 boards changing the frequency may cause issues -//| with other subsystems, such as USB, PWM, and PIO. +//| .. warning:: Overclocking likely voids your warranties and may reduce +//| the lifetime of the chip. +//| +//| .. warning:: Changing the frequency may cause issues with other +//| subsystems, such as USB, PWM, and PIO. To minimize issues, set the CPU +//| frequency before initializing other systems. //| """ #if CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY