Skip to content

Commit 963e72a

Browse files
committed
Squashed 'avr/cores/MCUdude_corefiles/' changes from fa31758e..a367b8b9
a367b8b9 Merge pull request #32 from cburstedde/feature-exact-timing e7f9313c Merge branch 'master' into feature-exact-timing c24da8fc Updated accuracy calculation 7efce618 Replace bit_is_set with "regular" and operation 5fba4671 Minimal accuracy tweaks 0f33b77c Merge pull request #31 from cburstedde/feature-exact-timing d10b6039 Update README 29922051 Exact or very accurate millis/micros/delay always 06573210 Non-functional tweaks 0dd91a67 Update README 25fad398 Add exact timing for 9.216 MHz 19b8514f Merge branch 'master' into feature-exact-timing 5d4ba89e Correct millis and micros for arbitrary frequencies 8913fb36 Add missing parentheses b622f985 Add exact timing for 10 MHz 0333bcea Update README 5af5045c Add accurate timing for 16.5 MHz e318ce1c Add exact timing for 6 MHz 0c0b4f3a Remove superfluous cast aeaadea0 Merge pull request #29 from cburstedde/feature-delayMicroseconds 4e74473d Merge pull request #28 from cburstedde/feature-correct-micros 6916530f Fix several corner cases in delayMicroseconds() 038385ee It makes a difference using unsigned math!! 912145b8 Go back to long multiply but still optimize 90910560 Use faster unsigned int multiply in micros() eeb6635e Merge pull request #27 from cburstedde/feature-exact-timing 21d1d6fa README typo 9be3a3e4 Disable alternate micros algorithm for powers of 2 bcd8baa5 Move timer0_exact static variable def into ISR 74550897 Rename correction #define and variable 9fef70e2 Reduce unnecessary long comparison to char 95166c10 Correct bad idea: access fract with interrupts off f64aa5d0 Remove unneeded unsigned char in micros() a9153640 White space in wiring.c f683310f Make micros zero-drift based on exact millis b28acdb8 Make 25 MHz a supported clock frequency 2a6c810c Comments and README edits 7cde04dd Merge pull request #26 from cburstedde/feature-delayMicroseconds 0172627e Merge branch 'master' into feature-delayMicroseconds 3b1142ff Merge pull request #25 from cburstedde/feature-correct-micros e858df7d Merge branch 'master' into feature-correct-micros b32ca2bc Merge pull request #24 from cburstedde/feature-correct-millis 0205fd55 delayMicroseconds() safe for us == 0 and >= 24 MHz 032bed72 Statement on delayMicroseconds() in README afa0f8f6 Use 60398UL unsigned long constant 9b7291ae Add 22.1184 and 18 MHz cases to delayMicroseconds ec16def1 Add/edit comments for delayMicroseconds() 4a89b74e Replace abs macro ... with a "safe" version of the original abs macro. Unlike __builtin_abs() this also deals with floats, which is pretty much required to ensure compatibility against the official Arduino core(s). 57b7486a Tune ppm values in README 059d20b1 Optimize away two increments from timing ISR 3d890c22 Improve micros() to 100 ppm for 7.37, 3.68, 1.84 d852091b Tiny README update 39112b38 Simplify millis calculation to fit into long int d1719372 Improve micros() accuracy for 14.7456, 12, 11.0952 84c8f335 micros() below crystal tolerance for 18.432, 20 MHz b8002ce0 Update README for new/improved micros() 23942517 Make micros calculation more efficient for 20 MHz d736735f Add 18 MHz clock to micros() 21cf2177 Improve accuracy of 24 MHz micros() 3b9aa899 Add 18 MHz clock to millis() calculation 65aa77f4 Fix timing error for non-power-of-two clocks 0091354f Add discussion of micros and delay to README af3b94c7 Non-functional clarification 2568a3f8 Add millis() discussion to README 56512e96 Add millis () correction to make 22.1184 MHz exact 2d42b729 Add 22.1184 MHz micros () correction 46fa6940 Prevent micros for 32 MHz to enter wrong case 49f5f250 Increase micros () accuracy for 18.432 and 20 MHz b51058fe Supply millis () correction for 24 MHz 993843d4 Add two odd frequencies 7.37 and 3.69 MHz 25c1c988 Correction makes millis() exact for several speeds git-subtree-dir: avr/cores/MCUdude_corefiles git-subtree-split: a367b8b9ab17653a379108e48d7696bf1c6ca336
1 parent 19e34e7 commit 963e72a

File tree

4 files changed

+489
-82
lines changed

4 files changed

+489
-82
lines changed

Arduino.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ void yield(void);
121121
#undef abs
122122
#endif
123123

