-
Notifications
You must be signed in to change notification settings - Fork 274
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
Fix off-by-one in the allowed range of the spi clock calculations #3266
base: main
Are you sure you want to change the base?
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! We should probably test the effect of this PR somehow. If you'd add a HIL test, that would be great, but I can do it, too if you prefer not faffing with those.
I don't really know how to do that. You have edit rights to my PR, or you could do it afterwards. |
@bugadani If you can tell me which tests you want me to add, I can try to see if I can make sense of it. |
You don't need to, I'll get to this eventually, sorry for the delay :) |
I did end up modifying the frequency validation as well, as it's somewhat related to how we calculate the dividers. This means I probably shouldn't be one of the people approving the PRs. Thanks for noticing the issue, though! |
4b66ebe
to
d1fed98
Compare
@bugadani Should I be doing anything? |
No, two team members will review this and most likely merge it as is, or maybe I'll be told off for changing more than I should have :) Either way, we'll handle the rest. |
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.
LGTM
// those values are not currently supported by the divider calculation. | ||
let max_divider = 16 * 64; // n * pre | ||
|
||
if self.frequency < source_freq / max_divider || self.frequency > source_freq / min_divider |
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.
The old code allowed a bit of undershoot. The minimum value is 78.125kHz, but the old code allowed 70kHz as well.
Thank you for your contribution!
We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:
Submission Checklist 📝
cargo xtask fmt-packages
command to ensure that all changed code is formatted correctly.CHANGELOG.md
in the proper section.Extra:
Pull Request Details 📖
Description
The actual clock value is
apb_clk/(SPI_CLKDIV_PRE + 1)/(SPI_CLKCNT_N + 1)
. The loop in question tries to brute-force a value forSPI_CLKCNT_N
, however the+1
is corrected later in the code, which means that the valid range forn
in this loop is1..=64
. However there is also an earlier check earlier, that guarantees thatn=1
would never be an optimal choice.This also matches the behavior from
apache/incubator-nuttx
which this code was inspired by.Testing
I have run this code before and after the change:
Before this change, the values were
SPI_CLKDIV_PRE=15, SPI_CLKDIV_N=62
, after this change the values wereSPI_CLKDIV_PRE=15, SPI_CLKDIV_N=63
.