From c7b6e66426dbff05b10e1f5aa64320f2071e8878 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 5 Aug 2020 10:03:20 -0500 Subject: [PATCH] Fixes for pulsein tick handling --- ports/atmel-samd/common-hal/pulseio/PulseIn.c | 15 +++++++++------ ports/atmel-samd/common-hal/pulseio/PulseIn.h | 1 + supervisor/shared/background_callback.c | 6 +++++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.c b/ports/atmel-samd/common-hal/pulseio/PulseIn.c index a47135d566..79f66ceaee 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.c @@ -44,6 +44,7 @@ #include "shared-bindings/pulseio/PulseIn.h" #include "supervisor/shared/tick.h" #include "supervisor/shared/translate.h" +#include "supervisor/port.h" // This timer is shared amongst all PulseIn objects as a higher resolution clock. static uint8_t refcount = 0; @@ -77,7 +78,7 @@ static void pulsein_set_config(pulseio_pulsein_obj_t* self, bool first_edge) { } void pulsein_interrupt_handler(uint8_t channel) { - // turn off interrupts while in the handler + // Turn off interrupts while in handler common_hal_mcu_disable_interrupts(); // Grab the current time first. uint32_t current_overflow = overflow_count; @@ -90,10 +91,8 @@ void pulsein_interrupt_handler(uint8_t channel) { uint32_t current_count = tc->COUNT16.COUNT.reg; pulseio_pulsein_obj_t* self = get_eic_channel_data(channel); - if (!supervisor_background_tasks_ok() ) { - common_hal_mcu_enable_interrupts(); - mp_raise_RuntimeError(translate("Input taking too long")); - return; + if (self->len == 0) { + update_background_ticks(); } if (self->first_edge) { self->first_edge = false; @@ -123,6 +122,11 @@ void pulsein_interrupt_handler(uint8_t channel) { self->start++; } } + if (!supervisor_background_tasks_ok() ) { + common_hal_mcu_enable_interrupts(); + mp_raise_RuntimeError(translate("Input taking too long")); + return; + } self->last_overflow = current_overflow; self->last_count = current_count; common_hal_mcu_enable_interrupts(); @@ -304,7 +308,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { self->start = (self->start + 1) % self->maxlen; self->len--; common_hal_mcu_enable_interrupts(); - return value; } diff --git a/ports/atmel-samd/common-hal/pulseio/PulseIn.h b/ports/atmel-samd/common-hal/pulseio/PulseIn.h index 99358178f2..a0f838b373 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseIn.h +++ b/ports/atmel-samd/common-hal/pulseio/PulseIn.h @@ -50,6 +50,7 @@ void pulsein_reset(void); void pulsein_interrupt_handler(uint8_t channel); void pulsein_timer_interrupt_handler(uint8_t index); +void update_background_ticks(void); #ifdef SAMD21 void rtc_set_continuous(void); void rtc_start_pulsein(void); diff --git a/supervisor/shared/background_callback.c b/supervisor/shared/background_callback.c index ddf723697b..af64f99aa6 100644 --- a/supervisor/shared/background_callback.c +++ b/supervisor/shared/background_callback.c @@ -37,12 +37,16 @@ STATIC volatile background_callback_t *callback_head, *callback_tail; #define CALLBACK_CRITICAL_BEGIN (common_hal_mcu_disable_interrupts()) #define CALLBACK_CRITICAL_END (common_hal_mcu_enable_interrupts()) -volatile uint64_t last_background_tick = 0; +uint64_t last_background_tick = 0; uint64_t get_background_ticks(void) { return last_background_tick; } +void update_background_ticks(void) { + last_background_tick = port_get_raw_ticks(NULL); +} + void background_callback_add_core(background_callback_t *cb) { CALLBACK_CRITICAL_BEGIN; if (cb->prev || callback_head == cb) {