-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add frequency setting for RP2040 boards. #7430
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! @Lanzaa, a few things to address.
One thing to check is if any code is assuming I think this could be fixed:
Another issue is code that has timing derived from assuming 125MHz. This may also be fixable, at least within a certain range: circuitpython/ports/raspberrypi/common-hal/floppyio/__init__.h Lines 29 to 35 in 037bfb3
For https://github.com/adafruit/circuitpython/blob/main/ports/raspberrypi/common-hal/neopixel_write/__init__.c, the PIO programs have been tuned assuming 125 MHz. |
Thank you for working on this PR! A number of people would like functionality like this. However, I have some remarks about problems that seem likely to occur when this functionality is made available and would like to see the documentation improved to anticipate them. Say that I create an object like PWMOut that has an associated frequency (1MHz), then change the CPU frequency from the default (125MHz) to 20MHz. As I understand it, the frequency of the PWMOut will also change, because it uses a divisor of the CPU frequency. In this case, the PWMOut frequency would change from 1MHz to 160kHz (20MHz/125). (audio, pulseio, spi, i2c, and anything using pio would also be affected) I am sure this is acceptable for some use cases but it deserves a big caveat in the documentation. Another concern is that this function will set high frequencies above the datasheet maximum. e.g., this led to the spurious report that calling the function twice didn't work: raspberrypi/pico-sdk#94 and even at frequencies that do "work" it reportedly interferes with USB functionality (reported in the same issue). Should the function allow "above datasheet maximum" settings, or should it reject them? Again, I think this needs a note in the documentation. Also only 440 of the 1-kHz granularity frequencies from 1kHz to 133MHz succeed in configuring, the other 132559 fail. Every 1MHz multiple 20MHz or above does succeed. This is because the clock setting routine in the SDK only sets the frequency if it can be exactly attained. But, confusingly, if setting 20_000_000Hz works then setting 20_000_001Hz will also work, because the 1Hz difference has been divided away already. (this is based not on testing on-device but on a standalone program that uses code copied out of pico-sdk: https://gist.github.com/fd42b3b7d99bbb321dd6db6ef77499aa) |
Sounds good. I will see what I can address. Hopefully I will update the PR before the start of next week. |
First of all I believe it would be best to 'by-default' prohibit overclocking. I think it's good to try to implement this so that all the modules are ready for any future rp2 chips with different frequencies. Also, @Lanzaa, to fix the pre-commit check run |
* Add warning about setting RP2040 frequency
I have changed
Any advice on where to put these properties?
@jepler Do you have any advice on how I could best test these issues? |
Thanks for the note! Please make it generic. The same clock caveat will apply across all micros. Please add a suggestion that one sets the frequency before starting to using anything else. That way they should be able to adjust to the new clock.
Don't add them. Frequency is enough. The common_hal code should verify the value is somewhat reasonable.
A logic analyzer or oscilloscope should be able to show you the frequency change in the pwm output. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making the requested changes.
I hadn't even considered the impact on USB. Does changing the frequency at all mean USB won't work any longer? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[20:13:24] >>> microcontroller.cpu.frequency=125_000_000; t0 = time.monotonic_ns(); t0; sum(range(1_000_000)); t1 = time.monotonic_ns(); (t1-t0) / 1e9
[20:13:44] 207238281250
[20:13:54] 499999500000
[20:13:54] 10.6943
[20:13:54] >>> microcontroller.cpu.frequency=300_000_000; t0 = time.monotonic_ns(); t0; sum(range(1_000_000)); t1 = time.monotonic_ns(); (t1-t0) / 1e9
[20:14:03] 226430664062
[20:14:07] 499999500000
[20:14:07] 4.45605
[20:14:07] >>> microcontroller.cpu.frequency=600_000_000; t0 = time.monotonic_ns(); t0; sum(range(1_000_000)); t1 = time.monotonic_ns(); (t1-t0) / 1e9
[tio 20:14:21] Disconnected
- Setting the frequency doesn't immediately disconnect USB
- and a computational task does get faster
- but setting it too high just breaks everything. What 'maximum overclock' should be accepted? Arduino IDE with
- before & after frequency changes,
neopixel_write
continues to work so clocks set up after the frequency change are correct.neopixel_write
constructs a fresh StateMachine instance every time - as expected, an existing PIO state machine operates at the wrong frequency. For instance, when using
adafruit_neopxl8
, changing the frequency after creating a pixel object caused it to malfunction, showing bright white instead of dim blue.
SystemCoreClock = setarmclock(frequency); | ||
return SystemCoreClock; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why were these lines removed? doesn't it stop the clock setting from becoming effective?
|
Changing this to draft for now. |
I've polished this up. @dhalbert please re-review since jepler is out. |
It can wait. It isn't needed for DVI. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Allow setting the cpu frequency for RP2040 boards.
My first PR here. I did very basic testing using a Pico board. Being able to lower the system frequency helps with power usage. At the standard 125MHz my board uses ~20mA and at 20MHz it uses ~6mA.