From e5685758e3015d276ac7b1ffe9fbea75e008fd9e Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 21 Sep 2015 11:59:20 +0200 Subject: fix pulseInLong considering overflow fixes #3830 --- cores/arduino/wiring_pulse.c | 14 ++++++++------ 1 file 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; -- cgit v1.2.3-18-g5258