Skip to content

rp2/rp2_pio: Validate state machine frequency in constructor. #7033

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

Merged
merged 1 commit into from
Apr 9, 2021

Conversation

dpgeorge
Copy link
Member

Fixes issue #7025.

} else if (args[ARG_freq].u_int == 0) {
div = 0;
// Special case of 0: set clkdiv to 0.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do the MicroPython PIO docs (if they exist yet) mention these special-cases of freq < 0 and freq == 0 ?

Copy link
Member Author

Choose a reason for hiding this comment

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

freq < 0 is just the default case (if freq is not specified in the constructor it defaults to -1) and not necessarily something the user needs to know about (but they could pass in -1 if they wanted...).

freq == 0 is not documented.

// Frequency given in Hz, compute clkdiv from it.
uint64_t div = (uint64_t)clock_get_hz(clk_sys) * 256ULL / (uint64_t)args[ARG_freq].u_int;
if (!(div >= 1 * 256 && div <= 65535 * 256)) {
mp_raise_ValueError(MP_ERROR_TEXT("freq out of range"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be helpful for this error message to give the min and max allowed SM freq for the current CPU frequency? 🤷

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a bit of work to do that, but could be useful (at least saves people reading the datasheet...!).

But since the bounds would be something like 1907.349 to 125e6, it makes me think that the freq argument should probably accept floats...

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I guess there's no reason not to expose non-integer frequencies.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the interests of getting this fix in, let's leave it for another PR to expose non-integer frequencies.

Fixes issue micropython#7025.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the rp2-pio-freq-validate branch from b78c66e to 2c9af1c Compare April 9, 2021 08:06
@dpgeorge dpgeorge merged commit 2c9af1c into micropython:master Apr 9, 2021
@dpgeorge dpgeorge deleted the rp2-pio-freq-validate branch April 9, 2021 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants