raspberrypi / pico-sdk

BSD 3-Clause "New" or "Revised" License
3.24k stars 837 forks source link

Ability to "set" OSC frequency dung runtime? #1728

Open warcow105 opened 3 weeks ago

warcow105 commented 3 weeks ago

Is there any way to implement, if it doesn't already exist, a way to "tell" the sdk that the osc is a different frequency during runtime. This would be for cases where a different device is supplying the clock to the rp2040 and the clock frequency might change based on system state. This would be helpful for keeping timers/delays/freq reporting accurate.

peterharperuk commented 3 weeks ago

I think you should ask your question on the forum. There's clock_configure_gpin which allows you to run a clock from an input on a gpio pin.

kilograham commented 3 weeks ago

Can you point out particular areas of concern

  1. are there methods doing calculations based on XOSC_KHZ that need to change
  2. Is there initial hw setup that needs to change in response to an XOSC change
warcow105 commented 3 weeks ago

Can you point out particular areas of concern

  1. are there methods doing calculations based on XOSC_KHZ that need to change
  2. Is there initial hw setup that needs to change in response to an XOSC change

If I am wrong please tell me, but I am pretty sure all delays/sleeps are based on the frequency the core is running at, and it bases the "frequency" on the osc/clock input frequency through the synth and clock tree. If the system boots up and runs for a portion of its time on a 12mhz clock then the circuit feeding the clock to the rp2040 kicks up to say 24.576mhz and asserts a gpio to let my code on the rp2040 know we just switched frequencies. As far as the pico sdk code is concerned we are still running at the same frequency and all the delays and sleeps are now out of whack. It would be convenient in rare use cases like this to be able to call a function where I can let the sdk know the new osc frequency.

This is probably a very rare use case, but I can see where the rp2040 is an aux processor to a larger processor that is in charge of the board clocks for power saving or whatever. Right now all my delays use a scaling factor that is updated when the external circuit changes the clock frequency.

lurch commented 3 weeks ago

Are you looking for https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#gab3a273e837ba1947bb5fd8fc97cf47e5 ?

warcow105 commented 2 weeks ago

Are you looking for https://www.raspberrypi.com/documentation/pico-sdk/high_level.html#gab3a273e837ba1947bb5fd8fc97cf47e5 ?

Nope, that is for actually setting the hardware. In the case that I am describing the external OSC input to the rp2040 has already changed so all of the frequencies are different.

I just want to tell the sdk functions that the system is actually running at a different speed than it thinks it is.

lurch commented 2 weeks ago

Ahhh, in that case I guess you'll need to look at the places in the SDK where XOSC_KHZ is actually used then, which is basically what Graham already said. https://github.com/search?q=repo%3Araspberrypi%2Fpico-sdk%20XOSC_KHZ&type=code

warcow105 commented 2 weeks ago

I think for my use case I need to make modified versions of some of the SDK functions that take a XOSC value, then use them and also reconfigure the clock tree whenever I change frequencies. You can close this unless someone wants to add an all-in-one function to the sdk, otherwise I will do a PR if I ever get around to making the changes on my end.

Michael