Skip to content

Commit 115bb6e

Browse files
Added pulseIn API function
1 parent 0c7ac4c commit 115bb6e

3 files changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <wiring_pulse.h>
2+
#include "boards.h"
3+
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
4+
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
5+
* to 3 minutes in length, but must be called at least a few dozen microseconds
6+
* before the start of the pulse. */
7+
8+
9+
/*
10+
* Roger Clark
11+
*
12+
* Note. The API spec for this function published on http://www.arduino.cc/en/Reference/PulseIn
13+
* doesn't reflect what either the AVR or SAM version of this function actualy do with regard to the timeout value
14+
*
15+
* "timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long) "
16+
*
17+
* Because the timeout, is actually coded as the total time to both wait while the input is in the state requested
18+
* then wait for the opposite state duration
19+
* then count the length of the pulse when it has the value of state (HIGH or LOW)
20+
*
21+
* So I think the code for both the AVR and the Due is wrong in that it doesnt match the spec
22+
*
23+
* I have done basically the same as the AVR and Due code, except to make the timeout a bit more accurate I have put in a dummy volatile varable
24+
* dummyWidth so that both the waiting while loops take the same number of clock cycles to execute as the acount width counting loop
25+
*
26+
* to be slighly more accurate the maxLoops variable really needs to take into account the loop setup code, but its probably as good as necessary
27+
*
28+
*/
29+
uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout )
30+
{
31+
// cache the port and bit of the pin in order to speed up the
32+
// pulse width measuring loop and achieve finer resolution. calling
33+
// digitalRead() instead yields much coarser resolution.
34+
35+
gpio_dev *dev=PIN_MAP[pin].gpio_device;
36+
uint32_t bit = (1U << PIN_MAP[pin].gpio_bit);
37+
38+
39+
uint32_t width = 0; // keep initialization out of time critical area
40+
41+
// convert the timeout from microseconds to a number of times through
42+
// the initial loop; it takes 16 clock cycles per iteration.
43+
uint32_t numloops = 0;
44+
uint32_t maxloops = timeout * ( F_CPU / 16000000);
45+
volatile uint32_t dummyWidth=0;
46+
47+
// wait for any previous pulse to end
48+
while ( (dev->regs->IDR & bit) == bit) {
49+
if (numloops++ == maxloops) {
50+
return 0;
51+
}
52+
dummyWidth++;
53+
}
54+
55+
// wait for the pulse to start
56+
while ((dev->regs->IDR & bit) != bit) {
57+
if (numloops++ == maxloops) {
58+
return 0;
59+
}
60+
dummyWidth++;
61+
}
62+
63+
// wait for the pulse to stop
64+
while ((dev->regs->IDR & bit) == bit) {
65+
if (numloops++ == maxloops) {
66+
return 0;
67+
}
68+
width++;
69+
}
70+
71+
// Excluding time taking up by the interrupts, it needs 16 clock cycles to look through the last while loop
72+
// 5 is added as a fiddle factor to correct for interrupts etc. But ultimately this would only be accurate if it was done ona hardware timer
73+
74+
return (uint32_t)( ( (unsigned long long)(width+5) * (unsigned long long) 16000000.0) /(unsigned long long)F_CPU ) ;
75+
}

STM32F1/cores/maple/wiring_pulse.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/******************************************************************************
2+
* The MIT License
3+
*
4+
* Copyright (c) 2015 Roger Clark
5+
*
6+
* Permission is hereby granted, free of charge, to any person
7+
* obtaining a copy of this software and associated documentation
8+
* files (the "Software"), to deal in the Software without
9+
* restriction, including without limitation the rights to use, copy,
10+
* modify, merge, publish, distribute, sublicense, and/or sell copies
11+
* of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*****************************************************************************/
26+
27+
28+
29+
#ifndef _WIRISH_PULSE_H_
30+
#define _WIRISH_PULSE_H_
31+
32+
#include <libmaple/gpio.h>
33+
34+
uint32_t pulseIn( uint32_t ulPin, uint32_t ulState, uint32_t ulTimeout = 1000000L ) ;
35+
36+
37+
#endif

STM32F1/cores/maple/wirish.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include <wirish_math.h>
6060
#include <wirish_time.h>
6161
#include <wirish_constants.h>
62+
#include <wiring_pulse.h>
6263

6364
#if STM32_MCU_SERIES == STM32_SERIES_F1 /* FIXME [0.0.13?] port to F2 */
6465
//#include <HardwareSPI.h>

0 commit comments

Comments
 (0)