1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-17 06:52:18 +01:00

SAM: fix pulseInLong timeout using micros()

This commit is contained in:
Martino Facchin 2015-09-21 11:10:33 +02:00
parent 6cdafbc20c
commit 606d56b3e5

View File

@ -69,24 +69,22 @@ uint32_t pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
uint32_t bit = p.ulPin; uint32_t bit = p.ulPin;
uint32_t stateMask = state ? bit : 0; uint32_t stateMask = state ? bit : 0;
// convert the timeout from microseconds to a number of times through unsigned long maxMicros = micros() + timeout;
// the initial loop; it takes 18 clock cycles per iteration.
unsigned long maxloops = microsecondsToClockCycles(timeout) / 10;
// wait for any previous pulse to end // wait for any previous pulse to end
while ((p.pPort->PIO_PDSR & bit) == stateMask) while ((p.pPort->PIO_PDSR & bit) == stateMask)
if (--maxloops == 0) if (micros() > maxMicros)
return 0; return 0;
// wait for the pulse to start // wait for the pulse to start
while ((p.pPort->PIO_PDSR & bit) != stateMask) while ((p.pPort->PIO_PDSR & bit) != stateMask)
if (--maxloops == 0) if (micros() > maxMicros)
return 0; return 0;
unsigned long start = micros(); unsigned long start = micros();
// wait for the pulse to stop // wait for the pulse to stop
while ((p.pPort->PIO_PDSR & bit) == stateMask) { while ((p.pPort->PIO_PDSR & bit) == stateMask) {
if (--maxloops == 0) if (micros() > maxMicros)
return 0; return 0;
} }
return micros() - start; return micros() - start;