Skip to content

Commit f5e505d

Browse files
committed
SAM: fix pulseInLong timeout using micros()
1 parent aec10c6 commit f5e505d

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

Diff for: cores/arduino/wiring_pulse.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,22 @@ 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-
// convert the timeout from microseconds to a number of times through
73-
// the initial loop; it takes 18 clock cycles per iteration.
74-
unsigned long maxloops = microsecondsToClockCycles(timeout) / 10;
72+
unsigned long maxMicros = micros() + timeout;
7573

7674
// wait for any previous pulse to end
7775
while ((p.pPort->PIO_PDSR & bit) == stateMask)
78-
if (--maxloops == 0)
76+
if (micros() > maxMicros)
7977
return 0;
8078

8179
// wait for the pulse to start
8280
while ((p.pPort->PIO_PDSR & bit) != stateMask)
83-
if (--maxloops == 0)
81+
if (micros() > maxMicros)
8482
return 0;
8583

8684
unsigned long start = micros();
8785
// wait for the pulse to stop
8886
while ((p.pPort->PIO_PDSR & bit) == stateMask) {
89-
if (--maxloops == 0)
87+
if (micros() > maxMicros)
9088
return 0;
9189
}
9290
return micros() - start;

0 commit comments

Comments
 (0)