From 6af1fbacc94c165ec9818585dc3527d675917908 Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Fri, 8 Jun 2018 16:41:42 +1000 Subject: [PATCH] Work on pulseio.PulseOut for #716 ESP8266 Switch to ets_delay_us but the PWM is still way too slow to be useful. --- ports/esp8266/common-hal/pulseio/PulseOut.c | 62 +++------------------ ports/esp8266/common-hal/pulseio/PulseOut.h | 2 +- 2 files changed, 10 insertions(+), 54 deletions(-) diff --git a/ports/esp8266/common-hal/pulseio/PulseOut.c b/ports/esp8266/common-hal/pulseio/PulseOut.c index f1bf00cf95..c5e63013d7 100644 --- a/ports/esp8266/common-hal/pulseio/PulseOut.c +++ b/ports/esp8266/common-hal/pulseio/PulseOut.c @@ -36,73 +36,29 @@ #include "mpconfigport.h" #include "shared-bindings/pulseio/PulseOut.h" -#define NO_CHANNEL (255) - -static uint16_t *pulse_buffer = NULL; -static volatile uint16_t pulse_index = 0; -static uint16_t pulse_length; -static volatile uint32_t current_compare = 0; - void pulseout_set(pulseio_pulseout_obj_t *self, bool state) { - // XXX double kludge - //uint32_t duty = state ? pwm_get_period() * 11 : 0; - uint32_t duty = state ? 1000 : 500; - pwm_set_duty(duty, self->channel); - pwm_start(); -} - -void pulseout_interrupt_handler(void *data) { - pulseio_pulseout_obj_t *self = data; - - if (pulse_buffer == NULL || self->channel == NO_CHANNEL) return; - if (pulse_index >= pulse_length) return; - pulse_index++; - pulseout_set(self, pulse_index % 2 == 0); - - os_timer_arm(&self->timer, pulse_buffer[pulse_index], 0); -} - -void pulseout_reset() { - pulse_buffer = NULL; + PIN_FUNC_SELECT(self->pin->peripheral, state ? self->pin->gpio_function : 0); } void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, const pulseio_pwmout_obj_t* carrier) { - self->channel = carrier->channel; + self->pin = carrier->pin; } bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) { - return self->channel == NO_CHANNEL; + return self->pin == NULL; } void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { - os_timer_disarm(&self->timer); - self->channel = NO_CHANNEL; + self->pin = NULL; pulseout_set(self, true); } -void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { - if (pulse_buffer != NULL) { - mp_raise_RuntimeError("Another send is already active"); +void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, + uint16_t* pulses, uint16_t length) { + for (uint16_t i = 0; itimer); - os_timer_setfn(&self->timer, pulseout_interrupt_handler, self); - os_timer_arm(&self->timer, pulse_buffer[0], 0); - pulseout_set(self, true); - - // XXX in the circumstances, is it worth messing with os_timer? - // it isn't especially accurate anyway ... - // might it not be simpler to just call mp_hal_delay_us() a lot? - while(pulse_index < length) { - ets_loop_iter(); - } - - os_timer_disarm(&self->timer); pulseout_set(self, false); - - pulse_buffer = NULL; } diff --git a/ports/esp8266/common-hal/pulseio/PulseOut.h b/ports/esp8266/common-hal/pulseio/PulseOut.h index 4af41ae6c1..c4bfdbcad3 100644 --- a/ports/esp8266/common-hal/pulseio/PulseOut.h +++ b/ports/esp8266/common-hal/pulseio/PulseOut.h @@ -36,7 +36,7 @@ typedef struct { mp_obj_base_t base; os_timer_t timer; - uint8_t channel; + const mcu_pin_obj_t *pin; } pulseio_pulseout_obj_t; void pulseout_reset(void);