Skip to content

Commit 5304904

Browse files
committed
Merge pull request #3864 from facchinm/pulseInLongOVF
fix pulseInLong considering overflow
2 parents 017640a + 480cd22 commit 5304904

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

Diff for: hardware/arduino/avr/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;

Diff for: hardware/arduino/sam/cores/arduino/wiring_pulse.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,24 @@ uint32_t pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
6969
uint32_t bit = p.ulPin;
7070
uint32_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 ((p.pPort->PIO_PDSR & bit) == stateMask)
76-
if (micros() > maxMicros)
75+
while ((p.pPort->PIO_PDSR & bit) == stateMask) {
76+
if (micros() - startMicros > timeout)
7777
return 0;
78+
}
7879

7980
// wait for the pulse to start
80-
while ((p.pPort->PIO_PDSR & bit) != stateMask)
81-
if (micros() > maxMicros)
81+
while ((p.pPort->PIO_PDSR & 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 ((p.pPort->PIO_PDSR & bit) == stateMask) {
87-
if (micros() > maxMicros)
89+
if (micros() - startMicros > timeout)
8890
return 0;
8991
}
9092
return micros() - start;

0 commit comments

Comments
 (0)