Skip to content

fix off-by-one in RP2040 PWM frequency setting#9398

Merged
dhalbert merged 3 commits into
adafruit:mainfrom
timchinowsky:main
Jul 3, 2024
Merged

fix off-by-one in RP2040 PWM frequency setting#9398
dhalbert merged 3 commits into
adafruit:mainfrom
timchinowsky:main

Conversation

@timchinowsky
Copy link
Copy Markdown

Setting of TOP in pwmio on RP2040 is off by one. Currently TOP is set to common_hal_mcu_processor_get_frequency() / frequency but from datasheet p. 527 that is too big by one: "if TOP is programmed to 254, the counter will have a period of 255 cycles." Verified that with current code PWM period resulting from requested 2500000 Hz PWM frequency is too long at 408 ns. With these changes frequency is exactly 400 ns as requested.

@dhalbert
Copy link
Copy Markdown
Collaborator

dhalbert commented Jul 3, 2024

I think this makes sense, but I want to make sure you get 100% duty cycle if the duty cycle is set to 65535. Could you test that if you have not already. See #4192, which fixed some other (?) PWM off-by-one issues. I want to make sure this isn't a regression from that fix.

@timchinowsky
Copy link
Copy Markdown
Author

Here's data showing correct behavior at the extremes:

pwm_extremes_explainer

While I was at it I fixed the rounding issue that I had earlier addressed for samd in #9343, and tested by the same method that I used there. Here are before and after plots:

Before

rp2040_pwm_905

After

rp2040_pwm_fixed

Copy link
Copy Markdown
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you for the testing!

@dhalbert dhalbert changed the title fix off-by-one in PWM frequency setting fix off-by-one in RP2040 PWM frequency setting Jul 3, 2024
@dhalbert dhalbert merged commit 7e39da5 into adafruit:main Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants