Fix: use correct clock division factor in DelayMs() / GetMs() #652
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The denominator for dividing a timer's frequency for
DelayMs()
/GetMs()
is completely wrong and in fact currently produces tick counts much shorter thanDelayUs()
/GetUs()
.Example with previous code before change:
This should make the problem obvious: DelayMs(100) should not result in a delay of fewer timer ticks than DelayUs(100) - the denominator is 5 orders of magnitude too big!
The Easy Fix (included here)
Easy fix: change the denominator to the correct value (1000)
The "Better" Fix (for future consideration)
There is still misleading/incorrect code here in some cases, as a given timer configured by the developer may be using a different prescaler and/or may overflow at 16-bits making requested delays impossible and/or making the ms/us getter methods rather useless/inaccurate.
It may instead be a better choice to move these calculations/methods out of
TimerHandle
and directly intoSystem
since there we at least know that the TIM2 instance is using the full PCLK speed with no prescaler.However removing these methods completely from the interface would be a breaking change.