10000 Use the version of pulse.c from Adfruit's nRF5 framework which replac… · h2zero/n-able-Arduino@b978788 · GitHub
[go: up one dir, main page]

Skip to content

Commit b978788

Browse files
jhmaloneyh2zero
authored andcommitted
Use the version of pulse.c from Adfruit's nRF5 framework which replaced the aassembly function countPulseASM() with a version written in C.
1 parent b3cc2fa commit b978788

File tree

2 files changed

+35
-187
lines changed

2 files changed

+35
-187
lines changed

cores/nRF5/pulse.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,53 @@
2323
// See pulse_asm.S
2424
extern unsigned long countPulseASM(const volatile uint32_t *port, uint32_t bit, uint32_t stateMask, unsigned long maxloops);
2525

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+
2648
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
2749
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
2850
* to 3 minutes in length, but must be called at least a few dozen microseconds
2951
* 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)
3153
{
3254
// cache the port and bit of the pin in order to speed up the
3355
// pulse width measuring loop and achieve finer resolution. calling
3456
// digitalRead() instead yields much coarser resolution.
3557
// 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;
3859
uint32_t stateMask = state ? bit : 0;
3960

4061
// 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;
4364

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

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;
5275
}

cores/nRF5/pulse_asm.S

Lines changed: 0 additions & 175 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0