Skip to content

Commit

Permalink
Merge pull request #7430 from Lanzaa/rp2040_cpu_frequency
Browse files Browse the repository at this point in the history
Add frequency setting for RP2040 boards.
  • Loading branch information
tannewt authored May 2, 2023
2 parents 488dca5 + 0f9fb33 commit 750615f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
3 changes: 1 addition & 2 deletions ports/mimxrt10xx/common-hal/microcontroller/Processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ 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 &&
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);
return SystemCoreClock;
}


Expand Down
25 changes: 25 additions & 0 deletions ports/raspberrypi/common-hal/microcontroller/Processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
#include <string.h>

#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"
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ports/raspberrypi/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ CIRCUITPY_BUILD_EXTENSIONS ?= uf2
USB_NUM_ENDPOINT_PAIRS = 8

INTERNAL_FLASH_FILESYSTEM = 1
CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY = 1
11 changes: 9 additions & 2 deletions shared-bindings/microcontroller/Processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/microcontroller/Processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 750615f

Please sign in to comment.