From 00d5f63e7cd3e489d3291ee7e90ab4ad46110b62 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 10 Mar 2020 16:16:58 -0700 Subject: [PATCH] Hopefully fix PulseIn --- ports/nrf/common-hal/pulseio/PulseIn.c | 87 +++++++++++++++++++------- ports/nrf/common-hal/pulseio/PulseIn.h | 4 +- 2 files changed, 65 insertions(+), 26 deletions(-) diff --git a/ports/nrf/common-hal/pulseio/PulseIn.c b/ports/nrf/common-hal/pulseio/PulseIn.c index 9316121ba8..a7d0fb28e7 100644 --- a/ports/nrf/common-hal/pulseio/PulseIn.c +++ b/ports/nrf/common-hal/pulseio/PulseIn.c @@ -29,6 +29,7 @@ #include #include +#include "nrf/timers.h" #include "py/mpconfig.h" #include "py/gc.h" #include "py/runtime.h" @@ -41,6 +42,20 @@ // obj array to map pin -> self since nrfx hide the mapping static pulseio_pulsein_obj_t* _objs[GPIOTE_CH_NUM]; +// A single timer is shared amongst all PulseIn objects as a common high speed clock reference. +static uint8_t refcount = 0; +static nrfx_timer_t *timer = NULL; + +static uint32_t overflow_count = 0; + +static void timer_overflow_event_handler(nrf_timer_event_t event_type, void *p_context) { + if (event_type != NRF_TIMER_EVENT_COMPARE0) { + // Other event. + return; + } + overflow_count++; +} + // return index of the object in array static int _find_pulsein_obj(pulseio_pulsein_obj_t* obj) { for(size_t i = 0; i < NRFX_ARRAY_SIZE(_objs); i++ ) { @@ -54,13 +69,8 @@ static int _find_pulsein_obj(pulseio_pulsein_obj_t* obj) { static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { // Grab the current time first. - uint32_t current_us = 0; - uint64_t current_ms = 0; - // FIXME! We need a higher resolution clock to measure against. - //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; + uint32_t current_overflow = overflow_count; + uint32_t current_count = nrfx_timer_capture(timer, 1); pulseio_pulsein_obj_t* self = NULL; for(size_t i = 0; i < NRFX_ARRAY_SIZE(_objs); i++ ) { @@ -78,18 +88,16 @@ static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action self->first_edge = false; } }else { - uint32_t ms_diff = current_ms - self->last_ms; - uint16_t us_diff = current_us - self->last_us; - uint32_t total_diff = us_diff; - - if (self->last_us > current_us) { - total_diff = 1000 + current_us - self->last_us; - if (ms_diff > 1) { - total_diff += (ms_diff - 1) * 1000; - } - } else { - total_diff += ms_diff * 1000; + // Wrapped around a number of times. + uint32_t total_diff = 0xffff; + // Wrapped around once so + if (self->last_overflow == current_overflow - 1) { + total_diff = current_count + (0xffffffff - self->last_count); + } else if (self->last_overflow == current_overflow) { + total_diff = current_count - self->last_count; } + + // Cap duration at 16 bits. uint16_t duration = 0xffff; if (total_diff < duration) { duration = total_diff; @@ -104,8 +112,8 @@ static void _pulsein_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action } } - self->last_ms = current_ms; - self->last_us = current_us; + self->last_overflow = current_overflow; + self->last_count = current_count; } void pulsein_reset(void) { @@ -114,6 +122,11 @@ void pulsein_reset(void) { } nrfx_gpiote_init(NRFX_GPIOTE_CONFIG_IRQ_PRIORITY); + if (timer != NULL) { + nrf_peripherals_free_timer(timer); + } + refcount = 0; + memset(_objs, 0, sizeof(_objs)); } @@ -129,6 +142,27 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); } + if (refcount == 0) { + timer = nrf_peripherals_allocate_timer(); + if (timer == NULL) { + mp_raise_RuntimeError(translate("All timers in use")); + } + overflow_count = 0; + + nrfx_timer_config_t timer_config = { + // PulseIn durations are in microseconds, so this is convenient. + .frequency = NRF_TIMER_FREQ_1MHz, + .mode = NRF_TIMER_MODE_TIMER, + .bit_width = NRF_TIMER_BIT_WIDTH_32, + .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, + }; + + nrfx_timer_init(timer, &timer_config, &timer_overflow_event_handler); + // Interrupt on overflow so we can track when it rolls over. + nrfx_timer_compare(timer, 0, 0, true); + } + refcount++; + self->pin = pin->number; self->maxlen = maxlen; self->idle_state = idle_state; @@ -136,8 +170,8 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu self->len = 0; self->first_edge = true; self->paused = false; - self->last_us = 0; - self->last_ms = 0; + self->last_overflow = 0; + self->last_count = 0; claim_pin(pin); @@ -173,6 +207,11 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { reset_pin_number(self->pin); self->pin = NO_PIN; + + refcount--; + if (refcount == 0) { + nrf_peripherals_free_timer(timer); + } } void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { @@ -207,8 +246,8 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t tri self->first_edge = true; self->paused = false; - self->last_ms = 0; - self->last_us = 0; + self->last_overflow = 0; + self->last_count = 0; nrfx_gpiote_in_event_enable(self->pin, true); } diff --git a/ports/nrf/common-hal/pulseio/PulseIn.h b/ports/nrf/common-hal/pulseio/PulseIn.h index 4b2c6eee3f..da5263ac9c 100644 --- a/ports/nrf/common-hal/pulseio/PulseIn.h +++ b/ports/nrf/common-hal/pulseio/PulseIn.h @@ -44,8 +44,8 @@ typedef struct { volatile uint16_t start; volatile uint16_t len; - volatile uint16_t last_us; - volatile uint64_t last_ms; + volatile size_t last_overflow; + volatile size_t last_count; } pulseio_pulsein_obj_t; void pulsein_reset(void);