Skip to content

Commit 7b7d61d

Browse files
committed
Shorten critical section in micros()
The timer registers have to be read with interrupts disabled. All other computations can be done after interrupts have been enabled again.
1 parent 6309212 commit 7b7d61d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Diff for: cores/arduino/wiring.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ unsigned long millis()
7878

7979
unsigned long micros() {
8080
unsigned long m;
81-
uint8_t oldSREG = SREG, t;
81+
uint8_t oldSREG = SREG, t, flags;
8282

8383
cli();
8484
m = timer0_overflow_count;
@@ -91,14 +91,15 @@ unsigned long micros() {
9191
#endif
9292

9393
#ifdef TIFR0
94-
if ((TIFR0 & _BV(TOV0)) && (t < 255))
95-
m++;
94+
flags = TIFR0;
9695
#else
97-
if ((TIFR & _BV(TOV0)) && (t < 255))
98-
m++;
96+
flags = TIFR;
9997
#endif
10098

10199
SREG = oldSREG;
100+
101+
if ((flags & _BV(TOV0)) && (t < 255))
102+
m++;
102103

103104
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
104105
}

0 commit comments

Comments
 (0)