diff options
author | Martino Facchin <m.facchin@arduino.cc> | 2015-09-21 10:24:48 +0200 |
---|---|---|
committer | Martino Facchin <m.facchin@arduino.cc> | 2015-09-21 10:24:48 +0200 |
commit | dc04933aa97bff7cf9833b6c97f7cf78509ff98f (patch) | |
tree | 412a75e9f8bdd2e0d725f5174e3514e439a39d93 | |
parent | e4f0bb22dac44b727ec6dd148f57ad4ef63b45cf (diff) |
pulseInLong: fix incorrect timeout handling
-rw-r--r-- | cores/arduino/wiring_pulse.c | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/cores/arduino/wiring_pulse.c b/cores/arduino/wiring_pulse.c index 76383e9..3212f13 100644 --- a/cores/arduino/wiring_pulse.c +++ b/cores/arduino/wiring_pulse.c @@ -69,25 +69,22 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) uint8_t port = digitalPinToPort(pin); uint8_t stateMask = (state ? bit : 0); - // convert the timeout from microseconds to a number of times through - // the initial loop; it takes 16 clock cycles per iteration. - unsigned long numloops = 0; - unsigned long maxloops = microsecondsToClockCycles(timeout); + unsigned long maxMicros = micros() + timeout; // wait for any previous pulse to end while ((*portInputRegister(port) & bit) == stateMask) - if (numloops++ == maxloops) + if (micros() > maxMicros) return 0; // wait for the pulse to start while ((*portInputRegister(port) & bit) != stateMask) - if (numloops++ == maxloops) + if (micros() > maxMicros) return 0; unsigned long start = micros(); // wait for the pulse to stop while ((*portInputRegister(port) & bit) == stateMask) { - if (numloops++ == maxloops) + if (micros() > maxMicros) return 0; } return micros() - start; |