-
-
Notifications
You must be signed in to change notification settings - Fork 87
Bugfix: micros()
returns smaller number when timer wraps
#52
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
Conversation
1. Fix for anomaly in micros() whwn the counter wrapped just before it was read 2. Specify timer configuration as constants to make micros() considerably faster
This fixes #49. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Kees-van-der-Oord ☕ 👋
Thank you for your PR. I'm inclined to merge it but there are a couple of suggestions I'd like you to tackle first.
micros()
returns smaller number when timer wraps
…roke down long formulas
cores/arduino/time.cpp
Outdated
#define _timer_index 0 | ||
#define _timer_get_underflow_bit() R_AGT0->AGTCR_b.TUNDF | ||
#define _timer_clock_divider TIMER_SOURCE_DIV_8 // dividers 1, 2 and 8 work because _timer_period is < 16-bit. the divider 4 seems not supported: acts as 1 | ||
#define _timer_clock_freq 24000000UL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might not work on all FSP supported boards (like Portenta C33). It's very important to fix this before merging since otherwise the number of ifdefs in the core would soon become unbearable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the defines have to go @Kees-van-der-Oord , as already mentioned here.
I don't see how there can be any runtime speed-up by having them in the code, as its simple string search and replace done by the preprocessor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see the comment here. I will try to find the most efficient implementation that also works for the Portenta C33.
cores/arduino/time.cpp
Outdated
#define _timer_index 0 | ||
#define _timer_get_underflow_bit() R_AGT0->AGTCR_b.TUNDF | ||
#define _timer_clock_divider TIMER_SOURCE_DIV_8 // dividers 1, 2 and 8 work because _timer_period is < 16-bit. the divider 4 seems not supported: acts as 1 | ||
#define _timer_clock_freq 24000000UL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the defines have to go @Kees-van-der-Oord , as already mentioned here.
I don't see how there can be any runtime speed-up by having them in the code, as its simple string search and replace done by the preprocessor.
optimized micros()
I just pushed a new version that is compatible with both the Uno R4 and the Portenta C33 (and any FSP chip with a reasonable AGT clock frequency). Optimizing the code a little more, I was able to almost match the execution time of micros() on the R4 to that on the R3:
the code to test the micros() function:
|
I found one of reasons why the performance is so slow: the function FspTimer::get_counter(). I just pushed a version in whch the call to this function is replaced with a direct counter registry read,
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your work @Kees-van-der-Oord.
We are getting there, but there has been a misunderstanding between you and me. I did not ask you to replace macros with variables and inline functions, I asked you to get rid of the macros alltogether and just inline those statements directly into the code.
optimized micros() even more ...
…nit of this constant.
… better readability.
Successfully tested for
|
Mass storage device
Mass storage device Former-commit-id: b4ed6c7
See issue 49