Skip to content

Conversation

esden
Copy link
Contributor

@esden esden commented Oct 5, 2025

Due to the integer rounding rules one has to subtract 1 from the numerator.

For example:

Let max clock be 55 and supplied clock be 110
110/55 = 2 which results in the divider being set to 4 and the clock after division ends up being 27 instead of 55 Subtracting 1 to the numerator get around the rounding issue 109/55 = 1 which results in the divider being set to 2 and the clock after division ends up being 55 which is exactly max clock

@esden esden force-pushed the fix/adc-prescaler-calc branch 2 times, most recently from 51ca96f to b74cb49 Compare October 5, 2025 22:59
@esden esden changed the title stm32/ADC: Fix prescaler calculation to include MAX frequency. stm32/ADC: Fix prescaler calculation to include max frequency. Oct 5, 2025
Due to the integer rounding rules one has to subtract 1 from the numerator.
For example:

Let max clock be 55 and supplied clock be 110
110/55 = 2 which results in the divider being set to 4 and the clock after division ends up being 27 instead of 55
Subtracting 1 to the numerator get around the rounding issue
109/55 = 1 which results in the divider being set to 2 and the clock after division ends up being 55 which is exactly max clock
@esden esden force-pushed the fix/adc-prescaler-calc branch from b74cb49 to a9727a1 Compare October 5, 2025 23:16
@esden
Copy link
Contributor Author

esden commented Oct 6, 2025

After talking with some people they pointed out that this should be better documented. So I also added a raw_prescaler function that centralizes the legend. I have the feeling that it will be useful in other places in the codebase too. I am not 100% sure that it should live in rcc, let me know if it should be moved somewhere else.

Looking forward to your ideas and comments. :)

@esden esden force-pushed the fix/adc-prescaler-calc branch from 10845fa to 26b3e23 Compare October 6, 2025 02:46
This also puts the explanation why the calculation has to be done that
way into one place so it does not need to be copied all over the codebase.
@esden esden force-pushed the fix/adc-prescaler-calc branch from 26b3e23 to 6831fdb Compare October 6, 2025 03:00
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.

1 participant