Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b4ba46

Browse files
chaveirofacchinm
authored andcommitted
Update wiring.c
In wiring lib, usualy a global interrupt disabled is issued when not realy needed. It's very bad for timed interrupt sensitive designs More info at: http://forum.arduino.cc/index.php?topic=168864.0
1 parent 6c861d8 commit 0b4ba46

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

cores/arduino/wiring.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,21 @@ ISR(TIMER0_OVF_vect)
6565
unsigned long millis()
6666
{
6767
unsigned long m;
68-
uint8_t oldSREG = SREG;
6968

70-
// disable interrupts while we read timer0_millis or we might get an
69+
// disable overflow interrupt while we read timer0_millis or we might get an
7170
// inconsistent value (e.g. in the middle of a write to timer0_millis)
72-
cli();
71+
// See: http://forum.arduino.cc/index.php?topic=168864.0
72+
TIMSK0 &= ~(1 << TOIE0); // Clear Overflow Interrupt Enable
7373
m = timer0_millis;
74-
SREG = oldSREG;
75-
74+
TIMSK0 |= 1 << TOIE0; // Sets Overflow Interrupt Enable
7675
return m;
7776
}
7877

7978
unsigned long micros() {
8079
unsigned long m;
81-
uint8_t oldSREG = SREG, t;
80+
uint8_t t;
8281

83-
cli();
82+
TIMSK0 &= ~(1 << TOIE0); // Clear Overflow Interrupt Enable
8483
m = timer0_overflow_count;
8584
#if defined(TCNT0)
8685
t = TCNT0;
@@ -98,7 +97,7 @@ unsigned long micros() {
9897
m++;
9998
#endif
10099

101-
SREG = oldSREG;
100+
TIMSK0 |= 1 << TOIE0; // Sets Overflow Interrupt Enable
102101

103102
return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
104103
}

0 commit comments

Comments
 (0)