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
1 changed files with 4 additions and 6 deletions

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 stateMask = state ? bit : 0;
// convert the timeout from microseconds to a number of times through
// the initial loop; it takes 18 clock cycles per iteration.
unsigned long maxloops = microsecondsToClockCycles(timeout) / 10;
unsigned long maxMicros = micros() + timeout;
// wait for any previous pulse to end
while ((p.pPort->PIO_PDSR & bit) == stateMask)
if (--maxloops == 0)
if (micros() > maxMicros)
return 0;
// wait for the pulse to start
while ((p.pPort->PIO_PDSR & bit) != stateMask)
if (--maxloops == 0)
if (micros() > maxMicros)
return 0;
unsigned long start = micros();
// wait for the pulse to stop
while ((p.pPort->PIO_PDSR & bit) == stateMask) {
if (--maxloops == 0)
if (micros() > maxMicros)
return 0;
}
return micros() - start;