diff --git a/hardware/arduino/sam/cores/arduino/wiring_pulse.cpp b/hardware/arduino/sam/cores/arduino/wiring_pulse.cpp index df5ee7804..22c3d2bec 100644 --- a/hardware/arduino/sam/cores/arduino/wiring_pulse.cpp +++ b/hardware/arduino/sam/cores/arduino/wiring_pulse.cpp @@ -22,7 +22,11 @@ /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds * to 3 minutes in length, but must be called at least a few dozen microseconds - * before the start of the pulse. */ + * before the start of the pulse. + * + * ATTENTION: + * This function performs better with short pulses in noInterrupt() context + */ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) { // cache the port and bit of the pin in order to speed up the @@ -47,3 +51,43 @@ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) else return 0; } + +/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH + * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds + * to 3 minutes in length, but must be called at least a few dozen microseconds + * before the start of the pulse. + * + * ATTENTION: + * this function relies on micros() so cannot be used in noInterrupt() context + */ +uint32_t pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) +{ + // cache the port and bit of the pin in order to speed up the + // pulse width measuring loop and achieve finer resolution. calling + // digitalRead() instead yields much coarser resolution. + PinDescription p = g_APinDescription[pin]; + 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; + + // wait for any previous pulse to end + while ((p.pPort->PIO_PDSR & bit) == stateMask) + if (--maxloops == 0) + return 0; + + // wait for the pulse to start + while ((p.pPort->PIO_PDSR & bit) != stateMask) + if (--maxloops == 0) + return 0; + + unsigned long start = micros(); + // wait for the pulse to stop + while ((p.pPort->PIO_PDSR & bit) == stateMask) { + if (--maxloops == 0) + return 0; + } + return micros() - start; +} \ No newline at end of file diff --git a/hardware/arduino/sam/cores/arduino/wiring_pulse.h b/hardware/arduino/sam/cores/arduino/wiring_pulse.h index 64e188ec3..3087cb93c 100644 --- a/hardware/arduino/sam/cores/arduino/wiring_pulse.h +++ b/hardware/arduino/sam/cores/arduino/wiring_pulse.h @@ -31,7 +31,7 @@ unsigned long countPulseASM(const volatile uint32_t *port, uint32_t bit, uint32_ * before the start of the pulse. */ extern uint32_t pulseIn( uint32_t ulPin, uint32_t ulState, uint32_t ulTimeout = 1000000L ) ; - +extern uint32_t pulseInLong( uint8_t pin, uint8_t state, unsigned long timeout = 1000000L ) ; #ifdef __cplusplus }