Use (CLK_PER / 2) instead of CLKTCA for millis / micros clock #72
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 PR switches the TCB clock select from TCA (which is, by default, CPU clock / 64) to the CPU clock / 2. Then it maintains a counter in the ISR to only increment the micros / millis count every 32 interrupts, because by switching the clock source, we get interrupts 32x more frequently.
The whole reason for this is that without this the millis clock depends on TCA0, which is supposed to drive PWM pins. Without this, users cannot increase the frequency of the PWM without increasing the speed at which millis counts.
Say a user wants to drive the PWM with 4x the default frequency. They set TCA0 to be CPU clock / 16, up from CPU clock / 64, and the PWM will work at 4x the frequency. One problem: millis() ticks 4x faster, and delay() delays for 4x shorter time. This PR corrects this behaviour.
This PR has been written to add as little overhead as possible to the timer ISR, which is important as this is now called 32x as often.