From 480cd227eab0c36e9c1eddc5c68c09589e500ab1 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 21 Sep 2015 11:59:20 +0200 Subject: [PATCH] fix pulseInLong considering overflow fixes #3830 --- hardware/arduino/avr/cores/arduino/wiring_pulse.c | 14 ++++++++------ .../arduino/sam/cores/arduino/wiring_pulse.cpp | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/wiring_pulse.c b/hardware/arduino/avr/cores/arduino/wiring_pulse.c index 3212f1312..d6e04347e 100644 --- a/hardware/arduino/avr/cores/arduino/wiring_pulse.c +++ b/hardware/arduino/avr/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; diff --git a/hardware/arduino/sam/cores/arduino/wiring_pulse.cpp b/hardware/arduino/sam/cores/arduino/wiring_pulse.cpp index f41cfdf6a..4ffc9aaa0 100644 --- a/hardware/arduino/sam/cores/arduino/wiring_pulse.cpp +++ b/hardware/arduino/sam/cores/arduino/wiring_pulse.cpp @@ -69,22 +69,24 @@ uint32_t pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) uint32_t bit = p.ulPin; uint32_t stateMask = state ? bit : 0; - unsigned long maxMicros = micros() + timeout; + unsigned long startMicros = micros(); // wait for any previous pulse to end - while ((p.pPort->PIO_PDSR & bit) == stateMask) - if (micros() > maxMicros) + while ((p.pPort->PIO_PDSR & bit) == stateMask) { + if (micros() - startMicros > timeout) return 0; + } // wait for the pulse to start - while ((p.pPort->PIO_PDSR & bit) != stateMask) - if (micros() > maxMicros) + while ((p.pPort->PIO_PDSR & bit) != stateMask) { + if (micros() - startMicros > timeout) return 0; + } unsigned long start = micros(); // wait for the pulse to stop while ((p.pPort->PIO_PDSR & bit) == stateMask) { - if (micros() > maxMicros) + if (micros() - startMicros > timeout) return 0; } return micros() - start;