From c847337c8d07f1e45d771b4ba7d775d21c37671b Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 16 Nov 2026 09:46:02 -0800 Subject: [PATCH] raspberrypi: lower core voltage after lowering the clock common_hal_mcu_processor_set_frequency() set the new core voltage before changing the clock. When lowering the clock, that ran the old, faster clock at the lower voltage for 10 ms. On the Fruit Jam, going from 300 MHz to 48 or 100 MHz ran 300 MHz at 1.10 V and crashed or hung the board every time. Raise the voltage before raising the clock, and lower it only after lowering the clock. Fixes the Fruit Jam part of #11882. Co-Authored-By: Claude Opus 5.5 --- .../common-hal/microcontroller/Processor.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/ports/raspberrypi/common-hal/microcontroller/Processor.c b/ports/raspberrypi/common-hal/microcontroller/Processor.c index 2ff7eda1cd9..8ac386cedd8 100644 --- a/ports/raspberrypi/common-hal/microcontroller/Processor.c +++ b/ports/raspberrypi/common-hal/microcontroller/Processor.c @@ -115,9 +115,14 @@ void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t } 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); + // Raise the voltage before raising the clock, and lower it after lowering + // the clock. + enum vreg_voltage current_voltage = vreg_get_voltage(); + if (voltage > current_voltage) { + vreg_set_voltage(voltage); + // Wait for a stable voltage + common_hal_time_delay_ms(10); + } #ifdef CIRCUITPY_PSRAM_CHIP_SELECT // Prevent interrupt handlers from accessing PSRAM until its timing matches @@ -129,6 +134,9 @@ void common_hal_mcu_processor_set_frequency(mcu_processor_obj_t *self, uint32_t mcu_processor_update_psram_timing(freq_khz); common_hal_mcu_enable_interrupts(); #endif + if (voltage < current_voltage) { + vreg_set_voltage(voltage); + } } void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {