|
| 1 | +#include <wiring_pulse.h> |
| 2 | +#include "boards.h" |
| 3 | +/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH |
| 4 | + * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds |
| 5 | + * to 3 minutes in length, but must be called at least a few dozen microseconds |
| 6 | + * before the start of the pulse. */ |
| 7 | + |
| 8 | + |
| 9 | + /* |
| 10 | + * Roger Clark |
| 11 | + * |
| 12 | + * Note. The API spec for this function published on http://www.arduino.cc/en/Reference/PulseIn |
| 13 | + * doesn't reflect what either the AVR or SAM version of this function actualy do with regard to the timeout value |
| 14 | + * |
| 15 | + * "timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long) " |
| 16 | + * |
| 17 | + * Because the timeout, is actually coded as the total time to both wait while the input is in the state requested |
| 18 | + * then wait for the opposite state duration |
| 19 | + * then count the length of the pulse when it has the value of state (HIGH or LOW) |
| 20 | + * |
| 21 | + * So I think the code for both the AVR and the Due is wrong in that it doesnt match the spec |
| 22 | + * |
| 23 | + * I have done basically the same as the AVR and Due code, except to make the timeout a bit more accurate I have put in a dummy volatile varable |
| 24 | + * dummyWidth so that both the waiting while loops take the same number of clock cycles to execute as the acount width counting loop |
| 25 | + * |
| 26 | + * to be slighly more accurate the maxLoops variable really needs to take into account the loop setup code, but its probably as good as necessary |
| 27 | + * |
| 28 | + */ |
| 29 | +uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout ) |
| 30 | +{ |
| 31 | + // cache the port and bit of the pin in order to speed up the |
| 32 | + // pulse width measuring loop and achieve finer resolution. calling |
| 33 | + // digitalRead() instead yields much coarser resolution. |
| 34 | + |
| 35 | + gpio_dev *dev=PIN_MAP[pin].gpio_device; |
| 36 | + uint32_t bit = (1U << PIN_MAP[pin].gpio_bit); |
| 37 | + |
| 38 | + |
| 39 | + uint32_t width = 0; // keep initialization out of time critical area |
| 40 | + |
| 41 | + // convert the timeout from microseconds to a number of times through |
| 42 | + // the initial loop; it takes 16 clock cycles per iteration. |
| 43 | + uint32_t numloops = 0; |
| 44 | + uint32_t maxloops = timeout * ( F_CPU / 16000000); |
| 45 | + volatile uint32_t dummyWidth=0; |
| 46 | + |
| 47 | + // wait for any previous pulse to end |
| 48 | + while ( (dev->regs->IDR & bit) == bit) { |
| 49 | + if (numloops++ == maxloops) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + dummyWidth++; |
| 53 | + } |
| 54 | + |
| 55 | + // wait for the pulse to start |
| 56 | + while ((dev->regs->IDR & bit) != bit) { |
| 57 | + if (numloops++ == maxloops) { |
| 58 | + return 0; |
| 59 | + } |
| 60 | + dummyWidth++; |
| 61 | + } |
| 62 | + |
| 63 | + // wait for the pulse to stop |
| 64 | + while ((dev->regs->IDR & bit) == bit) { |
| 65 | + if (numloops++ == maxloops) { |
| 66 | + return 0; |
| 67 | + } |
| 68 | + width++; |
| 69 | + } |
| 70 | + |
| 71 | + // Excluding time taking up by the interrupts, it needs 16 clock cycles to look through the last while loop |
| 72 | + // 5 is added as a fiddle factor to correct for interrupts etc. But ultimately this would only be accurate if it was done ona hardware timer |
| 73 | + |
| 74 | + return (uint32_t)( ( (unsigned long long)(width+5) * (unsigned long long) 16000000.0) /(unsigned long long)F_CPU ) ; |
| 75 | +} |
0 commit comments