Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
arduino-nRF5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
arduino-nRF5
Commits
d50e109c
Commit
d50e109c
authored
Nov 09, 2020
by
John Maloney
Committed by
Sandeep Mistry
Nov 10, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Map pin number argument to internal pin number using digitalPinToPin
Adjust loop count to microsecond computations
parent
772990ab
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
15 deletions
+13
-15
cores/nRF5/pulse.c
cores/nRF5/pulse.c
+13
-15
No files found.
cores/nRF5/pulse.c
View file @
d50e109c
...
...
@@ -27,27 +27,25 @@ extern unsigned long countPulseASM(const volatile uint32_t *port, uint32_t bit,
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
* to 3 minutes in length, but must be called at least a few dozen microseconds
* before the start of the pulse. */
uint32_t
pulseIn
(
uint32_t
p
in
,
uint32_t
state
,
uint32_t
timeout
)
uint32_t
pulseIn
(
uint32_t
ulP
in
,
uint32_t
state
,
uint32_t
timeout
)
{
// cache the port and bit of the pin in order to speed up the
// pulse width measuring loop and achieve finer resolution. calling
// digitalRead() instead yields much coarser resolution.
// PinDescription p = g_APinDescription[pin];
uint32_t
bit
=
1
<<
pin
;
//p.ulPin
;
uint32_t
bit
=
1
<<
digitalPinToPin
(
ulPin
)
;
uint32_t
stateMask
=
state
?
bit
:
0
;
// convert the timeout from microseconds to a number of times through
// the initial loop; it takes (roughly) 13 clock cycles per iteration.
uint32_t
maxloops
=
microsecondsToClockCycles
(
timeout
)
/
13
;
uint32_t
width
=
countPulseASM
(
&
(
NRF_GPIO
->
IN
),
bit
,
stateMask
,
maxloops
);
// convert the reading to microseconds. The loop has been determined
// to be 13 clock cycles long and have about 16 clocks between the edge
// and the start of the loop. There will be some error introduced by
// the interrupt handlers.
if
(
width
)
return
clockCyclesToMicroseconds
(
width
*
13
+
16
);
else
return
0
;
// the initial loop; it takes (roughly) 10 clock cycles per iteration.
uint32_t
maxloops
=
microsecondsToClockCycles
(
timeout
)
/
10
;
// count low-level loops during the pulse (or until maxLoops)
// a zero loopCount means that a complete pulse was not detected within the timeout
uint32_t
loopCount
=
countPulseASM
(
&
(
NRF_GPIO
->
IN
),
bit
,
stateMask
,
maxloops
);
// convert the reading to (approximate) microseconds. The loop time as measured with an
// oscilloscope is 10 cycles on a BBC micro:bit 1.3 (nRF51822). There is error because the
// time is quantized to an integral number of loops and because interrupt may steal cycles.
return
clockCyclesToMicroseconds
(
10
*
loopCount
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment