8000 Merge branch 'lacklustrlabs-new_pulseIn' · ArduinoWorks/Arduino_STM32@a5f9f90 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5f9f90

Browse files
Merge branch 'lacklustrlabs-new_pulseIn'
2 parents a4ae0ce + 628dc25 commit a5f9f90

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

STM32F1/cores/maple/stm32f1/wiring_pulse_f1.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <wiring_pulse.h>
22
#include "boards.h"
3+
#include "variant.h"
34
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
45
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
56
* to 3 minutes in length, but must be called at least a few dozen microseconds
@@ -28,13 +29,13 @@
2829
*/
2930
uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout )
3031
{
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
3233
// pulse width measuring loop and achieve finer resolution. calling
3334
// digitalRead() instead yields much coarser resolution.
3435

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);
3839

3940
uint32_t width = 0; // keep initialization out of time critical area
4041

@@ -45,23 +46,23 @@ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout )
4546
volatile uint32_t dummyWidth=0;
4647

4748
// wait for any previous pulse to end
48-
while ( (dev->regs->IDR & bit) == bit) {
49+
while ((*idr & bit) == stateMask) {
4950
if (numloops++ == maxloops) {
5051
return 0;
5152
}
52-
dummyWidth++;
53+
dummyWidth++;
5354
}
5455

5556
// wait for the pulse to start
56-
while ((dev->regs->IDR & bit) != bit) {
57+
while ((*idr & bit) != stateMask) {
5758
if (numloops++ == maxloops) {
5859
return 0;
5960
}
60-
dummyWidth++;
61+
dummyWidth++;
6162
}
6263

6364
// wait for the pulse to stop
64-
while ((dev->regs->IDR & bit) == bit) {
65+
while ((*idr & bit) == stateMask) {
6566
if (numloops++ == maxloops) {
6667
return 0;
6768
}

0 commit comments

Comments
 (0)
0