Suggested improvements for micros() and the timer0 overflow ISR #48
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.
This is a suggestion for speeding up micros() a bit and a slight reduction in overall system interrupt latency.
There are a lot of comments added about the changes to help understand the intent. These may be cleaned up and/or removed as desired (if the pull request is accepted).
The 32-bit timer0_overflow_count has been renamed to timer0_micros and now counts in microseconds directly. The rename might be okay as no other parts of the core refer to it. However, since timer0_overflow_count was not static, it is possible that clients (i.e. sketches) are referring to it in which case the old name s/b kept.
With the change above, micros() no longer needs a 32-bit multiply to compute the result. Since the 32-bit counter is now directly in microseconds, all that is required is a 16-bit multiply of TCNT0 to add to the counter. This saves well over 20 instructions (call to 32-bit multiply library function) during execution, but the overall code is a few bytes larger.
micros() keeps interrupts disabled for a shorter length of time now, resulting in slightly improved overall
interrupt latency for other interrupts.