Skip to content

Commit 750615f

Browse files
authored
Merge pull request #7430 from Lanzaa/rp2040_cpu_frequency
Add frequency setting for RP2040 boards.
2 parents 488dca5 + 0f9fb33 commit 750615f

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

ports/mimxrt10xx/common-hal/microcontroller/Processor.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,14 @@ float common_hal_mcu_processor_get_temperature(void) {
5858
#endif
5959
}
6060

61-
uint32_t common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self,
61+
void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self,
6262
uint32_t frequency) {
6363
uint32_t freq = frequency / 1000000;
6464
if (freq != 24 && freq != 150 && freq != 396 && freq != 450 && freq != 528 && freq != 600 &&
6565
freq != 720 && freq != 816 && freq != 912 && freq != 960 && freq != 1008) {
6666
mp_raise_ValueError(translate("Frequency must be 24, 150, 396, 450, 528, 600, 720, 816, 912, 960 or 1008 Mhz"));
6767
}
6868
SystemCoreClock = setarmclock(frequency);
69-
return SystemCoreClock;
7069
}
7170

7271

ports/raspberrypi/common-hal/microcontroller/Processor.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
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+
6388
void 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);

ports/raspberrypi/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ CIRCUITPY_BUILD_EXTENSIONS ?= uf2
4949
USB_NUM_ENDPOINT_PAIRS = 8
5050

5151
INTERNAL_FLASH_FILESYSTEM = 1
52+
CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY = 1

shared-bindings/microcontroller/Processor.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,15 @@
6767
//| frequency: int
6868
//| """The CPU operating frequency in Hertz.
6969
//|
70-
//| **Limitations:** Setting the ``frequency`` is possible only on some i.MX boards.
71-
//| On most boards, ``frequency`` is read-only.
70+
//| **Limitations:** On most boards, ``frequency`` is read-only. Setting
71+
//| the ``frequency`` is possible on RP2040 boards and some i.MX boards.
72+
//|
73+
//| .. warning:: Overclocking likely voids your warranties and may reduce
74+
//| the lifetime of the chip.
75+
//|
76+
//| .. warning:: Changing the frequency may cause issues with other
77+
//| subsystems, such as USB, PWM, and PIO. To minimize issues, set the CPU
78+
//| frequency before initializing other systems.
7279
//| """
7380

7481
#if CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY

shared-bindings/microcontroller/Processor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void);
3939
float common_hal_mcu_processor_get_temperature(void);
4040
void common_hal_mcu_processor_get_uid(uint8_t raw_id[]);
4141
float common_hal_mcu_processor_get_voltage(void);
42-
uint32_t common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency);
42+
void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t frequency);
4343

4444
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PROCESSOR_H

0 commit comments

Comments
 (0)