2828#include <string.h>
2929
3030#include "py/mphal.h"
31+ #include "py/runtime.h"
3132#include "common-hal/microcontroller/Processor.h"
3233#include "shared-bindings/microcontroller/Processor.h"
3334#include "shared-bindings/microcontroller/ResetReason.h"
35+ #include "shared-bindings/time/__init__.h"
3436
37+ #include "pico/stdlib.h"
3538#include "src/rp2_common/hardware_adc/include/hardware/adc.h"
3639#include "src/rp2_common/hardware_clocks/include/hardware/clocks.h"
40+ #include "src/rp2_common/hardware_vreg/include/hardware/vreg.h"
3741#include "src/rp2_common/hardware_watchdog/include/hardware/watchdog.h"
3842
3943#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) {
6064 return clock_get_hz (clk_sys );
6165}
6266
67+ void common_hal_mcu_processor_set_frequency (mcu_processor_obj_t * self , uint32_t frequency ) {
68+ uint vco , postdiv1 , postdiv2 ;
69+ uint32_t freq_khz = frequency / 1000 ;
70+ if (!check_sys_clock_khz (freq_khz , & vco , & postdiv1 , & postdiv2 )) {
71+ mp_arg_error_invalid (MP_QSTR_frequency );
72+ }
73+ // These voltages are approximate based on the PicoDVI examples.
74+ enum vreg_voltage voltage = VREG_VOLTAGE_1_10 ;
75+ if (freq_khz >= 400000 ) {
76+ voltage = VREG_VOLTAGE_1_30 ;
77+ } else if (freq_khz >= 300000 ) {
78+ voltage = VREG_VOLTAGE_1_20 ;
79+ } else if (freq_khz > 133000 ) {
80+ voltage = VREG_VOLTAGE_1_20 ;
81+ }
82+ vreg_set_voltage (voltage );
83+ // Wait for a stable voltage
84+ common_hal_time_delay_ms (10 );
85+ set_sys_clock_khz (freq_khz , false);
86+ }
87+
6388void common_hal_mcu_processor_get_uid (uint8_t raw_id []) {
6489 pico_unique_board_id_t retrieved_id ;
6590 pico_get_unique_board_id (& retrieved_id );
0 commit comments