1
1
#include < wiring_pulse.h>
2
2
#include " boards.h"
3
+ #include " variant.h"
3
4
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
4
5
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
5
6
* to 3 minutes in length, but must be called at least a few dozen microseconds
28
29
*/
29
30
uint32_t pulseIn ( uint32_t pin, uint32_t state, uint32_t timeout )
30
31
{
31
- // cache the port and bit of the pin in order to speed up the
32
+ // cache the IDR address and bit of the pin in order to speed up the
32
33
// pulse width measuring loop and achieve finer resolution. calling
33
34
// digitalRead() instead yields much coarser resolution.
34
35
35
- gpio_dev *dev=PIN_MAP[ pin]. gpio_device ;
36
- uint32_t bit = ( 1U << PIN_MAP[ pin]. gpio_bit );
37
-
36
+ __io uint32_t * const idr = portInputRegister ( digitalPinToPort ( pin)) ;
37
+ const uint32_t bit = digitalPinToBitMask ( pin);
38
+ const uint32_t stateMask = (state ? bit: 0 );
38
39
39
40
uint32_t width = 0 ; // keep initialization out of time critical area
40
41
@@ -45,23 +46,23 @@ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout )
45
46
volatile uint32_t dummyWidth=0 ;
46
47
47
48
// wait for any previous pulse to end
48
- while ( (dev-> regs -> IDR & bit) == bit ) {
49
+ while ((*idr & bit) == stateMask ) {
49
50
if (numloops++ == maxloops) {
50
51
return 0 ;
51
52
}
52
- dummyWidth++;
53
+ dummyWidth++;
53
54
}
54
55
55
56
// wait for the pulse to start
56
- while ((dev-> regs -> IDR & bit) != bit ) {
57
+ while ((*idr & bit) != stateMask ) {
57
58
if (numloops++ == maxloops) {
58
59
return 0 ;
59
60
}
60
- dummyWidth++;
61
+ dummyWidth++;
61
62
}
62
63
63
64
// wait for the pulse to stop
64
- while ((dev-> regs -> IDR & bit) == bit ) {
65
+ while ((*idr & bit) == stateMask ) {
65
66
if (numloops++ == maxloops) {
66
67
return 0 ;
67
68
}
0 commit comments