diff options
author | Martino Facchin <m.facchin@arduino.cc> | 2015-09-25 16:05:35 +0200 |
---|---|---|
committer | Martino Facchin <m.facchin@arduino.cc> | 2015-09-25 16:05:35 +0200 |
commit | d3f399fad58442d7d9873d299beffba89a64daaf (patch) | |
tree | add5c781e0c2796274b5c2058692f3fad7afdbcc /cores/arduino/wiring_pulse.c | |
parent | aba020da75d6301060bcb4293270e4dda5cb5343 (diff) | |
parent | e5685758e3015d276ac7b1ffe9fbea75e008fd9e (diff) |
Merge pull request #3864 from facchinm/pulseInLongOVF
fix pulseInLong considering overflow
Diffstat (limited to 'cores/arduino/wiring_pulse.c')
-rw-r--r-- | cores/arduino/wiring_pulse.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/cores/arduino/wiring_pulse.c b/cores/arduino/wiring_pulse.c index 3212f13..d6e0434 100644 --- a/cores/arduino/wiring_pulse.c +++ b/cores/arduino/wiring_pulse.c @@ -69,22 +69,24 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) uint8_t port = digitalPinToPort(pin); uint8_t stateMask = (state ? bit : 0); - unsigned long maxMicros = micros() + timeout; + unsigned long startMicros = micros(); // wait for any previous pulse to end - while ((*portInputRegister(port) & bit) == stateMask) - if (micros() > maxMicros) + while ((*portInputRegister(port) & bit) == stateMask) { + if (micros() - startMicros > timeout) return 0; + } // wait for the pulse to start - while ((*portInputRegister(port) & bit) != stateMask) - if (micros() > maxMicros) + while ((*portInputRegister(port) & bit) != stateMask) { + if (micros() - startMicros > timeout) return 0; + } unsigned long start = micros(); // wait for the pulse to stop while ((*portInputRegister(port) & bit) == stateMask) { - if (micros() > maxMicros) + if (micros() - startMicros > timeout) return 0; } return micros() - start; |