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

fix(PeriphDrivers): Improve accuracy of timer stop watch #1187

Merged

Conversation

perkinsmg
Copy link
Contributor

@perkinsmg perkinsmg commented Sep 20, 2024

Description

When the timer frequency is small, e.g. 1.8MHz, then dividing this frequency by 1000000 as an integer gives a huge error since it will round down to 1. Improve things by only dividing by 1000 rather than 1000000. We can multiply by 1000 in the numerator, because the prescale value is at most 128, and so 128 * 1000000 fits in uint32_t, and this is then multiplied by ticks which is also 32 bits but widened to 64 bits for the multiply. So the numerator won't overflow.

Checklist Before Requesting Review

  • PR Title follows correct guidelines.
  • Description of changes and all other relevant information.
  • (Optional) Link any related GitHub issues using a keyword
  • (Optional) Provide info on any relevant functional testing/validation. For API changes or significant features, this is not optional.

When the timer frequency is small, e.g. 1.8MHz, then dividing this frequency
by 1000000 as an integer gives a huge error since it will round down to 1.
Improve things by only dividing by 1000 rather than 1000000. We can multiply
by 1000 in the numerator, because the prescale value is at most 128,
and so 128 * 1000000 fits in uint32_t, and this is then multiplied by
ticks which is also 32 bits but widened to 64 bits for the multiply.
So the numerator won't overflow.

Signed-off-by: Michael Perkins <michael.perkins@analog.com>
Copy link
Contributor

@sihyung-maxim sihyung-maxim left a comment

Choose a reason for hiding this comment

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

Math checks out.

@sihyung-maxim sihyung-maxim merged commit c8cd2cb into analogdevicesinc:main Sep 24, 2024
15 checks passed
sihyung-maxim pushed a commit to analogdevicesinc/hal_adi that referenced this pull request Sep 24, 2024
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.

2 participants