Skip to content
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

Make all float->int/frac conversions round to nearest by default #2065

Merged
merged 5 commits into from
Nov 21, 2024

Conversation

kilograham
Copy link
Contributor

Under control of specific vars, but defaulted by PICO_CLKDIV_ROUND_NEAREST which now defaults to 1

This fixes #1931 which was PIO specific, but it seemed sensible to be consistent across all APIs

…nearest (which is a backwards incompatible change, but I think OK), and add ability to turn it off
@kilograham kilograham added this to the 2.1.0 milestone Nov 20, 2024
@lurch
Copy link
Contributor

lurch commented Nov 20, 2024

All looks sensible, and tools/extract_configs.py is happy with all the new PICO_CONFIG: additions 👍

@@ -155,6 +160,9 @@ static inline void pwm_config_set_phase_correct(pwm_config *c, bool phase_correc
*/
static inline void pwm_config_set_clkdiv(pwm_config *c, float div) {
valid_params_if(HARDWARE_PWM, div >= 1.f && div < 256.f);
#if PICO_PWM_CLKDIV_ROUND_NEAREST
div += 0.5f / (1 << PWM_CH0_DIV_INT_LSB); // round to the nearest fraction
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry - why are we using PWM_CH0_DIV_INT_LSB here rather than PWM_CH0_DIV_FRAC_MSB+1 - PWM_CH0_DIV_FRAC_LSB. Isn't it coincidence that it's the right value? It confuses me because there are 8 bits for the pwm int.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's (almost) exactly what I asked earlier - see my resolved comments above.

Copy link
Contributor

Choose a reason for hiding this comment

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

true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i used what was used in the code in the rest of the function... feel free to commit a new change to this PR to fix all of them to be more sensible

Copy link
Contributor

Choose a reason for hiding this comment

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

Done 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, but see that the original code still uses the "wrong" values, we should fix all.

Perhaps worth adding a "const int frac_bit_count = (BLAH);" to each function

Copy link
Contributor

Choose a reason for hiding this comment

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

wish I hadn't asked!

peterharperuk
peterharperuk previously approved these changes Nov 20, 2024
@lurch
Copy link
Contributor

lurch commented Nov 20, 2024

Latest commit just pushed. Let me know if it goes "too far" ( 🫣 ) and I'll revert it and push a simpler version without the field_width stuff.

@kilograham
Copy link
Contributor Author

nice; i think I'd prefer REG_FIELD_WIDTH() and as such it probably belongs in platform_defs.h. You'll have to duplicate it in both copies for now (maybe we'll extract a common header in the future)

@lurch
Copy link
Contributor

lurch commented Nov 20, 2024

i think I'd prefer REG_FIELD_WIDTH() and as such it probably belongs in platform_defs.h

Done! 👍

Copy link
Contributor Author

@kilograham kilograham left a comment

Choose a reason for hiding this comment

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

awesome

@kilograham kilograham merged commit 6bb44dd into develop Nov 21, 2024
8 checks passed
@kilograham kilograham deleted the clkdiv_rounding branch November 21, 2024 22:48
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.

3 participants