|
23 | 23 | // See pulse_asm.S |
24 | 24 | extern unsigned long countPulseASM(const volatile uint32_t *port, uint32_t bit, uint32_t stateMask, unsigned long maxloops); |
25 | 25 |
|
| 26 | +unsigned long countPulseASM(const volatile uint32_t *port, uint32_t bit, uint32_t stateMask, unsigned long maxloops) |
| 27 | +{ |
| 28 | + unsigned long width = 0; |
| 29 | + |
| 30 | + // wait for any previous pulse to end |
| 31 | + while ((*port & bit) == stateMask) |
| 32 | + if (--maxloops == 0) |
| 33 | + return 0; |
| 34 | + |
| 35 | + // wait for the pulse to start |
| 36 | + while ((*port & bit) != stateMask) |
| 37 | + if (--maxloops == 0) |
| 38 | + return 0; |
| 39 | + |
| 40 | + // wait for the pulse to stop |
| 41 | + while ((*port & bit) == stateMask) { |
| 42 | + if (++width == maxloops) |
| 43 | + return 0; |
| 44 | + } |
| 45 | + return width; |
| 46 | +} |
| 47 | + |
26 | 48 | /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH |
27 | 49 | * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds |
28 | 50 | * to 3 minutes in length, but must be called at least a few dozen microseconds |
29 | 51 | * before the start of the pulse. */ |
30 | | -uint32_t pulseIn(uint32_t ulPin, uint32_t state, uint32_t timeout) |
| 52 | +uint32_t pulseIn(uint32_t pin, uint32_t state, uint32_t timeout) |
31 | 53 | { |
32 | 54 | // cache the port and bit of the pin in order to speed up the |
33 | 55 | // pulse width measuring loop and achieve finer resolution. calling |
34 | 56 | // digitalRead() instead yields much coarser resolution. |
35 | 57 | // PinDescription p = g_APinDescription[pin]; |
36 | | - NRF_GPIO_Type* port = digitalPinToPort(ulPin); |
37 | | - uint32_t bit = digitalPinToBitMask(ulPin); |
| 58 | + uint32_t bit = digitalPinToBitMask(pin); //p.ulPin; |
38 | 59 | uint32_t stateMask = state ? bit : 0; |
39 | 60 |
|
40 | 61 | // convert the timeout from microseconds to a number of times through |
41 | | - // the initial loop; it takes (roughly) 10 clock cycles per iteration. |
42 | | - uint32_t maxloops = microsecondsToClockCycles(timeout) / 10; |
| 62 | + // the initial loop; it takes (roughly) 13 clock cycles per iteration. |
| 63 | + uint32_t maxloops = microsecondsToClockCycles(timeout) / 13; |
43 | 64 |
|
44 | | - // count low-level loops during the pulse (or until maxLoops) |
45 | | - // a zero loopCount means that a complete pulse was not detected within the timeout |
46 | | - uint32_t loopCount = countPulseASM(&(port->IN), bit, stateMask, maxloops); |
| 65 | + uint32_t width = countPulseASM(portInputRegister(digitalPinToPort(pin)), bit, stateMask, maxloops); |
47 | 66 |
|
48 | | - // convert the reading to (approximate) microseconds. The loop time as measured with an |
49 | | - // oscilloscope is 10 cycles on a BBC micro:bit 1.3 (nRF51822). There is error because the |
50 | | - // time is quantized to an integral number of loops and because interrupt may steal cycles. |
51 | | - return clockCyclesToMicroseconds(10 * loopCount); |
| 67 | + // convert the reading to microseconds. The loop has been determined |
| 68 | + // to be 13 clock cycles long and have about 16 clocks between the edge |
| 69 | + // and the start of the loop. There will be some error introduced by |
| 70 | + // the interrupt handlers. |
| 71 | + if (width) |
| 72 | + return clockCyclesToMicroseconds(width * 13 + 16); |
| 73 | + else |
| 74 | + return 0; |
52 | 75 | } |
0 commit comments