Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I ran into some issues trying to generate a 50 Hz PWM signal to drive an SG90 servo motor from a Seeeduino XIAO using an ATSAMD21. No matter what I tried I always ended up with a 733 Hz (48Mhz / 0xFFFF period counter). When I dug into the code it looked like the prescaler register wasn't being set and the PER register wasn't being leveraged to get an accurate frequency. This code resolves this issue and also addresses a couple of bugs that I encountered in the prescaler calculation. I tried to keep the code sane for the SAMD51, but to be honest I didn't have a dev board to test against. With these changes, I was able to generate and measure (using a USB logic analyzer) accurate PWM frequencies from 1Hz up to 46.8Khz (the max frequency attainable while preserving 10 bit resolution).