Hello,
nRF52832 BLE project using SDK 15.3.0 and SoftDevice 112 v6.1.1. Circuit has the output of a comparator connected to two GPIO pins (COMPARATOR_PIN1, COMPARATOR_PIN2). From time to time, a sensor will cause the comparator output to go LoToHi and a short time later (typically 100-300 milliseconds) HiToLo. I need to accurately measure the duration the pin is hi in milliseconds.
I use two timers along with GPIOTE and PPI. Timer2 is in timer mode with prescaler 9 (NRF_TIMER_FREQ_31250Hz). Timer1 is in counter mode and counts the number of times Timer2 overflows (compare value 0). Code to initialize timers:
GPIOTE and PPI are used to:
- start Timer2 when GPIO pin COMPARATOR_PIN1 goes LoToHi
- increment Timer1 when Timer2 overflows (compare value 0)
- capture Timer2 value when GPIO pin COMPARATOR_PIN2 goes HiToLo
- capture Timer1 value when GPIO pin COMPARATOR_PIN2 goes HiToLo
GPIOTE interrupt is enabled only for the HiToLo transition. Here's the code to set up GPIOTE and PPI:
Finally, when the GPIOTE interrupt handler is called, it calculates the duration the comparator output was hi, then resets for next event.
I use a function generator to simulate the comparator output. It pulls the GPIO pins hi for a specific duration and repeats. About once every 100 samples, the calculated duration is out significantly. Expect 250 ms and see 1559, for example.
Any errors in my code?
Should I be using sd_ppi_channel_alloc() instead of nrfx_ppi_channel_alloc()?
This all happens with a BLE link to a central (iOS device).
Many thanks,
Tim