[136] | 1 | /*
|
---|
| 2 | Copyright (c) 2011 Arduino. All right reserved.
|
---|
| 3 |
|
---|
| 4 | This library is free software; you can redistribute it and/or
|
---|
| 5 | modify it under the terms of the GNU Lesser General Public
|
---|
| 6 | License as published by the Free Software Foundation; either
|
---|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 8 |
|
---|
| 9 | This library is distributed in the hope that it will be useful,
|
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 12 | See the GNU Lesser General Public License for more details.
|
---|
| 13 |
|
---|
| 14 | You should have received a copy of the GNU Lesser General Public
|
---|
| 15 | License along with this library; if not, write to the Free Software
|
---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #include "Arduino.h"
|
---|
| 20 | #include "wiring_private.h"
|
---|
| 21 |
|
---|
| 22 | /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
|
---|
| 23 | * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
|
---|
| 24 | * to 3 minutes in length, but must be called at least a few dozen microseconds
|
---|
| 25 | * before the start of the pulse. */
|
---|
| 26 | extern uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout )
|
---|
| 27 | {
|
---|
| 28 | // cache the port and bit of the pin in order to speed up the
|
---|
| 29 | // pulse width measuring loop and achieve finer resolution. calling
|
---|
| 30 | // digitalRead() instead yields much coarser resolution.
|
---|
| 31 | PinDescription p = g_APinDescription[pin];
|
---|
| 32 | uint32_t width = 0; // keep initialization out of time critical area
|
---|
| 33 |
|
---|
| 34 | // convert the timeout from microseconds to a number of times through
|
---|
| 35 | // the initial loop; it takes 22 clock cycles per iteration.
|
---|
| 36 | uint32_t numloops = 0;
|
---|
| 37 | uint32_t maxloops = microsecondsToClockCycles(timeout) / 22;
|
---|
| 38 |
|
---|
| 39 | // wait for any previous pulse to end
|
---|
| 40 | while (PIO_Get(p.pPort, PIO_INPUT, p.ulPin) == state)
|
---|
| 41 | if (numloops++ == maxloops)
|
---|
| 42 | return 0;
|
---|
| 43 |
|
---|
| 44 | // wait for the pulse to start
|
---|
| 45 | while (PIO_Get(p.pPort, PIO_INPUT, p.ulPin) != state)
|
---|
| 46 | if (numloops++ == maxloops)
|
---|
| 47 | return 0;
|
---|
| 48 |
|
---|
| 49 | // wait for the pulse to stop
|
---|
| 50 | while (PIO_Get(p.pPort, PIO_INPUT, p.ulPin) == state) {
|
---|
| 51 | if (numloops++ == maxloops)
|
---|
| 52 | return 0;
|
---|
| 53 | width++;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // convert the reading to microseconds. The loop has been determined
|
---|
| 57 | // to be 52 clock cycles long and have about 16 clocks between the edge
|
---|
| 58 | // and the start of the loop. There will be some error introduced by
|
---|
| 59 | // the interrupt handlers.
|
---|
| 60 | return clockCyclesToMicroseconds(width * 52 + 16);
|
---|
| 61 | }
|
---|