diff --git a/ports/mimxrt10xx/common-hal/microcontroller/Processor.c b/ports/mimxrt10xx/common-hal/microcontroller/Processor.c index 8d20e10c483b..fb1b5b9c1cee 100644 --- a/ports/mimxrt10xx/common-hal/microcontroller/Processor.c +++ b/ports/mimxrt10xx/common-hal/microcontroller/Processor.c @@ -58,7 +58,7 @@ float common_hal_mcu_processor_get_temperature(void) { #endif } -uint32_t common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, +void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency) { uint32_t freq = frequency / 1000000; if (freq != 24 && freq != 150 && freq != 396 && freq != 450 && freq != 528 && freq != 600 && @@ -66,7 +66,6 @@ uint32_t common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, mp_raise_ValueError(translate("Frequency must be 24, 150, 396, 450, 528, 600, 720, 816, 912, 960 or 1008 Mhz")); } SystemCoreClock = setarmclock(frequency); - return SystemCoreClock; } diff --git a/ports/raspberrypi/common-hal/microcontroller/Processor.c b/ports/raspberrypi/common-hal/microcontroller/Processor.c index ace9060c91e8..e71cb75507b5 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Processor.c +++ b/ports/raspberrypi/common-hal/microcontroller/Processor.c @@ -28,12 +28,16 @@ #include #include "py/mphal.h" +#include "py/runtime.h" #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/rp2_common/hardware_watchdog/include/hardware/watchdog.h" #include "src/rp2040/hardware_regs/include/hardware/regs/vreg_and_chip_reset.h" @@ -60,6 +64,27 @@ uint32_t common_hal_mcu_processor_get_frequency(void) { return clock_get_hz(clk_sys); } +void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency) { + 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[]) { pico_unique_board_id_t retrieved_id; pico_get_unique_board_id(&retrieved_id); diff --git a/ports/raspberrypi/mpconfigport.mk b/ports/raspberrypi/mpconfigport.mk index ab60207b0d09..cdee9644102b 100644 --- a/ports/raspberrypi/mpconfigport.mk +++ b/ports/raspberrypi/mpconfigport.mk @@ -49,3 +49,4 @@ CIRCUITPY_BUILD_EXTENSIONS ?= uf2 USB_NUM_ENDPOINT_PAIRS = 8 INTERNAL_FLASH_FILESYSTEM = 1 +CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY = 1 diff --git a/shared-bindings/microcontroller/Processor.c b/shared-bindings/microcontroller/Processor.c index 986f81ea539d..e4819047daa5 100644 --- a/shared-bindings/microcontroller/Processor.c +++ b/shared-bindings/microcontroller/Processor.c @@ -67,8 +67,15 @@ //| frequency: int //| """The CPU operating frequency in Hertz. //| -//| **Limitations:** Setting the ``frequency`` is possible only on some i.MX boards. -//| On most boards, ``frequency`` is read-only. +//| **Limitations:** On most boards, ``frequency`` is read-only. Setting +//| the ``frequency`` is possible on RP2040 boards and some i.MX boards. +//| +//| .. 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 diff --git a/shared-bindings/microcontroller/Processor.h b/shared-bindings/microcontroller/Processor.h index 9a2f22b24507..473f25cbc6ae 100644 --- a/shared-bindings/microcontroller/Processor.h +++ b/shared-bindings/microcontroller/Processor.h @@ -39,6 +39,6 @@ mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void); float common_hal_mcu_processor_get_temperature(void); void common_hal_mcu_processor_get_uid(uint8_t raw_id[]); float common_hal_mcu_processor_get_voltage(void); -uint32_t common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency); +void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PROCESSOR_H