Skip to content

Add frequency setting for RP2040 boards. #7430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -52,15 +52,14 @@ float common_hal_mcu_processor_get_temperature(void) {
return temp;
}

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
3 changes: 3 additions & 0 deletions ports/raspberrypi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ INC += \
-isystem sdk/src/rp2_common/hardware_sync/include/ \
-isystem sdk/src/rp2_common/hardware_timer/include/ \
-isystem sdk/src/rp2_common/hardware_uart/include/ \
-isystem sdk/src/rp2_common/hardware_vreg/include/ \
-isystem sdk/src/rp2_common/hardware_watchdog/include/ \
-isystem sdk/src/rp2_common/hardware_xosc/include/ \
-isystem sdk/src/rp2_common/pico_multicore/include/ \
Expand Down Expand Up @@ -210,6 +211,7 @@ SRC_SDK := \
src/rp2_common/hardware_sync/sync.c \
src/rp2_common/hardware_timer/timer.c \
src/rp2_common/hardware_uart/uart.c \
src/rp2_common/hardware_vreg/vreg.c \
src/rp2_common/hardware_watchdog/watchdog.c \
src/rp2_common/hardware_xosc/xosc.c \
src/rp2_common/pico_bootrom/bootrom.c \
Expand All @@ -223,6 +225,7 @@ SRC_SDK := \
src/rp2_common/pico_printf/printf.c \
src/rp2_common/pico_runtime/runtime.c \
src/rp2_common/pico_stdio/stdio.c \
src/rp2_common/pico_stdlib/stdlib.c \
src/rp2_common/pico_unique_id/unique_id.c \
$(SRC_SDK_CYW43) \

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