Skip to content

Commit d3f399f

Browse files
committed
Merge pull request #3864 from facchinm/pulseInLongOVF
fix pulseInLong considering overflow
2 parents aba020d + e568575 commit d3f399f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

cores/arduino/wiring_pulse.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,24 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
6969
uint8_t port = digitalPinToPort(pin);
7070
uint8_t stateMask = (state ? bit : 0);
7171

72-
unsigned long maxMicros = micros() + timeout;
72+
unsigned long startMicros = micros();
7373

7474
// wait for any previous pulse to end
75-
while ((*portInputRegister(port) & bit) == stateMask)
76-
if (micros() > maxMicros)
75+
while ((*portInputRegister(port) & bit) == stateMask) {
76+
if (micros() - startMicros > timeout)
7777
return 0;
78+
}
7879

7980
// wait for the pulse to start
80-
while ((*portInputRegister(port) & bit) != stateMask)
81-
if (micros() > maxMicros)
81+
while ((*portInputRegister(port) & bit) != stateMask) {
82+
if (micros() - startMicros > timeout)
8283
return 0;
84+
}
8385

8486
unsigned long start = micros();
8587
// wait for the pulse to stop
8688
while ((*portInputRegister(port) & bit) == stateMask) {
87-
if (micros() > maxMicros)
89+
if (micros() - startMicros > timeout)
8890
return 0;
8991
}
9092
return micros() - start;

0 commit comments

Comments
 (0)