Merge pull request #7430 from Lanzaa/rp2040_cpu_frequency

Add frequency setting for RP2040 boards.
This commit is contained in:
Scott Shawcroft 2023-05-02 09:52:28 -07:00 committed by GitHub
commit 750615f2da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 5 deletions

View File

@ -58,7 +58,7 @@ 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 &&
@ -66,7 +66,6 @@ uint32_t common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self,
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;
}

View File

@ -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"
@ -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);

View File

@ -49,3 +49,4 @@ CIRCUITPY_BUILD_EXTENSIONS ?= uf2
USB_NUM_ENDPOINT_PAIRS = 8
INTERNAL_FLASH_FILESYSTEM = 1
CIRCUITPY_SETTABLE_PROCESSOR_FREQUENCY = 1

View File

@ -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

View File

@ -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