124-
#define abs(x) __builtin_abs(x)
124+
#define abs(x) ({ typeof (x) _x = (x); _x > 0 ? _x : -x; })
125125
#define sq(x) ({ typeof (x) _x = (x); _x * _x; })
126-
#define min(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
127-
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
128-
#define round(x) ({ typeof (x) _x = (x); _x >= 0 ? (long)_x + 0.5 : (long)_x - 0.5; })
126+
#define min(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
127+
#define max(a,b) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
128+
#define round(x) ({ typeof (x) _x = (x); _x >= 0 ? (long)(_x + 0.5) : (long)(_x - 0.5); })
129129
#define radians(deg) ((deg) * DEG_TO_RAD)
130130
#define degrees(rad) ((rad) * RAD_TO_DEG)
131131
#define constrain(x,low,high) ({ \

README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,102 @@
33

44
This repo contains the Arduino corefiles used with [MightyCore](https://github.com/MCUdude/MightyCore), [MegaCore](https://github.com/MCUdude/MegaCore), [MiniCore](https://github.com/MCUdude/MiniCore) and [MajorCore](https://github.com/MCUdude/MightyCore).
55

6+
67
## Supported devices
8+
79
* ATmega640, ATmega1280, ATmega2560
810
* ATmega64, ATmega128, ATmega1281, ATmega2561
911
* AT90CAN32, AT90CAN64, AT90CAN128
1012
* ATmega8535, ATmega16, ATmega32, ATmega164A/P, ATmega324A/P/PA/PB, ATmega644/P, ATmega1284/P
1113
* ATmega8515, ATmega162
1214
* ATmega8, ATmega48/P/PA/PB, ATmega88/P/PA/PB, ATmega168/P/PA/PB, ATmega328/P/PA/PB
1315

16+
1417
## Supported clock frequencies
15-
By supported I mean clocks that accurate timing is implemented for (millis, micros, delay, delayMicroseconds).
18+
19+
By supported I mean clocks that accurate timing is implemented for (millis,
20+
micros, delay, delayMicroseconds).
21+
1622
* 32 MHz
23+
* 25 MHz
1724
* 24 MHz
25+
* 22.1184 MHz
1826
* 20 MHz
1927
* 18.432 MHz
28+
* 18 MHz
29+
* 16.5 MHz
2030
* 16 MHz
2131
* 14.7456 MHz
2232
* 12 MHz
2333
* 11.0592 MHz
34+
* 10 MHz
35+
* 9.216 MHz
2436
* 8 MHz
2537
* 7.3728 MHz
38+
* 6 MHz
2639
* 4 MHz
2740
* 3.6864 MHz
2841
* 2 MHz
2942
* 1.8432 MHz
3043
* 1 MHz
44+
45+
46+
### Adding further clock frequencies
47+
48+
The calculation of `millis()`, `micros()` and `delay()` is automatic for
49+
arbitrary frequencies.
50+
Depending on the prime factors of the frequency, it is either exact or
51+
approximate to 60 ppm accuracy (worst-case).
52+
The only thing required is adding support in `delayMicroseconds()`.
53+
54+
55+
### Exactness of `delayMicroseconds()`
56+
57+
The `delayMicroseconds(unsigned int us)` implementation is exact up to a few
58+
cycles for the frequencies listed above.
59+
60+
The maximum input parameter to work reliably is 10000 for 10 milliseconds.
61+
Its result is affected by interrupts occurring, which may prolong the delay.
62+
63+
64+
### Exactness of `micros()` and `delay()`
65+
66+
For the clock speeds listed above, `micros()` is corrected to zero drift.
67+
Even for very long run times, the `micros()` function will precisely follow the
68+
oscillator used.
69+
70+
Frequencies not listed above are either exact or corrected to below 60 ppm drift
71+
and in exact sync with `millis()`.
72+
73+
Note that the result of `micros()` may jump up by several microseconds between
74+
consecutive calls and rolls over after one hour and eleven minutes.
75+
76+
The `delay()` function uses `micros()` internally and inherits its drift accuracy
77+
with slight variations due to function call overhead and processing.
78+
It is immune to interrupts and thus long-term accurate.
79+
80+
81+
### Exactness of `millis()`
82+
83+
For the clock speeds listed above, `millis()` is corrected to zero drift.
84+
Even for very long run times, the `millis()` function will precisely follow the
85+
oscillator used.
86+
87+
Frequencies not listed above are either exact or corrected to below 60 ppm drift
88+
and in exact sync with `micros()` and `delay()`.
89+
90+
We do not register the rollover of the `unsigned long` millis counter that
91+
occurs every 49.7 days; such would have to be done in the user's program.
92+
Often this is not necessary: The code
93+
94+
if (millis() - millis_old >= interval) {
95+
/* do something */
96+
millis_old += interval;
97+
}
98+
99+
is long-term accurate even when rolling over provided `millis_old` is of type
100+
`unsigned long`.
101+
102+
For clock speeds of 16 MHz and below, the return value of `millis()`
103+
occasionally jumps up by more than one (notwithstanding low/zero drift).
104+
Thus, when relying on consecutive returns, run at 16.5 MHz or higher.

0 commit comments

Comments
 (0)