From db82160eefa620d889d4e9df9b04bb476b103951 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 9 Jan 2019 15:15:30 +0700 Subject: [PATCH] fix pulsein incorrect compute --- ports/nrf/common-hal/pulseio/PulseIn.c | 4 +++- ports/nrf/common-hal/pulseio/PulseIn.h | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ports/nrf/common-hal/pulseio/PulseIn.c b/ports/nrf/common-hal/pulseio/PulseIn.c index fa05de148c..6e5825af62 100644 --- a/ports/nrf/common-hal/pulseio/PulseIn.c +++ b/ports/nrf/common-hal/pulseio/PulseIn.c @@ -59,6 +59,9 @@ static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action uint64_t current_ms; current_tick(¤t_ms, ¤t_us); + // current_tick gives us the remaining us until the next tick but we want the number since the last ms. + current_us = 1000 - current_us; + pulseio_pulsein_obj_t* self = NULL; for(int i = 0; i < NRFX_ARRAY_SIZE(_objs); i++ ) { if ( _objs[i] && _objs[i]->pin == pin ) { @@ -66,7 +69,6 @@ static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action break; } } - if ( !self ) return; if (self->first_edge) { diff --git a/ports/nrf/common-hal/pulseio/PulseIn.h b/ports/nrf/common-hal/pulseio/PulseIn.h index a029129ac6..432506418a 100644 --- a/ports/nrf/common-hal/pulseio/PulseIn.h +++ b/ports/nrf/common-hal/pulseio/PulseIn.h @@ -35,15 +35,17 @@ typedef struct { mp_obj_base_t base; uint8_t pin; + bool idle_state; + bool paused; + uint16_t* buffer; uint16_t maxlen; - bool idle_state; + volatile uint16_t start; volatile uint16_t len; volatile bool first_edge; - bool paused; - volatile uint64_t last_ms; volatile uint16_t last_us; + volatile uint64_t last_ms; } pulseio_pulsein_obj_t; void pulsein_reset(void);