rp-rs / rp-hal

A Rust Embedded-HAL for the rp series microcontrollers
https://crates.io/crates/rp2040-hal
Apache License 2.0
1.47k stars 237 forks source link

How to change/set sys_clk frequency #625

Open DmitriLyalikov opened 1 year ago

DmitriLyalikov commented 1 year ago

Hi all,

I am looking at overclocking my sys_clk to speed up the pio state machines, at for example, 250 Mhz.

I am using the standard 12Mhz XOSC on board and want to either configure the SYS_PLL, or sys_clk at runtime.

I have the following:

        // Configure the clocks
        let clocks = hal::clocks::init_clocks_and_plls(
            XTAL_FREQ_HZ,
            c.device.XOSC,
            c.device.CLOCKS,
            c.device.PLL_SYS,
            c.device.PLL_USB,
            &mut resets,
            &mut watchdog,
        )

in clocks/mod.rs the trait has ClocksManager and I wonder if i can simply do:

        self.system_clock
            .configure_clock(pll_sys, pll_sys.get_freq())?;

and instead of pll_sys.get_freq() replace to HertzU32::MHz(248)? If not, what is the correct mechanism to configure this? All help is appreciated. Thanks -Dmitri

jannic commented 1 year ago

It's not exactly as simple, as the clock source passed to configure_clock must have a higher frequency than the target frequency. So if you want to overclock, what you need to do is increase the frequency of pll_sys.

You can find a nice example how to do that in the Neotron BIOS: https://github.com/Neotron-Compute/Neotron-Pico-BIOS/blob/develop/src/main.rs#L346-L404