From 42156484ae0ebb709e5543fb69505fe88148edf0 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 10 Aug 2017 11:55:34 -0700 Subject: [PATCH] shared-bindings/atmel-samd: Fix and improve trigger duration timing on PulseIn.resume. --- atmel-samd/common-hal/pulseio/PulseIn.c | 12 +++++++++++- shared-bindings/pulseio/PulseIn.c | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/atmel-samd/common-hal/pulseio/PulseIn.c b/atmel-samd/common-hal/pulseio/PulseIn.c index f48045cf48..09cad14047 100644 --- a/atmel-samd/common-hal/pulseio/PulseIn.c +++ b/atmel-samd/common-hal/pulseio/PulseIn.c @@ -172,9 +172,19 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, pin_conf.direction = PORT_PIN_DIR_OUTPUT; pin_conf.input_pull = PORT_PIN_PULL_NONE; port_pin_set_config(self->pin, &pin_conf); + + // TODO(tannewt): delay_us isn't exactly correct so we adjust the value + // here before calling it. Find out why its not exact and fix it instead + // of hacking around it here. + uint32_t adjusted_duration = trigger_duration; + adjusted_duration *= 4; + adjusted_duration /= 5; + + common_hal_mcu_disable_interrupts(); port_pin_set_output_level(self->pin, !self->idle_state); - delay_us(trigger_duration); + common_hal_mcu_delay_us(adjusted_duration); port_pin_set_output_level(self->pin, self->idle_state); + common_hal_mcu_enable_interrupts(); } // Reconfigure the pin and make sure its set to detect the first edge. diff --git a/shared-bindings/pulseio/PulseIn.c b/shared-bindings/pulseio/PulseIn.c index 5cdaa4cf37..0abc066804 100644 --- a/shared-bindings/pulseio/PulseIn.c +++ b/shared-bindings/pulseio/PulseIn.c @@ -159,7 +159,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_pause_obj, pulseio_pulsein_obj_pause); STATIC mp_obj_t pulseio_pulsein_obj_resume(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_trigger_duration }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_trigger_duration, MP_ARG_OBJ, {.u_int = 0} }, + { MP_QSTR_trigger_duration, MP_ARG_INT, {.u_int = 0} }, }; pulseio_pulsein_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];