3232#include "common-hal/microcontroller/Processor.h"
3333#include "shared-bindings/microcontroller/Processor.h"
3434#include "shared-bindings/microcontroller/ResetReason.h"
35+ #include "shared-bindings/time/__init__.h"
3536
3637#include "pico/stdlib.h"
3738#include "src/rp2_common/hardware_adc/include/hardware/adc.h"
3839#include "src/rp2_common/hardware_clocks/include/hardware/clocks.h"
40+ #include "src/rp2_common/hardware_vreg/include/hardware/vreg.h"
3941
4042#include "src/rp2040/hardware_regs/include/hardware/regs/vreg_and_chip_reset.h"
4143#include "src/rp2040/hardware_regs/include/hardware/regs/watchdog.h"
@@ -62,9 +64,24 @@ uint32_t common_hal_mcu_processor_get_frequency(void) {
6264}
6365
6466void common_hal_mcu_processor_set_frequency (mcu_processor_obj_t * self , uint32_t frequency ) {
65- if (!set_sys_clock_khz (frequency / 1000 , false)) {
67+ uint vco , postdiv1 , postdiv2 ;
68+ uint32_t freq_khz = frequency / 1000 ;
69+ if (!check_sys_clock_khz (freq_khz , & vco , & postdiv1 , & postdiv2 )) {
6670 mp_arg_error_invalid (MP_QSTR_frequency );
6771 }
72+ // These voltages are approximate based on the PicoDVI examples.
73+ enum vreg_voltage voltage = VREG_VOLTAGE_1_10 ;
74+ if (freq_khz >= 400000 ) {
75+ voltage = VREG_VOLTAGE_1_30 ;
76+ } else if (freq_khz >= 300000 ) {
77+ voltage = VREG_VOLTAGE_1_20 ;
78+ } else if (freq_khz > 133000 ) {
79+ voltage = VREG_VOLTAGE_1_20 ;
80+ }
81+ vreg_set_voltage (voltage );
82+ // Wait for a stable voltage
83+ common_hal_time_delay_ms (10 );
84+ set_sys_clock_khz (freq_khz , false);
6885}
6986
7087void common_hal_mcu_processor_get_uid (uint8_t raw_id []) {
0 commit comments