/ teensy / wiring.c
wiring.c
 1  /*
 2    wiring.c - Partial implementation of the Wiring API for the ATmega8.
 3    Part of Arduino - http://www.arduino.cc/
 4  
 5    Copyright (c) 2005-2006 David A. Mellis
 6  
 7    Modified for Teensyduino by Paul Stoffregen, paul@pjrc.com
 8    http://www.pjrc.com/teensy/teensyduino.html
 9  
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2.1 of the License, or (at your option) any later version.
14  
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19  
20    You should have received a copy of the GNU Lesser General
21    Public License along with this library; if not, write to the
22    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23    Boston, MA  02111-1307  USA
24  */
25  
26  #include "wiring_private.h"
27  #include "pins_arduino.h"
28  #include "core_pins.h"
29  
30  
31  #define PULSEIN_CYCLES_PER_LOOP  21
32  #define PULSEIN_CYCLES_LATENCY   11
33  
34  /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
35   * or LOW, the type of pulse to measure.  Works on pulses from 2-3 microseconds
36   * to 3 minutes in length, but must be called at least a few dozen microseconds
37   * before the start of the pulse. */
38  unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
39  {
40  	// cache the port and bit of the pin in order to speed up the
41  	// pulse width measuring loop and achieve finer resolution.  calling
42  	// digitalRead() instead yields much coarser resolution.
43  	uint8_t bit = digitalPinToBitMask(pin);
44  	volatile uint8_t *reg = portInputRegister(digitalPinToPort(pin));
45  	uint8_t stateMask = (state ? bit : 0);
46  	unsigned long width = 0; // keep initialization out of time critical area
47  	
48  	// convert the timeout from microseconds to a number of times through
49  	// the initial loop
50  	unsigned long numloops = 0;
51  	//unsigned long maxloops = microsecondsToClockCycles(timeout) / PULSEIN_CYCLES_PER_LOOP;
52  	unsigned long maxloops = timeout * clockCyclesPerMicrosecond() / PULSEIN_CYCLES_PER_LOOP;
53  
54  	// wait for any previous pulse to end
55  	while ((*reg & bit) == stateMask)
56  		if (numloops++ == maxloops)
57  			return 0;
58  	
59  	// wait for the pulse to start
60  	while ((*reg & bit) != stateMask)
61  		if (numloops++ == maxloops)
62  			return 0;
63  	
64  	// wait for the pulse to stop
65  	while ((*reg & bit) == stateMask) {
66  		width++;
67  		if (numloops++ == maxloops)
68  			return 0;
69  	}
70  
71  	// convert the reading to microseconds. The loop has been determined
72  	// to be PULSEIN_CYCLES_LATENCY clock cycles long and have about
73  	// PULSEIN_CYCLES_PER_LOOP clocks between the edge and the start of
74  	// the loop. There will be some error introduced by the interrupt
75  	// handlers.
76  	//return clockCyclesToMicroseconds(PULSEIN_CYCLES_PER_LOOP * width + PULSEIN_CYCLES_LATENCY); 
77  	return (width * PULSEIN_CYCLES_PER_LOOP + PULSEIN_CYCLES_LATENCY + (clockCyclesPerMicrosecond() / 2)) / clockCyclesPerMicrosecond();
78  }
79  
80